-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cu
473 lines (382 loc) · 12.1 KB
/
kernel.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
const GLint WINDOW_WIDTH = 820;
const GLint WINDOW_HEIGHT = 640;
const int4 magneticField = { WINDOW_WIDTH / 3, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
__device__ const float dev_starB = 5e-6f; // Tsl
__device__ const float2 dev_starE = { 0.0f, 1e-1f }; // V/m
__device__ const float dev_lambda = 1e-3f;// m
__device__ const float dev_C = 3e8f; // Speed of light, m/s
const float lambda = 1e-3f;
const float C = 3e8f;
// location of electromagnetic field
__device__ const int4 dev_magneticField = {
WINDOW_WIDTH/3, // x-start
-WINDOW_HEIGHT*10, // y-start
WINDOW_WIDTH*10, // x-end
WINDOW_HEIGHT*10 // y-end
};
const float TIME_SCALE = 1e-4f;
const float starV = 5e4f; // m/s
const float V_MAX = starV / C;
const float V_MIN = 0.3f * V_MAX;
const float MAX_CHARGE = 1.6e-19f;
const float MIN_CHARGE = 0.3f * MAX_CHARGE;
__constant__ const float K = 1.5e21f;
const int MAX_CHARGE_COUNT = 20;
struct Particle {
float x;
float y;
float vx;
float vy;
float charge;
float mass;
bool isPhysical;
};
Particle charges[MAX_CHARGE_COUNT];
Particle* dev_charges;
int chargeCount = 0;
__device__ int* dev_chargeCount;
bool electricFieldEnabled = true;
__device__ bool* dev_electricFieldEnabled;
dim3 blocks, threads;
/* OpenGL interoperability */
GLuint vbo;
cudaGraphicsResource* cuda_vbo_resource;
static void cudaCheckError(cudaError_t err, const char* file, int line);
#define HANDLE_ERROR( err ) (cudaCheckError( err, __FILE__, __LINE__ ))
void createVBO(GLuint* vbo, cudaGraphicsResource** vbo_res, unsigned int vbo_res_flags);
void deleteVBO(GLuint* vbo, cudaGraphicsResource* vbo_res);
__device__ bool isInMagneticField(float x, float y) {
if (
x < dev_magneticField.x ||
x > dev_magneticField.z ||
y < dev_magneticField.y ||
y > dev_magneticField.w
) return false;
return true;
}
// differential equation
__device__ inline float4 dF(const Particle& p, bool* d_electricFieldEnabled) {
if (isInMagneticField(p.x, p.y)) {
float k = p.charge * dev_lambda / (p.mass * dev_C);
float B = dev_starB * k;
float2 E = {
dev_starE.x * k / dev_C,
dev_starE.y * k / dev_C
};
if (!(*d_electricFieldEnabled)) {
E.x = 0;
E.y = 0;
}
return {
p.vx,
p.vy,
E.x + B * p.vy,
E.y - B * p.vx
};
}
return {
p.vx,
p.vy,
0,
0
};
}
__global__ void dev_applyMagneticField(
uchar4* screen, Particle* d_charges,
int* d_chargesCount, bool* d_electricFieldEnabled, float dt
) {
int charge_i = blockIdx.x * blockDim.x + threadIdx.x;
if (charge_i >= *d_chargesCount) return;
Particle& particle = d_charges[charge_i];
if (!particle.isPhysical) return;
// RK4 method for solving ODE
Particle p2 = particle;
float4 d1 = dF(p2, d_electricFieldEnabled);
p2.x = particle.x + dt * d1.x / 2;
p2.y = particle.y + dt * d1.y / 2;
p2.vx = particle.vx + dt * d1.z / 2;
p2.vy = particle.vy + dt * d1.w / 2;
float4 d2 = dF(p2, d_electricFieldEnabled);
p2.x = particle.x + dt * d2.x / 2;
p2.y = particle.y + dt * d2.y / 2;
p2.vx = particle.vx + dt * d2.z / 2;
p2.vy = particle.vy + dt * d2.w / 2;
float4 d3 = dF(p2, d_electricFieldEnabled);
p2.x = particle.x + dt * d3.x;
p2.y = particle.y + dt * d3.y;
p2.vx = particle.vx + dt * d3.z;
p2.vy = particle.vy + dt * d3.w;
float4 d4 = dF(p2, d_electricFieldEnabled);
particle.x += dt / 6 * (d1.x + 2*d2.x + 2*d3.x + d4.x); // x + dx
particle.y += dt / 6 * (d1.y + 2*d2.y + 2*d3.y + d4.y); // y + dy
particle.vx += dt / 6 * (d1.z + 2 * d2.z + 2 * d3.z + d4.z);
particle.vy += dt / 6 * (d1.w + 2 * d2.w + 2 * d3.w + d4.w);
if (particle.x >= 10 * WINDOW_WIDTH) particle.isPhysical = false;
if (particle.x < -10 * WINDOW_WIDTH) particle.isPhysical = false;
if (particle.y >= 10 * WINDOW_HEIGHT) particle.isPhysical = false;
if (particle.y < -10 * WINDOW_HEIGHT) particle.isPhysical = false;
if (
particle.x < WINDOW_WIDTH &&
particle.x >= 0 &&
particle.y < WINDOW_HEIGHT &&
particle.y >= 0
) {
uchar4& pixel = screen[(int)particle.x + (int)particle.y * WINDOW_WIDTH];
pixel.y = 250;
}
}
__global__ void dev_clearFrame(uchar4* screen) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= WINDOW_WIDTH || y >= WINDOW_HEIGHT) return;
uchar4& pixel = screen[x + y * WINDOW_WIDTH];
pixel.x = 0;
pixel.y = 0;
pixel.z = 0;
pixel.w = 255;
}
// Compute electric field created by particles charges
__global__ void dev_renderFrame(uchar4* screen, Particle* dev_charges, int* d_chargesCount) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= WINDOW_WIDTH || y >= WINDOW_HEIGHT) return;
float E = 0;
for (char i = 0; i < *d_chargesCount; i++) {
const Particle& particle = dev_charges[i];
float2 t_force = {
x - particle.x, // dx
y - particle.y // dy
};
float length2 = t_force.x*t_force.x + t_force.y*t_force.y + 1;
E += particle.charge / length2;
}
uchar4& pixel = screen[x + y * WINDOW_WIDTH];
pixel.y = 0;
pixel.w = 255;
int brightness = K * fabs(E);
if (E > 0.0) {
pixel.x = pixel.x > brightness
? pixel.x
: brightness;
} else {
pixel.z = pixel.z > brightness
? pixel.z
: brightness;
}
}
float elapsedTime = 0.0f;
void idle(void) {
uchar4* screen;
size_t size;
HANDLE_ERROR(cudaGraphicsMapResources(1, &cuda_vbo_resource, 0));
HANDLE_ERROR(
cudaGraphicsResourceGetMappedPointer((void**)&screen, &size, cuda_vbo_resource)
);
cudaEvent_t startEvent, stopEvent;
HANDLE_ERROR(cudaEventCreate(&startEvent));
HANDLE_ERROR(cudaEventCreate(&stopEvent));
HANDLE_ERROR(cudaEventRecord(startEvent, 0));
//float elapsedTimeS = elapsedTime / 1000.0;
float elapsedTimeS = 1 / 1000.0f;
float dtau = elapsedTimeS * C / lambda;
dev_renderFrame<<<blocks, threads>>>(screen, dev_charges, dev_chargeCount);
dev_applyMagneticField<<<1, MAX_CHARGE_COUNT>>>(
screen, dev_charges, dev_chargeCount, dev_electricFieldEnabled,
dtau * TIME_SCALE
);
HANDLE_ERROR(cudaDeviceSynchronize());
HANDLE_ERROR(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
HANDLE_ERROR(cudaEventRecord(stopEvent, 0));
HANDLE_ERROR(cudaEventSynchronize(stopEvent));
HANDLE_ERROR(cudaEventElapsedTime(&elapsedTime, startEvent, stopEvent));
char fps[256];
sprintf(fps, "%3.2f ms per frame (FPS: %3.1f)", elapsedTime,
1000 / elapsedTime);
glutSetWindowTitle(fps);
glutPostRedisplay();
}
void draw(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glColor4f(0.4f, 0.4f, 1.0f, 0.2f);
glRecti(
magneticField.x, magneticField.y,
magneticField.z, magneticField.w
);
glutSwapBuffers();
}
void clearScreen() {
chargeCount = 0;
HANDLE_ERROR(
cudaMemcpy(dev_chargeCount, &chargeCount, sizeof(chargeCount), cudaMemcpyHostToDevice)
);
uchar4* screen;
size_t size;
HANDLE_ERROR(cudaGraphicsMapResources(1, &cuda_vbo_resource, 0));
HANDLE_ERROR(
cudaGraphicsResourceGetMappedPointer((void**)&screen, &size, cuda_vbo_resource)
);
dev_clearFrame<<<blocks, threads>>>(screen);
HANDLE_ERROR(cudaDeviceSynchronize());
HANDLE_ERROR(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
glutPostRedisplay();
}
void addCharge(int x, int y) {
if (chargeCount < MAX_CHARGE_COUNT) {
chargeCount++;
} else {
for (int i = 0; i < MAX_CHARGE_COUNT - 1; ++i) {
charges[i] = charges[i + 1];
}
}
float scale = rand() / (float)RAND_MAX; /* [0, 1.0] */
float newCharge = MIN_CHARGE + (float)scale * (MAX_CHARGE - MIN_CHARGE); /* [min, max] */
float scale2 = rand() / (float)RAND_MAX; /* [0, 1.0] */
if (scale2 < 0.5) {
newCharge = -newCharge;
}
float vScale = rand() / (float)RAND_MAX; /* [0, 1.0] */
charges[chargeCount - 1].x = (float)x;
charges[chargeCount - 1].y = (float)y;
charges[chargeCount - 1].charge = newCharge;
charges[chargeCount - 1].vx = V_MIN + vScale * (V_MAX - V_MIN);
charges[chargeCount - 1].vy = 0.0f;
charges[chargeCount - 1].mass = 9.11e-31f;
charges[chargeCount - 1].isPhysical = true;
}
void addCharges(int x, int y, int n) {
HANDLE_ERROR(
cudaMemcpy(charges, dev_charges, chargeCount * sizeof(Particle), cudaMemcpyDeviceToHost)
);
float disp = 40;
for (int i = 0; i < MAX_CHARGE_COUNT && i < n; i++) {
float dx = rand() / (float)RAND_MAX * disp;
float dy = rand() / (float)RAND_MAX * disp;
addCharge(int(x + dx - disp/2), int(y + dy - disp / 2));
printf("Charges %d\n", chargeCount);
}
HANDLE_ERROR(
cudaMemcpy(dev_charges, charges, chargeCount * sizeof(Particle), cudaMemcpyHostToDevice)
);
HANDLE_ERROR(
cudaMemcpy(dev_chargeCount, &chargeCount, sizeof(chargeCount), cudaMemcpyHostToDevice)
);
}
void toggleElectricField() {
electricFieldEnabled = !electricFieldEnabled;
if (electricFieldEnabled) {
printf("Electric Field is enabled.\n");
} else {
printf("Electric Field is disabled.\n");
}
HANDLE_ERROR(
cudaMemcpy(dev_electricFieldEnabled, &electricFieldEnabled,
sizeof(electricFieldEnabled), cudaMemcpyHostToDevice)
);
}
void onKeyboardEvent(unsigned char key, int x, int y) {
if (key == 'E' || key == 'e') {
toggleElectricField();
}
}
void onMouseEvent(int button, int state, int x, int y) {
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) {
clearScreen();
return;
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
addCharges(x, WINDOW_HEIGHT - y, 1);
return;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
clearScreen();
addCharges(x, WINDOW_HEIGHT - y, MAX_CHARGE_COUNT);
return;
}
}
void onResize(int width, int height) {
glutReshapeWindow(WINDOW_WIDTH, WINDOW_HEIGHT);
}
void initGlut(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
int posX = glutGet(GLUT_SCREEN_WIDTH) / 2 - WINDOW_WIDTH / 2;
int posY = glutGet(GLUT_SCREEN_HEIGHT) / 2 - WINDOW_HEIGHT / 2;
glutInitWindowPosition(posX, posY);
glutCreateWindow("Electro-magnetic fiels sim");
glutIdleFunc(idle);
glutDisplayFunc(draw);
glutMouseFunc(onMouseEvent);
glutKeyboardFunc(onKeyboardEvent);
glutReshapeFunc(onResize);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
// enable alpha-channel
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glewInit();
}
int main(int argc, char** argv) {
srand((unsigned int)time(0));
initGlut(argc, argv);
cudaDeviceProp properties;
HANDLE_ERROR(cudaGetDeviceProperties(&properties, 0));
threads.x = 32;
threads.y = properties.maxThreadsPerBlock / threads.x;
blocks.x = (WINDOW_WIDTH + threads.x) / threads.x;
blocks.y = (WINDOW_HEIGHT + threads.y) / threads.y;
cudaMalloc((void**)&dev_charges, sizeof(Particle) * MAX_CHARGE_COUNT);
cudaMalloc((void**)&dev_chargeCount, sizeof(chargeCount));
cudaMalloc((void**)&dev_electricFieldEnabled, sizeof(electricFieldEnabled));
HANDLE_ERROR(
cudaMemcpy(dev_chargeCount, &chargeCount,
sizeof(chargeCount), cudaMemcpyHostToDevice)
);
HANDLE_ERROR(
cudaMemcpy(
dev_electricFieldEnabled, &electricFieldEnabled,
sizeof(electricFieldEnabled), cudaMemcpyHostToDevice)
);
createVBO(&vbo, &cuda_vbo_resource, cudaGraphicsMapFlagsWriteDiscard);
glutMainLoop();
deleteVBO(&vbo, cuda_vbo_resource);
cudaFree(dev_charges);
cudaFree(dev_chargeCount);
return 0;
}
static void cudaCheckError(cudaError_t err, const char* file, int line) {
if (err != cudaSuccess) {
printf("%s in %s at line %d\n", cudaGetErrorString(err), file, line);
exit(EXIT_FAILURE);
}
}
void createVBO(
GLuint* vbo,
struct cudaGraphicsResource** vbo_res,
unsigned int vbo_res_flags
) {
unsigned int size = WINDOW_WIDTH * WINDOW_HEIGHT * sizeof(uchar4);
glGenBuffers(1, vbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, *vbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW);
HANDLE_ERROR(cudaGraphicsGLRegisterBuffer(vbo_res, *vbo, vbo_res_flags));
}
void deleteVBO(GLuint* vbo, struct cudaGraphicsResource* vbo_res) {
HANDLE_ERROR(cudaGraphicsUnregisterResource(cuda_vbo_resource));
glBindBuffer(1, *vbo);
glDeleteBuffers(1, vbo);
*vbo = 0;
}