-
Notifications
You must be signed in to change notification settings - Fork 6
/
lbm.cu
1210 lines (1038 loc) · 31.7 KB
/
lbm.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ============================================================ */
/* LATTICE BOLTZMANN SIMULATOR */
/* GPU accelerated with CUDA */
/* */
/* Copyright (c) 2017 Tom Scherlis and Henry Friedlander */
/* For SSA Physics 3 */
/* ============================================================ */
//comment out this line to hide prints:
//#define DEBUG
#define trace_x 50
#define trace_y 57
#define DEBUG_DELAY 0
#ifdef DEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
//#include <vector_types.h>
// OpenGL Graphics includes
#include <dependencies/common/inc/helper_gl.h>
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#include <dependencies/common/inc/GL/wglew.h>
#endif
#if defined(__APPLE__) || defined(__MACOSX)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include <GLUT/glut.h>
#ifndef glutCloseFunc
#define glutCloseFunc glutWMCloseFunc
#endif
#else
#include <dependencies/common/inc/GL/freeglut.h>
#endif
//Cuda includes
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <device_launch_parameters.h>
#include <dependencies/common/inc/helper_functions.h>
#include <dependencies/common/inc/helper_cuda.h>
//-----------------------------------------------------------------------//
// GLOBAL VARS AND STRUCT DEFS //
//-----------------------------------------------------------------------//
// texture and pixel objects
GLuint pbo = 0; // OpenGL pixel buffer object
GLuint tex = 0; // OpenGL texture object
struct cudaGraphicsResource *cuda_pbo_resource;
//timing variables:
unsigned long last_draw_time = 0;
unsigned long current_draw_time = 0;
float delta_t = 1;
typedef struct {
//velocities:
float ux; //x velocity
float uy; //y velocity
float rho; //density. aka rho
float f[9];
}lbm_node;
typedef struct {
char ex; //x location
char ey; //y location
float wt; //weight
unsigned char op; //opposite char
}d2q9_node;
typedef struct {
float viscosity;
float omega;
unsigned int height;
unsigned int width;
float contrast;
float v;
unsigned char mode;
unsigned int stepsPerRender;
}parameter_set;
parameter_set params;
//GPU/CPU interop memory pointers:
unsigned char state = 0;
lbm_node* array1;
lbm_node* array2;
lbm_node* array1_gpu;
lbm_node* array2_gpu;
unsigned char* barrier;
unsigned char* barrier_gpu;
d2q9_node* d2q9_gpu;
parameter_set* params_gpu;
char needsUpdate = 1;
int prex = -1;
int prey = -1;
enum directions {
d0 = 0,
dE,
dN,
dW,
dS,
dNE,
dNW,
dSW,
dSE
};
enum render_modes {
mRho,
mCurl,
mSpeed,
mUx,
mUy
};
//cuda error variables:
cudaError_t ierrAsync;
cudaError_t ierrSync;
void getParams(parameter_set* params)
{
params->viscosity = 0.005;
params->contrast = 75;
params->v = 0.1;
params->mode = mCurl;
params->stepsPerRender = 1;
}
//------------------------------------------------------------------------------//
// HELPER FUNCTIONS //
//------------------------------------------------------------------------------//
//get 1d flat index from row and col
int getIndex_cpu(int x, int y) {
return y * params.width + x;
}
//--------------------------------------------------------------------------------//
// CUDA HELPER AND RENDER FUNCTIONS //
//--------------------------------------------------------------------------------//
__device__
unsigned char clip(int n) {
return n > 255 ? 255 : (n < 0 ? 0 : n);
}
//get 1d flat index from row and col
__device__
int getIndex(int x, int y, parameter_set* params)
{
return y * params->width + x;
}
__device__
void printNode(lbm_node* node, lbm_node* before, lbm_node* after)
{
DEBUG_PRINT(("\t\t\ttest: %x\n", after));
DEBUG_PRINT(("\trho: %.6f\n\tux: %.6f\n\tuy: %.6f\n\tvN: %.6f\n\tvE: %.6f\n\tvW: %.6f\n\tvS: %.6f\n\tv0: %.6f\n\tvNW: %.6f\n\tvNE: %.6f\n\tvSW: %.6f\n\tvSE: %.6f\n",
node->rho,
node->ux,
node->uy,
(node->f)[dN],
(node->f)[dE],
(node->f)[dW],
(node->f)[dS],
(node->f)[d0],
(node->f)[dNW],
(node->f)[dNE],
(node->f)[dSW],
(node->f)[dSW]
));
DEBUG_PRINT(("\n\tbefore: %p \n\tafter: %p \n\t node : %p \n", before, after, node));
}
__device__
uchar4 getRGB_roh(float i, parameter_set* params)
{
uchar4 val;
if (i == i)
{
int j = (1 - i) * 255 * 10; // approximately -255 to 255;
val.x = 0;
val.w = 0;
val.z = 255;
if (j > 0)
{
val.y = clip(j);
val.z = 0;
}
else
{
val.z = clip(-j);
val.y = 0;
}
}
else
{
val.y = 0;
val.x = 255;
val.w = 0;
val.z = 255;
}
return val;
}
__device__
uchar4 getRGB_u(float i)
{
uchar4 val;
if (i == i)
{
val.w = 255;
val.x = 0;
val.y = clip(i*255.0 / 1.0);
val.z = 0;
}
else
{
val.w = 255;
val.x = 255;
val.y = 0;
val.z = 0;
}
return val;
}
__device__
float computeCurlMiddleCase(int x, int y, lbm_node * array1, parameter_set* params) {
return (array1[getIndex(x, y + 1, params)].ux
- array1[getIndex(x, y - 1, params)].ux)
- (array1[getIndex(x + 1, y, params)].uy
- array1[getIndex(x - 1, y, params)].uy);
}
__device__
uchar4 getRGB_curl(int x, int y, lbm_node* array, parameter_set* params)
{
uchar4 val;
val.x = 0;
val.w = 255;
if (0 < x && x < params->width - 1) {
if (0 < y && y < params->height - 1) {
//picture[getIndex(x,y)]
if (computeCurlMiddleCase(x, y, array, params) > 0)
{
val.y = clip(20 * params->contrast * computeCurlMiddleCase(x, y, array, params));
val.z = 0;
}
else
{
val.z = clip(20 * params->contrast * -1 * computeCurlMiddleCase(x, y, array, params));
val.y = 0;
}
}
//else {
// //picture[getIndex(x,y)]
// colorIndex = (int)(nColors * (0.5 + computeCurlEdgeCase(col, row, array) * contrast * 0.3));
//}
}
if (array[getIndex(x, y, params)].rho != array[getIndex(x, y, params)].rho)
{
val.x = 255;
val.y = 0;
val.z = 0;
val.w = 255;
}
return val;
}
__device__
void computeColor(lbm_node* array, int x, int y, parameter_set* params, uchar4* image, unsigned char* barrier, int prex, int prey)
{
int i = getIndex(x, y, params);
int prei = getIndex(prex, prey, params);
if (barrier[i] == 1)
{
image[i].w = 255;
image[i].x = 255;
image[i].y = 255;
image[i].z = 255;
}
else
{
switch (params->mode)
{
case(mRho):
image[i] = getRGB_roh(array[i].rho, params);
break;
case(mCurl):
image[i] = getRGB_curl(x, y, array, params);
break;
case(mSpeed):
image[i] = getRGB_u(sqrt(array[i].ux * array[i].ux + array[i].uy * array[i].uy));
break;
case(mUx):
image[i] = getRGB_u(array[i].ux);
break;
case(mUy):
image[i] = getRGB_u(array[i].uy);
break;
}
}
if (i == prei)
{
image[i].x = 255;
image[i].y = 0;
image[i].z = 0;
image[i].w = 255;
}
}
//--------------------------------------------------------------------------------//
// CUDA COLLIDE STEP KERNEL AND DEVICES //
//--------------------------------------------------------------------------------//
__device__
void macro_gen(float* f, float* ux, float* uy, float* rho, int i, parameter_set* params)
{
const float top_row = f[6] + f[2] + f[5];
const float mid_row = f[3] + f[0] + f[1];
const float bot_row = f[7] + f[4] + f[8];
if (i == getIndex(trace_x, trace_y, params))
for (int i = 0; i < 9;i++)
{
DEBUG_PRINT(("\t\tmacro_gen: f[%d]=%.6f\n", i, f[i]));
}
*rho = top_row + mid_row + bot_row;
if (*rho > 0)
{
*ux = ((f[5] + f[1] + f[8]) - (f[6] + f[3] + f[7])) / (*rho);
*uy = (bot_row - top_row) / (*rho);
}
else
{
*ux = 0;
*uy = 0;
}
return;
}
//return acceleration
__device__
float accel_gen(int node_num, float ux, float uy, float u2, float rho, d2q9_node* d2q9)
{
float u_direct = ux * d2q9[node_num].ex + uy * (-d2q9[node_num].ey);
float unweighted = 1 + 3 * u_direct + 4.5*u_direct*u_direct - 1.5*u2;
return rho * d2q9[node_num].wt * unweighted;
}
__global__
void collide(d2q9_node* d2q9, lbm_node* before, lbm_node* after, parameter_set* params, unsigned char* barrier)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int i = getIndex(x, y, params);
float omega = 1 / (3 * params->viscosity + 0.5);
//toss out out of bounds
if (x<0 || x >= params->width || y<0 || y >= params->height)
return;
if (x == trace_x && y == trace_y)
{
DEBUG_PRINT(("\n\nPre-Collision (before):\n"));
printNode(&(before[i]), before, after);
DEBUG_PRINT(("\n\nPre-Collision (after) (not used):\n"));
printNode(&(after[i]), before, after);
}
macro_gen(before[i].f, &(after[i].ux), &(after[i].uy), &(after[i].rho), i, params);
int dir = 0;
for (dir = 0; dir<9;dir += 1)
{
(after[i].f)[dir] = (before[i].f)[dir] + omega
* (accel_gen(dir, after[i].ux, after[i].uy,
after[i].ux * after[i].ux + after[i].uy
* after[i].uy, after[i].rho, d2q9) - (before[i].f)[dir]);
}
return;
}
//--------------------------------------------------------------------------------//
// CUDA STREAM STEP KERNEL AND DEVICES //
//--------------------------------------------------------------------------------//
__device__
void doLeftWall(int x, int y, lbm_node* after, d2q9_node* d2q9, float v, parameter_set* params)
{
//DEBUG_PRINT(("setting left wall to %.6f (wt: %.3f, v: %.3f)\n", d2q9[dE].wt * (1 + 3 * v + 3 * v * v), d2q9[dE].wt,v));
(after[getIndex(x, y, params)].f)[dE] = d2q9[dE].wt * (1 + 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dNE] = d2q9[dNE].wt * (1 + 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dSE] = d2q9[dSE].wt * (1 + 3 * v + 3 * v * v);
}
__device__
void doRightWall(int x, int y, lbm_node* after, d2q9_node* d2q9, float v, parameter_set* params)
{
(after[getIndex(x, y, params)].f)[dW] = d2q9[dW].wt * (1 - 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dNW] = d2q9[dNW].wt * (1 - 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dSW] = d2q9[dSW].wt * (1 - 3 * v + 3 * v * v);
}
//(top and bottom walls)
__device__
void doFlanks(int x, int y, lbm_node* after, d2q9_node* d2q9, float v, parameter_set* params)
{
(after[getIndex(x, y, params)].f)[d0] = d2q9[d0].wt * (1 - 1.5 * v * v);
(after[getIndex(x, y, params)].f)[dE] = d2q9[dE].wt * (1 + 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dW] = d2q9[dW].wt * (1 - 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dN] = d2q9[dN].wt * (1 - 1.5 * v * v);
(after[getIndex(x, y, params)].f)[dS] = d2q9[dS].wt * (1 - 1.5 * v * v);
(after[getIndex(x, y, params)].f)[dNE] = d2q9[dNE].wt * (1 + 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dSE] = d2q9[dSE].wt * (1 + 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dNW] = d2q9[dNW].wt * (1 - 3 * v + 3 * v * v);
(after[getIndex(x, y, params)].f)[dSW] = d2q9[dSW].wt * (1 - 3 * v + 3 * v * v);
}
__device__
void streamEdgeCases(int x, int y, lbm_node* after, unsigned char* barrier,
parameter_set* params, d2q9_node* d2q9)
{
if (x == 0)
{
if (barrier[getIndex(x, y, params)] != 1)
{
//DEBUG_PRINT(("doing left wall!"));
doLeftWall(x, y, after, d2q9, params->v, params);
}
}
else if (x == params->width - 1)
{
if (barrier[getIndex(x, y, params)] != 1)
{
doRightWall(x, y, after, d2q9, params->v, params);
}
}
else if (y == 0 || y == params->width - 1)
{
if (barrier[getIndex(x, y, params)] != 1)
{
doFlanks(x, y, after, d2q9, params->v, params);
}
}
}
//stream: handle particle propagation, ignoring edge cases.
__global__
void stream(d2q9_node* d2q9, lbm_node* before, lbm_node* after,
unsigned char* barrier, parameter_set* params)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int i = getIndex(x, y, params);
if (x == trace_x && y == trace_y)
{
DEBUG_PRINT(("\n\nPre-stream: (before)\n"));
printNode(&(before[i]), before, after);
}
//toss out out of bounds and edge cases
if (x < 0 || x >= params->width || y < 0 || y >= params->height)
return;
after[i].rho = before[i].rho;
after[i].ux = before[i].ux;
after[i].uy = before[i].uy;
if (!(x > 0 && x < params->width - 1 && y > 0 && y < params->height - 1))
{
//return;
streamEdgeCases(x, y, after, barrier, params, d2q9);
}
else
{
//propagate all f values around a bit
int dir = 0;
for (dir = 0;dir < 9;dir += 1)
{
(after[getIndex(d2q9[dir].ex + x, -d2q9[dir].ey + y, params)].f)[dir] =
before[i].f[dir];
}
}
}
//--------------------------------------------------------------------------------//
// CUDA BOUNCE STEP KERNEL AND DEVICES //
//--------------------------------------------------------------------------------//
/*__device__
void bounceEdgeCases(int x, int y, lbm_node* after, unsigned char* barrier,
parameter_set* params, d2q9_node* d2q9)
{
}*/
__global__
void bounceAndRender(d2q9_node* d2q9, lbm_node* before, lbm_node* after,
unsigned char* barrier, parameter_set* params, uchar4* image, int prex, int prey)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int i = getIndex(x, y, params);
if (x == trace_x && y == trace_y)
{
DEBUG_PRINT(("\n\npre-barriers:\n"));
printNode(&(after[i]), before, after);
}
//toss out out of bounds and edge cases
if (x < 0 || x >= params->width || y < 0 || y >= params->height)
return;
if (x > 0 && x < params->width - 1 && y>0 && y < params->height - 1)
{
if (barrier[i] == 1)
{
int dir;
for (dir = 1; dir < 9; dir += 1)
{
if (d2q9[dir].op > 0 && (after[i].f)[dir]>0)
{
(after[getIndex(d2q9[dir].ex + x, -d2q9[dir].ey + y, params)].f)[dir]
= (before[i].f)[d2q9[dir].op];
//printf("doin a barrier bounce! %d\n",dir);
//(after[i].f)[dir] += (after[i].f)[d2q9[dir].op];
// + (after[i].f)[dir];
//(after[i].f)[d2q9[dir].op] = 0;
}
}
}
}
else
{
//bounceEdgeCases(x, y, after, barrier, params, d2q9);
}
if (x == trace_x && y == trace_y)
{
DEBUG_PRINT(("\n\nFinal rendered:\n"));
printNode(&(after[i]), before, after);
}
computeColor(after, x, y, params, image, barrier, prex, prey);
}
//--------------------------------------------------------------------------------//
// CUDA INITIALIZER FUNCTIONS //
//--------------------------------------------------------------------------------//
//provide LBM constants for d2q9 style nodes
//assumes positive is up and right, whereas our program assumes positive down and right.
void init_d2q9(d2q9_node* d2q9)
{
d2q9[0].ex = 0; d2q9[0].ey = 0; d2q9[0].wt = 4.0 / 9.0; d2q9[0].op = 0;
d2q9[1].ex = 1; d2q9[1].ey = 0; d2q9[1].wt = 1.0 / 9.0; d2q9[1].op = 3;
d2q9[2].ex = 0; d2q9[2].ey = 1; d2q9[2].wt = 1.0 / 9.0; d2q9[2].op = 4;
d2q9[3].ex = -1; d2q9[3].ey = 0; d2q9[3].wt = 1.0 / 9.0; d2q9[3].op = 1;
d2q9[4].ex = 0; d2q9[4].ey = -1; d2q9[4].wt = 1.0 / 9.0; d2q9[4].op = 2;
d2q9[5].ex = 1; d2q9[5].ey = 1; d2q9[5].wt = 1.0 / 36.0; d2q9[5].op = 7;
d2q9[6].ex = -1; d2q9[6].ey = 1; d2q9[6].wt = 1.0 / 36.0; d2q9[6].op = 8;
d2q9[7].ex = -1; d2q9[7].ey = -1; d2q9[7].wt = 1.0 / 36.0; d2q9[7].op = 5;
d2q9[8].ex = 1; d2q9[8].ey = -1; d2q9[8].wt = 1.0 / 36.0; d2q9[8].op = 6;
}
void zeroSite(lbm_node* array, int index)
{
int dir = 0;
for (dir = 0; dir < 9; dir += 1)
{
(array[index].f)[dir] = 0;
}
array[index].rho = 1;
array[index].ux = 0;
array[index].uy = 0;
}
void initFluid() {
int W = params.width;
int H = params.height;
float v = params.v;
//printf("velocity is %.6f my dude\n", v);
barrier = (unsigned char*)calloc(W*H, sizeof(unsigned char));
array1 = (lbm_node*)calloc(W*H, sizeof(lbm_node));
array2 = (lbm_node*)calloc(W*H, sizeof(lbm_node));
lbm_node* before = array1;
d2q9_node* d2q9 = (d2q9_node*)calloc(9, sizeof(d2q9_node));
init_d2q9(d2q9);
DEBUG_PRINT(("\tTESTWEIGHT = %.6f", d2q9[dE].wt));
int i;
for (int x = 0; x < params.width; x++)
{
for (int y = 0; y < params.height; y++)
{
i = getIndex_cpu(x, y);
(before[i].f)[d0] = d2q9[d0].wt * (1 - 1.5 * v * v);
(before[i].f)[dE] = d2q9[dE].wt * (1 + 3 * v + 3 * v * v);
(before[i].f)[dW] = d2q9[dW].wt * (1 - 3 * v + 3 * v * v);
(before[i].f)[dN] = d2q9[dN].wt * (1 - 1.5 * v * v);
(before[i].f)[dS] = d2q9[dS].wt * (1 - 1.5 * v * v);
(before[i].f)[dNE] = d2q9[dNE].wt * (1 + 3 * v + 3 * v * v);
(before[i].f)[dSE] = d2q9[dSE].wt * (1 + 3 * v + 3 * v * v);
(before[i].f)[dNW] = d2q9[dNW].wt * (1 - 3 * v + 3 * v * v);
(before[i].f)[dSW] = d2q9[dSW].wt * (1 - 3 * v + 3 * v * v);
before[i].rho = 1;
before[i].ux = params.v;
before[i].uy = 0;
}
}
ierrSync = cudaMalloc(&d2q9_gpu, 9 * sizeof(d2q9_node));
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMalloc(¶ms_gpu, sizeof(parameter_set));
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMalloc(&barrier_gpu, sizeof(unsigned char)*W*H);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMalloc(&array1_gpu, sizeof(lbm_node)*W*H);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMalloc(&array2_gpu, sizeof(lbm_node)*W*H);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMemcpy(d2q9_gpu, d2q9, sizeof(d2q9_node) * 9, cudaMemcpyHostToDevice);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMemcpy(params_gpu, ¶ms, sizeof(params), cudaMemcpyHostToDevice);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMemcpy(barrier_gpu, barrier, sizeof(unsigned char)*W*H, cudaMemcpyHostToDevice);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMemcpy(array1_gpu, array1, sizeof(lbm_node)*W*H, cudaMemcpyHostToDevice);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
ierrSync = cudaMemcpy(array2_gpu, array2, sizeof(lbm_node)*W*H, cudaMemcpyHostToDevice);
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
cudaDeviceSynchronize();
return;
}
//determine front and back lattice buffer orientation
//and launch all 3 LBM kernels
void kernelLauncher(uchar4* image)
{
if (needsUpdate)
{
cudaMemcpy(barrier_gpu, barrier, sizeof(unsigned char)*params.width * params.height, cudaMemcpyHostToDevice);
cudaMemcpy(params_gpu, ¶ms, sizeof(params), cudaMemcpyHostToDevice);
needsUpdate = 0;
cudaDeviceSynchronize(); // Wait for the GPU to finish
}
lbm_node* before = array1_gpu;
lbm_node* after = array2_gpu;
DEBUG_PRINT(("these are the addresses: \n\t\tb4=%p\taft=%p\n\t\tar1=%p\tar2=%p", before, after, array1_gpu, array2_gpu));
//determine number of threads and blocks required
dim3 threads_per_block = dim3(32, 32, 1);
dim3 number_of_blocks = dim3(params.width / 32 + 1, params.height / 32 + 1, 1);
collide << < number_of_blocks, threads_per_block >> > (d2q9_gpu, before, after, params_gpu, barrier_gpu);
ierrSync = cudaGetLastError();
ierrAsync = cudaDeviceSynchronize(); // Wait for the GPU to finish
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
if (ierrAsync != cudaSuccess) { DEBUG_PRINT(("Async error: %s\n", cudaGetErrorString(ierrAsync))); }
before = array2_gpu;
after = array1_gpu;
stream << < number_of_blocks, threads_per_block >> > (d2q9_gpu, before, after, barrier_gpu, params_gpu);
ierrSync = cudaGetLastError();
ierrAsync = cudaDeviceSynchronize(); // Wait for the GPU to finish
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
if (ierrAsync != cudaSuccess) { DEBUG_PRINT(("Async error: %s\n", cudaGetErrorString(ierrAsync))); }
bounceAndRender << < number_of_blocks, threads_per_block >> > (d2q9_gpu, before, after, barrier_gpu, params_gpu, image, prex, prey);
ierrSync = cudaGetLastError();
ierrAsync = cudaDeviceSynchronize(); // Wait for the GPU to finish
if (ierrSync != cudaSuccess) { DEBUG_PRINT(("Sync error: %s\n", cudaGetErrorString(ierrSync))); }
if (ierrAsync != cudaSuccess) { DEBUG_PRINT(("Async error: %s\n", cudaGetErrorString(ierrAsync))); }
}
//-----------------------------------------------------------//
// BARRIER FUNCTIONS //
//-----------------------------------------------------------//
void clearBarriers()
{
for (int i = 0;i < params.width;i++)
{
for (int j = 0; j < params.height;j++)
{
barrier[getIndex_cpu(i, j)] = 0;
}
}
}
void drawLineDiagonal()
{
for (int i = 0; i < params.height/4; i++)
{
barrier[getIndex_cpu((params.width / 3) + (i / 3), params.height / 3 + i)] = 1;
}
}
void drawSquare()
{
for (int i = 0; i < params.height/4; i++)
{
for (int j = 0; j < params.height / 4; j++)
{
//if(i==0 || i== params.height / 4-1 || j==0 || j == params.height / 4-1)
barrier[getIndex_cpu(i+params.width/3, j+params.height * 3 / 8)] = 1;
}
}
}
//-----------------------------------------------------------//
// OPENGL CALLBACK FUNCTIONS //
//-----------------------------------------------------------//
char waitingForSpeed = 0;
char waitingForViscosity = 0;
char waitingForRate = 0;
//keyboard callback
void keyboard(unsigned char a, int b, int c)
{
DEBUG_PRINT(("%x pressed\n", a));
if (!(waitingForSpeed || waitingForViscosity || waitingForRate))
{
switch (a)
{
case'1':
params.mode = mRho;
printf("render mode set to rho\n");
break;
case'2':
params.mode = mCurl;
printf("render mode set to curl\n");
break;
case'3':
params.mode = mSpeed;
printf("render mode set to speed\n");
break;
case'4':
params.mode = mUx;
printf("render mode set to Ux\n");
break;
case'5':
params.mode = mUy;
printf("render mode set to Uy\n");
break;
case'q':
clearBarriers();
printf("Barriers Cleared!\n");
break;
case'w':
initFluid();
printf("Field Reset!\n");
break;
case'a':
clearBarriers();
//drawLineLong();
break;
case's':
clearBarriers();
//drawLineShort();
break;
case'd':
clearBarriers();
drawLineDiagonal();
break;
case'f':
clearBarriers();
drawSquare();
break;
case'z':
printf("Enter speed using 1-0:\n");
waitingForSpeed = 1;
break;
case'x':
printf("Enter viscosity using 1-0:\n");
waitingForViscosity = 1;
break;
case'c':
printf("Enter refresh rate using 1-0:\n");
waitingForRate = 1;
break;
default: break;
}
}
else if (waitingForViscosity)
{
switch (a)
{
case '1': params.viscosity = 0.003; break;
case '2': params.viscosity = 0.005; break;
case '3': params.viscosity = 0.008; break;
case '4': params.viscosity = 0.011; break;
case '5': params.viscosity = 0.016; break;
case '6': params.viscosity = 0.02; break;
case '7': params.viscosity = 0.04; break;
case '8': params.viscosity = 0.08; break;
case '9': params.viscosity = 0.13; break;
case '0': params.viscosity = 0.2; break;
default: break;
}
waitingForViscosity = 0;
printf("viscosity set to %.3f\n", params.viscosity);
}
else if (waitingForSpeed)
{
switch (a)
{
case '1': params.v = 0.01; break;
case '2': params.v = 0.03; break;
case '3': params.v = 0.05; break;
case '4': params.v = 0.07; break;
case '5': params.v = 0.09; break;
case '6': params.v = 0.11; break;
case '7': params.v = 0.13; break;
case '8': params.v = 0.14; break;
case '9': params.v = 0.17; break;
case '0': params.v = 0.2; break;
default: break;
}
waitingForSpeed = 0;
printf("speed set to %.2f\n", params.v);
}
else if (waitingForRate)
{
switch (a)
{
case '1': params.stepsPerRender = 1; break;
case '2': params.stepsPerRender = 2; break;
case '3': params.stepsPerRender = 3; break;
case '4': params.stepsPerRender = 4; break;
case '5': params.stepsPerRender = 5; break;
case '6': params.stepsPerRender = 6; break;
case '7': params.stepsPerRender = 7; break;
case '8': params.stepsPerRender = 8; break;
case '9': params.stepsPerRender = 9; break;
case '0': params.stepsPerRender = 10; break;
default: break;
}
waitingForRate = 0;
printf("refresh rate set to %d\n", params.stepsPerRender);
}
needsUpdate = 1;
}
//special keyboard callback
void handleSpecialKeypress(int a, int b, int c)
{
}
int current_button = GLUT_LEFT_BUTTON;
void mouseClick(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
if(button==GLUT_LEFT_BUTTON)
{
current_button = GLUT_LEFT_BUTTON;
int lx, ly; // lattice coordinates
lx = x * params.width / glutGet(GLUT_WINDOW_WIDTH);
ly = y * params.height / glutGet(GLUT_WINDOW_HEIGHT);
if (lx >= params.width || ly >= params.height)
return;
barrier[getIndex_cpu(lx, ly)] = 1;
needsUpdate = 1;
}
else if (button == GLUT_RIGHT_BUTTON)
{
current_button = GLUT_RIGHT_BUTTON;
int lx, ly; // lattice coordinates
lx = x * params.width / glutGet(GLUT_WINDOW_WIDTH);
ly = y * params.height / glutGet(GLUT_WINDOW_HEIGHT);
if (lx >= params.width || ly >= params.height)
return;
barrier[getIndex_cpu(lx, ly)] = 0;
needsUpdate = 1;
}
}
}
//mouse move callback
void mouseMove(int x, int y)
{
int lx, ly; // lattice coordinates
lx = x * params.width / glutGet(GLUT_WINDOW_WIDTH);
ly = y * params.height / glutGet(GLUT_WINDOW_HEIGHT);
if (lx >= params.width || ly >= params.height)
return;
prex = lx;
prey = ly;
}
//mouse drag callback
void mouseDrag(int x, int y)
{
int lx, ly; // lattice coordinates
lx = x * params.width / glutGet(GLUT_WINDOW_WIDTH);
ly = y * params.height / glutGet(GLUT_WINDOW_HEIGHT);
if (lx >= params.width || ly >= params.height)
return;
prex = lx;
prey = ly;
if (current_button == GLUT_LEFT_BUTTON)
{
barrier[getIndex_cpu(lx, ly)] = 1;
}
else if (current_button == GLUT_RIGHT_BUTTON)
{
barrier[getIndex_cpu(lx, ly)] = 0;
}
needsUpdate = 1;
}
//gl exit callback
void exitfunc()
{
//empty all cuda resources
if (pbo)
{
cudaGraphicsUnregisterResource(cuda_pbo_resource);
glDeleteBuffers(1, &pbo);
glDeleteTextures(1, &tex);
}
cudaFree(array1_gpu);
cudaFree(array2_gpu);
cudaFree(barrier_gpu);
cudaFree(params_gpu);
cudaFree(d2q9_gpu);
}
//display stats of all detected cuda capable devices,
//and return the number
int deviceQuery()