source: CIVL/examples/pthread/CDAC/pthread-vectorvector-multi.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[4a64ef2]1/**********************************************************************************
2 C-DAC Tech Workshop : hyPACK-2013
3 October 15-18,2013
4 Example : pthread-vectorvector-multi.c
5
6 Objective : To compute the vector-vector multiplication with pthreads using
7 block-striped partitioning for uniform data distribution. Assume
8 that the vectors are of size n and p is number of processors used
9 and n is a multiple of p.
10
11 Input : Vector Size, Number of Threads. Threads must be a factor of vector size.
12
13 Output : Dot product of the vectors.
14
15 Created : MAY-2013
16 E-mail : hpcfte@cdac.in
17
18*************************************************************************************/
19
20/* Uses Uniform Data Distribution. */
21
22#include <pthread.h>
23#include <sys/time.h>
24#include <stdlib.h>
25#include <stdio.h>
26
27#define MAXTHREADS 8
28
29pthread_mutex_t mutex_sum = PTHREAD_MUTEX_INITIALIZER;
30
31int *VecA, *VecB, sum = 0, dist;
32
33/* Thread callback function */
34void * doMyWork(int myId)
35{
36
37 int counter, mySum = 0;
38
39 printf("\n %d: I am taking the interval: %d - %d.", myId, ((myId - 1) * dist), ((myId * dist) - 1));
40 /*calculating local sum by each thread */
41 for (counter = ((myId - 1) * dist); counter <= ((myId * dist) - 1); counter++)
42 mySum += VecA[counter] * VecB[counter];
43
44
45 printf("\n %d: My Local Sum: %d.", myId, mySum);
46
47 /*updating global sum using mutex lock */
48 pthread_mutex_lock(&mutex_sum);
49
50 sum += mySum;
51
52 pthread_mutex_unlock(&mutex_sum);
53
54
55 return;
56
57}
58/*Main function start */
[afa100f]59int main(int argc, char *argv[])
[4a64ef2]60{
61
62 /*variable declaration */
63 int ret_count;
64 pthread_t * threads;
65
66 pthread_attr_t pta;
67
68 double time_start, time_end, diff;
69 struct timeval tv;
70 struct timezone tz;
71
72 int counter, NumThreads, VecSize;
73
74 printf("\n\t\t---------------------------------------------------------------------------");
75 printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
76 printf("\n\t\t Email : hpcfte@cdac.in");
77 printf("\n\t\t---------------------------------------------------------------------------");
78 printf("\n\t\t Objective : Vector-Vector Multiplication");
79 printf("\n\t\t Compute the vector-vector multiplication using block-striped partitioning ");
80 printf("\n\t\t for uniform data distribution");
81 printf("\n\t\t..........................................................................\n");
82
83
84 if (argc != 3)
85 {
86 printf(" Missing Arguments: exe <VectorSize> <NumThreads> \n");
87 return;
88 }
89
90 NumThreads = abs(atoi(argv[2]));
91
92 VecSize = abs(atoi(argv[1]));
93
94
95 if (VecSize == 0)
96 {
97 printf("\nVector Size is assumed as 100");
98 VecSize = 100;
99 }
100
101 if (NumThreads == 0)
102 {
103 printf("\nNumber of Threads are assumed as 4");
104 NumThreads =4;
105 }
106
107 printf("\n Size of Vector: %d.", VecSize);
108 printf("\n Number of Threads: %d.", NumThreads);
109
110
111 if (NumThreads > MAXTHREADS)
112 {
113 printf("\n Number of threads should be less than or equal to 8. Aborting.\n");
114 return ;
115 }
116 if (VecSize % NumThreads != 0)
117 {
118 printf("\n Number of threads not a factor of vector size. Aborting.\n");
119 return ;
120 }
121 /*Memory allocation for vectors */
122 VecA = (int *) malloc(sizeof(int) * VecSize);
123
124 VecB = (int *) malloc(sizeof(int) * VecSize);
125
126 pthread_attr_init(&pta);
127
128
129 threads = (pthread_t *) malloc(sizeof(pthread_t) * NumThreads);
130
131
132 dist = VecSize / NumThreads;
133
134 /*Vector A and Vector B intialization */
135 for (counter = 0; counter < VecSize; counter++)
136 {
137 VecA[counter] = 2;
138 VecB[counter] = 3;
139 }
140
141 /*calculating start time */
142 gettimeofday(&tv, &tz);
143 time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
144
145 /*Thread Creation */
146 for (counter = 0; counter < NumThreads; counter++)
147 {
148 ret_count=pthread_create(&threads[counter], &pta, (void *(*) (void *)) doMyWork, (void *) (counter + 1));
149 if(ret_count)
150 {
151 printf("\n ERROR: Return code from pthread_create() function is %d",ret_count);
152 exit(-1);
153 }
154
155 }
156 /*joining thread */
157 for (counter = 0; counter < NumThreads; counter++)
158 {
159 ret_count=pthread_join(threads[counter], NULL);
160 if(ret_count)
161 {
162 printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
163 exit(-1);
164 }
165 }
166 /*calculating end time*/
167 gettimeofday(&tv, &tz);
168 time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
169
170
171 printf("\n The Sum is: %d.", sum);
172 printf("\n Time in Seconds (T) : %lf\n", time_end - time_start);
173
174 ret_count=pthread_attr_destroy(&pta);
175 if(ret_count)
176 {
177 printf("\n ERROR : Return code from pthread_attr_destroy() is %d ",ret_count);
178 exit(-1);
179 }
180
181 return;
182
183}
Note: See TracBrowser for help on using the repository browser.