forked from lanl/CLAMR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.cpp
9024 lines (7844 loc) · 442 KB
/
state.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 "mesh/mesh.h"
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#include <queue>
#include "state.h"
#include "timer/timer.h"
#include "genmalloc/genmalloc.h"
#ifdef HAVE_MPI
#include <mpi.h>
#endif
#include <omp.h>
#undef DEBUG
//#define DEBUG 0
#undef DEBUG_RESTORE_VALS
#define TIMING_LEVEL 2
#if defined(HALF_PRECISION)
#define ZERO 0.0f
#define ONE 1.0f
#define HALF 0.5f
#define EPSILON 1.0f-30
#define STATE_EPS 15.0
// calc refine is done in single precision
#define REFINE_GRADIENT 0.10f
#define COARSEN_GRADIENT 0.05f
#define REFINE_HALF 0.5f
#define REFINE_NEG_THOUSAND -1000.0f
#elif defined(MINIMUM_PRECISION)
#define ZERO 0.0f
#define ONE 1.0f
#define HALF 0.5f
#define EPSILON 1.0f-30
#define STATE_EPS 15.0
// calc refine is done in single precision
#define REFINE_GRADIENT 0.10f
#define COARSEN_GRADIENT 0.05f
#define REFINE_HALF 0.5f
#define REFINE_NEG_THOUSAND -1000.0f
#elif defined(MIXED_PRECISION) // intermediate values calculated high precision and stored as floats
#define ZERO 0.0
#define ONE 1.0
#define HALF 0.5
#define EPSILON 1.0e-30
#define STATE_EPS .02
// calc refine is done in single precision
#define REFINE_GRADIENT 0.10f
#define COARSEN_GRADIENT 0.05f
#define REFINE_HALF 0.5f
#define REFINE_NEG_THOUSAND -1000.0f
#elif defined(FULL_PRECISION)
#define ZERO 0.0
#define ONE 1.0
#define HALF 0.5
#define EPSILON 1.0e-30
#define STATE_EPS .02
// calc refine is done in single precision
#define REFINE_GRADIENT 0.10
#define COARSEN_GRADIENT 0.05
#define REFINE_HALF 0.5
#define REFINE_NEG_THOUSAND -1000.0
#endif
#ifdef _OPENMP
//static bool iversion_flag = false;
#endif
bool phantom_debug = false;
typedef unsigned int uint;
static const char *state_timer_descriptor[STATE_TIMER_SIZE] = {
"state_timer_apply_BCs",
"state_timer_set_timestep",
"state_timer_finite_difference",
"state_timer_finite_diff_part1",
"state_timer_finite_diff_part2",
"state_timer_finite_diff_part3",
"state_timer_finite_diff_part4",
"state_timer_finite_diff_part5",
"state_timer_finite_diff_part6",
"state_timer_refine_potential",
"state_timer_calc_mpot",
"state_timer_rezone_all",
"state_timer_mass_sum",
"state_timer_read",
"state_timer_write"
};
const int CRUX_STATE_VERSION = 102;
const int num_int_vals = 1;
int int_vals[num_int_vals] = {CRUX_STATE_VERSION};
#ifdef HAVE_OPENCL
#include "state_kernel.inc"
#endif
struct esum_type{
double sum;
double correction;
};
#ifdef HAVE_MPI
MPI_Datatype MPI_TWO_DOUBLES;
MPI_Op KNUTH_SUM;
int commutative = 1;
void knuth_sum(struct esum_type *in, struct esum_type *inout, int *len, MPI_Datatype *MPI_TWO_DOUBLES);
#endif
int save_ncells;
#define CONSERVED_EQNS
//#define PRECISION_CHECK 1.0e-7
//#define PRECISION_CHECK_STATS 1
#ifdef PRECISION_CHECK_STATS
static int fail_prec_count = 0;
static double fail_F_plus_sum = 0.0;
static double fail_F_minus_sum = 0.0;
static double fail_G_plus_sum = 0.0;
static double fail_G_minus_sum = 0.0;
static double fail_wminusx_H_sum = 0.0;
static double fail_wplusx_H_sum = 0.0;
static double fail_wminusy_H_sum = 0.0;
static double fail_wplusy_H_sum = 0.0;
static int prec_count = 0;
static double F_plus_sum = 0.0;
static double F_minus_sum = 0.0;
static double G_plus_sum = 0.0;
static double G_minus_sum = 0.0;
static double wminusx_H_sum = 0.0;
static double wplusx_H_sum = 0.0;
static double wminusy_H_sum = 0.0;
static double wplusy_H_sum = 0.0;
static int fail_prec_avg_count = 0;
static double fail_F_plus_avg = 0.0;
static double fail_F_minus_avg = 0.0;
static double fail_G_plus_avg = 0.0;
static double fail_G_minus_avg = 0.0;
static double fail_wminusx_H_avg = 0.0;
static double fail_wplusx_H_avg = 0.0;
static double fail_wminusy_H_avg = 0.0;
static double fail_wplusy_H_avg = 0.0;
static int prec_avg_count = 0;
static double F_plus_avg = 0.0;
static double F_minus_avg = 0.0;
static double G_plus_avg = 0.0;
static double G_minus_avg = 0.0;
static double wminusx_H_avg = 0.0;
static double wplusx_H_avg = 0.0;
static double wminusy_H_avg = 0.0;
static double wplusy_H_avg = 0.0;
#endif
#ifdef PRECISION_CHECK
FILE *fprecise;
#endif
#define SQR(x) ( x*x )
#define MIN3(x,y,z) ( min( min(x,y), z) )
void doubleToHex(FILE *fp, double val){
fprintf(fp, "0x%02x", *(((unsigned char*)(&val))+sizeof(double)-1) );
for(int i=sizeof(double)-2; i >=0; i--) {
fprintf(fp, "%02x", *(((unsigned char*)(&val))+i) );
}
}
#ifdef HAVE_OPENCL
cl_kernel kernel_set_timestep;
cl_kernel kernel_reduction_min;
cl_kernel kernel_copy_state_data;
cl_kernel kernel_copy_state_ghost_data;
cl_kernel kernel_apply_boundary_conditions;
cl_kernel kernel_apply_boundary_conditions_local;
cl_kernel kernel_apply_boundary_conditions_ghost;
cl_kernel kernel_calc_finite_difference;
cl_kernel kernel_calc_finite_difference_via_faces_face;
cl_kernel kernel_calc_finite_difference_via_faces_cell;
cl_kernel kernel_calc_finite_difference_in_place_cell_comps;
cl_kernel kernel_calc_finite_difference_in_place_fixup;
cl_kernel kernel_calc_finite_difference_in_place_fill_new;
cl_kernel kernel_calc_finite_difference_via_face_in_place_face_comps;
cl_kernel kernel_calc_finite_difference_via_face_in_place_fixup;
cl_kernel kernel_calc_finite_difference_via_face_in_place_fill_new;
cl_kernel kernel_calc_finite_difference_regular_cells_comps;
cl_kernel kernel_calc_finite_difference_regular_cells_fill;
cl_kernel kernel_calc_finite_difference_regular_cells_face_comps;
cl_kernel kernel_calc_finite_difference_regular_cells_by_faces_fill;
cl_kernel kernel_refine_potential;
cl_kernel kernel_reduce_sum_mass_stage1of2;
cl_kernel kernel_reduce_sum_mass_stage2of2;
cl_kernel kernel_reduce_epsum_mass_stage1of2;
cl_kernel kernel_reduce_epsum_mass_stage2of2;
#endif
#pragma omp declare simd
inline real_t U_halfstep(// XXX Fix the subindices to be more intuitive XXX
real_t deltaT, // Timestep
real_t U_i, // Initial cell's (downwind's) state variable
real_t U_n, // Next cell's (upwind's) state variable
real_t F_i, // Initial cell's (downwind's) state variable flux
real_t F_n, // Next cell's (upwind's) state variable flux
real_t r_i, // Initial cell's (downwind's) center to face distance
real_t r_n, // Next cell's (upwind's) center to face distance
real_t A_i, // Cell's face surface area
real_t A_n, // Cell's neighbor's face surface area
real_t V_i, // Cell's volume
real_t V_n) { // Cell's neighbor's volume
return (( r_i*U_n + r_n*U_i ) / ( r_i + r_n ))
- HALF*deltaT*(( F_n*A_n*min(ONE, A_i/A_n) - F_i*A_i*min(ONE, A_n/A_i) )
/ ( V_n*min(HALF, V_i/V_n) + V_i*min(HALF, V_n/V_i) ));
}
inline real_t U_fullstep(
real_t deltaT,
real_t dr,
real_t U,
real_t F_plus,
real_t F_minus,
real_t G_plus,
real_t G_minus) {
#ifdef PRECISION_CHECK_BEST_PARENTHESIS
//"best" parentheses version
//return (U - (deltaT / dr)*((F_plus - F_minus) + (G_plus - G_minus)));
return (U + (-(deltaT/dr)*((F_plus-F_minus)+(G_plus-G_minus))));
#else
//original, no parentheses
return (U - (deltaT / dr)*(F_plus - F_minus + G_plus - G_minus));
#endif
}
#ifdef PRECISION_CHECK
inline void U_fullstep_precision_check(
int ic,
real_t deltaT_in,
real_t dr_in,
real_t U_in,
real_t U_new_in,
real_t F_plus_in,
real_t F_minus_in,
real_t G_plus_in,
real_t G_minus_in,
real_t wminusx_H_in,
real_t wplusx_H_in,
real_t wminusy_H_in,
real_t wplusy_H_in,
int *fail) {
float U = (float)U_in;
float deltaT = (float)deltaT_in;
float dr = (float)dr_in;
float F_plus = (float)F_plus_in;
float F_minus = (float)F_minus_in;
float G_plus = (float)G_plus_in;
float G_minus = (float)G_minus_in;
float wminusx_H = (float)wminusx_H_in;
float wplusx_H = (float)wplusx_H_in;
float wminusy_H = (float)wminusy_H_in;
float wplusy_H = (float)wplusy_H_in;
#ifdef PRECISION_CHECK_WITH_PARENTHESIS
//Some parentheses
double U_new = U - (deltaT / dr)*(F_plus - F_minus + G_plus - G_minus)
+( -wminusx_H + wplusx_H - wminusy_H + wplusy_H);
#else
#ifdef PRECISION_CHECK_BEST_PARENTHESIS
//"best" parentheses version
double U_new = U - (deltaT / dr)*((F_plus - F_minus) + (G_plus - G_minus))
// + (((-wminusx_H - wminusy_H) + wplusy_H) + wplusx_H);
+ (((wplusx_H + wplusy_H) - wminusy_H) - wminusx_H);
#else
//original, no parentheses
double U_new = U - (deltaT / dr)*(F_plus - F_minus + G_plus - G_minus)
+ -wminusx_H + wplusx_H - wminusy_H + wplusy_H;
#endif
#endif
*fail = 0;
if (fabs(U_new - U_new_in)/U_new > PRECISION_CHECK) {
fprintf(fprecise, "DEBUG -- found one at ic %d precision diff is %12.6lg relative %12.6lg\n",ic,fabs(U_new - U_new_in), fabs(U_new - U_new_in)/U_new);
*fail = 1;
#ifdef PRECISION_CHECK_STATS
fail_prec_count++;
fail_F_plus_sum += fabs(F_plus_in);
fail_F_minus_sum += fabs(F_minus_in);
fail_G_plus_sum += fabs(G_plus_in);
fail_G_minus_sum += fabs(G_minus_in);
fail_wminusx_H_sum += fabs(wminusx_H_in);
fail_wplusx_H_sum += fabs(wplusx_H_in);
fail_wminusy_H_sum += fabs(wminusy_H_in);
fail_wplusy_H_sum += fabs(wplusy_H_in);
#endif
}
#ifdef PRECISION_CHECK_STATS
prec_count++;
F_plus_sum += fabs(F_plus_in);
F_minus_sum += fabs(F_minus_in);
G_plus_sum += fabs(G_plus_in);
G_minus_sum += fabs(G_minus_in);
wminusx_H_sum += fabs(wminusx_H_in);
wplusx_H_sum += fabs(wplusx_H_in);
wminusy_H_sum += fabs(wminusy_H_in);
wplusy_H_sum += fabs(wplusy_H_in);
#endif
}
#endif
#pragma omp declare simd
inline real_t w_corrector(
real_t deltaT, // Timestep
real_t dr, // Cell's center to face distance
real_t U_eigen, // State variable's eigenvalue (speed)
real_t grad_half, // Centered gradient
real_t grad_minus, // Downwind gradient
real_t grad_plus) { // Upwind gradient
real_t nu = HALF * U_eigen * deltaT / dr;
nu = nu * (ONE - nu);
real_t rdenom = ONE / max(SQR(grad_half), EPSILON);
real_t rplus = (grad_plus * grad_half) * rdenom;
real_t rminus = (grad_minus * grad_half) * rdenom;
return HALF*nu*(ONE- max(MIN3(ONE, rplus, rminus), ZERO));
}
inline real_t U_reggrid_halfstep(// XXX Fix the subindices to be more intuitive XXX
real_t deltaT, // Timestep
real_t deltax, // Cell size in direction of flux
real_t U_i, // Initial cell's (downwind's) state variable
real_t U_n, // Next cell's (upwind's) state variable
real_t F_i, // Initial cell's (downwind's) state variable flux
real_t F_n) { // Next cell's (upwind's) state variable flux
return ( HALF*( ((U_n) + (U_i)) - (deltaT)/(deltax)*((F_n) - (F_i)) ) );
}
State::State(Mesh *mesh_in)
{
state_memory.memory_add(int_vals, (size_t)num_int_vals, 4, "state_int_vals", RESTART_DATA | REPLICATED_DATA);
state_memory.memory_add(cpu_timers, (size_t)STATE_TIMER_SIZE, 8, "state_cpu_timers", RESTART_DATA);
state_memory.memory_add(gpu_timers, (size_t)STATE_TIMER_SIZE, 8, "state_gpu_timers", RESTART_DATA);
for (int i = 0; i < STATE_TIMER_SIZE; i++){
cpu_timers[i] = 0.0;
}
for (int i = 0; i < STATE_TIMER_SIZE; i++){
gpu_timers[i] = 0L;
}
mesh = mesh_in;
#ifdef HAVE_MPI
int mpi_init;
MPI_Initialized(&mpi_init);
if (mpi_init){
MPI_Type_contiguous(2, MPI_DOUBLE, &MPI_TWO_DOUBLES);
MPI_Type_commit(&MPI_TWO_DOUBLES);
MPI_Op_create((MPI_User_function *)knuth_sum, commutative, &KNUTH_SUM);
// FIXME add fini and set size
if (mesh->parallel) state_memory.pinit(MPI_COMM_WORLD, 2L * 1024 * 1024 * 1024);
}
#endif
}
void State::init(int do_gpu_calc)
{
#ifdef PRECISION_CHECK
fprecise = fopen("precision.out","w");
#endif
if (do_gpu_calc) {
#ifdef HAVE_OPENCL
cl_context context = ezcl_get_context();
if (mesh->mype == 0) printf("Starting compile of kernels in state\n");
const char *defines = NULL;
cl_program program = ezcl_create_program_wsource(context, defines, state_kern_source);
kernel_set_timestep = ezcl_create_kernel_wprogram(program, "set_timestep_cl");
kernel_reduction_min = ezcl_create_kernel_wprogram(program, "finish_reduction_min_cl");
kernel_copy_state_data = ezcl_create_kernel_wprogram(program, "copy_state_data_cl");
kernel_copy_state_ghost_data = ezcl_create_kernel_wprogram(program, "copy_state_ghost_data_cl");
kernel_apply_boundary_conditions = ezcl_create_kernel_wprogram(program, "apply_boundary_conditions_cl");
kernel_apply_boundary_conditions_local = ezcl_create_kernel_wprogram(program, "apply_boundary_conditions_local_cl");
kernel_apply_boundary_conditions_ghost = ezcl_create_kernel_wprogram(program, "apply_boundary_conditions_ghost_cl");
kernel_calc_finite_difference = ezcl_create_kernel_wprogram(program, "calc_finite_difference_cl");
kernel_calc_finite_difference_via_faces_face = ezcl_create_kernel_wprogram(program, "calc_finite_difference_via_faces_face_comps_cl");
kernel_calc_finite_difference_via_faces_cell = ezcl_create_kernel_wprogram(program, "calc_finite_difference_via_faces_cell_comps_cl");
kernel_calc_finite_difference_in_place_cell_comps = ezcl_create_kernel_wprogram(program, "calc_finite_difference_in_place_cell_comps_cl");
kernel_calc_finite_difference_in_place_fixup = ezcl_create_kernel_wprogram(program, "calc_finite_difference_in_place_fixup_cl");
kernel_calc_finite_difference_in_place_fill_new = ezcl_create_kernel_wprogram(program, "calc_finite_difference_in_place_fill_new_cl");
kernel_calc_finite_difference_via_face_in_place_face_comps = ezcl_create_kernel_wprogram(program, "calc_finite_difference_via_face_in_place_face_comps_cl");
kernel_calc_finite_difference_via_face_in_place_fixup = ezcl_create_kernel_wprogram(program, "calc_finite_difference_via_face_in_place_fixup_cl");
kernel_calc_finite_difference_via_face_in_place_fill_new = ezcl_create_kernel_wprogram(program, "calc_finite_difference_via_face_in_place_fill_new_cl");
kernel_calc_finite_difference_regular_cells_comps = ezcl_create_kernel_wprogram(program, "calc_finite_difference_regular_cells_comps_cl");
kernel_calc_finite_difference_regular_cells_fill = ezcl_create_kernel_wprogram(program, "calc_finite_difference_regular_cells_fill_cl");
kernel_calc_finite_difference_regular_cells_face_comps = ezcl_create_kernel_wprogram(program, "calc_finite_difference_regular_cells_face_comps_cl");
kernel_calc_finite_difference_regular_cells_by_faces_fill = ezcl_create_kernel_wprogram(program, "calc_finite_difference_regular_cells_by_faces_fill_cl");
kernel_refine_potential = ezcl_create_kernel_wprogram(program, "refine_potential_cl");
kernel_reduce_sum_mass_stage1of2 = ezcl_create_kernel_wprogram(program, "reduce_sum_mass_stage1of2_cl");
kernel_reduce_sum_mass_stage2of2 = ezcl_create_kernel_wprogram(program, "reduce_sum_mass_stage2of2_cl");
kernel_reduce_epsum_mass_stage1of2 = ezcl_create_kernel_wprogram(program, "reduce_epsum_mass_stage1of2_cl");
kernel_reduce_epsum_mass_stage2of2 = ezcl_create_kernel_wprogram(program, "reduce_epsum_mass_stage2of2_cl");
ezcl_program_release(program);
if (mesh->mype == 0) printf("Finishing compile of kernels in state\n");
#endif
}
//printf("\nDEBUG -- Calling state memory memory malloc at line %d\n",__LINE__);
allocate(mesh->ncells);
//state_memory.memory_report();
//printf("DEBUG -- Finished state memory memory malloc at line %d\n\n",__LINE__);
}
void State::allocate(size_t ncells)
{
int flags = 0;
flags = (RESTART_DATA | REZONE_DATA | LOAD_BALANCE_MEMORY);
//if (mesh->parallel) flags = (flags | LOAD_BALANCE_MEMORY);
H = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), "H", flags);
U = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), "U", flags);
V = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), "V", flags);
#ifdef PRECISION_CHECK_GRAPHICS
PCHECK = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), "PCHECK", flags);
#endif
}
void State::resize(size_t new_ncells){
size_t current_size = state_memory.get_memory_size(H);
if (new_ncells > current_size) state_memory.memory_realloc_all(new_ncells);
//printf("\nDEBUG -- Calling state memory resize at line %d\n",__LINE__);
//state_memory.memory_report();
//printf("DEBUG -- Finished state memory resize at line %d\n\n",__LINE__);
}
void State::memory_reset_ptrs(void){
H = (state_t *)state_memory.get_memory_ptr("H");
U = (state_t *)state_memory.get_memory_ptr("U");
V = (state_t *)state_memory.get_memory_ptr("V");
#ifdef PRECISION_CHECK_GRAPHICS
PCHECK = (state_t *)state_memory.get_memory_ptr("PCHECK");
#endif
//printf("\nDEBUG -- Calling state memory reset_ptrs at line %d\n",__LINE__);
//state_memory.memory_report();
//printf("DEBUG -- Finished state memory reset_ptrs at line %d\n\n",__LINE__);
}
#ifdef HAVE_OPENCL
void State::gpu_memory_reset_ptrs(void)
{
dev_H = (cl_mem)gpu_state_memory.get_memory_ptr("dev_H");
dev_U = (cl_mem)gpu_state_memory.get_memory_ptr("dev_U");
dev_V = (cl_mem)gpu_state_memory.get_memory_ptr("dev_V");
}
#endif
void State::terminate(void)
{
#ifdef PRECISION_CHECK
fclose(fprecise);
#endif
#ifdef PRECISION_CHECK_STATS
printf("Stats are Fplus %lf Fminus %lf Gplus %lf Gminus %lf wminusx %lf wplusx %lf wminusy %lf wplusy %lf\n",
F_plus_avg/(double)prec_avg_count,
F_minus_avg/(double)prec_avg_count,
G_plus_avg/(double)prec_avg_count,
G_minus_avg/(double)prec_avg_count,
wminusx_H_avg/(double)prec_avg_count,
wplusx_H_avg/(double)prec_avg_count,
wminusy_H_avg/(double)prec_avg_count,
wplusy_H_avg/(double)prec_avg_count);
printf("Stats for fails are Fplus %lf Fminus %lf Gplus %lf Gminus %lf wminusx %lf wplusx %lf wminusy %lf wplusy %lf\n",
fail_F_plus_avg/(double)fail_prec_avg_count,
fail_F_minus_avg/(double)fail_prec_avg_count,
fail_G_plus_avg/(double)fail_prec_avg_count,
fail_G_minus_avg/(double)fail_prec_avg_count,
fail_wminusx_H_avg/(double)fail_prec_avg_count,
fail_wplusx_H_avg/(double)fail_prec_avg_count,
fail_wminusy_H_avg/(double)fail_prec_avg_count,
fail_wplusy_H_avg/(double)fail_prec_avg_count);
#endif
state_memory.memory_delete(H);
state_memory.memory_delete(U);
state_memory.memory_delete(V);
state_memory.memory_remove(int_vals);
state_memory.memory_remove(cpu_timers);
state_memory.memory_remove(gpu_timers);
#ifdef HAVE_OPENCL
ezcl_device_memory_delete(dev_deltaT);
gpu_state_memory.memory_delete(dev_H);
gpu_state_memory.memory_delete(dev_U);
gpu_state_memory.memory_delete(dev_V);
ezcl_kernel_release(kernel_set_timestep);
ezcl_kernel_release(kernel_reduction_min);
ezcl_kernel_release(kernel_copy_state_data);
ezcl_kernel_release(kernel_copy_state_ghost_data);
ezcl_kernel_release(kernel_apply_boundary_conditions);
ezcl_kernel_release(kernel_apply_boundary_conditions_local);
ezcl_kernel_release(kernel_apply_boundary_conditions_ghost);
ezcl_kernel_release(kernel_calc_finite_difference);
ezcl_kernel_release(kernel_calc_finite_difference_via_faces_face);
ezcl_kernel_release(kernel_calc_finite_difference_via_faces_cell);
ezcl_kernel_release(kernel_calc_finite_difference_in_place_cell_comps);
ezcl_kernel_release(kernel_calc_finite_difference_in_place_fixup);
ezcl_kernel_release(kernel_calc_finite_difference_in_place_fill_new);
ezcl_kernel_release(kernel_calc_finite_difference_via_face_in_place_face_comps);
ezcl_kernel_release(kernel_calc_finite_difference_via_face_in_place_fixup);
ezcl_kernel_release(kernel_calc_finite_difference_via_face_in_place_fill_new);
ezcl_kernel_release(kernel_calc_finite_difference_regular_cells_comps);
ezcl_kernel_release(kernel_calc_finite_difference_regular_cells_fill);
ezcl_kernel_release(kernel_calc_finite_difference_regular_cells_face_comps);
ezcl_kernel_release(kernel_calc_finite_difference_regular_cells_by_faces_fill);
ezcl_kernel_release(kernel_refine_potential);
ezcl_kernel_release(kernel_reduce_sum_mass_stage1of2);
ezcl_kernel_release(kernel_reduce_sum_mass_stage2of2);
ezcl_kernel_release(kernel_reduce_epsum_mass_stage1of2);
ezcl_kernel_release(kernel_reduce_epsum_mass_stage2of2);
#endif
#ifdef HAVE_MPI
if (mesh->parallel) state_memory.pfini();
#endif
}
#ifdef HAVE_MPI
void knuth_sum(struct esum_type *in, struct esum_type *inout, int *len, MPI_Datatype *MPI_TWO_DOUBLES)
{
double u, v, upt, up, vpp;
u = inout->sum;
v = in->sum + (in->correction+inout->correction);
upt = u + v;
up = upt - v;
vpp = upt - up;
inout->sum = upt;
inout->correction = (u - up) + (v - vpp);
// Just to block compiler warnings
//if (1==2) printf("DEBUG len %d datatype %lld\n",*len,(long long)(*MPI_TWO_DOUBLES) );
}
#endif
void State::add_boundary_cells(void)
{
struct timespec tstart_cpu;
cpu_timer_start(&tstart_cpu);
// This is for a mesh with no boundary cells -- they are added and
// the mesh sizes increased
size_t &ncells = mesh->ncells;
vector<int> &index = mesh->index;
vector<spatial_t> &x = mesh->x;
vector<spatial_t> &dx = mesh->dx;
vector<spatial_t> &y = mesh->y;
vector<spatial_t> &dy = mesh->dy;
int *i = mesh->i;
int *j = mesh->j;
uchar_t *level = mesh->level;
char_t *celltype = mesh->celltype;
int *nlft = mesh->nlft;
int *nrht = mesh->nrht;
int *nbot = mesh->nbot;
int *ntop = mesh->ntop;
vector<int> &lev_ibegin = mesh->lev_ibegin;
vector<int> &lev_iend = mesh->lev_iend;
vector<int> &lev_jbegin = mesh->lev_jbegin;
vector<int> &lev_jend = mesh->lev_jend;
// Pre-count number of cells to add
int icount = 0;
for (uint ic=0; ic<ncells; ic++) {
if (i[ic] == lev_ibegin[level[ic]]) icount++; // Left boundary
if (i[ic] == lev_iend[level[ic]]) icount++; // Right boundary
if (j[ic] == lev_jbegin[level[ic]]) icount++; // Bottom boundary
if (j[ic] == lev_jend[level[ic]]) icount++; // Top boundary
}
int new_ncells = ncells + icount;
// Increase the arrays for the new boundary cells
H=(state_t *)state_memory.memory_realloc(new_ncells, H);
U=(state_t *)state_memory.memory_realloc(new_ncells, U);
V=(state_t *)state_memory.memory_realloc(new_ncells, V);
//printf("\nDEBUG add_boundary cells\n");
//state_memory.memory_report();
//printf("DEBUG end add_boundary cells\n\n");
mesh->i =(int *)mesh->mesh_memory.memory_realloc(new_ncells, i);
mesh->j =(int *)mesh->mesh_memory.memory_realloc(new_ncells, j);
mesh->level =(uchar_t *)mesh->mesh_memory.memory_realloc(new_ncells, level);
// needs to cast char_t to void so doesn't mistake it for string
mesh->celltype =(char_t *)mesh->mesh_memory.memory_realloc(new_ncells, (void *)celltype);
mesh->nlft =(int *)mesh->mesh_memory.memory_realloc(new_ncells, nlft);
mesh->nrht =(int *)mesh->mesh_memory.memory_realloc(new_ncells, nrht);
mesh->nbot =(int *)mesh->mesh_memory.memory_realloc(new_ncells, nbot);
mesh->ntop =(int *)mesh->mesh_memory.memory_realloc(new_ncells, ntop);
//memory_reset_ptrs();
i = mesh->i;
j = mesh->j;
level = mesh->level;
celltype = mesh->celltype;
nlft = mesh->nlft;
nrht = mesh->nrht;
nbot = mesh->nbot;
ntop = mesh->ntop;
index.resize(new_ncells);
x.resize(new_ncells);
dx.resize(new_ncells);
y.resize(new_ncells);
dy.resize(new_ncells);
#pragma omp simd
for (int nc=ncells; nc<new_ncells; nc++) {
nlft[nc] = -1;
nrht[nc] = -1;
nbot[nc] = -1;
ntop[nc] = -1;
}
// In the first pass, set two of the neighbor indices and all
// the other data to be brought across. Set the inverse of the
// the velocity to enforce the reflective boundary condition
uint nc=ncells;
for (uint ic=0; ic<ncells; ic++) {
if (i[ic] == lev_ibegin[level[ic]]) {
nlft[ic] = nc;
nlft[nc] = nc;
nrht[nc] = ic;
i[nc] = lev_ibegin[level[ic]]-1;
j[nc] = j[ic];
level[nc] = level[ic];
dx[nc] = dx[ic];
dy[nc] = dy[ic];
x[nc] = x[ic]-dx[ic];
y[nc] = y[ic];
H[nc] = H[ic];
U[nc] = -U[ic];
V[nc] = V[ic];
nc++;
}
if (i[ic] == lev_iend[level[ic]]) {
nrht[ic] = nc;
nrht[nc] = nc;
nlft[nc] = ic;
i[nc] = lev_iend[level[ic]]+1;
j[nc] = j[ic];
level[nc] = level[ic];
dx[nc] = dx[ic];
dy[nc] = dy[ic];
x[nc] = x[ic]+dx[ic];
y[nc] = y[ic];
H[nc] = H[ic];
U[nc] = -U[ic];
V[nc] = V[ic];
nc++;
}
if (j[ic] == lev_jbegin[level[ic]]) {
nbot[ic] = nc;
nbot[nc] = nc;
ntop[nc] = ic;
i[nc] = i[ic];
j[nc] = lev_jbegin[level[ic]]-1;
level[nc] = level[ic];
dx[nc] = dx[ic];
dy[nc] = dy[ic];
x[nc] = x[ic];
y[nc] = y[ic]-dy[ic];
H[nc] = H[ic];
U[nc] = U[ic];
V[nc] = -V[ic];
nc++;
}
if (j[ic] == lev_jend[level[ic]]) {
ntop[ic] = nc;
ntop[nc] = nc;
nbot[nc] = ic;
i[nc] = i[ic];
j[nc] = lev_jend[level[ic]]+1;
level[nc] = level[ic];
dx[nc] = dx[ic];
dy[nc] = dy[ic];
x[nc] = x[ic];
y[nc] = y[ic]+dy[ic];
H[nc] = H[ic];
U[nc] = U[ic];
V[nc] = -V[ic];
nc++;
}
}
// Now set the other two neighbor indices
for (int nc=ncells; nc<new_ncells; nc++) {
if (i[nc] == lev_ibegin[level[nc]]-1) {
// Need to check if also a bottom boundary cell
if (j[nc] == lev_jbegin[level[nc]]){
nbot[nc] = nc;
} else {
nbot[nc] = nlft[nbot[nrht[nc]]];
}
if (j[nc] == lev_jend[level[nc]]){
ntop[nc] = nc;
} else {
ntop[nc] = nlft[ntop[nrht[nc]]];
}
}
if (i[nc] == lev_iend[level[nc]]+1) {
if (level[nc] <= level[nbot[nlft[nc]]]){
if (j[nc] == lev_jbegin[level[nc]]){
nbot[nc] = nc;
} else {
nbot[nc] = nrht[nbot[nlft[nc]]];
}
if (j[nc] == lev_jend[level[nc]]){
ntop[nc] = nc;
} else {
ntop[nc] = nrht[ntop[nlft[nc]]];
}
// calculation is a little different if going through a
// finer zoned region
} else {
nbot[nc] = nrht[nrht[nbot[nlft[nc]]]];
ntop[nc] = nrht[nrht[ntop[nlft[nc]]]];
}
}
if (j[nc] == lev_jbegin[level[nc]]-1) {
if (i[nc] == lev_ibegin[level[nc]]){
nlft[nc] = nc;
} else {
nlft[nc] = nbot[nlft[ntop[nc]]];
}
if (i[nc] == lev_iend[level[nc]]){
nrht[nc] = nc;
} else {
nrht[nc] = nbot[nrht[ntop[nc]]];
}
}
if (j[nc] == lev_jend[level[nc]]+1) {
if (level[nc] <= level[nlft[nbot[nc]]]){
if (i[nc] == lev_ibegin[level[nc]]){
nlft[nc] = nc;
} else {
nlft[nc] = ntop[nlft[nbot[nc]]];
}
if (i[nc] == lev_iend[level[nc]]){
nrht[nc] = nc;
} else {
nrht[nc] = ntop[nrht[nbot[nc]]];
}
} else {
nlft[nc] = ntop[ntop[nlft[nbot[nc]]]];
nrht[nc] = ntop[ntop[nrht[nbot[nc]]]];
}
}
}
save_ncells = ncells;
ncells = new_ncells;
cpu_timers[STATE_TIMER_APPLY_BCS] += cpu_timer_stop(tstart_cpu);
}
void State::apply_boundary_conditions(void)
{
static int *nlft, *nrht, *nbot, *ntop;
#ifdef _OPENMP
#pragma omp master
{
#endif
nlft = mesh->nlft;
nrht = mesh->nrht;
nbot = mesh->nbot;
ntop = mesh->ntop;
if (mesh->ncells_ghost < mesh->ncells) mesh->ncells_ghost = mesh->ncells;
#ifdef _OPENMP
}
#pragma omp barrier
#endif
// This is for a mesh with boundary cells
int lowerBound, upperBound;
mesh->get_bounds(lowerBound, upperBound);
for (int ic=lowerBound; ic<upperBound; ic++) {
if (mesh->is_left_boundary(ic)) {
int nr = nrht[ic];
if (nr < (int)mesh->ncells) {
H[ic] = H[nr];
U[ic] = -U[nr];
V[ic] = V[nr];
}
}
if (mesh->is_right_boundary(ic)) {
int nl = nlft[ic];
if (nl < (int)mesh->ncells) {
H[ic] = H[nl];
U[ic] = -U[nl];
V[ic] = V[nl];
}
}
if (mesh->is_bottom_boundary(ic)) {
int nt = ntop[ic];
if (nt < (int)mesh->ncells) {
H[ic] = H[nt];
U[ic] = U[nt];
V[ic] = -V[nt];
}
}
if (mesh->is_top_boundary(ic)) {
int nb = nbot[ic];
if (nb < (int)mesh->ncells) {
H[ic] = H[nb];
U[ic] = U[nb];
V[ic] = -V[nb];
}
}
}
if (mesh->numpe > 1) {
#ifdef HAVE_MPI
#ifdef _OPENMP
#pragma omp barrier
#pragma omp master
{
#endif
H=(state_t *)state_memory.memory_realloc(mesh->ncells_ghost, H);
U=(state_t *)state_memory.memory_realloc(mesh->ncells_ghost, U);
V=(state_t *)state_memory.memory_realloc(mesh->ncells_ghost, V);
L7_Update(&H[0], L7_STATE_T, mesh->cell_handle);
L7_Update(&U[0], L7_STATE_T, mesh->cell_handle);
L7_Update(&V[0], L7_STATE_T, mesh->cell_handle);
#ifdef _OPENMP
}
#pragma omp barrier
#endif
#endif
// This is for a mesh with boundary cells
for (int ic=lowerBound; ic<upperBound; ic++) {
if (mesh->is_left_boundary(ic)) {
int nr = nrht[ic];
if (nr >= (int)mesh->ncells) {
H[ic] = H[nr];
U[ic] = -U[nr];
V[ic] = V[nr];
}
}
if (mesh->is_right_boundary(ic)) {
int nl = nlft[ic];
if (nl >= (int)mesh->ncells) {
H[ic] = H[nl];
U[ic] = -U[nl];
V[ic] = V[nl];
}
}
if (mesh->is_bottom_boundary(ic)) {
int nt = ntop[ic];
if (nt >= (int)mesh->ncells) {
H[ic] = H[nt];
U[ic] = U[nt];
V[ic] = -V[nt];
}
}
if (mesh->is_top_boundary(ic)) {
int nb = nbot[ic];
if (nb >= (int)mesh->ncells) {
H[ic] = H[nb];
U[ic] = U[nb];
V[ic] = -V[nb];
}
}
}
}
}
void State::remove_boundary_cells(void)
{
if(! mesh->have_boundary) {
#ifdef _OPENMP
#pragma omp barrier
#pragma omp master
{
#endif
size_t &ncells = mesh->ncells;
// Resize to drop all the boundary cells
ncells = save_ncells;
H=(state_t *)state_memory.memory_realloc(save_ncells, H);
U=(state_t *)state_memory.memory_realloc(save_ncells, U);
V=(state_t *)state_memory.memory_realloc(save_ncells, V);
//printf("\nDEBUG remove_boundary cells\n");
//state_memory.memory_report();
//printf("DEBUG end remove_boundary cells\n\n");
mesh->i = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->i);
mesh->j = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->j);
mesh->level = (uchar_t *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->level);
// needs to cast char_t to void so doesn't mistake it for string
mesh->celltype = (char_t *)mesh->mesh_memory.memory_realloc(save_ncells, (void *)mesh->celltype);
mesh->nlft = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->nlft);
mesh->nrht = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->nrht);
mesh->nbot = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->nbot);
mesh->ntop = (int *)mesh->mesh_memory.memory_realloc(save_ncells, mesh->ntop);
// Reset the neighbors due to the dropped boundary cells
mesh->index.resize(save_ncells);
mesh->x.resize(save_ncells);
mesh->dx.resize(save_ncells);
mesh->y.resize(save_ncells);
mesh->dy.resize(save_ncells);
#ifdef _OPENMP
}
#pragma omp barrier
#endif
mesh->set_bounds(mesh->ncells);
int lowerBound, upperBound;
mesh->get_bounds(lowerBound, upperBound);
for (int ic=lowerBound; ic<upperBound; ic++) {
if (mesh->i[ic] == mesh->lev_ibegin[mesh->level[ic]]) mesh->nlft[ic] = ic;
if (mesh->i[ic] == mesh->lev_iend[mesh->level[ic]]) mesh->nrht[ic] = ic;
if (mesh->j[ic] == mesh->lev_jbegin[mesh->level[ic]]) mesh->nbot[ic] = ic;
if (mesh->j[ic] == mesh->lev_jend[mesh->level[ic]]) mesh->ntop[ic] = ic;
}
} // if have_boundary
}
double State::set_timestep(double g, double sigma)
{
double globalmindeltaT;
struct timespec tstart_cpu;
cpu_timer_start(&tstart_cpu);
static double mindeltaT;
int lowerBounds, upperBounds;
mesh->set_bounds(mesh->ncells);
mesh->get_bounds(lowerBounds, upperBounds);
#ifdef _OPENMP
#pragma omp barrier
#pragma omp master
{
#endif
mindeltaT = 1000;
#ifdef _OPENMP
}
#pragma omp barrier
#endif
double mymindeltaT = 1000.0; // private for each thread
#pragma omp simd reduction(min:mymindeltaT)
for (int ic=lowerBounds; ic<upperBounds; ic++) {
if (mesh->celltype[ic] == REAL_CELL) {
uchar_t lev = mesh->level[ic];
double wavespeed = sqrt(g*H[ic]);
double xspeed = (fabs(U[ic])+wavespeed)/mesh->lev_deltax[lev];
double yspeed = (fabs(V[ic])+wavespeed)/mesh->lev_deltay[lev];
double deltaT=sigma/(xspeed+yspeed);