-
Notifications
You must be signed in to change notification settings - Fork 0
/
experimental_iterative_state.cpp
3132 lines (2771 loc) · 143 KB
/
experimental_iterative_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
/*
* Copyright (c) 2011-2013, Los Alamos National Security, LLC.
* All rights Reserved.
*
* Copyright 2011-2012. Los Alamos National Security, LLC. This software was produced
* under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
* Laboratory (LANL), which is operated by Los Alamos National Security, LLC
* for the U.S. Department of Energy. The U.S. Government has rights to use,
* reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS
* ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
* ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified
* to produce derivative works, such modified software should be clearly marked,
* so as not to confuse it with the version available from LANL.
*
* Additionally, redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Los Alamos National Security, LLC, Los Alamos
* National Laboratory, LANL, the U.S. Government, nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE LOS ALAMOS NATIONAL SECURITY, LLC AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL
* SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* CLAMR -- LA-CC-11-094
* This research code is being developed as part of the
* 2011 X Division Summer Workshop for the express purpose
* of a collaborative code for development of ideas in
* the implementation of AMR codes for Exascale platforms
*
* AMR implementation of the Wave code previously developed
* as a demonstration code for regular grids on Exascale platforms
* as part of the Supercomputing Challenge and Los Alamos
* National Laboratory
*
* Authors: Bob Robey XCP-2 [email protected]
* Neal Davis [email protected], [email protected]
* David Nicholaeff [email protected], [email protected]
* Dennis Trujillo [email protected], [email protected]
*
*/
#include "mesh/mesh.h"
#include <unistd.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include "state.h"
#include "kdtree/KDTree.h"
//#include "reorder.h"
#include "timer/timer.h"
#ifdef HAVE_MPI
#include <mpi.h>
#endif
#undef DEBUG
//#define DEBUG 1
#undef DEBUG_RESTORE_VALS
#define TIMING_LEVEL 2
#if 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
typedef unsigned int uint;
#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 KAHAN_SUM;
int commutative = 1;
void kahan_sum(struct esum_type *in, struct esum_type *inout, int *len, MPI_Datatype *MPI_TWO_DOUBLES);
#endif
int save_ncells;
#define CONSERVED_EQNS
#define SQR(x) ( x*x )
#define MIN3(x,y,z) ( min( min(x,y), z) )
#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_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
State::State(Mesh *mesh_in)
{
cpu_time_apply_BCs = 0.0;
cpu_time_set_timestep = 0.0;
cpu_time_finite_difference = 0.0;
cpu_time_refine_potential = 0.0;
cpu_time_calc_mpot = 0.0;
cpu_time_mass_sum = 0.0;
gpu_time_apply_BCs = 0L;
gpu_time_set_timestep = 0L;
gpu_time_finite_difference = 0L;
gpu_time_refine_potential = 0L;
gpu_time_calc_mpot = 0L;
gpu_time_mass_sum = 0L;
gpu_time_read = 0L;
gpu_time_write = 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 *)kahan_sum, commutative, &KAHAN_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)
{
if (do_gpu_calc) {
#ifdef HAVE_OPENCL
cl_context context = ezcl_get_context();
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_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);
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;
#ifdef HAVE_J7
if (mesh->parallel) flags = LOAD_BALANCE_MEMORY;
#endif
H = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), flags, "H");
U = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), flags, "U");
V = (state_t *)state_memory.memory_malloc(ncells, sizeof(state_t), flags, "V");
}
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");
//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__);
}
void State::terminate(void)
{
state_memory.memory_delete(H);
state_memory.memory_delete(U);
state_memory.memory_delete(V);
#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_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 kahan_sum(struct esum_type *in, struct esum_type *inout, int *len, MPI_Datatype *MPI_TWO_DOUBLES)
{
double corrected_next_term, new_sum;
corrected_next_term = in->sum +(in->correction+inout->correction);
new_sum = inout->sum + corrected_next_term;
inout->correction = corrected_next_term - (new_sum - inout->sum);
inout->sum = new_sum;
// 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 timeval 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;
int *level = mesh->level;
int *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, sizeof(state_t), H);
U=(state_t *)state_memory.memory_realloc(new_ncells, sizeof(state_t), U);
V=(state_t *)state_memory.memory_realloc(new_ncells, sizeof(state_t), 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, sizeof(int), i);
mesh->j =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), j);
mesh->level =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), level);
mesh->celltype =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), celltype);
mesh->nlft =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), nlft);
mesh->nrht =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), nrht);
mesh->nbot =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), nbot);
mesh->ntop =(int *)mesh->mesh_memory.memory_realloc(new_ncells, sizeof(int), 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);
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_time_apply_BCs += cpu_timer_stop(tstart_cpu);
}
void State::apply_boundary_conditions_local(void)
{
size_t &ncells = mesh->ncells;
int *nlft = mesh->nlft;
int *nrht = mesh->nrht;
int *nbot = mesh->nbot;
int *ntop = mesh->ntop;
// This is for a mesh with boundary cells
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (uint ic=0; ic<ncells; ic++) {
if (mesh->is_left_boundary(ic)) {
int nr = nrht[ic];
if (nr < (int)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)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)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)ncells) {
H[ic] = H[nb];
U[ic] = U[nb];
V[ic] = -V[nb];
}
}
}
}
void State::apply_boundary_conditions_ghost(void)
{
size_t &ncells = mesh->ncells;
int *nlft = mesh->nlft;
int *nrht = mesh->nrht;
int *nbot = mesh->nbot;
int *ntop = mesh->ntop;
// This is for a mesh with boundary cells
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (uint ic=0; ic<ncells; ic++) {
if (mesh->is_left_boundary(ic)) {
int nr = nrht[ic];
if (nr >= (int)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)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)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)ncells) {
H[ic] = H[nb];
U[ic] = U[nb];
V[ic] = -V[nb];
}
}
}
}
void State::apply_boundary_conditions(void)
{
size_t &ncells = mesh->ncells;
int *nlft = mesh->nlft;
int *nrht = mesh->nrht;
int *nbot = mesh->nbot;
int *ntop = mesh->ntop;
// This is for a mesh with boundary cells
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (uint ic=0; ic<ncells; ic++) {
if (mesh->is_left_boundary(ic)) {
int nr = nrht[ic];
H[ic] = H[nr];
U[ic] = -U[nr];
V[ic] = V[nr];
}
if (mesh->is_right_boundary(ic)) {
int nl = nlft[ic];
H[ic] = H[nl];
U[ic] = -U[nl];
V[ic] = V[nl];
}
if (mesh->is_bottom_boundary(ic)) {
int nt = ntop[ic];
H[ic] = H[nt];
U[ic] = U[nt];
V[ic] = -V[nt];
}
if (mesh->is_top_boundary(ic)) {
int nb = nbot[ic];
H[ic] = H[nb];
U[ic] = U[nb];
V[ic] = -V[nb];
}
}
}
void State::remove_boundary_cells(void)
{
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;
int *level = mesh->level;
int *celltype = mesh->celltype;
int *nlft = mesh->nlft;
int *nrht = mesh->nrht;
int *nbot = mesh->nbot;
int *ntop = mesh->ntop;
if(mesh->have_boundary) return;
// Resize to drop all the boundary cells
ncells = save_ncells;
H=(state_t *)state_memory.memory_realloc(save_ncells, sizeof(state_t), H);
U=(state_t *)state_memory.memory_realloc(save_ncells, sizeof(state_t), U);
V=(state_t *)state_memory.memory_realloc(save_ncells, sizeof(state_t), 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, sizeof(int), i);
mesh->j = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), j);
mesh->level = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), level);
mesh->celltype = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), celltype);
mesh->nlft = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), nlft);
mesh->nrht = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), nrht);
mesh->nbot = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), nbot);
mesh->ntop = (int *)mesh->mesh_memory.memory_realloc(save_ncells, sizeof(int), ntop);
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(save_ncells);
x.resize(save_ncells);
dx.resize(save_ncells);
y.resize(save_ncells);
dy.resize(save_ncells);
// Reset the neighbors due to the dropped boundary cells
for (uint ic=0; ic<ncells; ic++) {
if (i[ic] == mesh->lev_ibegin[level[ic]]) nlft[ic] = ic;
if (i[ic] == mesh->lev_iend[level[ic]]) nrht[ic] = ic;
if (j[ic] == mesh->lev_jbegin[level[ic]]) nbot[ic] = ic;
if (j[ic] == mesh->lev_jend[level[ic]]) ntop[ic] = ic;
}
}
double State::set_timestep(double g, double sigma)
{
double globalmindeltaT;
double mindeltaT = 1000.0;
struct timeval tstart_cpu;
cpu_timer_start(&tstart_cpu);
size_t ncells = mesh->ncells;
#ifdef HAVE_MPI
int parallel = mesh->parallel;
#endif
int *&celltype = mesh->celltype;
int *&level = mesh->level;
int ic;
#ifdef _OPENMP
#pragma omp parallel
{
double mymindeltaT = 1000.0;
#pragma omp for
#endif
for (ic=0; ic<(int)ncells; ic++) {
if (celltype[ic] == REAL_CELL) {
int lev = 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);
#ifdef _OPENMP
if (deltaT < mymindeltaT) mymindeltaT = deltaT;
#else
if (deltaT < mindeltaT) mindeltaT = deltaT;
#endif
}
}
#ifdef _OPENMP
#pragma omp critical
if (mymindeltaT < mindeltaT) mindeltaT = mymindeltaT;
}
#endif
globalmindeltaT = mindeltaT;
#ifdef HAVE_MPI
if (parallel) MPI_Allreduce(&mindeltaT, &globalmindeltaT, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
#endif
cpu_time_set_timestep += cpu_timer_stop(tstart_cpu);
return(globalmindeltaT);
}
#ifdef HAVE_OPENCL
double State::gpu_set_timestep(double sigma)
{
double deltaT, globalmindeltaT;
struct timeval tstart_cpu;
cpu_timer_start(&tstart_cpu);
cl_command_queue command_queue = ezcl_get_command_queue();
size_t &ncells = mesh->ncells;
#ifdef HAVE_MPI
int ¶llel = mesh->parallel;
#endif
cl_mem &dev_level = mesh->dev_level;
cl_mem &dev_celltype = mesh->dev_celltype;
cl_mem &dev_levdx = mesh->dev_levdx;
cl_mem &dev_levdy = mesh->dev_levdy;
assert(dev_H);
assert(dev_U);
assert(dev_V);
assert(dev_level);
assert(dev_celltype);
assert(dev_levdx);
assert(dev_levdy);
size_t local_work_size = 128;
size_t global_work_size = ((ncells+local_work_size - 1) /local_work_size) * local_work_size;
size_t block_size = global_work_size/local_work_size;
cl_mem dev_redscratch = ezcl_malloc(NULL, const_cast<char *>("dev_redscratch"), &block_size, sizeof(cl_real_t), CL_MEM_READ_WRITE, 0);
/*
__kernel void set_timestep_cl(
const int ncells, // 0 Total number of cells.
const real_t sigma, // 1
__global const state_t *H, // 2
__global const state_t *U, // 3
__global const state_t *V, // 4
__global const int *level, // 5 Array of level information.
__global const int *celltype, // 6
__global const real_t *lev_dx, // 7
__global const real_t *lev_dy, // 8
__global real_t *redscratch, // 9
__global real_t *deltaT, // 10
__local real_t *tile) // 11
*/
real_t sigma_local = sigma;
ezcl_set_kernel_arg(kernel_set_timestep, 0, sizeof(cl_int), (void *)&ncells);
ezcl_set_kernel_arg(kernel_set_timestep, 1, sizeof(cl_real_t), (void *)&sigma_local);
ezcl_set_kernel_arg(kernel_set_timestep, 2, sizeof(cl_mem), (void *)&dev_H);
ezcl_set_kernel_arg(kernel_set_timestep, 3, sizeof(cl_mem), (void *)&dev_U);
ezcl_set_kernel_arg(kernel_set_timestep, 4, sizeof(cl_mem), (void *)&dev_V);
ezcl_set_kernel_arg(kernel_set_timestep, 5, sizeof(cl_mem), (void *)&dev_level);
ezcl_set_kernel_arg(kernel_set_timestep, 6, sizeof(cl_mem), (void *)&dev_celltype);
ezcl_set_kernel_arg(kernel_set_timestep, 7, sizeof(cl_mem), (void *)&dev_levdx);
ezcl_set_kernel_arg(kernel_set_timestep, 8, sizeof(cl_mem), (void *)&dev_levdy);
ezcl_set_kernel_arg(kernel_set_timestep, 9, sizeof(cl_mem), (void *)&dev_redscratch);
ezcl_set_kernel_arg(kernel_set_timestep, 10, sizeof(cl_mem), (void *)&dev_deltaT);
ezcl_set_kernel_arg(kernel_set_timestep, 11, local_work_size*sizeof(cl_real_t), NULL);
ezcl_enqueue_ndrange_kernel(command_queue, kernel_set_timestep, 1, NULL, &global_work_size, &local_work_size, NULL);
if (block_size > 1){
/*
__kernel void finish_reduction_min_cl(
const int isize,
__global real_t *redscratch,
__global real_t *deltaT,
__local real_t *tile)
*/
ezcl_set_kernel_arg(kernel_reduction_min, 0, sizeof(cl_int), (void *)&block_size);
ezcl_set_kernel_arg(kernel_reduction_min, 1, sizeof(cl_mem), (void *)&dev_redscratch);
ezcl_set_kernel_arg(kernel_reduction_min, 2, sizeof(cl_mem), (void *)&dev_deltaT);
ezcl_set_kernel_arg(kernel_reduction_min, 3, local_work_size*sizeof(cl_real_t), NULL);
ezcl_enqueue_ndrange_kernel(command_queue, kernel_reduction_min, 1, NULL, &local_work_size, &local_work_size, NULL);
}
real_t deltaT_local;
ezcl_enqueue_read_buffer(command_queue, dev_deltaT, CL_TRUE, 0, sizeof(cl_real_t), &deltaT_local, NULL);
deltaT = deltaT_local;
globalmindeltaT = deltaT;
#ifdef HAVE_MPI
if (parallel) MPI_Allreduce(&deltaT, &globalmindeltaT, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
#endif
ezcl_device_memory_delete(dev_redscratch);
gpu_time_set_timestep += (long)(cpu_timer_stop(tstart_cpu)*1.0e9);
return(globalmindeltaT);
}
#endif
void State::fill_circle(double circ_radius,// Radius of circle in grid units.
double fill_value, // Circle height for shallow water.
double background) // Background height for shallow water.
{
size_t &ncells = mesh->ncells;
vector<spatial_t> &x = mesh->x;
vector<spatial_t> &dx = mesh->dx;
vector<spatial_t> &y = mesh->y;
vector<spatial_t> &dy = mesh->dy;
for (uint ic = 0; ic < ncells; ic++)
{ H[ic] = background;
U[ic] = V[ic] = 0.0; }
// Clear the old k-D tree and generate new data (slow but necessary here).
//KDTree_Destroy(&mesh->tree);
mesh->kdtree_setup();
int nez;
vector<int> ind(ncells);
vector<double> weight(ncells);
#ifdef FULL_PRECISION
KDTree_QueryCircleInterior_Double(&mesh->tree, &nez, &(ind[0]), circ_radius, ncells,
&x[0], &dx[0],
&y[0], &dy[0]);
#else
KDTree_QueryCircleInterior_Float(&mesh->tree, &nez, &(ind[0]), circ_radius, ncells,
&x[0], &dx[0],
&y[0], &dy[0]);
#endif
for (int ic = 0; ic < nez; ++ic)
{ H[ind[ic]] = fill_value; }
#ifdef FULL_PRECISION
KDTree_QueryCircleIntersectWeighted_Double(&mesh->tree, &nez, &(ind[0]), &(weight[0]),
circ_radius, ncells,
&x[0], &dx[0],
&y[0], &dy[0]);
#else
KDTree_QueryCircleIntersectWeighted_Float(&mesh->tree, &nez, &(ind[0]), &(weight[0]),
circ_radius, ncells,
&x[0], &dx[0],
&y[0], &dy[0]);
#endif
for (int ic = 0; ic < nez; ++ic)
{ H[ind[ic]] = background + (fill_value - background) * weight[ic]; }
KDTree_Destroy(&mesh->tree);
}
void State::state_reorder(vector<int> iorder)
{
H = state_memory.memory_reorder(H, &iorder[0]);
U = state_memory.memory_reorder(U, &iorder[0]);
V = state_memory.memory_reorder(V, &iorder[0]);
//printf("\nDEBUG reorder cells\n");
//state_memory.memory_report();
//printf("DEBUG end reorder cells\n\n");
}
void State::rezone_all(int icount, int jcount, vector<int> mpot)
{
mesh->rezone_all(icount, jcount, mpot, 1, state_memory);
memory_reset_ptrs();
}
#ifdef HAVE_OPENCL
void State::gpu_rezone_all(int icount, int jcount, bool localStencil)
{
// Just to get rid of compiler warnings
if (1 == 2) printf("DEBUG -- localStencil is %d\n",localStencil);
mesh->gpu_rezone_all(icount, jcount, dev_mpot, gpu_state_memory);
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
//define macro for squaring a number
#define SQ(x) ((x)*(x))
//define macro to find minimum of 3 values
//#define MIN3(a,b,c) (min(min((a),(b)),(c)))
void State::calc_finite_difference(double deltaT){
// Physical Constants
real_t g = 9.80; // gravitational constant
real_t ghalf = HALF*g;
// Timers
struct timeval tstart_cpu;
cpu_timer_start(&tstart_cpu);
// Grab the Physical Adaptive Mesh Cells
size_t ncells = mesh->ncells;
size_t &ncells_ghost = mesh->ncells_ghost;
if (ncells_ghost < ncells) ncells_ghost = ncells;
#ifdef HAVE_MPI
// Populate the ghost regions since the calc neighbors has just been
// established for the mesh shortly before
if (mesh->numpe > 1) {
apply_boundary_conditions_local();
H=(state_t *)state_memory.memory_realloc(ncells_ghost, sizeof(state_t), H);
U=(state_t *)state_memory.memory_realloc(ncells_ghost, sizeof(state_t), U);
V=(state_t *)state_memory.memory_realloc(ncells_ghost, sizeof(state_t), 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);
apply_boundary_conditions_ghost();
} else {
apply_boundary_conditions();
}
#else
apply_boundary_conditions();
#endif
int flags = 0;
#if defined (HAVE_J7)
if (mesh->parallel) flags = LOAD_BALANCE_MEMORY;
#endif
state_t *H_new = (state_t *) state_memory.memory_malloc
(ncells_ghost, sizeof(state_t), flags, "H_new");
state_t *U_new = (state_t *) state_memory.memory_malloc
(ncells_ghost, sizeof(state_t), flags, "U_new");
state_t *V_new = (state_t *) state_memory.memory_malloc
(ncells_ghost, sizeof(state_t), flags, "V_new");
// XXX Add state_memory.memory_calloc for convenience XXX