-
Notifications
You must be signed in to change notification settings - Fork 8
/
collisions_test.cc
331 lines (278 loc) · 8.72 KB
/
collisions_test.cc
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
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <cuda_runtime.h>
#include "collisions.cuh"
#include "collisions.h"
#include "collisions_cpu.h"
#define NUM_OBJECTS 16384
#define MAX_SPEED 0.5
#define MAX_DIM 0.5
#define COLS 8
#define gpuErrChk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
unsigned int num_blocks = 100;
unsigned int threads_per_block = 512;
unsigned int object_size = NUM_OBJECTS * DIM * sizeof(float);
unsigned int cell_size = NUM_OBJECTS * DIM_2 * sizeof(uint32_t);
unsigned int cell_count;
unsigned int *d_temp;
float *positions;
float *velocities;
float *dims;
float *d_positions;
float *d_velocities;
float *d_dims;
uint32_t *cells;
uint32_t *objects;
uint32_t *d_cells;
uint32_t *d_cells_temp;
uint32_t *d_objects;
uint32_t *d_objects_temp;
uint32_t *d_radices;
uint32_t *d_radix_sums;
inline void gpuAssert(cudaError_t code, const char *file, int line,
bool abort = true) {
if (code != cudaSuccess) {
fprintf(stderr,"GPUassert: %s %s %4d\n", cudaGetErrorString(code), file,
line);
exit(code);
}
}
/**
* @brief Frees memory associated with test variables.
*/
void FinishTests() {
free(positions);
free(velocities);
free(dims);
free(cells);
free(objects);
cudaFree(d_temp);
cudaFree(d_positions);
cudaFree(d_velocities);
cudaFree(d_dims);
cudaFree(d_cells);
cudaFree(d_cells_temp);
cudaFree(d_objects);
cudaFree(d_objects_temp);
cudaFree(d_radices);
cudaFree(d_radix_sums);
}
/**
* @brief Initializes variables for testing.
*/
void InitTests() {
positions = (float *) malloc(object_size);
velocities = (float *) malloc(object_size);
dims = (float *) malloc(object_size);
cells = (uint32_t *) malloc(cell_size);
objects = (uint32_t *) malloc(cell_size);
cudaMalloc((void **) &d_temp, 2 * sizeof(unsigned int));
cudaMalloc((void **) &d_positions, object_size);
cudaMalloc((void **) &d_velocities, object_size);
cudaMalloc((void **) &d_dims, object_size);
cudaMalloc((void **) &d_cells, cell_size);
cudaMalloc((void **) &d_cells_temp, cell_size);
cudaMalloc((void **) &d_objects, cell_size);
cudaMalloc((void **) &d_objects_temp, cell_size);
cudaMalloc((void **) &d_radices, NUM_BLOCKS * GROUPS_PER_BLOCK *
NUM_RADICES * sizeof(uint32_t));
cudaMalloc((void **) &d_radix_sums, NUM_RADICES * sizeof(uint32_t));
}
/**
* @brief Tests cudaCellCollide by testing it against a CPU implementation.
*/
void TestCellCollide() {
printf("Testing CellCollide...\n");
unsigned int test_count;
unsigned int a = cudaCellCollide(d_cells, d_objects, d_positions,
d_velocities, d_dims, NUM_OBJECTS,
cell_count, d_temp, &test_count, num_blocks,
threads_per_block);
unsigned int b = CellCollide(positions, velocities, dims, NUM_OBJECTS);
unsigned int d = NUM_OBJECTS * (NUM_OBJECTS - 1) / 2;
printf("Collisions encountered on GPU: %d\n"
"Collisions encountered on CPU: %d\n"
"Collision tests performed on GPU: %d\n"
"Collision tests performed on CPU: %d\n\n", a, b, test_count, d);
assert(a >= b);
assert(test_count < d);
}
/**
* @brief Tests cudaInitCells for cell assignment correctness.
* @param n number of objects whose cells are checked and displayed.
*/
void TestInitCells(int n) {
printf("Testing InitCells...\n");
cell_count = cudaInitCells(d_cells, d_objects, d_positions, d_dims,
NUM_OBJECTS, MAX_DIM, d_temp, num_blocks,
threads_per_block);
cudaMemcpy(cells, d_cells, cell_size, cudaMemcpyDeviceToHost);
cudaMemcpy(objects, d_objects, cell_size, cudaMemcpyDeviceToHost);
if (NUM_OBJECTS < n) {
n = NUM_OBJECTS;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < DIM_2; j++) {
uint32_t cell = cells[i * DIM_2 + j];
printf("%8x %8x\t", cell, objects[i * DIM_2 + j]);
if (COLS < DIM_2 * 2) {
printf("\n");
}
for (int k = 0; k < DIM; k++) {
assert(cell == UINT32_MAX || abs((cell >> (DIM - k) * 8 & 0xff) *
MAX_DIM - positions[i + k * NUM_OBJECTS]) <
dims[i + k * NUM_OBJECTS]);
}
}
if (!((i + 1) % (COLS / DIM_2 / 2))) {
printf("\n");
} else {
printf("\t");
}
}
printf("\n");
}
/**
* @brief Tests cudaInitObjects for object constraint satisfaction.
* @param n number of objects whose properties are checked and displayed.
*/
void TestInitObjects(int n) {
printf("Testing InitObjects...\n");
cudaInitObjects(d_positions, d_velocities, d_dims, NUM_OBJECTS, MAX_SPEED,
MAX_DIM, num_blocks, threads_per_block);
cudaMemcpy(positions, d_positions, object_size, cudaMemcpyDeviceToHost);
cudaMemcpy(velocities, d_velocities, object_size, cudaMemcpyDeviceToHost);
cudaMemcpy(dims, d_dims, object_size, cudaMemcpyDeviceToHost);
if (NUM_OBJECTS < n) {
n = NUM_OBJECTS;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < DIM; j++) {
printf("% f ", positions[i + j * NUM_OBJECTS]);
assert(positions[i + j * NUM_OBJECTS] >= 0 &&
positions[i + j * NUM_OBJECTS] < 1);
}
if (COLS < DIM * 3) {
printf("\n");
} else {
printf("\t");
}
for (int j = 0; j < DIM; j++) {
printf("% f ", velocities[i + j * NUM_OBJECTS]);
assert(abs(velocities[i + j * NUM_OBJECTS]) < MAX_SPEED);
}
if (COLS < DIM * 3) {
printf("\n");
} else {
printf("\t");
}
for (int j = 0; j < DIM; j++) {
printf("% f ", dims[i + j * NUM_OBJECTS]);
assert(dims[i + j * NUM_OBJECTS] < MAX_DIM / 2);
}
if (!(COLS / DIM / 3) || !((i + 1) % (COLS / DIM / 3))) {
printf("\n");
} else {
printf("\t");
}
}
printf("\n");
}
/**
* @brief Tests cudaPrefixSum for correctness.
* @param n number of elements on which to perform a prefix sum.
*/
void TestPrefixSum(int n) {
printf("Testing PrefixSum...\n");
uint32_t *arr = (uint32_t *) malloc(n * sizeof(uint32_t));
uint32_t *d_arr;
cudaMalloc((void **) &d_arr, n * sizeof(uint32_t));
for (int i = 0; i < n; i++) {
arr[i] = 1;
}
cudaMemcpy(d_arr, arr, n * sizeof(uint32_t), cudaMemcpyHostToDevice);
cudaPrefixSum(d_arr, n);
cudaPrefixSum(d_arr, n);
cudaMemcpy(arr, d_arr, n * sizeof(uint32_t), cudaMemcpyDeviceToHost);
for (int i = 0; i < n; i++) {
printf("%d\t", arr[i]);
if (!((i + 1) % COLS)) {
printf("\n");
}
assert((int) arr[i] == i * (i - 1) / 2);
}
free(arr);
cudaFree(d_arr);
printf("\n");
}
/**
* @brief Tests cudaInitCells for count and cudaSortCells for radix sort.
* @param n when multiplied by DIM_2, the number of cells to check.
*/
void TestSortCells(int n) {
printf("TestingSortCells...\n");
cudaSortCells(d_cells, d_objects, d_cells_temp, d_objects_temp,
d_radices, d_radix_sums, NUM_OBJECTS);
cudaMemcpy(cells, d_cells, cell_size, cudaMemcpyDeviceToHost);
cudaMemcpy(objects, d_objects, cell_size, cudaMemcpyDeviceToHost);
assert(cell_count && cell_count <= NUM_OBJECTS * DIM_2);
assert(cells[cell_count] == UINT32_MAX && cells[cell_count - 1] !=
UINT32_MAX);
if (NUM_OBJECTS < n) {
n = NUM_OBJECTS;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < DIM_2; j++) {
uint32_t cell = cells[i * DIM_2 + j];
printf("%8x %8x\t", cell, objects[i * DIM_2 + j]);
if (COLS < DIM_2 * 2) {
printf("\n");
}
for (int k = 0; k < DIM; k++) {
assert((!i && !j) || cell >= cells[i * DIM_2 + j - 1]);
}
}
if (!((i + 1) % (COLS / DIM_2 / 2))) {
printf("\n");
} else {
printf("\t");
}
}
printf("\n");
}
/**
* @brief Tests cudaSumReduce for correctness.
* @param n number of elements t0 sum.
*/
void TestSumReduce(int n) {
printf("Testing SumReduce...\n");
unsigned int *arr = (unsigned int *) malloc(n * sizeof(unsigned int));
unsigned int *d_arr;
cudaMalloc((void **) &d_arr, n * sizeof(unsigned int));
for (int i = 0; i < n; i++) {
arr[i] = i;
}
cudaMemcpy(d_arr, arr, n * sizeof(unsigned int), cudaMemcpyHostToDevice);
unsigned int sum = cudaSumReduce(d_arr, n, d_temp);
printf("Sum of integers between 0 and %d: %d\n", n, sum);
cudaMemcpy(arr, d_arr, n * sizeof(unsigned int), cudaMemcpyDeviceToHost);
assert((int) sum == n * (n - 1) / 2);
free(arr);
cudaFree(d_arr);
printf("\n");
}
int main(int argc, char *argv[]) {
InitTests();
TestPrefixSum(256);
TestSumReduce(256);
TestInitObjects(16);
TestInitCells(16);
TestSortCells(16);
TestCellCollide();
FinishTests();
gpuErrChk(cudaGetLastError());
printf("All tests passed.\n");
return 0;
}