-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpu_optimize_storm_drift.cu
831 lines (624 loc) · 27.9 KB
/
gpu_optimize_storm_drift.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
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <algorithm>
#include <cuda_runtime.h>
//#include <nvtoolsext.h>
#include "gpu_optimize_storm_drift.h"
/* Define global variables */
int static_n_coordinates;
int static_n_timepoints;
size_t static_n_coordinate_pairs;
__device__ float * static_d_coords_x;
__device__ float * static_d_coords_y;
__device__ float * static_d_coords_z;
__device__ int * static_d_coords_time;
__device__ int * static_d_pair_indices_i;
__device__ int * static_d_pair_indices_j;
__global__ void calculate_osd_cost_function_2d(
int const n_coordinates,
int const n_timepoints,
size_t const n_coordinate_pairs,
size_t const process_start_index,
float gaussian_scale,
float * d_drift_trajectory,
float * d_wa_function_values,
int flag_calculate_derivatives,
float * d_derivatives)
{
int const n_threads_per_block = blockDim.x;
int const block_index = blockIdx.x;
int const thread_index = threadIdx.x;
int const proc_id = process_start_index + (block_index * n_threads_per_block) + thread_index;
// is this a valid process ID?
bool const process_valid = (proc_id < n_coordinate_pairs);
int const src_index = (process_valid) ? proc_id : 0;
int const coord_index_i = static_d_pair_indices_i[src_index];
int const coord_index_j = static_d_pair_indices_j[src_index];
int const coord_t_i = static_d_coords_time[coord_index_i];
float const coord_x_i = static_d_coords_x[coord_index_i] + d_drift_trajectory[coord_t_i];
float const coord_y_i = static_d_coords_y[coord_index_i] + d_drift_trajectory[coord_t_i + n_timepoints];
int const coord_t_j = static_d_coords_time[coord_index_j];
float const coord_x_j = static_d_coords_x[coord_index_j] + d_drift_trajectory[coord_t_j];
float const coord_y_j = static_d_coords_y[coord_index_j] + d_drift_trajectory[coord_t_j + n_timepoints];
float const delta_x = coord_x_i - coord_x_j;
float const delta_y = coord_y_i - coord_y_j;
float const dist_sq = delta_x * delta_x + delta_y * delta_y;
float cost_fn_value = exp(-(dist_sq) / gaussian_scale);
// if this is a valid process, store the results
if (process_valid)
{
// store the cost function
d_wa_function_values[proc_id - process_start_index] = -cost_fn_value;
// store the derivatives
float old_value;
if (flag_calculate_derivatives == 1)
{
if (coord_t_i != coord_t_j)
{
cost_fn_value = cost_fn_value * (2.0 / gaussian_scale);
old_value = atomicAdd(d_derivatives + coord_t_i, (cost_fn_value * delta_x));
old_value = atomicAdd(d_derivatives + coord_t_j, -(cost_fn_value * delta_x));
old_value = atomicAdd(d_derivatives + coord_t_i + n_timepoints, (cost_fn_value * delta_y));
old_value = atomicAdd(d_derivatives + coord_t_j + n_timepoints, -(cost_fn_value * delta_y));
}
}
}
__syncthreads();
}
__global__ void calculate_osd_cost_function_3d(
int const n_coordinates,
int const n_timepoints,
size_t const n_coordinate_pairs,
size_t const process_start_index,
float gaussian_scale,
float * d_drift_trajectory,
float * d_wa_function_values,
int flag_calculate_derivatives,
float * d_derivatives)
{
int const n_threads_per_block = blockDim.x;
int const block_index = blockIdx.x;
int const thread_index = threadIdx.x;
int const proc_id = process_start_index + (block_index * n_threads_per_block) + thread_index;
// is this a valid process ID?
bool const process_valid = (proc_id < n_coordinate_pairs);
int const src_index = (process_valid) ? proc_id : 0;
int const coord_index_i = static_d_pair_indices_i[src_index];
int const coord_index_j = static_d_pair_indices_j[src_index];
int const coord_t_i = static_d_coords_time[coord_index_i];
float const coord_x_i = static_d_coords_x[coord_index_i] + d_drift_trajectory[coord_t_i];
float const coord_y_i = static_d_coords_y[coord_index_i] + d_drift_trajectory[coord_t_i + n_timepoints];
float const coord_z_i = static_d_coords_z[coord_index_i] + d_drift_trajectory[coord_t_i + 2 * n_timepoints];
int const coord_t_j = static_d_coords_time[coord_index_j];
float const coord_x_j = static_d_coords_x[coord_index_j] + d_drift_trajectory[coord_t_j];
float const coord_y_j = static_d_coords_y[coord_index_j] + d_drift_trajectory[coord_t_j + n_timepoints];
float const coord_z_j = static_d_coords_z[coord_index_j] + d_drift_trajectory[coord_t_j + 2 * n_timepoints];
float const delta_x = coord_x_i - coord_x_j;
float const delta_y = coord_y_i - coord_y_j;
float const delta_z = coord_z_i - coord_z_j;
float const dist_sq = delta_x * delta_x + delta_y * delta_y + delta_z * delta_z;
float cost_fn_value = exp(-(dist_sq)/gaussian_scale);
// if this is a valid process, store the results
if (process_valid)
{
// store the cost function
d_wa_function_values[proc_id - process_start_index] = -cost_fn_value;
// store the derivatives
float old_value;
if (flag_calculate_derivatives == 1)
{
if (coord_t_i != coord_t_j)
{
cost_fn_value = cost_fn_value * (2.0 / gaussian_scale);
old_value = atomicAdd(d_derivatives + coord_t_i, (cost_fn_value * delta_x));
old_value = atomicAdd(d_derivatives + coord_t_j, -(cost_fn_value * delta_x));
old_value = atomicAdd(d_derivatives + coord_t_i + n_timepoints, (cost_fn_value * delta_y));
old_value = atomicAdd(d_derivatives + coord_t_j + n_timepoints, -(cost_fn_value * delta_y));
old_value = atomicAdd(d_derivatives + coord_t_i + (2 * n_timepoints), (cost_fn_value * delta_z));
old_value = atomicAdd(d_derivatives + coord_t_j + (2 * n_timepoints), -(cost_fn_value * delta_z));
}
}
}
__syncthreads();
}
int gpu_opt_storm_drift_compute_2d(
int n_coordinates,
int n_timepoints,
size_t n_coordinate_pairs,
float gaussian_scale,
float * drift_trajectory,
float * output_cost_function,
int flag_calculate_derivatives,
float * output_derivatives)
{
cudaError_t cuda_status;
//nvtxMarkA("Start of gpu_opt_storm_drift_compute");
if (n_coordinates != static_n_coordinates)
{
throw std::runtime_error("Invalid number of coordinates");
}
if (n_timepoints != static_n_timepoints)
{
throw std::runtime_error("Invalid number of time points");
}
if (n_coordinate_pairs != static_n_coordinate_pairs)
{
throw std::runtime_error("Invalid number of coordinate pairs");
}
// Copy the drift trajectory to the GPU
float * d_drift_trajectory{ nullptr };
cuda_status = cudaMalloc(&d_drift_trajectory, 2 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_drift_trajectory, drift_trajectory, 2 * n_timepoints * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// Initialize an array in which to store the derivatives (this will always be a float array)
float * d_derivatives{ nullptr };
if (flag_calculate_derivatives == 1)
{
cuda_status = cudaMalloc(&d_derivatives, 2 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemset(d_derivatives, 0, 2 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
}
// Initialize the cost function value (stored internally as double precision)
double tmp_cost_function = 0.0;
// Divide the work into chunks
int const proc_chunk_size = 16777216;
int const n_chunks = (int)std::ceil((double)n_coordinate_pairs / (double)proc_chunk_size);
// allocate thrust device vectors as working arrays
thrust::device_vector<float> dev_vec_wa_function_values(proc_chunk_size);
thrust::device_vector<double> dev_vec_wa_function_values_dbl(proc_chunk_size);
float * d_wa_function_values = thrust::raw_pointer_cast(dev_vec_wa_function_values.data());
for (int i = 0; i < n_chunks; i++)
{
size_t start_index = i*((size_t)proc_chunk_size);
size_t end_index = std::min(start_index + proc_chunk_size - 1, n_coordinate_pairs - 1);
int cur_chunk_n_pairs = (int)(end_index - start_index + 1);
// clear the working array
thrust::fill(dev_vec_wa_function_values.begin(), dev_vec_wa_function_values.end(), 0.0);
// initialize the number of blocks and threads
int const n_threads_per_block = 128;
int const n_blocks = (int)std::ceil(double(cur_chunk_n_pairs) / double(n_threads_per_block));
// calculate the gaussian contribution from each process
calculate_osd_cost_function_2d << < n_blocks, n_threads_per_block >> > (
n_coordinates,
n_timepoints,
n_coordinate_pairs,
start_index,
gaussian_scale,
d_drift_trajectory,
d_wa_function_values,
flag_calculate_derivatives,
d_derivatives);
// copy the working array to a double precision array and sum as double precision
thrust::copy(dev_vec_wa_function_values.begin(), dev_vec_wa_function_values.end(), dev_vec_wa_function_values_dbl.begin());
// sum the cost function values in the double precision array and add the result to the accumulated cost function
tmp_cost_function += thrust::reduce(dev_vec_wa_function_values_dbl.begin(), dev_vec_wa_function_values_dbl.end(), 0.0, thrust::plus<double>());
}
*output_cost_function = (float) tmp_cost_function;
// copy the derivatives to host memory
if (flag_calculate_derivatives == 1)
{
cuda_status = cudaMemcpy(output_derivatives, d_derivatives, 2 * n_timepoints * sizeof(float), cudaMemcpyDeviceToHost);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaFree(d_derivatives);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
}
cuda_status = cudaFree(d_drift_trajectory);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// deallocate memory associated with the device vectors
dev_vec_wa_function_values.clear();
dev_vec_wa_function_values.shrink_to_fit();
dev_vec_wa_function_values_dbl.clear();
dev_vec_wa_function_values_dbl.shrink_to_fit();
return 0;
}
int gpu_opt_storm_drift_compute_3d(
int n_coordinates,
int n_timepoints,
size_t n_coordinate_pairs,
float gaussian_scale,
float * drift_trajectory,
float * output_cost_function,
int flag_calculate_derivatives,
float * output_derivatives)
{
cudaError_t cuda_status;
//nvtxMarkA("Start of gpu_opt_storm_drift_compute");
if (n_coordinates != static_n_coordinates)
{
throw std::runtime_error("Invalid number of coordinates");
}
if (n_timepoints != static_n_timepoints)
{
throw std::runtime_error("Invalid number of time points");
}
if (n_coordinate_pairs != static_n_coordinate_pairs)
{
throw std::runtime_error("Invalid number of coordinate pairs");
}
// copy the drift trajectory to the GPU
float * d_drift_trajectory{ nullptr };
cuda_status = cudaMalloc(&d_drift_trajectory, 3 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_drift_trajectory, drift_trajectory, 3 * n_timepoints * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// initialize an array in which to store the derivatives
float * d_derivatives{ nullptr };
if (flag_calculate_derivatives == 1)
{
cuda_status = cudaMalloc(&d_derivatives, 3 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemset(d_derivatives, 0, 3 * n_timepoints * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
}
// initialize the cost function value
double tmp_cost_function = 0.0;
// divide the work into chunks
int const proc_chunk_size = 16777216;
int const n_chunks = (int)std::ceil((double)n_coordinate_pairs / (double)proc_chunk_size);
// allocate thrust device vectors as working arrays
thrust::device_vector<float> dev_vec_wa_function_values(proc_chunk_size);
thrust::device_vector<double> dev_vec_wa_function_values_dbl(proc_chunk_size);
float * d_wa_function_values = thrust::raw_pointer_cast(dev_vec_wa_function_values.data());
for (int i = 0; i < n_chunks; i++)
{
size_t start_index = i*((size_t)proc_chunk_size);
size_t end_index = std::min(start_index + proc_chunk_size - 1, n_coordinate_pairs - 1);
int cur_chunk_n_pairs = (int)(end_index - start_index + 1);
// clear the working array
thrust::fill(dev_vec_wa_function_values.begin(), dev_vec_wa_function_values.end(), 0.0);
// initialize the number of blocks and threads
int const n_threads_per_block = 128;
int const n_blocks = (int)std::ceil(double(cur_chunk_n_pairs) / double(n_threads_per_block));
//calculate the gaussian contribution from each process
calculate_osd_cost_function_3d <<< n_blocks, n_threads_per_block >>> (
n_coordinates,
n_timepoints,
n_coordinate_pairs,
start_index,
gaussian_scale,
d_drift_trajectory,
d_wa_function_values,
flag_calculate_derivatives,
d_derivatives);
// copy the working array to a double precision array and sum as double precision
thrust::copy(dev_vec_wa_function_values.begin(), dev_vec_wa_function_values.end(), dev_vec_wa_function_values_dbl.begin());
// sum the cost function values in the double precision array and add the result to the accumulated cost function
tmp_cost_function += thrust::reduce(dev_vec_wa_function_values_dbl.begin(), dev_vec_wa_function_values_dbl.end(), 0.0, thrust::plus<double>());
}
*output_cost_function = (float) tmp_cost_function;
if (flag_calculate_derivatives == 1)
{
// copy the derivatives to host memory
cuda_status = cudaMemcpy(output_derivatives, d_derivatives, (size_t)3 * n_timepoints * sizeof(float), cudaMemcpyDeviceToHost);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaFree(d_derivatives);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
}
cuda_status = cudaFree(d_drift_trajectory);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// deallocate memory associated with the device vectors
dev_vec_wa_function_values.clear();
dev_vec_wa_function_values.shrink_to_fit();
dev_vec_wa_function_values_dbl.clear();
dev_vec_wa_function_values_dbl.shrink_to_fit();
return 0;
}
int gpu_opt_storm_drift_initialize_2d(
int n_coordinates,
int n_timepoints,
float * coordinates_x,
float * coordinates_y,
int * coordinates_time,
size_t n_coordinate_pairs,
int * pair_indices_i,
int * pair_indices_j)
{
//nvtxMarkA("Start of gpu_opt_storm_drift_initialize");
cudaError_t cuda_status;
// allocate space for storate arrays
float * d_coordinates_x{ nullptr };
float * d_coordinates_y{ nullptr };
int * d_coordinates_time{ nullptr };
int * d_pair_indices_i{ nullptr };
int * d_pair_indices_j{ nullptr };
cuda_status = cudaMalloc(&d_coordinates_x, n_coordinates * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_coordinates_y, n_coordinates * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_coordinates_time, n_coordinates * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_pair_indices_i, n_coordinate_pairs * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_pair_indices_j, n_coordinate_pairs * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// copy the data to the GPU
cuda_status = cudaMemcpy(d_coordinates_x, coordinates_x, n_coordinates * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_coordinates_y, coordinates_y, n_coordinates * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_coordinates_time, coordinates_time, n_coordinates * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_pair_indices_i, pair_indices_i, n_coordinate_pairs * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_pair_indices_j, pair_indices_j, n_coordinate_pairs * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// store the device pointer addresses in global variables on the device
cuda_status = cudaMemcpyToSymbol(static_d_coords_x, &d_coordinates_x, sizeof(d_coordinates_x), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_coords_y, &d_coordinates_y, sizeof(d_coordinates_y), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_coords_time, &d_coordinates_time, sizeof(d_coordinates_time), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_pair_indices_i, &d_pair_indices_i, sizeof(d_pair_indices_i), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_pair_indices_j, &d_pair_indices_j, sizeof(d_pair_indices_j), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// store global scalar variables
static_n_coordinates = n_coordinates;
static_n_timepoints = n_timepoints;
static_n_coordinate_pairs = n_coordinate_pairs;
//nvtxMarkA("End of gpu_opt_storm_drift_initialize");
return 0;
}
int gpu_opt_storm_drift_initialize_3d(
int n_coordinates,
int n_timepoints,
float * coordinates_x,
float * coordinates_y,
float * coordinates_z,
int * coordinates_time,
size_t n_coordinate_pairs,
int * pair_indices_i,
int * pair_indices_j)
{
//nvtxMarkA("Start of gpu_opt_storm_drift_initialize");
cudaError_t cuda_status;
// allocate space for storate arrays
float * d_coordinates_x{ nullptr };
float * d_coordinates_y{ nullptr };
float * d_coordinates_z{ nullptr };
int * d_coordinates_time{ nullptr };
int * d_pair_indices_i{ nullptr };
int * d_pair_indices_j{ nullptr };
cuda_status = cudaMalloc(&d_coordinates_x, n_coordinates * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_coordinates_y, n_coordinates * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_coordinates_z, n_coordinates * sizeof(float));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_coordinates_time, n_coordinates * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_pair_indices_i, n_coordinate_pairs * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMalloc(&d_pair_indices_j, n_coordinate_pairs * sizeof(int));
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// copy the data to the GPU
cuda_status = cudaMemcpy(d_coordinates_x, coordinates_x, n_coordinates * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_coordinates_y, coordinates_y, n_coordinates * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_coordinates_z, coordinates_z, n_coordinates * sizeof(float), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_coordinates_time, coordinates_time, n_coordinates * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_pair_indices_i, pair_indices_i, n_coordinate_pairs * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpy(d_pair_indices_j, pair_indices_j, n_coordinate_pairs * sizeof(int), cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// store the device pointer addresses in global variables on the device
cuda_status = cudaMemcpyToSymbol(static_d_coords_x, &d_coordinates_x, sizeof(d_coordinates_x), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_coords_y, &d_coordinates_y, sizeof(d_coordinates_y), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_coords_z, &d_coordinates_z, sizeof(d_coordinates_z), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_coords_time, &d_coordinates_time, sizeof(d_coordinates_time), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_pair_indices_i, &d_pair_indices_i, sizeof(d_pair_indices_i), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
cuda_status = cudaMemcpyToSymbol(static_d_pair_indices_j, &d_pair_indices_j, sizeof(d_pair_indices_j), 0, cudaMemcpyHostToDevice);
if (cuda_status != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(cuda_status));
}
// store global variables
static_n_coordinates = n_coordinates;
static_n_timepoints = n_timepoints;
static_n_coordinate_pairs = n_coordinate_pairs;
//nvtxMarkA("End of gpu_opt_storm_drift_initialize");
return 0;
}
int gpu_opt_storm_drift_free_2d()
{
cudaError_t cuda_status;
// free device memory
static_n_coordinates = 0;
static_n_timepoints = 0;
static_n_coordinate_pairs = 0;
float * d_coordinates_x{ nullptr };
float * d_coordinates_y{ nullptr };
int * d_coordinates_time{ nullptr };
int * d_pair_indices_i{ nullptr };
int * d_pair_indices_j{ nullptr };
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_x, static_d_coords_x, sizeof(d_coordinates_x), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_y, static_d_coords_y, sizeof(d_coordinates_y), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_time, static_d_coords_time, sizeof(d_coordinates_time), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_pair_indices_i, static_d_pair_indices_i, sizeof(d_pair_indices_i), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_pair_indices_j, static_d_pair_indices_j, sizeof(d_pair_indices_j), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaFree(d_coordinates_x);
cuda_status = cudaFree(d_coordinates_y);
cuda_status = cudaFree(d_coordinates_time);
cuda_status = cudaFree(d_pair_indices_i);
cuda_status = cudaFree(d_pair_indices_j);
//cuda_status = cudaDeviceReset();
return 0;
}
int gpu_opt_storm_drift_free_3d()
{
cudaError_t cuda_status;
// free device memory
static_n_coordinates = 0;
static_n_timepoints = 0;
static_n_coordinate_pairs = 0;
float * d_coordinates_x{ nullptr };
float * d_coordinates_y{ nullptr };
float * d_coordinates_z{ nullptr };
int * d_coordinates_time{ nullptr };
int * d_pair_indices_i{ nullptr };
int * d_pair_indices_j{ nullptr };
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_x, static_d_coords_x, sizeof(d_coordinates_x), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_y, static_d_coords_y, sizeof(d_coordinates_y), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_z, static_d_coords_z, sizeof(d_coordinates_z), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_coordinates_time, static_d_coords_time, sizeof(d_coordinates_time), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_pair_indices_i, static_d_pair_indices_i, sizeof(d_pair_indices_i), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaMemcpyFromSymbol(&d_pair_indices_j, static_d_pair_indices_j, sizeof(d_pair_indices_j), 0, cudaMemcpyDeviceToHost);
cuda_status = cudaFree(d_coordinates_x);
cuda_status = cudaFree(d_coordinates_y);
cuda_status = cudaFree(d_coordinates_z);
cuda_status = cudaFree(d_coordinates_time);
cuda_status = cudaFree(d_pair_indices_i);
cuda_status = cudaFree(d_pair_indices_j);
//cuda_status = cudaDeviceReset();
return 0;
}