-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleSystem.cu
331 lines (269 loc) · 10.1 KB
/
particleSystem.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
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
// This file contains C wrappers around the some of the CUDA API and the
// kernel functions so that they can be called from "particleSystem.cpp"
#include <cutil_inline.h> // includes cuda.h and cuda_runtime_api.h
#include <shrQATest.h>
#include <cstdlib>
#include <cstdio>
#include <string.h>
#if defined(__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif
#include <cuda_gl_interop.h>
#include "thrust/device_ptr.h"
#include "thrust/for_each.h"
#include "thrust/iterator/zip_iterator.h"
#include "thrust/sort.h"
#include "particles_kernel.cu"
extern "C"
{
void cudaInit(int argc, char **argv)
{
int devID;
// use command-line specified CUDA device, otherwise use device with highest Gflops/s
if( cutCheckCmdLineFlag(argc, (const char**)argv, "device") ) {
devID = cutilDeviceInit(argc, argv);
if (devID < 0) {
printf("No CUDA Capable devices found, exiting...\n");
shrQAFinishExit(argc, (const char **)argv, QA_WAIVED);
}
} else {
devID = cutGetMaxGflopsDeviceId();
cudaSetDevice( devID );
}
}
void cudaGLInit(int argc, char **argv)
{
// use command-line specified CUDA device, otherwise use device with highest Gflops/s
if( cutCheckCmdLineFlag(argc, (const char**)argv, "device") ) {
cutilDeviceInit(argc, argv);
} else {
cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );
}
}
void allocateArray(void **devPtr, size_t size)
{
cutilSafeCall(cudaMalloc(devPtr, size));
}
void freeArray(void *devPtr)
{
cutilSafeCall(cudaFree(devPtr));
}
void threadSync()
{
cutilSafeCall(cutilDeviceSynchronize());
}
void copyArrayToDevice(void* device, const void* host, int offset, int size)
{
cutilSafeCall(cudaMemcpy((char *) device + offset, host, size, cudaMemcpyHostToDevice));
}
void registerGLBufferObject(uint vbo, struct cudaGraphicsResource **cuda_vbo_resource)
{
cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_vbo_resource, vbo,
cudaGraphicsMapFlagsNone));
}
void unregisterGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
{
cutilSafeCall(cudaGraphicsUnregisterResource(cuda_vbo_resource));
}
void *mapGLBufferObject(struct cudaGraphicsResource **cuda_vbo_resource)
{
void *ptr;
cutilSafeCall(cudaGraphicsMapResources(1, cuda_vbo_resource, 0));
size_t num_bytes;
cutilSafeCall(cudaGraphicsResourceGetMappedPointer((void **)&ptr, &num_bytes,
*cuda_vbo_resource));
return ptr;
}
void unmapGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
{
cutilSafeCall(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
}
void copyArrayFromDevice(void* host, const void* device,
struct cudaGraphicsResource **cuda_vbo_resource, int size)
{
if (cuda_vbo_resource)
device = mapGLBufferObject(cuda_vbo_resource);
cutilSafeCall(cudaMemcpy(host, device, size, cudaMemcpyDeviceToHost));
if (cuda_vbo_resource)
unmapGLBufferObject(*cuda_vbo_resource);
}
void setParameters(SimParams *hostParams)
{
// copy parameters to constant memory
cutilSafeCall( cudaMemcpyToSymbol(params, hostParams, sizeof(SimParams)) );
}
//Round a / b to nearest higher integer value
uint iDivUp(uint a, uint b){
return (a % b != 0) ? (a / b + 1) : (a / b);
}
// compute grid and thread block size for a given number of elements
void computeGridSize(uint n, uint blockSize, uint &numBlocks, uint &numThreads)
{
numThreads = min(blockSize, n);
numBlocks = iDivUp(n, numThreads);
}
void integrateSystem(float *pos,
float *vel,
float deltaTime,
uint numParticles)
{
thrust::device_ptr<float4> d_pos4((float4 *)pos);
thrust::device_ptr<float4> d_vel4((float4 *)vel);
thrust::for_each(
thrust::make_zip_iterator(thrust::make_tuple(d_pos4, d_vel4)),
thrust::make_zip_iterator(thrust::make_tuple(d_pos4+numParticles, d_vel4+numParticles)),
integrate_functor(deltaTime));
}
void calcHash(uint* gridParticleHash,
uint* gridParticleIndex,
float* pos,
int numParticles)
{
uint numThreads, numBlocks;
computeGridSize(numParticles, 256, numBlocks, numThreads);
// execute the kernel
calcHashD<<< numBlocks, numThreads >>>(gridParticleHash,
gridParticleIndex,
(float4 *) pos,
numParticles);
// check if kernel invocation generated an error
cutilCheckMsg("Kernel execution failed");
}
void reorderDataAndFindCellStart(uint* cellStart,
uint* cellEnd,
float* sortedPos,
float* sortedVel,
uint* gridParticleHash,
uint* gridParticleIndex,
float* oldPos,
float* oldVel,
uint numParticles,
uint numCells)
{
uint numThreads, numBlocks;
computeGridSize(numParticles, 256, numBlocks, numThreads);
// set all cells to empty
cutilSafeCall(cudaMemset(cellStart, 0xffffffff, numCells*sizeof(uint)));
#if USE_TEX
cutilSafeCall(cudaBindTexture(0, oldPosTex, oldPos, numParticles*sizeof(float4)));
cutilSafeCall(cudaBindTexture(0, oldVelTex, oldVel, numParticles*sizeof(float4)));
#endif
uint smemSize = sizeof(uint)*(numThreads+1);
reorderDataAndFindCellStartD<<< numBlocks, numThreads, smemSize>>>(
cellStart,
cellEnd,
(float4 *) sortedPos,
(float4 *) sortedVel,
gridParticleHash,
gridParticleIndex,
(float4 *) oldPos,
(float4 *) oldVel,
numParticles);
cutilCheckMsg("Kernel execution failed: reorderDataAndFindCellStartD");
#if USE_TEX
cutilSafeCall(cudaUnbindTexture(oldPosTex));
cutilSafeCall(cudaUnbindTexture(oldVelTex));
#endif
}
void collide(float* newVel,
float* sortedPos,
float* sortedVel,
uint* gridParticleIndex,
uint* cellStart,
uint* cellEnd,
uint numParticles,
uint numCells)
{
#if USE_TEX
cutilSafeCall(cudaBindTexture(0, oldPosTex, sortedPos, numParticles*sizeof(float4)));
cutilSafeCall(cudaBindTexture(0, oldVelTex, sortedVel, numParticles*sizeof(float4)));
cutilSafeCall(cudaBindTexture(0, cellStartTex, cellStart, numCells*sizeof(uint)));
cutilSafeCall(cudaBindTexture(0, cellEndTex, cellEnd, numCells*sizeof(uint)));
#endif
// thread per particle
uint numThreads, numBlocks;
computeGridSize(numParticles, 64, numBlocks, numThreads);
// execute the kernel
collideD<<< numBlocks, numThreads >>>((float4*)newVel,
(float4*)sortedPos,
(float4*)sortedVel,
gridParticleIndex,
cellStart,
cellEnd,
numParticles);
// check if kernel invocation generated an error
cutilCheckMsg("Kernel execution failed");
#if USE_TEX
cutilSafeCall(cudaUnbindTexture(oldPosTex));
cutilSafeCall(cudaUnbindTexture(oldVelTex));
cutilSafeCall(cudaUnbindTexture(cellStartTex));
cutilSafeCall(cudaUnbindTexture(cellEndTex));
#endif
}
void sortParticles(uint *dGridParticleHash, uint *dGridParticleIndex, uint numParticles)
{
thrust::sort_by_key(thrust::device_ptr<uint>(dGridParticleHash),
thrust::device_ptr<uint>(dGridParticleHash + numParticles),
thrust::device_ptr<uint>(dGridParticleIndex));
}
void eject(float* newVel,
float* sortedPos,
float* sortedVel,
uint* gridParticleIndex,
uint* cellStart,
uint* cellEnd,
uint numParticles,
uint numCells)
{
int N = 10;
//dim3 tpb(N,1,1);
curandState* devStates;
cudaMalloc ( &devStates, N*sizeof( curandState ) );
#if USE_TEX
cutilSafeCall(cudaBindTexture(0, oldPosTex, sortedPos, numParticles*sizeof(float4)));
cutilSafeCall(cudaBindTexture(0, oldVelTex, sortedVel, numParticles*sizeof(float4)));
cutilSafeCall(cudaBindTexture(0, cellStartTex, cellStart, numCells*sizeof(uint)));
cutilSafeCall(cudaBindTexture(0, cellEndTex, cellEnd, numCells*sizeof(uint)));
#endif
// thread per particle
uint numThreads, numBlocks;
float *devRand, *hostResults ;
int i;
computeGridSize(numParticles, 64, numBlocks, numThreads);
//setup_kernel <<< numThreads, numBlocks >>> ( devStates);
//hostResults = (float*) calloc(64 * 64 , sizeof(float)) ;
//cudaMalloc((void **) & devRand , 64 * 64 * sizeof(float)) ;
//cudaMemset(devRand , 0, 64*64*sizeof(int));
random<<< numBlocks, numThreads >>>((float4*)newVel,
(float4*)sortedPos,
(float4*)sortedVel,
gridParticleIndex,
cellStart,
cellEnd,
numParticles
);
// check if kernel invocation generated an error
/*cutilCheckMsg("Kernel execution failed");
cudaMemcpy(hostResults, devRand, 64*64*sizeof(int), cudaMemcpyDeviceToHost);
for ( i = 0; i < 64 * 64; i ++) {
printf("Fraction with low bit set was %10.13f \n" ,(float) hostResults[i]);}*/
#if USE_TEX
cutilSafeCall(cudaUnbindTexture(oldPosTex));
cutilSafeCall(cudaUnbindTexture(oldVelTex));
cutilSafeCall(cudaUnbindTexture(cellStartTex));
cutilSafeCall(cudaUnbindTexture(cellEndTex));
#endif
}
} // extern "C"