forked from weft/warp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
whistory.cpp
2845 lines (2419 loc) · 105 KB
/
whistory.cpp
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
#include <vector>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <cmath>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <cuda.h>
#include <curand.h>
#include <cudpp.h>
#include <Python.h>
#include <png++/png.hpp>
#include "datadef.h"
#include "wprimitive.h"
#include "wgeometry.h"
#include "optix_stuff.h"
#include "warp_cuda.h"
#include "whistory.h"
#include "check_cuda.h"
// instantiate single, global optix object
optix_stuff optix_obj;
// whistory class
whistory::~whistory(){
//clear device
check_cuda(cudaDeviceReset());
}
void whistory::print_banner(){
using namespace std;
cout << "\n";
cout << "\e[1;32m" << "-- Weaving All the Random Particles ---" << "\e[m \n";
cout << "\e[1;31m";
cout << "___ ________________________ " << "\n";
cout << "__ | / /__ |__ __ \\__ __ \\" << "\n";
cout << "__ | /| / /__ /| |_ /_/ /_ /_/ /" << "\n";
cout << "__ |/ |/ / _ ___ | _, _/_ ____/ " << "\n";
cout << "____/|__/ /_/ |_/_/ |_| /_/ " << "\n";
cout << "\e[m \n";
cout << "\e[1;32m" << "Monte Carlo Neutron Transport at WARP speed!" << "\e[m \n";
cout << "\n";
}
whistory::whistory(unsigned Nin, wgeometry problem_geom_in){
// do problem gemetry stuff first
problem_geom = problem_geom_in;
// set run flag
RUN_FLAG = 1;
// keff stuff
keff_sum = 0.0;
keff2_sum = 0.0;
keff_err = 0.0;
// device data stuff
N = Nin;
// 3 should be more than enough for criticality, might not be for fixed
Ndataset = Nin * 1.5;
reduced_yields_total = 0;
reduced_weight_total = 0;
initial_weight_total = 0;
accel_type = "Sbvh";
// default device to 0
compute_device = 0;
// not initialized
is_initialized = 0;
// prints
print_flag = 1;
dump_flag = 0;
}
void whistory::init(){
//
// WELCOME!
//
print_banner();
//
// EARLY DEVICE SETUP
//
// device must be set BEFORE an CUDA creation (streams included)
check_cuda(cudaSetDevice(compute_device));
//clear device
check_cuda(cudaDeviceReset());
// create streams
for (int i = 0; i < 4; ++i){
check_cuda(cudaStreamCreate(&stream[i]));
}
//check_cuda(cudaDeviceSetCacheConfig(cudaFuncCachePreferL1));
//
// OPTIX
//
optix_obj.N=Ndataset;
optix_obj.stack_size_multiplier=1;
optix_obj.init(problem_geom,compute_device,accel_type);
if(print_flag >= 1){
optix_obj.print();
}
check_cuda(cudaPeekAtLastError());
//
// CUDA
//
// set device, need to set again after optix sometimes...
check_cuda(cudaSetDevice(compute_device));
if(print_flag >= 1){
std::cout << "\e[1;32m" << "Dataset size is "<< N << "\e[m \n";
}
// block/thread structure
NUM_THREADS = 256;
blks = ( N + NUM_THREADS - 1 ) / NUM_THREADS;
if(print_flag >= 1){
std::cout << "\e[1;32m" << "Compute device set to "<< compute_device << "\e[m \n";
}
if(print_flag >= 2){
device_report();
}
// set print buffer size
check_cuda(cudaDeviceSetLimit(cudaLimitPrintfFifoSize, (size_t) 10*1048576 ));
// fission points array
fiss_img = new long unsigned [300*300]; for(int i=0;i<300*300;i++){fiss_img[i]=0;}
// init counters to 0
total_bytes_scatter = 0;
total_bytes_energy = 0;
//copy any info needed from the geometry read
memcpy(outer_cell_dims,optix_obj.outer_cell_dims,6*sizeof(float));
outer_cell = optix_obj.get_outer_cell();
outer_cell_type = optix_obj.get_outer_cell_type();
isotopes = problem_geom.isotopes;
n_isotopes = problem_geom.n_isotopes;
n_materials = problem_geom.n_materials;
n_tallies = problem_geom.n_tallies;
// map edge array and reduced values
n_edges = 11;
check_cuda(cudaHostAlloc( &edges , n_edges*sizeof(unsigned), cudaHostAllocMapped));
check_cuda(cudaHostAlloc( &reduced_yields , 1*sizeof(unsigned), cudaHostAllocMapped));
check_cuda(cudaHostAlloc( &reduced_weight , 1*sizeof(float) , cudaHostAllocMapped));
check_cuda(cudaHostGetDevicePointer(&d_edges , edges , 0));
check_cuda(cudaHostGetDevicePointer(&d_reduced_yields , reduced_yields , 0));
check_cuda(cudaHostGetDevicePointer(&d_reduced_weight , reduced_weight , 0));
// set anything else
filename = "warp";
//
// init/copy the rest not done previously or in OptiX
//
init_cross_sections();
init_host();
init_device();
init_RNG();
init_CUDPP();
// launch a test kernel...
//test_function( NUM_THREADS, N, d_xsdata, d_particles, d_tally, d_remap);
//check_cuda(cudaPeekAtLastError());
// get total memory usage and print if asked
check_cuda(cudaMemGetInfo(&mem_free, &mem_total));
// set inititalized flag, print done if flagged
is_initialized = 1;
if(print_flag >= 2){
printf("\e[1;31mDone with init\e[m\n");
printf("\e[1;32m--- Total Memory Usage ---\e[m\n");
printf(" Total: %8.2f MB\n", (mem_total)/(1024.0*1024.0));
printf(" Used: %8.2f MB\n",(mem_total-mem_free)/(1024.0*1024.0));
printf(" Free: %8.2f MB\n", (mem_free)/(1024.0*1024.0));
}
}
void whistory::init_host(){
// init tally arrays
dh_tally = new tally_data[ n_tallies];
h_tally = new tally_data_host[n_tallies];
// compute minimum size, in case dataset is shorter than tally, etc...
unsigned minimum_size = Ndataset * sizeof(spatial_data)/sizeof(float);
for( int i=0 ; i<n_tallies ; i++ ){
if (h_tally[i].length > minimum_size){
minimum_size = h_tally[i].length;
}
}
// allocate
h_particles.space = new spatial_data [Ndataset];
h_particles.E = new float [Ndataset];
h_particles.Q = new float [Ndataset];
h_particles.rn_bank = new unsigned [Ndataset];
h_particles.index = new unsigned [Ndataset];
h_particles.cellnum = new unsigned [Ndataset];
h_particles.matnum = new unsigned [Ndataset];
h_particles.rxn = new unsigned [Ndataset];
h_particles.isonum = new unsigned [Ndataset];
h_particles.talnum = new int [Ndataset];
h_particles.yield = new unsigned [Ndataset];
h_particles.weight = new float [Ndataset];
remap = new unsigned [Ndataset];
zeros = new unsigned [minimum_size];
ones = new unsigned [minimum_size];
fones = new float [minimum_size];
mones = new int [minimum_size];
// init dataset lengths
for(int k=0;k<Ndataset;k++){
h_particles.space[k].x = 0.0;
h_particles.space[k].y = 0.0;
h_particles.space[k].z = 0.0;
h_particles.space[k].xhat = 0.0;
h_particles.space[k].yhat = 0.0;
h_particles.space[k].zhat = 0.0;
h_particles.space[k].surf_dist = 10000.0;
h_particles.space[k].enforce_BC = 0;
h_particles.space[k].norm[0] = 1;
h_particles.space[k].norm[1] = 0;
h_particles.space[k].norm[2] = 0;
h_particles.E[k] = 2.5;
h_particles.Q[k] = 0.0;
h_particles.cellnum[k] = 0;
h_particles.talnum[k] = -1;
h_particles.matnum[k] = 0;
h_particles.rxn[k] = 0;
h_particles.isonum[k] = 0;
h_particles.yield[k] = 0;
h_particles.weight[k] = 1;
remap[k] = k;
}
// init minimum lengths
for(int k=0;k<minimum_size;k++){
zeros[k] = 0;
ones[k] = 1;
fones[k] = 1.0;
mones[k] = -1;
}
// set host tally size, bounds, allocate, and zero out all tallies
for(int i=0;i<n_tallies;i++){
h_tally[i].cell = problem_geom.tally_cells[i];
h_tally[i].length = 1024;
h_tally[i].E_min = 1e-11;
h_tally[i].E_max = 20;
h_tally[i].score = new float [h_tally[i].length];
h_tally[i].square = new float [h_tally[i].length];
h_tally[i].count = new unsigned [h_tally[i].length];
h_tally[i].score_total = new double [h_tally[i].length];
h_tally[i].square_total = new double [h_tally[i].length];
h_tally[i].count_total = new long unsigned [h_tally[i].length];
for(int k=0;k<h_tally[i].length;k++){
h_tally[i].score[ k] = 0.0;
h_tally[i].square[ k] = 0.0;
h_tally[i].count[ k] = 0.0;
h_tally[i].score_total[ k] = 0.0;
h_tally[i].square_total[k] = 0.0;
h_tally[i].count_total[ k] = 0.0;
}
}
}
void whistory::init_device(){
// copy pointers initialized by optix
dh_particles.space = (spatial_data*) optix_obj.positions_ptr;
dh_particles.cellnum = (unsigned*) optix_obj.cellnum_ptr;
dh_particles.talnum = (int*) optix_obj.talnum_ptr;
dh_particles.matnum = (unsigned*) optix_obj.matnum_ptr;
dh_particles.rxn = (unsigned*) optix_obj.rxn_ptr;
d_remap = (unsigned*) optix_obj.remap_ptr;
// compute minimum size, in case dataset is shorter than tally, etc...
unsigned minimum_size = Ndataset * sizeof(spatial_data)/sizeof(float);
for( int i=0 ; i<n_tallies ; i++ ){
if (h_tally[i].length > minimum_size){
minimum_size = h_tally[i].length;
}
}
// init others only used on CUDA side
check_cuda(cudaMalloc( &d_tally , n_tallies*sizeof(tally_data) ));
check_cuda(cudaMalloc( &d_particles , 1*sizeof(particle_data) ));
check_cuda(cudaMalloc( &dh_particles.E , Ndataset*sizeof(float) ));
check_cuda(cudaMalloc( &dh_particles.Q , Ndataset*sizeof(float) ));
check_cuda(cudaMalloc( &dh_particles.rn_bank , Ndataset*sizeof(float) ));
check_cuda(cudaMalloc( &dh_particles.isonum , Ndataset*sizeof(unsigned) ));
check_cuda(cudaMalloc( &dh_particles.yield , Ndataset*sizeof(unsigned) ));
check_cuda(cudaMalloc( &dh_particles.weight , Ndataset*sizeof(float) ));
check_cuda(cudaMalloc( &dh_particles.index , Ndataset*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_valid_result , Ndataset*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_valid_N , 1*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_fissile_points , Ndataset*sizeof(spatial_data) ));
check_cuda(cudaMalloc( &d_fissile_energy , Ndataset*sizeof(float) ));
check_cuda(cudaMalloc( &d_scanned , Ndataset*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_num_completed , 1*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_num_active , 1*sizeof(unsigned) ));
check_cuda(cudaMalloc( &d_zeros , minimum_size*sizeof(unsigned) ));
// copy values from initialized host arrays
check_cuda(cudaMemcpy( dh_particles.space , h_particles.space , Ndataset*sizeof(spatial_data) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( d_fissile_points , h_particles.space , Ndataset*sizeof(spatial_data) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.cellnum , h_particles.cellnum , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.talnum , h_particles.talnum , Ndataset*sizeof(int) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.matnum , h_particles.matnum , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.rxn , h_particles.rxn , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.E , h_particles.E , Ndataset*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( d_fissile_energy , zeros , Ndataset*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.Q , h_particles.Q , Ndataset*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.rn_bank , h_particles.rn_bank , Ndataset*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.isonum , h_particles.isonum , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.yield , h_particles.yield , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.weight , h_particles.weight , Ndataset*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_particles.index , h_particles.index , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( d_valid_result , zeros , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( d_remap , remap , Ndataset*sizeof(unsigned) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( d_zeros , zeros , minimum_size*sizeof(unsigned) , cudaMemcpyHostToDevice ));
// init tally containers
for( int i=0 ; i<n_tallies ; i++ ){
// allocate
check_cuda(cudaMalloc( &dh_tally[i].score , h_tally[i].length*sizeof(float*) ));
check_cuda(cudaMalloc( &dh_tally[i].square , h_tally[i].length*sizeof(float*) ));
check_cuda(cudaMalloc( &dh_tally[i].count , h_tally[i].length*sizeof(unsigned*) ));
// copy initialized values
dh_tally[i].cell = h_tally[i].cell;
dh_tally[i].length = h_tally[i].length;
dh_tally[i].E_min = h_tally[i].E_min;
dh_tally[i].E_max = h_tally[i].E_max;
check_cuda(cudaMemcpy( dh_tally[i].score , h_tally[i].score , h_tally[i].length*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_tally[i].square , h_tally[i].square , h_tally[i].length*sizeof(float) , cudaMemcpyHostToDevice ));
check_cuda(cudaMemcpy( dh_tally[i].count , h_tally[i].count , h_tally[i].length*sizeof(unsigned) , cudaMemcpyHostToDevice ));
}
// copy host structures (containing the device pointers) to the device structure
check_cuda(cudaMemcpy( d_particles, &dh_particles , 1*sizeof(particle_data), cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy( d_tally, dh_tally , n_tallies*sizeof(tally_data), cudaMemcpyHostToDevice));
// check errors
check_cuda(cudaThreadSynchronize());
check_cuda(cudaPeekAtLastError());
}
void whistory::init_RNG(){
unsigned seed = time( NULL );
if(print_flag >= 2){
std::cout << "\e[1;32m" << "Initializing random number bank on device using MTGP32 with seed of " << seed << "..." << "\e[m \n";
}
curandCreateGenerator( &rand_gen , CURAND_RNG_PSEUDO_MTGP32 ); //mersenne twister type
curandSetPseudoRandomGeneratorSeed( rand_gen , 123456789ULL );
curandGenerate( rand_gen , dh_particles.rn_bank , Ndataset );
cudaMemcpy(h_particles.rn_bank , dh_particles.rn_bank , Ndataset*sizeof(unsigned) , cudaMemcpyDeviceToHost); // copy bank back to keep seeds
check_cuda(cudaPeekAtLastError());
}
void whistory::update_RNG(){
curandGenerate( rand_gen , dh_particles.rn_bank , Ndataset );
check_cuda(cudaPeekAtLastError());
}
void whistory::init_CUDPP(){
if(print_flag >= 2){
std::cout << "\e[1;32m" << "Initializing CUDPP..." << "\e[m \n";
}
// global objects
res = cudppCreate(&theCudpp);
if (res != CUDPP_SUCCESS){fprintf(stderr, "Error initializing CUDPP Library.\n");}
if(print_flag >= 2){
std::cout << " configuring compact..." << "\n";
}
// sort stuff
compact_config.op = CUDPP_ADD;
compact_config.datatype = CUDPP_INT;
compact_config.algorithm = CUDPP_COMPACT;
compact_config.options = CUDPP_OPTION_FORWARD;
res = cudppPlan(theCudpp, &compactplan, compact_config, Ndataset, 1, 0);
if (CUDPP_SUCCESS != res){printf("Error creating CUDPPPlan for compact\n");exit(-1);}
if(print_flag >= 2){
std::cout << " configuring reduction..." << "\n";
}
// int reduction stuff
redu_int_config.op = CUDPP_ADD;
redu_int_config.datatype = CUDPP_INT;
redu_int_config.algorithm = CUDPP_REDUCE;
redu_int_config.options = 0;
res = cudppPlan(theCudpp, &reduplan_int, redu_int_config, Ndataset, 1, 0);
if (CUDPP_SUCCESS != res){printf("Error creating CUDPPPlan for reduction\n");exit(-1);}
// float reduction stuff
redu_float_config.op = CUDPP_ADD;
redu_float_config.datatype = CUDPP_FLOAT;
redu_float_config.algorithm = CUDPP_REDUCE;
redu_float_config.options = 0;
res = cudppPlan(theCudpp, &reduplan_float, redu_float_config, Ndataset, 1, 0);
if (CUDPP_SUCCESS != res){printf("Error creating CUDPPPlan for reduction\n");exit(-1);}
if(print_flag >= 2){
std::cout << " configuring scan..." << "\n";
}
// int reduction stuff
scan_int_config.op = CUDPP_ADD;
scan_int_config.datatype = CUDPP_INT;
scan_int_config.algorithm = CUDPP_SCAN;
scan_int_config.options = CUDPP_OPTION_EXCLUSIVE;
res = cudppPlan(theCudpp, &scanplan_int, scan_int_config, Ndataset, 1, 0);
if (CUDPP_SUCCESS != res){printf("Error creating CUDPPPlan for scan\n");exit(-1);}
if(print_flag >= 2){
std::cout << " configuring radix sort..." << "\n";
}
// int reduction stuff
radix_config.algorithm = CUDPP_SORT_RADIX;
radix_config.datatype = CUDPP_UINT;
radix_config.options = CUDPP_OPTION_KEY_VALUE_PAIRS;
res = cudppPlan(theCudpp, &radixplan, radix_config, Ndataset, 1, 0);
if (CUDPP_SUCCESS != res){printf("Error creating CUDPPPlan for radix sort\n");exit(-1);}
check_cuda(cudaPeekAtLastError());
}
unsigned whistory::reduce_yield(){
unsigned reduced_yields;
res = cudppReduce(reduplan_int, d_reduced_yields, dh_particles.yield, N);
if (res != CUDPP_SUCCESS){fprintf(stderr, "Error in reducing yield values\n");exit(-1);}
check_cuda(cudaMemcpy(&reduced_yields, d_reduced_yields, 1*sizeof(unsigned), cudaMemcpyDeviceToHost));
return reduced_yields;
}
float whistory::reduce_weight(){
float reduced_weight;
res = cudppReduce(reduplan_float, d_reduced_weight, dh_particles.weight, N);
if (res != CUDPP_SUCCESS){fprintf(stderr, "Error in reducing weight values\n");exit(-1);}
check_cuda(cudaMemcpy(&reduced_weight, d_reduced_weight, 1*sizeof(float), cudaMemcpyDeviceToHost));
return reduced_weight;
}
void whistory::accumulate_keff(unsigned converged, unsigned iteration, double* keff, float* keff_cycle){
float this_count, this_square, this_mean, keff_err2;
long unsigned reduced_yields = reduce_yield();
double reduced_weight = reduce_weight();
double initial_weight = N;
printf("reduce_yield %lu reduce_weight %6.4E starting weight %6.4E\n",reduced_yields,reduced_weight,initial_weight);
*keff_cycle = reduced_yields / reduced_weight;
if(converged){
reduced_yields_total += reduced_yields;
reduced_weight_total += reduced_weight;
initial_weight_total += initial_weight;
*keff = reduced_yields_total / initial_weight_total;
keff_sum += *keff_cycle;
keff2_sum += (*keff_cycle)*(*keff_cycle);
this_count = iteration + 1;
this_square = keff2_sum ;
this_mean = keff_sum ;
//printf("this_mean %10.8E this_square %10.8E iteration %d\n",this_mean,this_square, iteration+1);
keff_err2 = (1.0/((this_count - 1.0))) * ( (this_count*this_square)/(this_mean*this_mean) - 1.0 ) ;
if(keff_err2>0.0){
keff_err = sqrtf(keff_err2);}
else{
keff_err = 0.0;}
}
//printf("reduced_total %lu reduced %u keff %10.8E keff_cycle %10.8E iteration %u\n",reduced_yields_total,reduced_yields,*keff,*keff_cycle,iteration+1);
}
void whistory::accumulate_tally(){
for(int i=0;i<n_tallies;i++){
//copy data to host
check_cuda(cudaMemcpy(h_tally[i].score, dh_tally[i].score, h_tally[i].length*sizeof(float), cudaMemcpyDeviceToHost));
check_cuda(cudaMemcpy(h_tally[i].square, dh_tally[i].square, h_tally[i].length*sizeof(float), cudaMemcpyDeviceToHost));
check_cuda(cudaMemcpy(h_tally[i].count, dh_tally[i].count, h_tally[i].length*sizeof(unsigned), cudaMemcpyDeviceToHost));
check_cuda(cudaThreadSynchronize());
check_cuda(cudaDeviceSynchronize());
//zero out vectors
check_cuda(cudaMemcpy(dh_tally[i].score, d_zeros, h_tally[i].length*sizeof(float), cudaMemcpyDeviceToDevice));
check_cuda(cudaMemcpy(dh_tally[i].square, d_zeros, h_tally[i].length*sizeof(float), cudaMemcpyDeviceToDevice));
check_cuda(cudaMemcpy(dh_tally[i].count, d_zeros, h_tally[i].length*sizeof(unsigned), cudaMemcpyDeviceToDevice));
check_cuda(cudaThreadSynchronize());
check_cuda(cudaDeviceSynchronize());
//perform sums on 64bit host side values, zero 32bit arrays
for(unsigned k=0 ; k<h_tally[i].length; k++){
h_tally[i].score_total[ k] += h_tally[i].score[ k];
h_tally[i].square_total[k] += h_tally[i].square[k];
h_tally[i].count_total[ k] += h_tally[i].count[ k];
h_tally[i].score[ k] = 0.0;
h_tally[i].square[k] = 0.0;
h_tally[i].count[ k] = 0;
}
}
}
void whistory::copy_python_buffer(float** device_pointer,float** host_pointer,std::string function_name){
// version for two float pointers
// misc variables for maths
unsigned bytes,rows,columns,ndim;
// python variables
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue, *pString, *pBuffObj, *pObjList;
PyObject *call_result;
PyObject *call_string,*arg_string;
PyObject *pClass;
Py_buffer pBuff;
// get the MT array buffer from Python
call_string = PyString_FromString(function_name.c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
Py_DECREF(call_string);
if (PyObject_CheckBuffer(call_result)){
PyObject_GetBuffer(call_result, &pBuff,PyBUF_ND);
}
else{
PyErr_Print();
fprintf(stderr, "Returned object does not support buffer interface\n");
return;
}
// get array size info
get_Py_buffer_dims(&rows,&columns,&bytes,&pBuff);
// allocate xs_data pointer arrays
*host_pointer = new float [rows*columns];
// check to make sure bytes *= elements
assert(bytes==rows*columns*sizeof(float));
// copy python buffer contents to pointer
memcpy( *host_pointer, pBuff.buf , bytes );
// allocate device memory now that we know the size!
cudaMalloc(device_pointer,bytes);
// copy the xs data to device
cudaMemcpy(*device_pointer, *host_pointer, bytes, cudaMemcpyHostToDevice);
// release python variable to free memory
Py_DECREF(call_result);
}
void whistory::copy_python_buffer(unsigned** device_pointer,unsigned** host_pointer,std::string function_name){
// version for two unsigned pointers
// misc variables for maths
unsigned bytes,rows,columns;
// python variables
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue, *pString, *pBuffObj, *pObjList;
PyObject *call_result;
PyObject *call_string,*arg_string;
PyObject *pClass;
Py_buffer pBuff;
// get the MT array buffer from Python
call_string = PyString_FromString(function_name.c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
Py_DECREF(call_string);
if (PyObject_CheckBuffer(call_result)){
PyObject_GetBuffer(call_result, &pBuff,PyBUF_ND);
}
else{
PyErr_Print();
fprintf(stderr, "Returned object does not support buffer interface\n");
return;
}
// get array size info
get_Py_buffer_dims(&rows,&columns,&bytes,&pBuff);
// allocate xs_data pointer arrays
*host_pointer = new unsigned [rows*columns];
// check to make sure bytes *= elements
assert(bytes==rows*columns*sizeof(unsigned));
// copy python buffer contents to pointer
memcpy( *host_pointer, pBuff.buf , bytes );
// allocate device memory now that we know the size!
cudaMalloc(device_pointer,bytes);
// copy the xs data to device
cudaMemcpy(*device_pointer, *host_pointer, bytes, cudaMemcpyHostToDevice);
// release python variable to free memory
Py_DECREF(call_result);
}
void whistory::copy_python_buffer(float** host_pointer,std::string function_name){
// version for one (host) float pointer
// misc variables for maths
unsigned bytes,rows,columns;
// python variables
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue, *pString, *pBuffObj, *pObjList;
PyObject *call_result;
PyObject *call_string,*arg_string;
PyObject *pClass;
Py_buffer pBuff;
// get the MT array buffer from Python
call_string = PyString_FromString(function_name.c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
Py_DECREF(call_string);
if (PyObject_CheckBuffer(call_result)){
PyObject_GetBuffer(call_result, &pBuff,PyBUF_ND);
}
else{
PyErr_Print();
fprintf(stderr, "Returned object does not support buffer interface\n");
return;
}
// get array size info
get_Py_buffer_dims(&rows,&columns,&bytes,&pBuff);
// allocate xs_data pointer arrays
*host_pointer = new float [rows*columns];
// check to make sure bytes *= elements
assert(bytes==rows*columns*sizeof(float));
// copy python buffer contents to pointer
memcpy( *host_pointer, pBuff.buf , bytes );
// release python variable to free memory
Py_DECREF(call_result);
}
void whistory::copy_python_buffer(unsigned** host_pointer,std::string function_name){
// version for one (host) unsigned pointer
// misc variables for maths
unsigned bytes,rows,columns;
// python variables
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue, *pString, *pBuffObj, *pObjList;
PyObject *call_result;
PyObject *call_string,*arg_string;
PyObject *pClass;
Py_buffer pBuff;
// get the MT array buffer from Python
call_string = PyString_FromString(function_name.c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
Py_DECREF(call_string);
if (PyObject_CheckBuffer(call_result)){
PyObject_GetBuffer(call_result, &pBuff,PyBUF_ND);
}
else{
PyErr_Print();
fprintf(stderr, "Returned object does not support buffer interface\n");
return;
}
// get array size info
get_Py_buffer_dims(&rows,&columns,&bytes,&pBuff);
// allocate xs_data pointer arrays
*host_pointer = new unsigned [rows*columns];
// check to make sure bytes *= elements
assert(bytes==rows*columns*sizeof(unsigned));
// copy python buffer contents to pointer
memcpy( *host_pointer, pBuff.buf , bytes );
// release python variable to free memory
Py_DECREF(call_result);
}
int whistory::init_python(){
// misc variables
int do_final;
// python variables
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue, *pString, *pBuffObj, *pObjList;
PyObject *call_result;
PyObject *call_string,*arg_string;
PyObject *pClass;
Py_buffer pBuff;
if (Py_IsInitialized()){
if(print_flag >= 2){
printf("Python interpreter already initialized\n");
}
do_final=0;
}
else{
Py_Initialize();
do_final=1;
}
pName = PyString_FromString("unionize");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if ( pModule==NULL ){
PyErr_Print();
fprintf(stderr, "Failed to import \"%s\"\n", "unionize");
return 0;
}
pName = PyString_FromString("cross_section_data");
xsdat_instance = PyObject_CallMethodObjArgs(pModule,pName,NULL);
PyErr_Print();
Py_DECREF(pName);
if (xsdat_instance != NULL) {
// init the libraries wanted
for (int i=0; i<n_isotopes; i++){
call_string = PyString_FromString("_add_isotope");
arg_string = PyString_FromString(isotopes[i].c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, arg_string, NULL);
PyErr_Print();
Py_DECREF(arg_string);
Py_DECREF(call_string);
Py_DECREF(call_result);
}
// read the tables
call_string = PyString_FromString("_read_tables");
arg_string = PyString_FromString(problem_geom.datapath.c_str());
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, arg_string, NULL);
PyErr_Print();
Py_DECREF(arg_string);
Py_DECREF(call_string);
Py_DECREF(call_result);
// unionize the main energy grid across all isotopes
call_string = PyString_FromString("_unionize");
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
PyErr_Print();
Py_DECREF(call_string);
Py_DECREF(call_result);
// make the total MT reaction list from all isotopes
call_string = PyString_FromString("_insert_reactions");
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
PyErr_Print();
Py_DECREF(call_string);
Py_DECREF(call_result);
// allocate the unionized array
call_string = PyString_FromString("_allocate_arrays");
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
PyErr_Print();
Py_DECREF(call_string);
Py_DECREF(call_result);
// insert and interpolate the cross sections
call_string = PyString_FromString("_interpolate");
call_result = PyObject_CallMethodObjArgs(xsdat_instance, call_string, NULL);
PyErr_Print();
Py_DECREF(call_string);
Py_DECREF(call_result);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to instanciate \"%s\"\n", "unionize.cross_section_data");
}
return do_final;
}
void whistory::get_Py_buffer_dims(unsigned* rows, unsigned* columns, unsigned* bytes, Py_buffer* pBuff){
// get size
bytes[0] = pBuff[0].len;
// get dimensions
if (pBuff[0].ndim == 0){
rows[0] = 1;
columns[0] = 1;
}
else if (pBuff[0].ndim == 1){
rows[0] = pBuff[0].shape[0];
columns[0] = 1;
}
else if (pBuff[0].ndim == 2){
rows[0] = pBuff[0].shape[0];
columns[0] = pBuff[0].shape[1];
}
else {
fprintf(stderr, "Python buffer returned array with dimension > 2!!!\n");
}
}
void whistory::copy_scatter_data(){
// get scattering ditributions from PyNE and set up data heirarchy on host and device
if(print_flag >= 2){
std::cout << "\e[1;32m" << "Loading/copying scattering data and linking..." << "\e[m \n";
}
// python variables for arguments
PyObject *row_obj, *col_obj, *call_string, *obj_list;
PyObject *lower_erg_obj, *lower_len_obj, *lower_law_obj, *lower_intt_obj, *lower_var_obj, *lower_pdf_obj, *lower_cdf_obj;
PyObject *upper_erg_obj, *upper_len_obj, *upper_law_obj, *upper_intt_obj, *upper_var_obj, *upper_pdf_obj, *upper_cdf_obj, *next_dex_obj;
Py_buffer lower_var_buff, lower_pdf_buff, lower_cdf_buff, lower_nu_buff, upper_var_buff, upper_pdf_buff, upper_cdf_buff, upper_nu_buff;
// local temp variables
int row, col;
float lower_nu_t, lower_nu_p, upper_nu_t, upper_nu_p;
float *lower_nu, *upper_nu;
int this_isotope;
unsigned nu_d_got_data=0;
unsigned array_index, next_dex, lower_rows;
unsigned lower_var_buff_bytes, lower_pdf_buff_bytes, lower_cdf_buff_bytes, lower_nu_buff_bytes;
unsigned lower_var_buff_rows, lower_pdf_buff_rows, lower_cdf_buff_rows, lower_nu_buff_rows;
unsigned lower_var_buff_columns, lower_pdf_buff_columns, lower_cdf_buff_columns, lower_nu_buff_columns;
unsigned upper_var_buff_bytes, upper_pdf_buff_bytes, upper_cdf_buff_bytes, upper_nu_buff_bytes;
unsigned upper_var_buff_rows, upper_pdf_buff_rows, upper_cdf_buff_rows, upper_nu_buff_rows;
unsigned upper_var_buff_columns, upper_pdf_buff_columns, upper_cdf_buff_columns, upper_nu_buff_columns;
dist_data h_lower_dist, h_upper_dist;
dist_data dh_lower_dist, dh_upper_dist;
dist_data *d_lower_dist, *d_upper_dist;
//dist_container *dh_dist_scatter;
// compute some sizes
unsigned total_rows = h_xsdata.energy_grid_len;
unsigned total_cols = h_xsdata.total_reaction_channels + h_xsdata.n_isotopes;
// allocate host arrays
h_xsdata.dist_scatter = new dist_container [total_rows * total_cols];
dh_dist_scatter = new dist_container [total_rows * total_cols];
// allocate nu arrays
lower_nu = new float [2];
upper_nu = new float [2];
// init to null pointers
for (row=0 ; row<h_xsdata.energy_grid_len; row++){ //start after the total xs vectors
for (col=0 ; col<h_xsdata.total_reaction_channels ; col++){
array_index = row*total_cols + col;
h_xsdata.dist_scatter[array_index].upper = 0x0;
h_xsdata.dist_scatter[array_index].lower = 0x0;
dh_dist_scatter[array_index].upper = 0x0;
dh_dist_scatter[array_index].lower = 0x0;
}
}
// scan through the scattering array, copying the scattering distributions and replicating pointers to
// them for each main energy grid points within the distribution's grid points
//
// --- The current method copies the scattering distributions twice.
// --- This can be eliminated via copying pointers instead of reiniting ditributions,
// --- but this has been left for now for simplicity.
//
for( col=h_xsdata.n_isotopes ; col<total_cols ; col++ ){ // going down column to stay within to same isotope, not effcient for caching, but OK here since only done at init
// rest row index
row = 0;
// print some nice info if flagged
if(print_flag >= 2){
this_isotope = -1;
for (int i=0;i<h_xsdata.n_isotopes+1;i++){
if(col>=(h_xsdata.rxn_numbers_total[i]+h_xsdata.n_isotopes) & col<(h_xsdata.rxn_numbers_total[i+1]+h_xsdata.n_isotopes)){
this_isotope = i;
break;
}
}
printf(" Getting scatter data for isotope %s - reaction %u\n",isotopes[this_isotope].c_str(),h_xsdata.rxn_numbers[col]);
}
while(row<total_rows){
// compute array index
array_index = row*total_cols + col;
// call python object, which returns the two buffers
// and the main e grid index that it needs to be replicated to
row_obj = PyInt_FromLong (row);
col_obj = PyInt_FromLong (col);
call_string = PyString_FromString("_get_scatter_data");
obj_list = PyObject_CallMethodObjArgs(xsdat_instance, call_string, row_obj, col_obj, NULL);
PyErr_Print();
// get objects in the returned list
lower_erg_obj = PyList_GetItem(obj_list,0);
lower_len_obj = PyList_GetItem(obj_list,1);
lower_law_obj = PyList_GetItem(obj_list,2);
lower_intt_obj = PyList_GetItem(obj_list,3);
lower_var_obj = PyList_GetItem(obj_list,4);
lower_pdf_obj = PyList_GetItem(obj_list,5);
lower_cdf_obj = PyList_GetItem(obj_list,6);
upper_erg_obj = PyList_GetItem(obj_list,7);
upper_len_obj = PyList_GetItem(obj_list,8);
upper_law_obj = PyList_GetItem(obj_list,9);
upper_intt_obj = PyList_GetItem(obj_list,10);
upper_var_obj = PyList_GetItem(obj_list,11);
upper_pdf_obj = PyList_GetItem(obj_list,12);
upper_cdf_obj = PyList_GetItem(obj_list,13);
next_dex_obj = PyList_GetItem(obj_list,14);
PyErr_Print();
// copy erg, law, intt and next index. These will always be datatypes as shown. len can be a single int value for regular scattering, or a float array for nu data
h_lower_dist.erg = PyFloat_AsDouble( lower_erg_obj);
h_lower_dist.law = PyInt_AsLong( lower_law_obj);
h_lower_dist.intt = PyInt_AsLong( lower_intt_obj);
h_upper_dist.erg = PyFloat_AsDouble( upper_erg_obj);
h_upper_dist.law = PyInt_AsLong( upper_law_obj);
h_upper_dist.intt = PyInt_AsLong( upper_intt_obj);
next_dex = PyInt_AsLong( next_dex_obj);
PyErr_Print();
// decide what to put in the array according to length reported
if(h_lower_dist.law==-2){
// below threshold, do nothing except go to where the threshold is
row = next_dex;
}
else if (h_lower_dist.law==-1){
// this is a fission reaction and this is nu data
// get the precursor energy data to replicate on all arrays, only call once
if ( nu_d_got_data == 0 ){
// set flag that data has already been gotten, so new arrays aren't written for each nu point
nu_d_got_data = 1;
// get buffers from python
if (PyObject_CheckBuffer(lower_var_obj) &
PyObject_CheckBuffer(lower_pdf_obj) &
PyObject_CheckBuffer(lower_cdf_obj) &
PyObject_CheckBuffer(upper_var_obj) &
PyObject_CheckBuffer(upper_pdf_obj) &
PyObject_CheckBuffer(upper_cdf_obj) )
{
PyObject_GetBuffer( lower_var_obj, &lower_var_buff, PyBUF_ND);
PyObject_GetBuffer( lower_pdf_obj, &lower_pdf_buff, PyBUF_ND);
PyObject_GetBuffer( lower_cdf_obj, &lower_cdf_buff, PyBUF_ND);
PyObject_GetBuffer( upper_var_obj, &upper_var_buff, PyBUF_ND);
PyObject_GetBuffer( upper_pdf_obj, &upper_pdf_buff, PyBUF_ND);
PyObject_GetBuffer( upper_cdf_obj, &upper_cdf_buff, PyBUF_ND);
PyErr_Print();
}
else{
PyErr_Print();
fprintf(stderr, "Returned object does not support buffer interface\n");
return;
}
// since lengths are not the same for these arrays (multiplexed-ish), don't check lengths against each other, trust python object sizes
// get array size info
get_Py_buffer_dims(&lower_var_buff_rows, &lower_var_buff_columns, &lower_var_buff_bytes, &lower_var_buff);
get_Py_buffer_dims(&lower_pdf_buff_rows, &lower_pdf_buff_columns, &lower_pdf_buff_bytes, &lower_pdf_buff);
get_Py_buffer_dims(&lower_cdf_buff_rows, &lower_cdf_buff_columns, &lower_cdf_buff_bytes, &lower_cdf_buff);
get_Py_buffer_dims(&upper_var_buff_rows, &upper_var_buff_columns, &upper_var_buff_bytes, &upper_var_buff);
get_Py_buffer_dims(&upper_pdf_buff_rows, &upper_pdf_buff_columns, &upper_pdf_buff_bytes, &upper_pdf_buff);
get_Py_buffer_dims(&upper_cdf_buff_rows, &upper_cdf_buff_columns, &upper_cdf_buff_bytes, &upper_cdf_buff);
// get new pointers for temp arrays according to length reported
h_lower_dist.var = new float[lower_var_buff_bytes/sizeof(float)];
h_lower_dist.pdf = new float[lower_pdf_buff_bytes/sizeof(float)];
h_lower_dist.cdf = new float[lower_cdf_buff_bytes/sizeof(float)];
h_upper_dist.var = new float[upper_var_buff_bytes/sizeof(float)];
h_upper_dist.pdf = new float[upper_pdf_buff_bytes/sizeof(float)];
h_upper_dist.cdf = new float[upper_cdf_buff_bytes/sizeof(float)];
// allocate device distribution arrays
check_cuda(cudaMalloc(&dh_lower_dist.var, lower_var_buff_bytes)); // arrays are all different lengths now
check_cuda(cudaMalloc(&dh_lower_dist.pdf, lower_pdf_buff_bytes));
check_cuda(cudaMalloc(&dh_lower_dist.cdf, lower_cdf_buff_bytes));
check_cuda(cudaMalloc(&dh_upper_dist.var, upper_var_buff_bytes));
check_cuda(cudaMalloc(&dh_upper_dist.pdf, upper_pdf_buff_bytes));
check_cuda(cudaMalloc(&dh_upper_dist.cdf, upper_cdf_buff_bytes));
check_cuda(cudaPeekAtLastError());
// copy data from python buffer to host pointer in array
memcpy(h_lower_dist.var, lower_var_buff.buf, lower_var_buff_bytes);
memcpy(h_lower_dist.pdf, lower_pdf_buff.buf, lower_pdf_buff_bytes);
memcpy(h_lower_dist.cdf, lower_cdf_buff.buf, lower_cdf_buff_bytes);
memcpy(h_upper_dist.var, upper_var_buff.buf, upper_var_buff_bytes);
memcpy(h_upper_dist.pdf, upper_pdf_buff.buf, upper_pdf_buff_bytes);
memcpy(h_upper_dist.cdf, upper_cdf_buff.buf, upper_cdf_buff_bytes);
// copy data from host arrays to device arrays
check_cuda(cudaMemcpy(dh_lower_dist.var, h_lower_dist.var, lower_var_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy(dh_lower_dist.pdf, h_lower_dist.pdf, lower_pdf_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy(dh_lower_dist.cdf, h_lower_dist.cdf, lower_cdf_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy(dh_upper_dist.var, h_upper_dist.var, upper_var_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy(dh_upper_dist.pdf, h_upper_dist.pdf, upper_pdf_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaMemcpy(dh_upper_dist.cdf, h_upper_dist.cdf, upper_cdf_buff_bytes, cudaMemcpyHostToDevice));
check_cuda(cudaThreadSynchronize());
check_cuda(cudaPeekAtLastError());