-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
67 lines (54 loc) · 1.44 KB
/
common.h
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
/*
common.c
Jordan Kremer
Dalton Bohning
Contains code shared between brent-kung and openmp.
This mostly includes testing and a C implementation.
*/
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
//Inclusive, iterative version of prefix sum in C
void parallelScan_iterative(float *X, float *Y, int size) {
float acc = X[0];
Y[0] = acc;
for (int i = 1; i < size; ++i) {
acc += X[i];
Y[i] = acc;
}
}
//Runs the iterative version and verifies the results
bool verify(float *X, float *Y, int size){
float *Y_ = (float*) malloc(size * sizeof(float));
parallelScan_iterative(X, Y_, size);
for (int i = 0; i < size; ++i){
if (Y[i] != Y_[i]) {
printf("Expected %.0f but got %.0f at Y[%d]\n", Y_[i], Y[i], i);
free(Y_);
return false;
}
}
free(Y_);
return true;
}
//Print 10 elements per line
void printArray(float *A, int size){
for(int i = 0; i < size; ++i) {
printf("%.0f ", A[i]);
if((i+1) % 10 == 0){
printf("\n");
}
}
printf("\n");
}
/* Every 100th index gets the value of 1.
All others get the value 0.
This is done to maintain the precision of floats,
since the maximum precise integer that can be represented
is 2^24 = 16,777,216.
This allows the maximum array size to be
2^24 * 100 = 1,677,721,600, while maintaining precision. */
void initArray(float *A, int size) {
for(int i = 0; i < size; ++i)
A[i] = (i % 100 == 0) ? 1 : 0;
}