-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathkernel.cu
More file actions
301 lines (251 loc) · 8.53 KB
/
kernel.cu
File metadata and controls
301 lines (251 loc) · 8.53 KB
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
#include <stdio.h>
#include <cuda.h>
#include <cmath>
#include "glm/glm.hpp"
#include "utilities.h"
#include "kernel.h"
#if SHARED == 1
#define ACC(x,y,z) sharedMemAcc(x,y,z)
#else
#define ACC(x,y,z) naiveAcc(x,y,z)
#endif
//GLOBALS
dim3 threadsPerBlock(blockSize);
int numObjects;
const float planetMass = 3e8;
const __device__ float starMass = 5e10;
const float scene_scale = 2e2; //size of the height map in simulation space
glm::vec4 * dev_pos;
glm::vec3 * dev_vel;
glm::vec3 * dev_acc;
void checkCUDAError(const char *msg, int line = -1)
{
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err)
{
if( line >= 0 )
{
fprintf(stderr, "Line %d: ", line);
}
fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
exit(EXIT_FAILURE);
}
}
__host__ __device__
unsigned int hash(unsigned int a){
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
return a;
}
//Function that generates static.
__host__ __device__
glm::vec3 generateRandomNumberFromThread(float time, int index)
{
thrust::default_random_engine rng(hash(index*time));
thrust::uniform_real_distribution<float> u01(0,1);
return glm::vec3((float) u01(rng), (float) u01(rng), (float) u01(rng));
}
//Generate randomized starting positions for the planets in the XY plane
//Also initialized the masses
__global__
void generateRandomPosArray(int time, int N, glm::vec4 * arr, float scale, float mass)
{
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
if(index < N)
{
glm::vec3 rand = scale*(generateRandomNumberFromThread(time, index)-0.5f);
arr[index].x = rand.x;
arr[index].y = rand.y;
arr[index].z = 0.0f;//rand.z;
arr[index].w = mass;
}
}
//Determine velocity from the distance from the center star. Not super physically accurate because
//the mass ratio is too close, but it makes for an interesting looking scene
__global__
void generateCircularVelArray(int time, int N, glm::vec3 * arr, glm::vec4 * pos)
{
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
if(index < N)
{
glm::vec3 R = glm::vec3(pos[index].x, pos[index].y, pos[index].z);
float r = glm::length(R) + EPSILON;
float s = sqrt(G*starMass/r);
glm::vec3 D = glm::normalize(glm::cross(R/r,glm::vec3(0,0,1)));
arr[index].x = s*D.x;
arr[index].y = s*D.y;
arr[index].z = s*D.z;
}
}
//Generate randomized starting velocities in the XY plane
__global__
void generateRandomVelArray(int time, int N, glm::vec3 * arr, float scale)
{
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
if(index < N)
{
glm::vec3 rand = scale*(generateRandomNumberFromThread(time, index) - 0.5f);
arr[index].x = rand.x;
arr[index].y = rand.y;
arr[index].z = 0.0;//rand.z;
}
}
//TODO: Determine force between two bodies
__device__
glm::vec3 calculateAcceleration(glm::vec4 us, glm::vec4 them)
{
// G*m_us*m_them
//F = -------------
// r^2
//
// G*m_us*m_them G*m_them
//a = ------------- = --------
// m_us*r^2 r^2
glm::vec3 r((them.x-us.x), (them.y-us.y), (them.z-us.z));
float r2 = glm::dot(r,r);
if (r2 == 0)
return glm::vec3(0,0,0);
else {
float a = G*them.w / r2;
return a * (r/sqrt(r2));
}
}
//TODO: Core force calc kernel global memory
__device__
glm::vec3 naiveAcc(int N, glm::vec4 my_pos, glm::vec4 * their_pos)
{
// acceleration due to star
glm::vec3 acc = calculateAcceleration(my_pos, glm::vec4(0,0,0,starMass));
// acceleration due to other planets
for (int i = 0; i < N; i++)
acc += calculateAcceleration(my_pos, their_pos[i]);
// return the total acceleration
return acc;
}
//TODO: Core force calc kernel shared memory
__device__
glm::vec3 sharedMemAcc(int N, glm::vec4 my_pos, glm::vec4 * their_pos)
{
__shared__ glm::vec4 them_shared[blockSize];
int tx = threadIdx.x;
// acceleration due to star
glm::vec3 acc = calculateAcceleration(my_pos, glm::vec4(0,0,0,starMass));
// acceleration due to other planets
for (int m = 0; m < ceil((double)N/blockSize); ++m) {
// copy block of planets into shared memory
if (m*blockSize + tx < N)
them_shared[tx] = their_pos[m*blockSize + tx];
__syncthreads();
// compute acceleration contributions from block
for (int k = 0; k < blockSize; ++k)
if (m*blockSize + k < N) { acc += calculateAcceleration(my_pos, them_shared[k]); }
__syncthreads();
}
// total acceleration of my planet
return acc;
}
//Simple Euler integration scheme
__global__
void updateF(int N, float dt, glm::vec4 * pos, glm::vec3 * vel, glm::vec3 * acc)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
glm::vec4 my_pos;
glm::vec3 accel;
if(index < N) my_pos = pos[index];
accel = ACC(N, my_pos, pos);
if(index < N) acc[index] = accel;
}
__global__
void updateS(int N, float dt, glm::vec4 * pos, glm::vec3 * vel, glm::vec3 * acc)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
if( index < N )
{
vel[index] += acc[index] * dt;
pos[index].x += vel[index].x * dt;
pos[index].y += vel[index].y * dt;
pos[index].z += vel[index].z * dt;
}
}
//Update the vertex buffer object
//(The VBO is where OpenGL looks for the positions for the planets)
__global__
void sendToVBO(int N, glm::vec4 * pos, float * vbo, int width, int height, float s_scale)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
float c_scale_w = -2.0f / s_scale;
float c_scale_h = -2.0f / s_scale;
if(index<N)
{
vbo[4*index+0] = pos[index].x*c_scale_w;
vbo[4*index+1] = pos[index].y*c_scale_h;
vbo[4*index+2] = 0;
vbo[4*index+3] = 1;
}
}
//Update the texture pixel buffer object
//(This texture is where openGL pulls the data for the height map)
__global__
void sendToPBO(int N, glm::vec4 * pos, float4 * pbo, int width, int height, float s_scale)
{
int index = threadIdx.x + (blockIdx.x * blockDim.x);
int x = index % width;
int y = index / width;
float w2 = width / 2.0;
float h2 = height / 2.0;
float c_scale_w = width / s_scale;
float c_scale_h = height / s_scale;
glm::vec3 color(0.05, 0.15, 0.3);
glm::vec3 acc = ACC(N, glm::vec4((x-w2)/c_scale_w,(y-h2)/c_scale_h,0,1), pos);
if(x<width && y<height)
{
float mag = sqrt(sqrt(acc.x*acc.x + acc.y*acc.y + acc.z*acc.z));
// Each thread writes one pixel location in the texture (textel)
pbo[index].w = (mag < 1.0f) ? mag : 1.0f;
}
}
/*************************************
* Wrappers for the __global__ calls *
*************************************/
//Initialize memory, update some globals
void initCuda(int N)
{
numObjects = N;
dim3 fullBlocksPerGrid((int)ceil(float(N)/float(blockSize)));
cudaMalloc((void**)&dev_pos, N*sizeof(glm::vec4));
checkCUDAErrorWithLine("Kernel failed!");
cudaMalloc((void**)&dev_vel, N*sizeof(glm::vec3));
checkCUDAErrorWithLine("Kernel failed!");
cudaMalloc((void**)&dev_acc, N*sizeof(glm::vec3));
checkCUDAErrorWithLine("Kernel failed!");
generateRandomPosArray<<<fullBlocksPerGrid, blockSize>>>(1, numObjects, dev_pos, scene_scale, planetMass);
checkCUDAErrorWithLine("Kernel failed!");
generateCircularVelArray<<<fullBlocksPerGrid, blockSize>>>(2, numObjects, dev_vel, dev_pos);
checkCUDAErrorWithLine("Kernel failed!");
cudaThreadSynchronize();
}
void cudaNBodyUpdateWrapper(float dt)
{
dim3 fullBlocksPerGrid((int)ceil(float(numObjects)/float(blockSize)));
updateF<<<fullBlocksPerGrid, blockSize, blockSize*sizeof(glm::vec4)>>>(numObjects, dt, dev_pos, dev_vel, dev_acc);
checkCUDAErrorWithLine("Kernel failed!");
updateS<<<fullBlocksPerGrid, blockSize>>>(numObjects, dt, dev_pos, dev_vel, dev_acc);
checkCUDAErrorWithLine("Kernel failed!");
cudaThreadSynchronize();
}
void cudaUpdateVBO(float * vbodptr, int width, int height)
{
dim3 fullBlocksPerGrid((int)ceil(float(numObjects)/float(blockSize)));
sendToVBO<<<fullBlocksPerGrid, blockSize>>>(numObjects, dev_pos, vbodptr, width, height, scene_scale);
cudaThreadSynchronize();
}
void cudaUpdatePBO(float4 * pbodptr, int width, int height)
{
dim3 fullBlocksPerGrid((int)ceil(float(width*height)/float(blockSize)));
sendToPBO<<<fullBlocksPerGrid, blockSize, blockSize*sizeof(glm::vec4)>>>(numObjects, dev_pos, pbodptr, width, height, scene_scale);
cudaThreadSynchronize();
}