-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhof.c
611 lines (473 loc) · 14.1 KB
/
hof.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
// Typedef for general void* fun(void*) function
typedef void* gen_fnptr(void*);
typedef void fast_fnptr(void*,void*);
#define NUMTHREADS 3
/* Wrapper Functions for error checking */
void errorReportNum(char* string);
void errorReport(char* string);
int Pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*routine)(void *), void *arg)
{
int rc = pthread_create(tid, attr, routine, arg);
if (rc)
{
errno = rc;
errorReportNum("Couldn't create a thread!");
}
return rc;
}
typedef struct map_shared_info {
size_t fromsize;
size_t tosize;
void* inputarr;
void* outputarr;
void* mapfn;
} msi;
typedef struct map_threadStruct {
size_t threadLen;
size_t startingOffsetIndex;
msi *mapinfo;
} map_ts;
/* Definition of map
*
* map(size, in_type, out_type, in_array, out_array, fn)
*
* size - size of array
* in_type - type of input array
* out_type - type of output array
* in_array - address to input array
* out_array - address to store output array
* fn - function to map
*
*/
/*
silly error-reporting function
*/
void errorReport(char* string)
{
printf("ERROR: %s\n", string);
fflush(stdout);
exit(1);
}
void errorReportNum(char* string)
{
printf("ERROR: %s\n%s\n", string, strerror(errno));
fflush(stdout);
exit(1);
}
void* thread_map_fun(void* vargp);
void* thread_map_fast_fun(void* vargp);
#define map(size,type1,type2,inarr,outarr, fn) (map_fun(size, sizeof(type1), sizeof(type2),(void*)inarr,(void*) outarr, (gen_fnptr*)fn))
void map_fun(size_t arrlen, size_t fromsize, size_t tosize, void* inputarr, void* outputarr, gen_fnptr* mapfn)
{
// Optimize acc in registers -- we can explicitly do this, do we want to?
int tosizeacc=0;
int fromsizeacc=0;
int i;
int rc;
int offset = arrlen / NUMTHREADS;
/* lastOffset compensates for a remainder of jobs, if the jobs
aren't divided cleanly */
int lastOffset = arrlen - (offset * (NUMTHREADS - 1));
// Dispatch worker threads
pthread_t threads[NUMTHREADS];
map_ts* threadStuff;
msi mapinfo;
// initialize shared map info
mapinfo.fromsize = fromsize;
mapinfo.tosize = tosize;
mapinfo.inputarr = inputarr;
mapinfo.outputarr = outputarr;
mapinfo.mapfn = mapfn;
for (i=0; i < NUMTHREADS - 1; i++)
{
threadStuff = malloc(sizeof(map_ts));
if (threadStuff == NULL) errorReport("couldn't malloc threadStuff in map");
threadStuff->threadLen = offset;
threadStuff->startingOffsetIndex = offset * i;
threadStuff->mapinfo = &mapinfo;
rc = pthread_create(&threads[i],NULL, thread_map_fun, (void *)threadStuff);
if (rc)
{
errno = rc;
errorReportNum("couldn't create a thread in Map");
}
}
threadStuff = malloc(sizeof(map_ts));
if (threadStuff == NULL) errorReport("couldn't malloc threadStuff in map");
threadStuff->threadLen = lastOffset;
threadStuff->startingOffsetIndex = offset * i;
threadStuff->mapinfo = &mapinfo;
rc = pthread_create(&threads[i], NULL, thread_map_fun, (void *)threadStuff);
if (rc)
{
errno = rc;
errorReportNum("couldn't create a thread in Map");
}
// Reap worker threads
for (i=0; i < NUMTHREADS; i++)
{
pthread_join(threads[i], NULL); // rc holds the return code for error checking later
}
}
void* thread_map_fun(void* vargp)
{
map_ts* threadStuff = (map_ts *)vargp;
int threadLen = threadStuff->threadLen;
msi *mapinfo = threadStuff->mapinfo;
int fromsize = mapinfo->fromsize;
int tosize = mapinfo->tosize;
void* inputarr = mapinfo->inputarr;
void* outputarr = mapinfo->outputarr;
gen_fnptr* mapfn = (gen_fnptr*)mapinfo->mapfn;
int fromsizeacc = threadStuff->startingOffsetIndex * fromsize;
int tosizeacc = threadStuff->startingOffsetIndex * tosize;
int i;
for (i = 0; i < threadLen; i++)
{
// Get the pointer to the output and copy it over
// to new array
void* retnptr = mapfn((char*)inputarr + fromsizeacc);
memcpy((char*)outputarr + tosizeacc, retnptr, tosize);
// We ask the user to give us allocated space that we own
free(retnptr);
// Update accumulator sizes (For optimization)
fromsizeacc += fromsize;
tosizeacc += tosize;
}
free(threadStuff);
return NULL;
}
#define map_fast(size,type1,type2,inarr,outarr, fn) (map_fast_fun(size, sizeof(type1), sizeof(type2),(void*)inarr,(void*) outarr, (fast_fnptr*)fn))
void map_fast_fun(size_t arrlen, size_t fromsize, size_t tosize, void* inputarr, void* outputarr, fast_fnptr* mapfn)
{
// Optimize acc in registers
int tosizeacc=0;
int fromsizeacc=0;
int i;
int rc;
int offset = arrlen / NUMTHREADS;
/* lastOffset compensates for a remainder of jobs, if the jobs
aren't divided cleanly */
int lastOffset = arrlen - (offset * (NUMTHREADS - 1));
// Dispatch worker threads
pthread_t threads[NUMTHREADS];
map_ts* threadStuff;
msi mapinfo;
// initialize shared map info
mapinfo.fromsize = fromsize;
mapinfo.tosize = tosize;
mapinfo.inputarr = inputarr;
mapinfo.outputarr = outputarr;
mapinfo.mapfn = mapfn;
for (i=0; i < NUMTHREADS - 1; i++)
{
threadStuff = malloc(sizeof(map_ts));
if (threadStuff == NULL) errorReport("couldn't malloc threadStuff in map");
threadStuff->threadLen = offset;
threadStuff->startingOffsetIndex = offset * i;
threadStuff->mapinfo = &mapinfo;
rc = pthread_create(&threads[i],NULL, thread_map_fast_fun, (void *)threadStuff);
if (rc)
{
errno = rc;
errorReportNum("couldn't create a thread in Map");
}
}
threadStuff = malloc(sizeof(map_ts));
if (threadStuff == NULL) errorReport("couldn't malloc threadStuff in map");
threadStuff->threadLen = lastOffset;
threadStuff->startingOffsetIndex = offset * i;
threadStuff->mapinfo = &mapinfo;
rc = pthread_create(&threads[i], NULL, thread_map_fast_fun, (void *)threadStuff);
if (rc)
{
errno = rc;
errorReportNum("couldn't create a thread in Map");
}
// Reap worker threads
for (i=0; i < NUMTHREADS; i++)
{
pthread_join(threads[i], NULL); // rc holds the return code for error checking later
}
}
void* thread_map_fast_fun(void* vargp)
{
map_ts* threadStuff = (map_ts *)vargp;
int threadLen = threadStuff->threadLen;
int fromsize = threadStuff->mapinfo->fromsize;
int tosize = threadStuff->mapinfo->tosize;
int fromsizeacc = threadStuff->startingOffsetIndex * fromsize;
int tosizeacc = threadStuff->startingOffsetIndex * tosize;
void* inputarr = threadStuff->mapinfo->inputarr;
void* outputarr = threadStuff->mapinfo->outputarr;
fast_fnptr* mapfn = (fast_fnptr*)threadStuff->mapinfo->mapfn;
int i;
for (i = 0; i < threadLen; i++)
{
// Get the pointer to the output and copy it over
// to new array
mapfn((char*)inputarr + fromsizeacc, (char*)outputarr + tosizeacc);
// Update accumulator sizes (For optimization)
fromsizeacc += fromsize;
tosizeacc += tosize;
}
free(threadStuff);
return NULL;
}
typedef void* tab_fnptr(int*);
#define tabulate(size,type,fn) (tabulate_fun(size, sizeof(type), (tab_fnptr*)fn))
void* tabulate_fun(size_t arrlen, size_t size, tab_fnptr* tabfn)
{
// Create an array of inmap_ts - This can be threaded!
int *inputarr = malloc(sizeof(int) * arrlen);
int i;
for (i=0; i < arrlen; i++)
{
inputarr[i] = i;
}
// Allocate memory for tabulated array and map the function on it
void* outputarr = malloc(sizeof(size) * arrlen);
map_fun(arrlen, sizeof(int), size, inputarr, outputarr, (gen_fnptr*)tabfn);
return outputarr;
}
/* Reduce Data structures */
typedef void* reducefun(void*,void*);
typedef struct reduce_shared_info
{
size_t elesize;
reducefun* reducefn;
void* baseCase;
size_t reducedSize;
} reduceinfo;
typedef struct reduce_threadstruct
{
size_t arrlen;
void *inputarr;
reduceinfo *rdinfo;
} reduce_ts;
/* Method declaration */
#define reduce(n,type, inarr, fn) reduce_fun_wrapper(n,sizeof(type),inarr,(reducefun*)fn);
void* reduce_fun_wrapper(size_t arrlen, size_t elesize, void* inputarr, reducefun* reducefn);
void* reduce_fun(size_t arrlen, void* inputarr, reduceinfo* rdinfo);
void* reduce_fun_wrapper(size_t arrlen, size_t elesize, void* inputarr, reducefun* reducefn)
{
reduceinfo rdinfo;
rdinfo.elesize = elesize;
rdinfo.reducefn = reducefn;
return reduce_fun(arrlen, inputarr, &rdinfo);
}
reduce_ts* get_reduce_ts(size_t arrlen, void* inputarr, reduceinfo* rdinfo)
{
reduce_ts *rts = malloc(sizeof(reduce_ts));
rts->arrlen = arrlen;
rts->inputarr = inputarr;
rts->rdinfo = rdinfo;
return rts;
}
void* thread_reduce_fun(void* tinfo)
{
reduce_ts *rts = tinfo;
size_t arrlen = rts->arrlen;
void** inputarr = (void**)rts->inputarr;
reduceinfo* rdinfo = rts->rdinfo;
size_t elesize = rdinfo->elesize;
reducefun* reducefn = rdinfo->reducefn;
void* finalResult = malloc(elesize);
void* result = malloc(elesize);
int i = 0;
memcpy(result, inputarr, elesize);
for (i = 0; i < arrlen - 1; i++)
{
void * tmp=result;
result = reducefn(result, (void*)(((char*)inputarr) + elesize*(i+1)));
free(tmp);
}
memcpy(finalResult, result, rdinfo->elesize);
return finalResult;
}
void* reduce_fun(size_t arrlen, void* inputarr, reduceinfo* rdinfo)
{
// Initialize reduce info variables
void** result = calloc(NUMTHREADS, sizeof(void*));
void* finalResult;
size_t elesize = rdinfo->elesize;
reducefun* reducefn = rdinfo->reducefn;
// If singleton case
if (arrlen<=1)
{
finalResult = malloc(elesize);
memcpy(finalResult, inputarr, elesize);
free(result);
return finalResult;
}
int offset = arrlen / NUMTHREADS;
/* lastOffset compensates for a remainder of jobs, if the jobs
aren't divided cleanly */
int lastOffset = arrlen - (offset * (NUMTHREADS - 1));
// Calculate split
size_t mid = arrlen/2; // or div by threadnum
// Reduce subtrees in threads
// Non-threaded version
reduce_ts* rts[NUMTHREADS];
//Prepare and use threads
int i;
pthread_t pid[NUMTHREADS];
for (i = 0; i < NUMTHREADS - 1; i++)
{
rts[i] = get_reduce_ts(offset, ((char *)inputarr + offset*i * elesize), rdinfo);
Pthread_create(&pid[i], NULL, thread_reduce_fun, (void*) rts[i]);
}
rts[i] = get_reduce_ts(lastOffset, ((char *)inputarr + offset*i*elesize), rdinfo);
Pthread_create(&pid[i], NULL, thread_reduce_fun, (void*) rts[i]);
for (i = 0; i < NUMTHREADS; i++)
{
pthread_join(pid[i], &(result[i]));
}
// Reduce results
finalResult = malloc(elesize);
memcpy(finalResult, result[0], elesize);
for (i = 0; i < NUMTHREADS-1; i++)
{
void* tmp = finalResult;
finalResult = reducefn(tmp, result[i+1]);
free(tmp);
}
free(result);
return finalResult;
}
/**
*
* ALL DA TESTING FUNCTIONS IN DA HOUSE
*
*
**/
int* square(int* a)
{
int* result=malloc(sizeof(int));
int num = *a;
*result = num*num;
return result;
}
float* fraction(int* a)
{
float* result=malloc(sizeof(int));
int num = *a;
*result = (float)(1) / (float)num;
return result;
}
struct tup
{
int orig;
int origtimes2;
};
struct tup* evaltup(int* a)
{
struct tup* retnval = malloc(sizeof(struct tup));
int num = *a;
retnval->orig = num;
retnval->origtimes2 = num*2;
return retnval;
}
void evaltup_fast(int* a, struct tup* b)
{
int num = *a;
b->orig = num;
b->origtimes2 = num * 2;
}
// reduce
int* intadd(int* a, int* b)
{
int* result = malloc(sizeof(int));
*result = *a + *b;
return result;
}
// tabulate
double* dblx5fn (int* a)
{
double* result = malloc(sizeof(double));
*result = (double)(*a *5);
return result;
}
// Main function for testing
int main(int argc, char **argv)
{
printf("Main Function\n");
printf("Test 1: square\n");
int intarr[10] = {0,1,2,3,4,5,6,7,8,9};
int retnarr[10];
map(10,int,int,intarr, retnarr, &square);
int i=0;
for (i=0;i<10;i++)
{
printf("%d ", retnarr[i]);
}
printf("END\n\n");
// Test 2
// Output 1/x
//
printf("Test 2: 1/x\n");
float retnarr2[10];
map(10,int,int,intarr, retnarr2, &fraction);
i=0;
for (i=0;i<10;i++)
{
printf("%f ", retnarr2[i]);
}
printf("END\n\n");
// Test 3
// Times 2 struct
//
struct tup retnarr3[10];
printf("Test 3: orig and orig*2 struct\n");
map(10,int,struct tup, intarr, retnarr3, &evaltup);
i=0;
for (i=0;i<10;i++)
{
printf("(%d,%d) ", retnarr3[i].orig, retnarr3[i].origtimes2);
}
printf("END\n\n");
// Test 4
// Times 2 struct FAST MAP
//
struct tup retnarr4[10];
printf("Test 4: orig and orig*2 struct - MAP FAST\n");
map_fast(10,int,struct tup, intarr, retnarr4, &evaltup_fast);
i=0;
for (i=0;i<10;i++)
{
printf("(%d,%d) ", retnarr4[i].orig, retnarr4[i].origtimes2);
}
printf("END\n\n");
// Test 5
// Simple reduce
printf("Test 5: Reduce addition 0 - 9\n");
int* intresult = reduce(10,int,intarr,(reducefun*) &intadd);
printf("%d\n", *intresult);
printf("END\n\n");
// Test 5a
// Simple reduce big
printf("Test 5: Reduce addition 0 - 1499\n");
int size = 1500;
int intarrbig[size];
for (i=0; i<size; i++)
{
intarrbig[i]=i;
}
intresult = reduce(size,int,intarrbig,(reducefun*) &intadd);
printf("%d\n", *intresult);
printf("END\n\n");
// Test 6 Tabulate double *5
printf("Test 6: Tabulate Doubles\n");
double* dblarr = tabulate(20, double, &dblx5fn);
for (i=0; i<20; i++)
{
printf("%f ", dblarr[i]);
}
printf("END\n\n");
return 0;
}