-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticleSystem_cuda.cu
170 lines (135 loc) · 5.22 KB
/
particleSystem_cuda.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
// 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 <cstdlib>
#include <cstdio>
#include <string.h>
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <helper_functions.h>
#include "thrust/device_ptr.h"
#include "thrust/for_each.h"
#include "thrust/iterator/zip_iterator.h"
#include "thrust/sort.h"
#include "particles_kernel_impl.cuh"
extern "C"
{
void cudaInit(int argc, char **argv)
{
int devID;
// use command-line specified CUDA device, otherwise use device with highest Gflops/s
devID = findCudaDevice(argc, (const char **)argv);
if (devID < 0)
{
printf("No CUDA Capable devices found, exiting...\n");
exit(EXIT_SUCCESS);
}
}
void allocateArray(void **devPtr, size_t size)
{
checkCudaErrors(cudaMalloc(devPtr, size));
}
void freeArray(void *devPtr)
{
checkCudaErrors(cudaFree(devPtr));
}
void threadSync()
{
checkCudaErrors(cudaDeviceSynchronize());
}
void copyArrayToDevice(void *device, void *host, int size)
{
checkCudaErrors(cudaMemcpy(device, host, size, cudaMemcpyHostToDevice));
}
void copyArrayFromDevice(void* device, void* host, int size)
{
checkCudaErrors(cudaMemcpy(host, device, size, cudaMemcpyDeviceToHost));
}
void setParameters(SimParams *hostParams)
{
// copy parameters to constant memory
checkCudaErrors(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 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,
(float3 *) pos,
numParticles);
// check if kernel invocation generated an error
getLastCudaError("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
checkCudaErrors(cudaMemset(cellStart, 0xffffffff, numCells*sizeof(uint)));
uint smemSize = sizeof(uint)*(numThreads+1);
reorderDataAndFindCellStartD<<< numBlocks, numThreads, smemSize>>>(
cellStart,
cellEnd,
(float3 *) sortedPos,
(float4 *) sortedVel,
gridParticleHash,
gridParticleIndex,
(float3 *) oldPos,
(float4 *) oldVel,
numParticles);
getLastCudaError("Kernel execution failed: reorderDataAndFindCellStartD");
}
void collide(float *newVel,
float *newPos,
float *sortedVel,
float *sortedPos,
uint *cellStart,
uint *cellEnd,
uint numParticles_new,
uint numCells)
{
// thread per particle
uint numThreads, numBlocks;
computeGridSize(numParticles_new, 64, numBlocks, numThreads);
// execute the kernel
collideD<<< numBlocks, numThreads >>>((float3 *)newVel,
(float3 *)newPos,
(float4 *)sortedVel,
(float3*)sortedPos,
cellStart,
cellEnd,
numParticles_new);
// check if kernel invocation generated an error
getLastCudaError("Kernel execution failed");
}
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));
}
} // extern "C"