-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacciSphereOpenACC.cpp
379 lines (333 loc) · 10.9 KB
/
fibonacciSphereOpenACC.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <float.h>
#include <openacc.h>
#define PI 3.14159265358979323846
const double PHI = ((sqrt(5) - 1) / 2); // not to be confused with phi
const double GA = PHI * 2.0 * PI;
struct UnitVector {
float x;
float y;
float z;
float theta; // longitude in radians [-PI, PI]
float phi; // latitude in radians [0, PI]
};
struct Bin {
int offset;
float theta;
};
// Declare Functions
float randomFloat();
float randomFloatMax(float max);
float randomFloatMinMax(float min, float max);
int getOffsetAfter(Bin *map, int length, float phi);
int getOffsetBefore(Bin *map, int length, float phi);
void merge(Bin *merged, Bin *left, int leftCount, Bin *right, int rightCount);
void mergeSort(Bin *array, int length);
void generateUnitVectorFromIndex(UnitVector *p, int index, int length);
void generateUnitVectorFromCartesian(UnitVector *p);
void generateUnitVectorFromSpherical(UnitVector *p);
float distanceBetween(UnitVector *p1, UnitVector *p2);
// arg[1]: number of bins to generate
// arg[2]: number of points to generate
// arg[3]: seed (optional)
int main(int argc, char **argv) {
bool debug = false;
int numBins = 100000;
int numPoints = 1000000;
time_t seed = time(NULL);
if (debug) {
seed = 123456789;
}
else {
if ((argc > 5) || (3 > argc)) {
printf("Need 2 or 3 arguments:\nBins\nPoints\nSeed (optional)\n");
return 1;
}
numBins = atoi(argv[1]);
numPoints = atoi(argv[2]);
if (3 < argc) {
seed = atoi(argv[3]);
}
}
printf("Bins: %d\n", numBins);
printf("Points: %d\n", numPoints);
printf("Seed: %d\n", seed);
srand(seed);
struct timeval startGenPoints;
struct timeval endGenPoints;
gettimeofday(&startGenPoints, NULL);
UnitVector *points;
points = (UnitVector *) malloc(numPoints * sizeof(UnitVector));
if (points == NULL) {
printf("Error allocating memory for the array of points\n");
return 1;
}
float x;
float y;
float z;
float mag;
// Generate Points
UnitVector *p = NULL;
int pointCounter = 0;
while (pointCounter < numPoints) {
x = randomFloatMinMax(-1, 1);
y = randomFloatMinMax(-1, 1);
z = randomFloatMinMax(-1, 1);
mag = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
if ((0 < mag) && (mag < 1)) { // skip if all random numbers are 0 or if the random point is outside of the unit sphere
p = &points[pointCounter];
p->x = x / mag;
p->y = y / mag;
p->z = z / mag;
generateUnitVectorFromCartesian(p);
pointCounter++;
}
}
gettimeofday(&endGenPoints, NULL);
struct timeval startGenMaps;
struct timeval endGenMaps;
gettimeofday(&startGenMaps, NULL);
float radius = pow((3 * numBins) / (4 * PI), 1.0 / 3.0);
float exactCircumference = 2.0 * PI * radius;
int circumference = ceil(exactCircumference);
// Generate Unsorted Maps
Bin **maps;
maps = (Bin **) calloc(circumference, sizeof(Bin *));
if (maps == NULL) {
printf("Error allocating memory for the maps\n");
return 1;
}
maps[0] = (Bin *) malloc(circumference * sizeof(Bin));
if (maps[0] == NULL) {
printf("Error allocating memory for map with circumference %d\n", circumference);
return 1;
}
Bin *map = maps[0];
Bin *bin = NULL;
int binCounter;
for (binCounter = 0; binCounter < circumference; binCounter++) {
bin = &map[binCounter];
bin->theta = fmod(GA * binCounter, PI * 2.0); // [0, 2PI] only positive because we either add or subtract offsets
bin->offset = binCounter;
}
int mapCounter;
for (mapCounter = 1; mapCounter < circumference; mapCounter++) {
maps[mapCounter] = (Bin *) malloc((circumference - mapCounter) * sizeof(Bin));
if (maps[mapCounter] == NULL) {
printf("Error allocating memory for map with circumference %d\n", circumference - mapCounter);
return 1;
}
memcpy(maps[mapCounter], maps[mapCounter - 1], (circumference - mapCounter) * sizeof(Bin));
}
// Sort Maps
#pragma acc parallel
{
#pragma acc loop
for (mapCounter = 0; mapCounter < circumference; mapCounter++) {
mergeSort(maps[mapCounter], circumference - mapCounter);
}
}
gettimeofday(&endGenMaps, NULL);
struct timeval startBinPoints;
struct timeval endBinPoints;
gettimeofday(&startBinPoints, NULL);
// Bin Points
int *results = (int *) calloc(numBins, sizeof(int));
if (results == NULL) {
printf("Error allocating memory for results\n");
return 1;
}
int numPotentials = 4;
int badPoints = 0;
#pragma acc parallel
{
int localCircumference;
int mapIndex;
float upperThetaOffset;
float lowerThetaOffset;
int guessIndex;
UnitVector guess;
int bestPointIndex;
float bestDistance;
float testDistance;
UnitVector testPoint;
bool found;
int guessCounter;
#pragma acc loop reduction (+ : badPoints)
for (pointCounter = 0; pointCounter < numPoints; pointCounter++) {
int *potentials = (int*) malloc(numPotentials * sizeof(int));
// if (potentials == NULL) {
// printf("Error allocating memory for potentials\n");
// }
p = &points[pointCounter];
guessIndex = floor(numBins * (p->z + 1) / 2);
generateUnitVectorFromIndex(&guess, guessIndex, numBins);
localCircumference = ceil(exactCircumference * sqrt(pow(p->x, 2) + pow(p->y, 2)));
if (localCircumference > circumference) {
localCircumference = circumference;
}
mapIndex = circumference - localCircumference;
if (guess.theta < p->theta) {
upperThetaOffset = p->theta - guess.theta;
lowerThetaOffset = (2 * PI) - upperThetaOffset;
}
else {
lowerThetaOffset = guess.theta - p->theta;
upperThetaOffset = (2 * PI) - lowerThetaOffset;
}
potentials[0] = getOffsetAfter(maps[mapIndex], localCircumference, lowerThetaOffset);
potentials[1] = getOffsetBefore(maps[mapIndex], localCircumference, lowerThetaOffset);
potentials[2] = getOffsetBefore(maps[mapIndex], localCircumference, upperThetaOffset);
potentials[3] = getOffsetAfter(maps[mapIndex], localCircumference, upperThetaOffset);
found = false;
bestDistance = FLT_MAX;
for (guessCounter = 0; guessCounter < numPotentials; guessCounter++) {
potentials[guessCounter] *= pow(-1, (guessCounter / 2) + 1);
potentials[guessCounter] += guessIndex;
if ((0 <= potentials[guessCounter]) && (potentials[guessCounter] < numBins)) {
generateUnitVectorFromIndex(&testPoint, potentials[guessCounter], numBins);
testDistance = distanceBetween(&testPoint, p);
if (testDistance < bestDistance) {
bestPointIndex = potentials[guessCounter];
bestDistance = testDistance;
}
}
}
if (found) {
#pragma acc atomic capture
{
results[bestPointIndex]++;
}
}
else {
badPoints++;
}
free(potentials);
}
}
gettimeofday(&endBinPoints, NULL);
if (badPoints > 0) {
printf("\n");
printf("Bad Points:%d\n", badPoints);
}
printf("\n");
printf("Timing:\n");
printf("Point Generation: %f\n", ((double)(1000000 * (endGenPoints.tv_sec - startGenPoints.tv_sec)) + (double)(endGenPoints.tv_usec - startGenPoints.tv_usec)) / (double)1000000);
printf("Map Generation: %f\n", ((double)(1000000 * (endGenMaps.tv_sec - startGenMaps.tv_sec)) + (double)(endGenMaps.tv_usec - startGenMaps.tv_usec)) / (double)1000000);
printf("Point Binning: %f\n", ((double)(1000000 * (endBinPoints.tv_sec - startBinPoints.tv_sec)) + (double)(endBinPoints.tv_usec - startBinPoints.tv_usec)) / (double)1000000);
// printf("\n");
// printf("Results:\n");
// for (binCounter = 0; binCounter < numBins; binCounter++) {
// printf("%d: %d\n", binCounter, results[binCounter]);
// }
// Clean Up
free(results);
for (mapCounter = 0; mapCounter < circumference; mapCounter++) {
free(maps[mapCounter]);
}
free(maps);
free(points);
return 0;
}
float randomFloat() {
return rand() / (float) RAND_MAX;
};
float randomFloatMax(float max) {
return max * randomFloat();
};
float randomFloatMinMax(float min, float max) {
return randomFloatMax(max - min) + min;
};
int getOffsetAfter(Bin *map, int length, float theta) {
int offset = 0;
int binCounter;
for (binCounter = 0; binCounter < length; binCounter++) {
if (theta < map[binCounter].theta) {
return map[binCounter].offset;
}
}
return map[0].offset;
}
int getOffsetBefore(Bin *map, int length, float theta) {
int offset = 0;
int binCounter;
for (binCounter = length - 1; binCounter >= 0; binCounter--) {
if (theta > map[binCounter].theta) {
return map[binCounter].offset;
}
}
return map[length - 1].offset;
}
// will overwrite the unit vector at p
void generateUnitVectorFromIndex(UnitVector *p, int index, int length) {
p->phi = acos(-1.0 + (2.0 * ((float) (index % length) / (float) (length - 1))));
p->theta = fmod(GA * index, PI * 2.0); // [0, 2PI]
if (p->theta > PI) {
p->theta -= 2 * PI; // [-PI, PI]
}
generateUnitVectorFromSpherical(p);
}
// expecting x, y, and z to be filled in already
void generateUnitVectorFromCartesian(UnitVector *p) {
p->phi = acos(p->z);
p->theta = atan2(p->y, p->x);
}
// expecting phi and theta to be filled in already
void generateUnitVectorFromSpherical(UnitVector *p) {
p->x = sin(p->phi) * cos(p->theta);
p->y = sin(p->phi) * sin(p->theta);
p->z = cos(p->phi);
}
float distanceBetween(UnitVector *p1, UnitVector *p2) {
float distance = 0;
distance += pow(p1->x - p2->x, 2);
distance += pow(p1->y - p2->y, 2);
distance += pow(p1->z - p2->z, 2);
return sqrt(distance);
}
// Merged Sort Adapted From:
// https://gist.github.com/mycodeschool/9678029#file-mergesort_c-c-L25
void merge(Bin *merged, Bin *left, int leftCount, Bin *right, int rightCount) {
int leftIndex = 0;
int rightIndex = 0;
int mergedIndex = 0;
while ((leftIndex < leftCount) && (rightIndex < rightCount)) {
if (left[leftIndex].theta > right[rightIndex].theta) { // > ascending, < descending
memcpy(&merged[mergedIndex++], &right[rightIndex++], sizeof(Bin));
}
else {
memcpy(&merged[mergedIndex++], &left[leftIndex++], sizeof(Bin));
}
}
while (leftIndex < leftCount) {
memcpy(&merged[mergedIndex++], &left[leftIndex++], sizeof(Bin));
}
while (rightIndex < rightCount) {
memcpy(&merged[mergedIndex++], &right[rightIndex++], sizeof(Bin));
}
}
void mergeSort(Bin *array, int length) {
int mid;
int i;
Bin *left;
Bin *right;
if (length < 2) {
return;
}
mid = length / 2;
left = (Bin*) malloc(mid * sizeof(Bin));
right = (Bin*) malloc((length - mid) * sizeof(Bin));
// TODO: check to make sure allocation worked?
memcpy(left, array, mid * sizeof(Bin));
memcpy(right, &array[mid], (length - mid) * sizeof(Bin));
mergeSort(left, mid);
mergeSort(right, length - mid);
merge(array, left, mid, right, length - mid);
free(left);
free(right);
}