forked from michal1000w/GPUSmoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRID3D.h
1681 lines (1268 loc) · 51.6 KB
/
GRID3D.h
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
#pragma once
//VDB
//#define HOSTALLOC
//#define NEW_HOST_ALLOC
#include "cutil_math.h"
#define _USE_MATH_DEFINES
#include <nanovdb/NanoVDB.h>
#include <openvdb/openvdb.h>
#include <openvdb/tools/GridTransformer.h>
//#include <openvdb/tools/Interpolation.h>
#include <openvdb/tools/LevelSetSphere.h>
#include <openvdb/tools/SignedFloodFill.h>
#include <cmath>
#include <cuda_runtime.h>
#include <tbb/parallel_for.h>
#include <tbb/atomic.h>
#include "printUtils.h"
#define SIZEOF_FLOAT3 (sizeof(float) * 3)
//#define SIZEOF_FLOAT3 (sizeof(float3))
#define SUPER_NULL -122.1123123
#define VELOCITY_NOISE
/*
#define CHECK cudaError_t error = cudaGetLastError();\
if (error != cudaSuccess){\
printf("CUDA Error: %s\n", cudaGetErrorString(error));\
}
#define cudaCheckError() { \
cudaError_t e=cudaGetLastError(); \
if(e!=cudaSuccess) { \
printf("Cuda failure %s:%d: '%s'\n",__FILE__,__LINE__,cudaGetErrorString(e)); \
} \
}
*/
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
inline std::vector<int> enumerate(int start_value, int max_values) {
std::vector<int> output;
output.push_back(start_value);
int j = 0;
for (int i = 0; i < max_values; i++) {
if (i == start_value) j++;
output.push_back(i + j);
}
return output;
}
inline void __checkCudaErrors(cudaError err, const char* file, const int line)
{
if (cudaSuccess != err)
{
printf("\n\n%s line:(%i) : CUDA Runtime API error %d: %s.\n", file, line, (int)err, cudaGetErrorString(err));
exit(-1);
}
}
template <typename T>
inline void multiGPU_clear(std::vector<T*>* gridd, int size, int devices_count, int deviceIndex) {
std::vector<std::thread> threads;
//printLn("ZEROOO(" << size << ")";
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
checkCudaErrors(cudaSetDevice(lista[device_id]));
printLn(device_id);
checkCudaErrors(cudaMemsetAsync(gridd->at(device_id), 0, size));
//checkCudaErrors(cudaMemcpy(&dst[device_id], &src, sizeof(float) * size, cudaMemcpyHostToDevice));
//printLn("Zeroed";
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copyHTD(int devices_count, std::vector<float*>* dst, float* src, int size, int deviceIndex) {
std::vector<std::thread> threads;
printLn("CPPPPPP(" << dst->size() << ")");
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
checkCudaErrors(cudaSetDevice(lista[device_id]));
printLn(device_id);
checkCudaErrors(cudaMemcpyAsync(dst->at(device_id), src, sizeof(float) * size, cudaMemcpyHostToDevice));
//checkCudaErrors(cudaMemcpy(&dst[device_id], &src, sizeof(float) * size, cudaMemcpyHostToDevice));
printLn("Copy");
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, std::vector<float3*>& dst, float3* src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst[device_id], src, SIZEOF_FLOAT3 * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, float* dst, std::vector<float*>& src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst, src[device_id], sizeof(float) * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, float3* dst, std::vector<float3*>& src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst, src[device_id], SIZEOF_FLOAT3 * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, float* dst, float* src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst, src, sizeof(float) * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, float3* dst, float3* src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst, src, SIZEOF_FLOAT3 * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, std::vector<float*>* dst, std::vector<float*>* src, int size, cudaMemcpyKind type, int deviceIndex) { //dobre
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst->at(device_id), src->at(device_id), sizeof(float) * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copyn(int devices_count, std::vector<float*>* dst, std::vector<float*>* src, int size, cudaMemcpyKind type, int deviceIndex, std::string message = "") { //dobre
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
printLn("Cuda device set. ID: " << device_id << " Device: " << lista[device_id]);
printLn("Copy -> " << device_id << " at " << message);
checkCudaErrors(cudaMemcpyAsync(dst->at(device_id), src->at(device_id), sizeof(float) * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
inline void multiGPU_copy(int devices_count, std::vector<float3*>& dst, std::vector<float3*>& src, int size, cudaMemcpyKind type, int deviceIndex) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
checkCudaErrors(cudaMemcpyAsync(dst[device_id], src[device_id], SIZEOF_FLOAT3 * size, type));
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
}
template <typename T>
inline std::vector<T*> multiGPU_malloc(int devices_count, int deviceIndex, long long size) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
std::vector<T*> _dst;
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
_dst.push_back(new T);
std::mutex m1;
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
printLn(device_id);
#ifndef HOSTALLOC
checkCudaErrors(cudaMalloc((void**)&_dst[device_id], sizeof(T) * size));
#else
checkCudaErrors(cudaHostAlloc(&_dst[device_id], size * sizeof(T), cudaHostAllocMapped));
#endif
cudaDeviceSynchronize();
printLn(device_id);
}));
}
for (auto& thread : threads)
thread.join();
printLn("Done");
//allow_p2p_sharing(devices_count);
return _dst;
}
inline std::vector<float3*> multiGPU_malloc3(int devices_count, int deviceIndex, long long size) {
std::vector<std::thread> threads;
auto lista = enumerate(deviceIndex, devices_count);
std::vector<float3*> _dst;
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
_dst.push_back(new float3);
std::mutex m1;
for (unsigned int device_id = 0; device_id < devices_count; device_id++)
{
threads.push_back(std::thread([&, device_id]() {
cudaSetDevice(lista[device_id]);
printLn(device_id);
#ifndef HOSTALLOC
checkCudaErrors(cudaMalloc((void**)&_dst[device_id], SIZEOF_FLOAT3 * size));
#else
checkCudaErrors(cudaHostAlloc(&_dst[device_id], size * SIZEOF_FLOAT3, cudaHostAllocMapped));
#endif
cudaDeviceSynchronize();
}));
}
for (auto& thread : threads)
thread.join();
printLn("Done");
//allow_p2p_sharing(devices_count);
return _dst;
}
inline void multiGPU_free(int devices_count, std::vector<float*>& dst) {
for (auto device : dst)
{
checkCudaErrors(cudaFree(device));
}
dst.clear();
}
inline void multiGPU_free(int devices_count, std::vector<float3*>& dst) {
for (auto device : dst)
{
checkCudaErrors(cudaFree(device));
}
dst.clear();
}
class GRID3D {
void deletep(float&) {}
void deletep(float*& ptr) {
delete[] ptr;
ptr = nullptr;
}
void deletec(float3&) {}
void deletec(float3*& ptr) {
delete[] ptr;
ptr = nullptr;
}
void initNoiseGrid(int deviceIndex, bool vell = true) {
grid_noise = new float[1];
grid_noise[0] = SUPER_NULL;
if (vell) {
grid_vel = new float3[size()];
grid_vel[0] = make_float3(SUPER_NULL, SUPER_NULL, SUPER_NULL);
}
if (!cuda_velocity_initialized) {
cuda_velocity_initialized = true;
grid_vel_gpu = multiGPU_malloc3(deviceCount, deviceIndex, size());
}
}
bool cuda_velocity_initialized = false;
public:
GRID3D(int devicesCount = 1, int deviceIndex = 0) {
this->deviceCount = devicesCount;
cudaSetDevice(deviceIndex);
resolution.x = resolution.y = resolution.z = 1;
grid = new float[1];
grid[0] = 0.0;
grid_temp = new float[1];
grid_temp[0] = 0.0;
initNoiseGrid(deviceIndex);
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
}
GRID3D(int x, int y, int z, int deviceCount, int deviceIndex) {
this->deviceCount = deviceCount;
resolution.x = x;
resolution.y = y;
resolution.z = z;
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[(long long)x * (long long)y * (long long)z];
grid_temp = new float[(long long)x * (long long)y * (long long)z];
#endif
for (long long i = 0; i < size(); i++) {
grid[i] = 0.0;
grid_temp[i] = 0.0;
}
initNoiseGrid(deviceIndex);
}
GRID3D(int3 dim, int deviceCount, int deviceIndex) {
this->deviceCount = deviceCount;
resolution.x = dim.x;
resolution.y = dim.y;
resolution.z = dim.z;
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
for (long long i = 0; i < size(); i++) {
grid[i] = 0.0;
grid_temp[i] = 0.0;
}
initNoiseGrid(deviceIndex);
}
GRID3D(int elem, float* grid, int deviceCount, int deviceIndex) {
this->deviceCount = deviceCount;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&this->grid, elem * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, elem * sizeof(float), cudaHostAllocMapped));
#else
this->grid = new float[elem];
grid_temp = new float[elem];
#endif
for (int i = 0; i < elem; i++) {
this->grid[i] = grid[i];
this->grid_temp[i] = grid[i];
}
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
initNoiseGrid(deviceIndex);
}
GRID3D(int3 dim, float* grid_src, int deviceCount, int deviceIndex) {
this->deviceCount = deviceCount;
this->resolution = dim;
//grid = new float[(long long)dim.x * (long long)dim.y * (long long)dim.z];
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
//cudaMemcpyAsync(grid, grid_src, sizeof(float) * size(), cudaMemcpyDeviceToHost,0);
multiGPU_copy(deviceCount, grid, grid_src, size(), cudaMemcpyDeviceToHost, deviceIndex);
grid_temp = new float[1];
initNoiseGrid(deviceIndex);
}
void load_from_device(int3 dim, float* grid_src, int deviceIndex, bool debug = false) {
freeOnlyGrid(); //free
this->resolution = dim;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
checkCudaErrors(cudaMemcpyAsync(grid, grid_src, sizeof(float) * size(), cudaMemcpyDeviceToHost));
//multiGPU_copy(deviceCount, grid, grid_src, size(), cudaMemcpyDeviceToHost, deviceIndex);
//cudaCheckError();
}
#ifdef VELOCITY_NOISE
void load_from_device3D(int3 dim, float3* grid_src, int deviceIndex) {
if (this->resolution.x == dim.x && this->resolution.y == dim.y && this->resolution.z == dim.z) {
//printLn("Copying";
//checkCudaErrors(cudaMemcpyAsync(grid_vel, grid_src, SIZEOF_FLOAT3 * size(), cudaMemcpyDeviceToHost,0));
multiGPU_copy(deviceCount, grid_vel, grid_src, size(), cudaMemcpyDeviceToHost, deviceIndex);
}
else {
//printLn("Free data";
this->free_velocity();
this->resolution = dim;
//printLn("Allocating memory";
grid_vel = new float3[size()];
//printLn("Copying";
//checkCudaErrors(cudaMemcpyAsync(grid_vel, grid_src, SIZEOF_FLOAT3 * size(), cudaMemcpyDeviceToHost,0));
multiGPU_copy(deviceCount, grid_vel, grid_src, size(), cudaMemcpyDeviceToHost, deviceIndex);
}
//cudaCheckError();
//printLn("Copied from device" << std::endl;
}
#endif
GRID3D(int x, int y, int z, float* vdb, int deviceCount, int deviceIndex) {
resolution.x = x;
resolution.y = y;
resolution.z = z;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
for (long long i = 0; i < size(); i++) {
grid[i] = vdb[i];
grid_temp[i] = vdb[i];
}
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
initNoiseGrid(deviceIndex);
}
float operator()(int x, int y, int z) {
float output = 0.0;
long long iter = z * resolution.y * resolution.x + y * resolution.x + x;
if (iter <= size())
output = grid[iter];
else {
printLn("GRID READ ERROR:");
printLn("Max ID: " << size() << "\nGiven ID: " << iter);
}
return output;
}
float operator()(openvdb::Coord ijk) {
float output = 0.0;
long long iter = ijk[2] * resolution.y * resolution.x + ijk[1] * resolution.x + ijk[0];
if (iter <= size())
output = grid[iter];
else {
printLn("GRID READ ERROR:");
printLn("Max ID: " << size() << "\nGiven ID: " << iter);
}
return output;
}
float get(openvdb::Coord ijk) {
float output = 0.0;
long long iter = ijk[2] * resolution.y * resolution.x + ijk[1] * resolution.x + ijk[0];
if (iter <= size())
output = grid[iter];
else {
printLn("GRID READ ERROR:\n");
printLn("Max ID: " << size() << "\nGiven ID: " << iter);
}
return output;
}
float get(nanovdb::Coord ijk) {
float output = 0.0;
long long iter = ijk.z() * resolution.y * resolution.x + ijk.y() * resolution.x + ijk.x();
if (iter <= size())
output = grid[iter];
else {
printLn("GRID READ ERROR:");
printLn("Max ID: " << size() << "\nGiven ID: " << iter);
}
return output;
}
GRID3D operator=(const GRID3D& rhs) {
free();
resolution.x = rhs.resolution.x;
resolution.y = rhs.resolution.y;
resolution.z = rhs.resolution.z;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
for (long long i = 0; i < size(); i++) {
grid[i] = rhs.grid[i];
grid_temp[i] = rhs.grid_temp[i];
}
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
printLn("But in the end");
return *this;
}
GRID3D operator=(const GRID3D* rhs) {
free();
resolution.x = rhs->resolution.x;
resolution.y = rhs->resolution.y;
resolution.z = rhs->resolution.z;
grid = rhs->grid;
grid_temp = rhs->grid_temp;
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
//initNoiseGrid();
return *this;
}
void set_pointer(const GRID3D* rhs) {
free();
resolution.x = rhs->resolution.x;
resolution.y = rhs->resolution.y;
resolution.z = rhs->resolution.z;
grid = rhs->grid;
grid_temp = rhs->grid_temp;
//grid_noise = new float[1];
//initNoiseGrid();
}
GRID3D load(const GRID3D* rhs) {
free();
resolution.x = rhs->resolution.x;
resolution.y = rhs->resolution.y;
resolution.z = rhs->resolution.z;
#ifdef NEW_HOST_ALLOC
checkCudaErrors(cudaHostAlloc(&grid, size() * sizeof(float), cudaHostAllocMapped));
checkCudaErrors(cudaHostAlloc(&grid_temp, size() * sizeof(float), cudaHostAllocMapped));
#else
grid = new float[size()];
grid_temp = new float[size()];
#endif
for (long long i = 0; i < size(); i++) {
grid[i] = rhs->grid[i];
grid_temp[i] = rhs->grid_temp[i];
}
//grid_noise = new float[1];
//grid_noise[0] = 0.0;
//initNoiseGrid();
return *this;
}
void combine_with_temp_grid(const GRID3D* rhs) {
deletep(grid_temp);
grid_temp = rhs->grid;
}
void normalizeData() {
for (int i = 0; i < size(); i++) {
if (grid[i] < 0.01)
grid[i] = 0.0f;
grid[i] = fmin(grid[i], 1.0);
grid[i] = fmax(grid[i], 0.0);
}
}
void addNoise() {
float ratio = 10000.0 / 300.0;
ratio = ratio * 0.5;
for (int i = 0; i < size(); i++) {
if (grid_temp[i] > 0.0) {
float random = (float)(rand() % 10000) / 300.0;
random -= ratio;
grid_temp[i] += random;
grid_temp[i] = max(0.5f, grid_temp[i]);
}
}
}
void copyToDevice(int deviceIndex, bool addNoise = true) {
if (addNoise)
this->addNoise();
//checkCudaErrors(cudaMalloc((void**)&vdb_temp, sizeof(float) * size()));
printLn("Malloc");
vdb_temp = multiGPU_malloc<float>(deviceCount, deviceIndex, size());
//checkCudaErrors(cudaMemcpyAsync(vdb_temp, grid_temp, sizeof(float) * size(), cudaMemcpyHostToDevice,0));
printLn("Copy");
multiGPU_copyHTD(deviceCount, &vdb_temp, grid_temp, size(), deviceIndex);
normalizeData();
//copy to device
//checkCudaErrors(cudaMalloc((void**)&vdb, sizeof(float) * size()));
vdb = multiGPU_malloc<float>(deviceCount, deviceIndex, size());
//checkCudaErrors(cudaMemcpyAsync(vdb, grid, sizeof(float) * size(), cudaMemcpyHostToDevice,0));
multiGPU_copyHTD(deviceCount, &vdb, grid, size(), deviceIndex);
//cudaCheckError();
}
#ifdef VELOCITY_NOISE
void copyToDeviceVel(int deviceIndex) {
//checkCudaErrors(cudaMalloc((void**)&grid_vel_gpu, SIZEOF_FLOAT3 * size()));
grid_vel_gpu = multiGPU_malloc3(deviceCount, deviceIndex, size());
//checkCudaErrors(cudaMemcpyAsync(grid_vel_gpu, grid_vel, SIZEOF_FLOAT3 * size(), cudaMemcpyHostToDevice,0));
multiGPU_copy(deviceCount, grid_vel_gpu, grid_vel, size(), cudaMemcpyHostToDevice, deviceIndex);
//cudaCheckError();
}
#endif
void copyToDeviceNoise(int NTS, int deviceIndex, int deviceCount = 1) {
//checkCudaErrors(cudaMalloc((void**)&vdb_noise, sizeof(float) * NTS * NTS * NTS));
printLn("Malloc << " << NTS);
vdb_noise = multiGPU_malloc<float>(deviceCount, deviceIndex, NTS * NTS * NTS);
//this->generateTile(NTS);
//checkCudaErrors(cudaMemcpyAsync(vdb_noise, grid_noise, sizeof(float) * NTS * NTS * NTS, cudaMemcpyHostToDevice,0));
printLn("Copy(" << deviceCount << ")");
multiGPU_copyHTD(deviceCount, &vdb_noise, grid_noise, NTS * NTS * NTS, deviceIndex);
printLn("Endl");
//cudaCheckError();
}
void set(int x, int y, int z, float value) {
grid[z * resolution.y * resolution.x + y * resolution.x + x] = value;
}
void set_temp(int x, int y, int z, float value) {
grid_temp[z * resolution.y * resolution.x + y * resolution.x + x] = value;
}
int3 get_resolution() {
return resolution;
}
int get_max_resolution() {
return max(max(resolution.x, resolution.y), resolution.z);
}
void freeOnlyGrid() {
deletep(grid);
}
void free() {
//printLn("Free grid memory" << std::endl;
deletep(grid);
deletep(grid_temp);
//deletep(grid_noise);
}
#ifdef VELOCITY_NOISE
void free_velocity() {
deletec(grid_vel);
}
#endif
void free_noise() {
deletep(grid_noise);
grid_noise = new float[0];
grid_noise[0] = SUPER_NULL;
}
void freeCuda() {
multiGPU_free(deviceCount, vdb);
multiGPU_free(deviceCount, vdb_temp);
}
void freeCuda1() {
multiGPU_free(deviceCount, vdb);
}
void freeNoise() {
multiGPU_free(deviceCount, vdb_noise);
}
#ifdef VELOCITY_NOISE
void freeCudaVel() {
multiGPU_free(deviceCount, grid_vel_gpu);
this->cuda_velocity_initialized = false;
}
#endif
~GRID3D() {
//free();
}
float* get_grid() const {
return this->grid;
}
float* get_grid_temp() const {
return this->grid_temp;
}
std::vector<float*>* get_grid_device() {
return &this->vdb;
}
std::vector<float*>* get_grid_device_temp() {
return &this->vdb_temp;
}
std::vector<float*>* get_grid_device_noise() {
return &this->vdb_noise;
}
#ifdef VELOCITY_NOISE
std::vector<float3*>* get_grid_device_vel() {
return &this->grid_vel_gpu;
}
#endif
void UpScale(int power, int SEED = 2, int frame = 0, float offset = 0.5, float scale = 0.1, int noise_scale = 128,
int apply_method = 0, float intensity = 1, float time_anim = 0.1) {
int noise_tile_size = power * min(min(resolution.x, resolution.y) //max max
, resolution.z);
noise_tile_size = noise_scale;
srand(SEED);
if (this->grid_noise[0] < -100)
generateTile(noise_tile_size);
if (apply_method == 0)
applyNoise(intensity, noise_tile_size, offset, scale, frame, time_anim);
else if (apply_method == 1)
applyNoise2(intensity, noise_tile_size, offset, scale, frame, time_anim);
else if (apply_method == 2)
applyCurl(intensity, noise_tile_size, offset, scale, frame, time_anim);
}
void LoadNoise(GRID3D* rhs) {
this->grid_noise = rhs->grid_noise;
}
#ifdef VELOCITY_NOISE
void LoadVelocity(GRID3D* rhs) {
this->grid_vel = rhs->grid_vel;
}
#endif
bool is_noise_grid() {
if (this->grid_noise[0] < -100)
return false;
else
return true;
}
float get_noise_status() {
return this->grid_noise[0];
}
inline float evaluate(float3 pos, int tile, int3 resolution, int NTS = 0, float offset = 0.5, float scale = 0.1,
float time_anim = 0.1)
{
pos.x *= resolution.x;
pos.y *= resolution.y;
pos.z *= resolution.z;
pos.x += 1; pos.y += 1; pos.z += 1;
// time anim
pos.x += time_anim; pos.y += time_anim; pos.z += time_anim;
pos.x *= scale;
pos.y *= scale;
pos.z *= scale;
const int n3 = NTS * NTS * NTS;
float v = WNoiseDx(pos, &this->grid_noise[int(tile * NTS) % n3], NTS);
//float v = WNoise(pos, &this->grid_noise[int(tile * n3 * 0.01) % n3], NOISE_TILE_SIZE);
v += offset;//offset //0.5
//v *= scale;//scale //0.1
return v;
}
void applyNoise(float intensity = 0.2f, int NTS = 0, float offset = 0.5, float scale = 0.1, int frame = 0,
float time_anim = 0.1) {
if (NTS == 0)
NTS = min(min(resolution.x, resolution.y), resolution.z);
int NTS2 = NTS * NTS;
int NTS3 = NTS2 * NTS;
//printLn("Applying noise" << std::endl;
//offset *= 1.4; //1.2
float tempp = 0.0;
int THREADS = 16;
int sizee = ceil((double)resolution.x / (double)THREADS);
tbb::parallel_for(0, THREADS, [&](int i) {
int end = (i * sizee) + (sizee);
if (end > resolution.x) {
end = resolution.x;
}
for (int x = i * sizee; x < end; x++)
for (int y = 0; y < resolution.y; y++)
for (int z = 0; z < resolution.z; z++) {
float* position = &this->grid[z * resolution.x * resolution.y +
y * resolution.x + x];
//if (*position >= 0.01) {
if (*position != 0.0) {
*position += evaluate(make_float3(x, y, z), frame % 512, resolution, NTS, offset, scale, time_anim) * intensity * fmax(0.05, fmin((*position), 1.0));
}
}
});
}
void applyNoise2(float intensity = 0.2f, int NTS = 0, float offset = 0.5, float scale = 0.1, int frame = 0,
float time_anim = 0.1) {
if (NTS == 0)
NTS = min(min(resolution.x, resolution.y), resolution.z);
int NTS2 = NTS * NTS;
int NTS3 = NTS2 * NTS;
//printLn("Applying noise" << std::endl;
float tempp = 0.0;
int THREADS = 16;
int sizee = ceil((double)resolution.x / (double)THREADS);
//weights
//float* weights = new float[size()];
//computeCoeff(grid_noise,NTS);
//printLn("Computed" << std::endl;