-
Notifications
You must be signed in to change notification settings - Fork 8
/
collisions.cu
631 lines (528 loc) · 19.3 KB
/
collisions.cu
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include <climits>
#include <cstdio>
#include <stdint.h>
#include <cuda_runtime.h>
#include <curand.h>
#include "collisions.cuh"
#include "collisions.h"
__device__ void dPrefixSum(uint32_t *values, unsigned int n) {
int offset = 1;
int a;
uint32_t temp;
// upsweep
for (int d = n / 2; d; d /= 2) {
__syncthreads();
if (threadIdx.x < d) {
a = (threadIdx.x * 2 + 1) * offset - 1;
values[a + offset] += values[a];
}
offset *= 2;
}
if (!threadIdx.x) {
values[n - 1] = 0;
}
// downsweep
for (int d = 1; d < n; d *= 2) {
__syncthreads();
offset /= 2;
if (threadIdx.x < d) {
a = (threadIdx.x * 2 + 1) * offset - 1;
temp = values[a];
values[a] = values[a + offset];
values[a + offset] += temp;
}
}
}
__device__ void dSumReduce(unsigned int *values, unsigned int *out) {
// wait for the whole array to be populated
__syncthreads();
// sum by reduction, using half the threads in each subsequent iteration
unsigned int threads = blockDim.x;
unsigned int half = threads / 2;
while (half) {
if (threadIdx.x < half) {
// only keep going if the thread is in the first half threads
for (int k = threadIdx.x + half; k < threads; k += half) {
values[threadIdx.x] += values[k];
}
threads = half;
}
half /= 2;
// make sure all the threads are on the same iteration
__syncthreads();
}
// only let one thread update the current sum
if (!threadIdx.x) {
atomicAdd(out, values[0]);
}
}
__global__ void cellCollideKernel(uint32_t *cells, uint32_t *objects,
float *positions, float *velocities,
float *dims, unsigned int n, unsigned int m,
unsigned int cells_per_thread,
unsigned int *collision_count,
unsigned int *test_count) {
extern __shared__ unsigned int t[];
int thread_start = (blockIdx.x * blockDim.x + threadIdx.x) *
cells_per_thread;
int thread_end = thread_start + cells_per_thread;
int start = -1;
int i = thread_start;
uint32_t last = UINT32_MAX;
uint32_t home;
uint32_t phantom;
unsigned int h;
unsigned int p;
unsigned int collisions = 0;
unsigned int tests = 0;
float dh;
float dp;
float dx;
float d;
while (1) {
// find cell ID change indices
if (i >= m || cells[i] >> 1 != last) {
// at least one home-cell object and at least one other object present
if (start + 1 && h >= 1 && h + p >= 2) {
for (int j = start; j < start + h; j++) {
home = objects[j] >> 1;
dh = dims[home];
for (int k = j + 1; k < i; k++) {
// count the number of tests performed
tests++;
phantom = objects[k] >> 1;
dp = dims[phantom] + dh;
d = 0;
for (int l = 0; l < DIM; l++) {
dx = positions[phantom + l * n] -
positions[home + l * n];
d += dx * dx;
}
// if collision
if (d < dp * dp) {
collisions++;
}
}
}
}
// if we're already past the cells assigned to this thread, we're done
if (i > thread_end || i >= m) {
break;
}
// the first thread starts immediately; the others wait until a change
if (i != thread_start || !blockIdx.x && !threadIdx.x) {
// reset counters for new cell
h = 0;
p = 0;
start = i;
}
last = cells[i] >> 1;
}
// only process collisions that are not handled by a previous thread
if (start + 1) {
if (objects[i] & 0x01) {
// increment home cells
h++;
} else {
// increment phantom cells
p++;
}
}
i++;
}
t[threadIdx.x] = collisions;
dSumReduce(t, collision_count);
__syncthreads();
t[threadIdx.x] = tests;
dSumReduce(t, test_count);
}
__global__ void InitCellKernel(uint32_t *cells, uint32_t *objects,
float *positions, float *dims, unsigned int n,
float cell_dim, unsigned int *cell_count) {
extern __shared__ unsigned int t[];
unsigned int count = 0;
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x *
blockDim.x) {
uint32_t hash = 0;
unsigned int sides = 0;
int h = i * DIM_2;
int m = 1;
int q;
int r;
float x;
float a;
// find home cell
for (int j = 0; j < DIM; j++) {
x = positions[n * j + i];
// cell ID is simply the bits of each cell coordinate concatenated
hash = hash << 8 | (uint32_t) (x / cell_dim);
// determine if the cell is close enogh to overlap cells on the side
x -= floor(x / cell_dim) * cell_dim;
// we're only using the first dimension (assume circular objects)
a = dims[i];
sides <<= 2;
// keep track of which side of the center, if any, the object overlaps
if (x < a) {
sides |= 3;
} else if (cell_dim - x < a) {
sides |= 1;
}
}
// bit 0 unset indicates home cell
cells[h] = hash << 1 | 0x00;
objects[h] = i << 1 | 0x01;
count++;
// find phantom cells in the Moore neighborhood
for (int j = 0; j < DIM_3; j++) {
// skip the home (center) cell since it's already been added
if (j == DIM_3 / 2) {
continue;
}
// run through the components of each potential side cell
q = j;
hash = 0;
for (int k = 0; k < DIM; k++) {
r = q % 3 - 1;
x = positions[n * k + i];
// skip this cell if the object is on the wrong side
if (r && (sides >> (DIM - k - 1) * 2 & 0x03 ^ r) & 0x03 ||
x + r * cell_dim < 0 || x + r * cell_dim >= 1) {
hash = UINT32_MAX;
break;
}
// cell ID of the neighboring cell
hash = hash << 8 | (uint32_t) (x / cell_dim) + r;
q /= 3;
}
// only add this cell to the list if there's potential overlap
if (hash != UINT32_MAX) {
// count total number of cells occupied
count++;
h++;
cells[h] = hash << 1 | 0x01;
// bit 0 set indicates phantom cell
objects[h] = i << 1 | 0x00;
// keep track of number of cells occupied
m++;
}
}
// fill up remaining cells
while (m < DIM_2) {
h++;
cells[h] = UINT32_MAX;
objects[h] = i << 2;
m++;
}
}
// perform reduction to count number of cells occupied
t[threadIdx.x] = count;
dSumReduce(t, cell_count);
}
__global__ void PrefixSumKernel(uint32_t *values, unsigned int n) {
extern __shared__ uint32_t s[];
for (int i = 0; i < n; i++) {
s[i] = values[i];
}
dPrefixSum(s, n);
for (int i = 0; i < n; i++) {
values[i] = s[i];
}
}
__global__ void RadixOrderKernel(uint32_t *keys_in, uint32_t *values_in,
uint32_t *keys_out, uint32_t *values_out,
uint32_t *radices, uint32_t *radix_sums,
unsigned int n, unsigned int cells_per_group,
int shift) {
extern __shared__ uint32_t s[];
uint32_t *t = s + NUM_RADICES;
int group = threadIdx.x / THREADS_PER_GROUP;
int group_start = (blockIdx.x * GROUPS_PER_BLOCK + group) * cells_per_group;
int group_end = group_start + cells_per_group;
uint32_t k;
// initialize shared memory
for (int i = threadIdx.x; i < NUM_RADICES; i += blockDim.x) {
s[i] = radix_sums[i];
// copy the last element in each prefix-sum to a separate array
if (!((i + 1) % (NUM_RADICES / NUM_BLOCKS))) {
t[i / (NUM_RADICES / NUM_BLOCKS)] = s[i];
}
}
__syncthreads();
// add padding to array for prefix-sum
for (int i = threadIdx.x + NUM_BLOCKS; i < PADDED_BLOCKS; i += blockDim.x) {
t[i] = 0;
}
__syncthreads();
// calculate prefix-sum on radix counters
dPrefixSum(t, PADDED_BLOCKS);
__syncthreads();
// add offsets to prefix-sum values
for (int i = threadIdx.x; i < NUM_RADICES; i += blockDim.x) {
s[i] += t[i / (NUM_RADICES / NUM_BLOCKS)];
}
__syncthreads();
// add offsets to radix counters
for (int i = threadIdx.x; i < GROUPS_PER_BLOCK * NUM_RADICES; i +=
blockDim.x) {
t[i] = radices[(i / GROUPS_PER_BLOCK * NUM_BLOCKS + blockIdx.x) *
GROUPS_PER_BLOCK + i % GROUPS_PER_BLOCK] + s[i / GROUPS_PER_BLOCK];
}
__syncthreads();
// rearrange key-value pairs
for (int i = group_start + threadIdx.x % THREADS_PER_GROUP; i < group_end &&
i < n; i += THREADS_PER_GROUP) {
// need only avoid bank conflicts by group
k = (keys_in[i] >> shift & NUM_RADICES - 1) * GROUPS_PER_BLOCK + group;
// write key-value pairs sequentially by thread in the thread group
for (int j = 0; j < THREADS_PER_GROUP; j++) {
if (threadIdx.x % THREADS_PER_GROUP == j) {
keys_out[t[k]] = keys_in[i];
values_out[t[k]] = values_in[i];
t[k]++;
}
}
}
}
__global__ void RadixSumKernel(uint32_t *radices, uint32_t *radix_sums) {
extern __shared__ uint32_t s[];
uint32_t total;
uint32_t left = 0;
uint32_t *radix = radices + blockIdx.x * NUM_RADICES * GROUPS_PER_BLOCK;
for (int j = 0; j < NUM_RADICES / NUM_BLOCKS; j++) {
// initialize shared memory
for (int i = threadIdx.x; i < NUM_BLOCKS * GROUPS_PER_BLOCK; i +=
blockDim.x) {
s[i] = radix[i];
}
__syncthreads();
// add padding to array for prefix-sum
for (int i = threadIdx.x + NUM_BLOCKS * GROUPS_PER_BLOCK; i <
PADDED_GROUPS; i += blockDim.x) {
s[i] = 0;
}
__syncthreads();
if (!threadIdx.x) {
total = s[PADDED_GROUPS - 1];
}
// calculate prefix-sum on radix counters
dPrefixSum(s, PADDED_GROUPS);
__syncthreads();
// copy to global memory
for (int i = threadIdx.x; i < NUM_BLOCKS * GROUPS_PER_BLOCK; i +=
blockDim.x) {
radix[i] = s[i];
}
__syncthreads();
// calculate total sum and copy to global memory
if (!threadIdx.x) {
total += s[PADDED_GROUPS - 1];
// calculate prefix-sum on local radices
radix_sums[blockIdx.x * NUM_RADICES / NUM_BLOCKS + j] = left;
total += left;
left = total;
}
// move to next radix
radix += NUM_BLOCKS * GROUPS_PER_BLOCK;
}
}
__global__ void RadixTabulateKernel(uint32_t *keys, uint32_t *radices,
unsigned int n,
unsigned int cells_per_group, int shift) {
extern __shared__ uint32_t s[];
int group = threadIdx.x / THREADS_PER_GROUP;
int group_start = (blockIdx.x * GROUPS_PER_BLOCK + group) * cells_per_group;
int group_end = group_start + cells_per_group;
uint32_t k;
// initialize shared memory
for (int i = threadIdx.x; i < GROUPS_PER_BLOCK * NUM_RADICES; i +=
blockDim.x) {
s[i] = 0;
}
__syncthreads();
// count instances of each radix
for (int i = group_start + threadIdx.x % THREADS_PER_GROUP; i < group_end &&
i < n; i += THREADS_PER_GROUP) {
// need only avoid bank conflicts by group
k = (keys[i] >> shift & NUM_RADICES - 1) * GROUPS_PER_BLOCK + group;
// increment radix counters sequentially by thread in the thread group
for (int j = 0; j < THREADS_PER_GROUP; j++) {
if (threadIdx.x % THREADS_PER_GROUP == j) {
s[k]++;
}
}
}
__syncthreads();
// copy to global memory
for (int i = threadIdx.x; i < GROUPS_PER_BLOCK * NUM_RADICES; i +=
blockDim.x) {
radices[(i / GROUPS_PER_BLOCK * NUM_BLOCKS + blockIdx.x) *
GROUPS_PER_BLOCK + i % GROUPS_PER_BLOCK] = s[i];
}
}
__global__ void ScaleOffsetKernel(float *arr, float scale, float offset,
unsigned int n) {
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x *
blockDim.x) {
arr[i] = arr[i] * scale + offset;
}
}
__global__ void SumReduceKernel(unsigned int *values, unsigned int n,
unsigned int *out) {
extern __shared__ unsigned int t[];
unsigned int sum = 0;
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += gridDim.x *
blockDim.x) {
sum += values[i];
}
t[threadIdx.x] = sum;
dSumReduce(t, out);
}
/**
* @brief Performs narrow-phase collision detection on a sorted cell array.
* @param cells array of cells sorted by cudaSortCells.
* @param objects array of corresponding objects.
* @param positions array of object positions.
* @param velocities array of object velocities.
* @param dims array of object sizes.
* @param num_objects the number of objects in the arrays.
* @param num_cells the number of occupied cells.
* @param temp temporary global memory variable of length at least two.
* @param num_tests returns the number of collision tests performed.
* @return The number of collisions encountered.
*/
unsigned int cudaCellCollide(uint32_t *cells, uint32_t *objects,
float *positions, float *velocities, float *dims,
unsigned int num_objects, unsigned int num_cells,
unsigned int *temp, unsigned int *test_count,
unsigned int num_blocks,
unsigned int threads_per_block) {
unsigned int cells_per_thread = (num_cells - 1) / num_blocks /
threads_per_block + 1;
unsigned int collision_count;
cudaMemset(temp, 0, 2 * sizeof(unsigned int));
cellCollideKernel<<<num_blocks, threads_per_block,
threads_per_block * sizeof(unsigned int)>>>(
cells, objects, positions, velocities, dims, num_objects, num_cells,
cells_per_thread, temp, temp + 1);
cudaMemcpy(&collision_count, temp, sizeof(unsigned int),
cudaMemcpyDeviceToHost);
cudaMemcpy(test_count, temp + 1, sizeof(unsigned int),
cudaMemcpyDeviceToHost);
return collision_count;
}
/**
* @brief Constructs cell array.
* @param cells array of cells.
* @param objects array of corresponding objects.
* @param positions array of object positions.
* @param dims array of object sizes
* @param num_objects the number of objects to process.
* @param cell_dim the size of each cell in any dimension.
* @param temp temporary global memory variable of length at least one.
* @return The number of cells associations occupied
*/
unsigned int cudaInitCells(uint32_t *cells, uint32_t *objects,
float *positions, float *dims,
unsigned int num_objects, float cell_dim,
unsigned int *temp, unsigned int num_blocks,
unsigned int threads_per_block) {
unsigned int cell_count;
cudaMemset(temp, 0, sizeof(unsigned int));
InitCellKernel<<<num_blocks, threads_per_block,
threads_per_block * sizeof(unsigned int)>>>(
cells, objects, positions, dims, num_objects, cell_dim, temp);
cudaMemcpy(&cell_count, temp, sizeof(unsigned int), cudaMemcpyDeviceToHost);
return cell_count;
}
/**
* @brief Randomly generates object properties.
* @param positions array of positions.
* @param velocities array of velocities.
* @param dims array of dimensions.
* @param num_objects the number of objects to generate.
* @param max_speed the maximum possible speed in any dimension.
* @param max_dim the maximum size in any dimension.
*/
void cudaInitObjects(float *positions, float *velocities, float *dims,
unsigned int num_objects, float max_speed, float max_dim,
unsigned int num_blocks, unsigned int threads_per_block) {
curandGenerator_t generator;
curandCreateGenerator(&generator, CURAND_RNG_PSEUDO_DEFAULT);
// randomly generate positions ranging from (0, 0) to (1, 1)
curandGenerateUniform(generator, positions, num_objects * DIM);
// randomly generate speeds ranging from -max_speed to max_speed
curandGenerateUniform(generator, velocities, num_objects * DIM);
ScaleOffsetKernel<<<num_blocks, threads_per_block>>>(
velocities, max_speed * 2, -max_speed, num_objects * DIM);
// randomly generate sizes ranging from max_dim / 2 to max_dim
curandGenerateUniform(generator, dims, num_objects * DIM);
ScaleOffsetKernel<<<num_blocks, threads_per_block>>>(
dims, max_dim / 4, max_dim / 4, num_objects * DIM);
}
/**
* @brief Parallelized prefix-sum algorithm.
* @param values array of values.
* @param n the number of values to process.
*/
void cudaPrefixSum(uint32_t *values, unsigned int n) {
PrefixSumKernel<<<1, GROUPS_PER_BLOCK * THREADS_PER_GROUP,
n * sizeof(uint32_t)>>>(
values, n);
}
/**
* @brief Radix sorts an array of objects using occupations as keys.
* @param cells the input array of cells.
* @param objects the input array of objects.
* @param cells_temp sorted array of cells.
* @param objects_temp array of objects sorted by corresponding cells.
* @param radices working array to hold radix data.
* @param radix_sums working array to hold radix prefix sums.
* @param num_objects the number of objects included.
*/
void cudaSortCells(uint32_t *cells, uint32_t *objects, uint32_t *cells_temp,
uint32_t *objects_temp, uint32_t *radices,
uint32_t *radix_sums, unsigned int num_objects) {
unsigned int cells_per_group = (num_objects * DIM_2 - 1) / NUM_BLOCKS /
GROUPS_PER_BLOCK + 1;
uint32_t *cells_swap;
uint32_t *objects_swap;
// stable sort, works on bits of increasing level
for (int i = 0; i < 32; i += L) {
RadixTabulateKernel<<<NUM_BLOCKS, GROUPS_PER_BLOCK * THREADS_PER_GROUP,
GROUPS_PER_BLOCK * NUM_RADICES * sizeof(uint32_t)>>>(
cells, radices, num_objects * DIM_2, cells_per_group, i);
RadixSumKernel<<<NUM_BLOCKS, GROUPS_PER_BLOCK * THREADS_PER_GROUP,
PADDED_GROUPS * sizeof(uint32_t)>>>(
radices, radix_sums);
RadixOrderKernel<<<NUM_BLOCKS, GROUPS_PER_BLOCK * THREADS_PER_GROUP,
NUM_RADICES * sizeof(uint32_t) + GROUPS_PER_BLOCK *
NUM_RADICES * sizeof(uint32_t)>>>(
cells, objects, cells_temp, objects_temp, radices, radix_sums,
num_objects * DIM_2, cells_per_group, i);
// cells sorted up to this bit are in cells_temp; swap for the next pass
cells_swap = cells;
cells = cells_temp;
cells_temp = cells_swap;
objects_swap = objects;
objects = objects_temp;
objects_temp = objects_swap;
}
}
/**
* @brief Sum array by reduction.
* @param values array of values.
* @param n the number of values to process.
* @param temp temporary global memory variable of length at least one.
* @return The sum of the array
*/
unsigned int cudaSumReduce(unsigned int *values, unsigned int n,
unsigned int *temp) {
int sum;
cudaMemset(temp, 0, sizeof(unsigned int));
SumReduceKernel<<<1, GROUPS_PER_BLOCK * THREADS_PER_GROUP,
GROUPS_PER_BLOCK * THREADS_PER_GROUP *
sizeof(unsigned int)>>>(
values, n, temp);
cudaMemcpy(&sum, temp, sizeof(unsigned int), cudaMemcpyDeviceToHost);
return sum;
}