/
main.c
440 lines (367 loc) · 9.91 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include "arrayHelpers.h"
// Struct for passing data to threads
typedef struct {
/* Data */
float** inArray;
float** outArray;
int arrayX;
int arrayY;
float precision;
/* Sync stuff */
int numThreads;
int* finishedThreads;
pthread_barrier_t* barrier;
pthread_mutex_t* finLock;
} LoopData;
// Global 'debugging' variable
int __VERBOSE;
/* Checks if the values in oldArray differ from the values in newArray by less
than the precision. Returns 1 if the don't differ, 0 if they do.
*/
int checkDiff( float** oldArray,
float** newArray,
int arrayX,
int arrayY,
float precision)
{
int i,j;
for(i = 1; i < arrayX - 1; i++)
{
for(j = 1; j < arrayY - 1; j++)
{
float oldVal = oldArray[i][j],
newVal = newArray[i][j];
// if the values differ by more than the precision
if( abs(oldVal - newVal) > precision )
{
return 0; // the arrays are too different
}
}
}
// Assume we passed
return 1;
}
/* Averages the values surrounding cardinal values in inArray and sets the
average to outArray. Ignores edges.
*/
void averageFour( float** inArray, float** outArray, int arrayX, int arrayY)
{
int i, j;
//from pos 1 to arraySize-2 as edges are fixed
for(i = 1; i < arrayX - 1; i++)
{
for(j = 1; j < arrayY - 1; j++)
{
float n,s,e,w;
n = inArray[i-1][j];
s = inArray[i+1][j];
e = inArray[i][j+1];
w = inArray[i][j-1];
outArray[i][j] = (n + s + e + w) / 4.0f;
}
}
}
/* The processing loop. Kinda superstep style, everyone copies,
everyone averages and then everyone checks if they're done.
Will quit when every thread says it has finished.
*/
void* threadLoop( void* inData)
{
LoopData* theData = (LoopData*) inData;
int i, diff = 0;
// Little bit of indirection to make access easier/simpler
float** currArray = theData->inArray;
float** nextArray = theData->outArray;
int arrayX = theData->arrayX;
int arrayY = theData->arrayY;
float precision = theData->precision;
int* finishedThreads = theData->finishedThreads;
for( i = 0; i < arrayX; i++)
{
memcpy(nextArray[i], theData->inArray[i], arrayY * sizeof(float));
}
while( 1 )
{
//copy the next array into the working copy.
for( i = 1; i < arrayX - 1; i++)
{
memcpy(currArray[i], nextArray[i], arrayY * sizeof(float));
}
//Wait until everyones done that
if(theData->barrier != NULL)
{
pthread_barrier_wait(theData->barrier);
}
averageFour(currArray, nextArray, arrayX, arrayY);
diff = checkDiff(currArray, nextArray, arrayX, arrayY, precision);
if (diff != 0)
{
if(theData->finLock != NULL)
{
pthread_mutex_lock(theData->finLock);
}
(*finishedThreads)++;
if(theData->finLock != NULL)
{
pthread_mutex_unlock(theData->finLock);
}
}
// Wait for everyone again
if(theData->barrier != NULL)
{
pthread_barrier_wait(theData->barrier);
}
// If everyone is done we can go
if ((*finishedThreads) == theData->numThreads)
{
break;
}
else //otherwise we have to try again
{
(*finishedThreads) = 0;
// Didn't lock as we're setting to a constant and the only other
// modification of the variable is behind a barrier in the loop.
}
}
return 0;
}
/* Sets up and runs the threads
*/
void relaxationThreaded(float** inArray,
float** outArray,
int arraySize,
float precision,
int numThreads)
{
if (numThreads > 0)
{
// Calculate granularity
int currPos = 0;
// plus 2 because we want to overlap and edges are
// kept constant by the functions.
int threadChunk = (arraySize / numThreads) + 2;
// To store the data for the thread function
LoopData loopDataArray[numThreads];
pthread_t threads[numThreads];
// All threads must reach the barrier before we continue
pthread_barrier_t theBarrier;
pthread_barrier_init(&theBarrier, NULL, numThreads);
// Shared counter so processes know when they're finished
int finishedThreads = 0;
// Lock for the counter
pthread_mutex_t theLock;
pthread_mutex_init(&theLock, NULL);
// Loop and create/start threads
int i;
for ( i = 0; i < numThreads; i++)
{
// The last chunk is a bit of a special case
if ( i == (numThreads - 1) )
{
int columnsRemaining = (arraySize - currPos);
loopDataArray[i].arrayX = columnsRemaining;
threadChunk = columnsRemaining;
}
else // It shouldn't be possible for any chunk but the last to go
{ // OOB, so keep that assumption (maybe bad practice, but
// protections higher up should hide it)
loopDataArray[i].arrayX = threadChunk;
}
loopDataArray[i].inArray = &inArray[currPos];
loopDataArray[i].outArray = &outArray[currPos];
loopDataArray[i].arrayY = arraySize;
loopDataArray[i].precision = precision;
loopDataArray[i].barrier = &theBarrier;
loopDataArray[i].numThreads = numThreads;
loopDataArray[i].finishedThreads= &finishedThreads;
loopDataArray[i].finLock = &theLock;
if (__VERBOSE)
{
printf("Starting thread %d.\n", i);
}
pthread_create(&threads[i], NULL,
threadLoop, (void*)&loopDataArray[i]);
currPos += threadChunk - 2;
}
// join will block if the thread is going, otherwise doesn't block.
for( i = 0; i < numThreads; i++)
{
pthread_join(threads[i], NULL);
}
// Cleanup
pthread_barrier_destroy(&theBarrier);
pthread_mutex_destroy(&theLock);
}
else
{
//== Serial Computation ==
LoopData data;
int finishedThreads = 0;
data.inArray = inArray;
data.outArray = outArray;
data.arrayX = arraySize;
data.arrayY = arraySize;
data.precision = precision;
data.barrier = NULL;
data.finLock = NULL;
data.numThreads = 1;
data.finishedThreads = &finishedThreads;
threadLoop((void*)&data);
}
}
/* Hopefully useful information on how to run the program.
*/
void printUsage()
{
printf("Arguments are:\n"
"\t-s\t:\tInteger\t-\tThe size of the matrix\n"
"\t-p\t:\tFloat\t-\tThe precision to work to\n"
"\t-t\t:\tInteger\t-\tThe number of threads to use\n"
"\t-r\t:\tInteger\t-\tSeed to use when filling the array. "
"Zero will use current time() as the seed\n"
"\t-v\t:\tNone\t-\tFlag to enable more console spew\n"
"\t-c\t:\tNone\t-\tFlag to enable the correctness test\n");
// Windows command to stop console applications closing immediately.
system("pause");
exit(0);
}
int main(int argc, char **argv)
{
// Initial values (should get from cmd line)
int arraySize = 10;
float precision = 10;
int numThreads = 2;
__VERBOSE = 0;
int arrSeed = time(0);
int testRight = 0;
// Read options
// -s is the size, -p is the precision and -t is number of threads.
// -v turns on some debug spew
int c;
opterr = 0;
while ((c = getopt (argc, argv, "s:p:t:vr:c")) != -1)
{
switch (c)
{
case 'p':
if (sscanf(optarg, "%f", &precision) != 1)
{
fprintf (stderr,
"Option -%c requires a float argument.\n",
optopt);
printUsage();
}
break;
case 'r':
if (sscanf(optarg, "%i", &arrSeed) != 1)
{
fprintf (stderr,
"Option -%c requires an integer argument.\n",
optopt);
printUsage();
}
break;
case 's':
if (sscanf(optarg, "%i", &arraySize) != 1)
{
fprintf (stderr,
"Option -%c requires an integer argument.\n",
optopt);
printUsage();
}
break;
case 't':
if (sscanf(optarg, "%i", &numThreads) != 1)
{
fprintf (stderr,
"Option -%c requires an integer argument.\n",
optopt);
printUsage();
}
break;
case 'v':
__VERBOSE = 1;
break;
case 'c':
testRight = 1;
break;
default:
printUsage();
}
}
#ifdef PTHREAD_THREADS_MAX // Linux don't got this
if (numThreads > PTHREAD_THREADS_MAX)
{
numThreads = PTHREAD_THREADS_MAX;
}
#endif
if (numThreads && (arraySize / numThreads) < 2)
{
numThreads = arraySize / 2;
}
// Start timer
struct timeval start_tv, end_tv;
gettimeofday(&start_tv,NULL);
printf("Starting to relax %d square array to precision %f.",
arraySize, precision);
printf("Using %d thread%s (other than main).\n",
numThreads, numThreads == 1 ? "" : "s");
printf(". Seed: %d.\n", arrSeed);
// Initializing and mallocing the arrays
float** currArray = make2DFloatArray(arraySize, arraySize);
float** nextArray = make2DFloatArray(arraySize, arraySize);
initArray(currArray, arraySize, arrSeed);
// Do the work
relaxationThreaded(currArray, nextArray, arraySize, precision, numThreads);
// Stop timer here.
gettimeofday(&end_tv,NULL);
int correct = 0;
if(testRight)
{
// Create 2 more arrays, we'll do it again serially
float** currArray2 = make2DFloatArray(arraySize, arraySize);
float** nextArray2 = make2DFloatArray(arraySize, arraySize);
initArray(currArray2, arraySize, arrSeed);
if(__VERBOSE)
{
printf("Original:\n");
printSquareArray(currArray, arraySize);
}
relaxationThreaded(currArray2, nextArray2, arraySize, precision, 0);
correct = checkDiff(nextArray, nextArray2, arraySize, arraySize, precision);
if(__VERBOSE)
{
printf("\nThreaded:\n");
printSquareArray(nextArray, arraySize);
printf("\nSerial:\n");
printSquareArray(nextArray2, arraySize);
}
// Cleanup
free2DFloatArray(currArray2, arraySize);
free2DFloatArray(nextArray2, arraySize);
}
// Cleanup
free2DFloatArray(currArray, arraySize);
free2DFloatArray(nextArray, arraySize);
long long durr_us = (end_tv.tv_sec * (long long)1000000 + end_tv.tv_usec) -
(start_tv.tv_sec * (long long)1000000 + start_tv.tv_usec);
printf("Relaxed %d square matrix in %llu microsceonds\n",
arraySize, durr_us);
if(testRight)
{
printf("Threaded relaxation result %s the serial result.\n",
correct ? "matched" : "didnt match");
}
// Windows command to stop console applications closing immediately.
system("pause");
return 0;
}