-
Notifications
You must be signed in to change notification settings - Fork 28
/
state_kern.cl
3914 lines (3236 loc) · 148 KB
/
state_kern.cl
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-2019, Triad National Security, LLC.
* All rights Reserved.
*
* CLAMR -- LA-CC-11-094
*
* Copyright 2011-2019. Triad National Security, LLC. This software was produced
* under U.S. Government contract 89233218CNA000001 for Los Alamos National
* Laboratory (LANL), which is operated by Triad 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
* TRIAD 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 Triad 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 TRIAD 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 TRIAD 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]
*
*/
#if !defined(REG_INTEGER) && !defined(SHORT_INTEGER) && !defined(MIN_INTEGER)
#define REG_INTEGER
#endif
#if defined(MIN_INTEGER)
// define all to needed ranges and then typedef or define to actual
typedef unsigned short ushort_t; // 0 to 65,535
typedef short short_t; // -32,768 to 32,767
typedef unsigned char uchar_t; // 0 to 255
typedef char char_t; // -128 to 127
#ifdef HAVE_OPENCL
typedef cl_ushort cl_ushort_t;
typedef cl_short cl_short_t;
typedef cl_uchar cl_uchar_t;
typedef cl_char cl_char_t;
#endif
#elif defined(SHORT_INTEGER)
typedef unsigned short ushort_t;
typedef short short_t;
typedef unsigned short uchar_t;
typedef short char_t;
#ifdef HAVE_OPENCL
typedef cl_ushort cl_ushort_t;
typedef cl_short cl_short_t;
typedef cl_short cl_uchar_t;
typedef cl_short cl_char_t;
#endif
#elif defined(REG_INTEGER)
typedef unsigned int ushortt_t;
typedef int short_t;
typedef unsigned int uchar_t;
typedef int char_t;
#ifdef HAVE_OPENCL
typedef cl_uint cl_ushort_t;
typedef cl_int cl_short_t;
typedef cl_uint cl_uchar_t;
typedef cl_int cl_char_t;
#endif
#endif
#if !defined(FULL_PRECISION) && !defined(MIXED_PRECISION) && !defined(MINIMUM_PRECISION)
#define FULL_PRECISION
#endif
#ifdef NO_CL_DOUBLE
#undef FULL_PRECISION
#undef MIXED_PRECISION
#define MINIMUM_PRECISION
#endif
#if defined(MINIMUM_PRECISION)
typedef float state_t; // this is for physics state variables ncell in size
typedef float4 state4_t;
typedef float real_t; // this is used for intermediate calculations
typedef float2 real2_t;
typedef float4 real4_t;
#define ZERO 0.0f
#define HALF 0.5f
#define QUARTER 0.25f
#define ONE 1.0f
#define GRAVITATIONAL_CONSTANT 9.80f
#define THOUSAND 1000.0f
#define EPSILON 1.0f-30
#define STATE_EPS 15.0f
#define CONSERVATION_EPS 15.0f
// calc refine is done in single precision
#define REFINE_GRADIENT 0.10f
#define COARSEN_GRADIENT 0.05f
#define REFINE_ONE 1.0f
#define REFINE_HALF 0.5f
#define REFINE_NEG_THOUSAND -1000.0f
#elif defined(MIXED_PRECISION) // intermediate values calculated high precision and stored as floats
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
typedef float state_t;
typedef float4 state4_t;
typedef double real_t;
typedef double2 real2_t;
typedef double4 real4_t;
#define ZERO 0.0
#define HALF 0.5
#define QUARTER 0.25
#define ONE 1.0
#define GRAVITATIONAL_CONSTANT 9.80
#define THOUSAND 1000.0
#define EPSILON 1.0e-30
#define STATE_EPS .02
#define CONSERVATION_EPS .02
// calc refine is done in single precision
#define REFINE_ONE 1.0f
#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)
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
typedef double state_t;
typedef double4 state4_t;
typedef double real_t;
typedef double2 real2_t;
typedef double4 real4_t;
#define ZERO 0.0
#define HALF 0.5
#define QUARTER 0.25
#define ONE 1.0
#define GRAVITATIONAL_CONSTANT 9.80
#define THOUSAND 1000.0
#define EPSILON 1.0e-30
#define STATE_EPS .02
#define CONSERVATION_EPS .02
// calc refine is done in single precision
#define REFINE_ONE 1.0f
#define REFINE_GRADIENT 0.10
#define COARSEN_GRADIENT 0.05
#define REFINE_HALF 0.5
#define REFINE_NEG_THOUSAND -1000.0
#endif
#define TWO 2
//#define __OLD_STENCIL__
#define __NEW_STENCIL__
enum boundary
{ REAL_CELL = 1, // Denotes cell type of real cell.
LEFT_BOUNDARY = -1, // Denotes left boundary ghost cell.
RIGHT_BOUNDARY = -2, // Denotes right boundary ghost cell.
BOTTOM_BOUNDARY= -3, // Denotes bottom boundary ghost cell.
TOP_BOUNDARY = -4, // Denotes top boundary ghost cell.
FRONT_BOUNDARY = -5, // Denotes front boundary ghost cell.
BACK_BOUNDARY = -6 }; // Denotes back boundary ghost cell.
enum orientation
{ SW, // SW quadrant.
NW, // NW quadrant.
NE, // NE quadrant.
SE }; // SE quadrant.
int is_lower_left(int i, int j) { return(i % 2 == 0 && j % 2 == 0); }
int is_lower_right(int i, int j) { return(i % 2 == 1 && j % 2 == 0); }
int is_upper_left(int i, int j) { return(i % 2 == 0 && j % 2 == 1); }
int is_upper_right(int i, int j) { return(i % 2 == 1 && j % 2 == 1); }
int SUM_INT(int a, int b)
{
return a + b;
}
real_t SUM(real_t a, real_t b)
{
return a + b;
}
real_t MIN(real_t a, real_t b)
{
return min(a, b);
}
#define REDUCE_IN_TILE(operation, _tile_arr) \
for (int offset = ntX >> 1; offset > MIN_REDUCE_SYNC_SIZE; offset >>= 1) \
{ \
if (tiX < offset) \
{ \
_tile_arr[tiX] = operation(_tile_arr[tiX], _tile_arr[tiX+offset]); \
} \
barrier(CLK_LOCAL_MEM_FENCE); \
} \
if (tiX < MIN_REDUCE_SYNC_SIZE) \
{ \
for (int offset = MIN_REDUCE_SYNC_SIZE; offset > 1; offset >>= 1) \
{ \
_tile_arr[tiX] = operation(_tile_arr[tiX], _tile_arr[tiX+offset]); \
barrier(CLK_LOCAL_MEM_FENCE); \
} \
_tile_arr[tiX] = operation(_tile_arr[tiX], _tile_arr[tiX+1]); \
}
void reduction_min_within_tile1(__local real_t *tile)
{
const unsigned int tiX = get_local_id(0);
const unsigned int ntX = get_local_size(0);
REDUCE_IN_TILE(MIN, tile);
}
void reduction_sum_within_tile(__local real_t *tile)
{
const unsigned int tiX = get_local_id(0);
const unsigned int ntX = get_local_size(0);
REDUCE_IN_TILE(SUM, tile);
}
void reduction_sum_int2_within_tile(__local int8 *itile)
{
const unsigned int tiX = get_local_id(0);
const unsigned int ntX = get_local_size(0);
for (int offset = ntX >> 1; offset > MIN_REDUCE_SYNC_SIZE; offset >>= 1)
{
if (tiX < offset)
{
itile[tiX].s01 += itile[tiX+offset].s01;
}
barrier(CLK_LOCAL_MEM_FENCE);
}
if (tiX < MIN_REDUCE_SYNC_SIZE)
{
for (int offset = MIN_REDUCE_SYNC_SIZE; offset > 1; offset >>= 1)
{
itile[tiX].s01 += itile[tiX+offset].s01;
barrier(CLK_LOCAL_MEM_FENCE);
}
itile[tiX].s01 += itile[tiX+1].s01;
}
}
__kernel void set_timestep_cl(
const int ncells, // 0 Total number of cells.
const real_t sigma, // 1
__global const state_t *H_in, // 2
__global const state_t *U_in, // 3
__global const state_t *V_in, // 4
__global const uchar_t *level, // 5 Array of level information.
__global const char_t *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
{
const unsigned int giX = get_global_id(0);
const unsigned int tiX = get_local_id(0);
tile[tiX] = 1000.0;
if (giX >= ncells) return;
const unsigned int group_id = get_group_id(0);
const unsigned int ntX = get_local_size(0);
// Set physical constants.
const real_t g = GRAVITATIONAL_CONSTANT; // gravitational constant
//--MEMORY MANAGEMENT-------------------------------------------------------
// Set values for the main cell.
real_t H = H_in[giX];
real_t U = U_in[giX];
real_t V = V_in[giX];
uchar_t lev = level[giX];
char_t type = celltype[giX];
//--CALCULATIONS------------------------------------------------------------
if (type == REAL_CELL){
real_t wavespeed = sqrt(g * H);
real_t xspeed = (fabs(U) + wavespeed)/lev_dx[lev];
real_t yspeed = (fabs(V) + wavespeed)/lev_dy[lev];
tile[tiX] = sigma/(xspeed+yspeed);
}
barrier(CLK_LOCAL_MEM_FENCE);
reduction_min_within_tile1(tile);
// Write the local value back to an array size of the number of groups
if (tiX == 0){
redscratch[group_id] = tile[0];
(*deltaT) = tile[0];
}
}
/* finish_reduction */
__kernel void finish_reduction_min_cl(
const int isize,
__global real_t *redscratch,
__global real_t *deltaT,
__local real_t *tile)
{
const unsigned int tiX = get_local_id(0);
const unsigned int ntX = get_local_size(0);
int giX = tiX;
tile[tiX] = 1.0e20;
if (tiX < isize) tile[tiX] = redscratch[giX];
for (giX += ntX; giX < isize; giX += ntX) {
if (redscratch[giX] < tile[tiX]) tile[tiX] = redscratch[giX];
}
barrier(CLK_LOCAL_MEM_FENCE);
reduction_min_within_tile1(tile);
if (tiX == 0) {
(*deltaT) = tile[0];
}
}
//#ifdef __APPLE_CC__
//#define max(a,b) ((a) > (b) ? (a) : (b))
//#define fabs(a) ( (a) < 0 ? -(a) : a)
//#endif
void setup_tile(__local state4_t *tile,
__local int8 *itile,
const int isize,
__global const state_t *H,
__global const state_t *U,
__global const state_t *V,
__global const int *nlft,
__global const int *nrht,
__global const int *ntop,
__global const int *nbot,
__global const uchar_t *level
);
void setup_refine_tile(
__local state_t *tile,
__local int8 *itile,
const int isize,
__global const state_t *H,
__global const int *nlft,
__global const int *nrht,
__global const int *ntop,
__global const int *nbot,
__global const uchar_t *level
);
void setup_xface(
__local int8 *xface,
__global const int *map_xface2cell_lower,
__global const int *map_xface2cell_upper
);
void setup_yface(
__local int8 *yface,
__global const int *map_yface2cell_lower,
__global const int *map_yface2cell_upper
);
__kernel void copy_state_data_cl(
const int isize, // 0
__global state_t *H, // 1
__global state_t *U, // 2
__global state_t *V, // 3
__global state_t *H_new, // 4
__global state_t *U_new, // 5
__global state_t *V_new) // 6
{
const uint giX = get_global_id(0);
if (giX >= isize) return;
H_new[giX] = H[giX];
U_new[giX] = U[giX];
V_new[giX] = V[giX];
}
__kernel void copy_state_ghost_data_cl(
const int ncells, // 0
const int nghost, // 1
__global state_t *H, // 2
__global state_t *H_add, // 3
__global state_t *U, // 4
__global state_t *U_add, // 5
__global state_t *V, // 6
__global state_t *V_add) // 7
{
const uint giX = get_global_id(0);
if (giX >= nghost) return;
H[ncells+giX] = H_add[giX];
U[ncells+giX] = U_add[giX];
V[ncells+giX] = V_add[giX];
}
#ifndef SET_TILE_VARIABLES
#define SET_TILE_VARIABLES
// Define macros for local tile access.
#define Hval(i) ( tile[i].s0 )
#define Uval(i) ( tile[i].s1 )
#define Vval(i) ( tile[i].s2 )
#define Hrefval(i) ( tile[i] )
#define nlftval(i) ( itile[i].s0 )
#define nrhtval(i) ( itile[i].s1 )
#define ntopval(i) ( itile[i].s2 )
#define nbotval(i) ( itile[i].s3 )
#define levelval(i) ( itile[i].s4 )
#define mpotval(i) ( itile[i].s5 )
#endif
#ifndef SET_FACE_VARIABLES
#define SET_FACE_VARIABLES
// Define macros for local face access
//#define xfacelower(i) ( xface[i].s0 )
//#define xfaceupper(i) ( xface[i].s1 )
//#define yfacelower(i) ( yface[i].s0 )
//#define yfaceupper(i) ( yface[i].s1 )
#endif
#define SQ(x) ( (x)*(x) )
#define MIN3(a,b,c) ( min(min((a),(b)),(c)) )
#define HXFLUX(ic) ( U[ic] )
#define UXFLUX(ic) ( SQ(U[ic])/H[ic] + ghalf*SQ(H[ic]) )
#define UVFLUX(ic) ( U[ic]*V[ic]/H[ic] )
#define HXFLUXIC ( Uic )
#define HXFLUXNL ( Ul )
#define HXFLUXNR ( Ur )
#define HXFLUXNB ( Ub )
#define HXFLUXNT ( Ut )
#define UXFLUXIC ( SQ(Uic)/Hic + ghalf*SQ(Hic) )
#define UXFLUXNL ( SQ(Ul)/Hl + ghalf*SQ(Hl) )
#define UXFLUXNR ( SQ(Ur)/Hr + ghalf*SQ(Hr) )
#define UXFLUXNB ( SQ(Ub)/Hb + ghalf*SQ(Hb) )
#define UXFLUXNT ( SQ(Ut)/Ht + ghalf*SQ(Ht) )
#define UVFLUXIC ( Uic*Vic/Hic )
#define UVFLUXNL ( Ul*Vl/Hl )
#define UVFLUXNR ( Ur*Vr/Hr )
#define UVFLUXNB ( Ub*Vb/Hb )
#define UVFLUXNT ( Ut*Vt/Ht )
#define HYFLUX(ic) ( V[ic] )
#define VUFLUX(ic) ( V[ic]*U[ic]/H[ic] )
#define VYFLUX(ic) ( SQ(V[ic])/H[ic] + ghalf*SQ(H[ic]) )
#define HYFLUXIC ( Vic )
#define HYFLUXNL ( Vl )
#define HYFLUXNR ( Vr )
#define HYFLUXNB ( Vb )
#define HYFLUXNT ( Vt )
#define VUFLUXIC ( Vic*Uic/Hic )
#define VUFLUXNL ( Vl*Ul/Hl )
#define VUFLUXNR ( Vr*Ur/Hr )
#define VUFLUXNB ( Vb*Ub/Hb )
#define VUFLUXNT ( Vt*Ut/Ht )
#define VYFLUXIC ( SQ(Vic)/Hic + ghalf*SQ(Hic) )
#define VYFLUXNL ( SQ(Vl)/Hl + ghalf*SQ(Hl) )
#define VYFLUXNR ( SQ(Vr)/Hr + ghalf*SQ(Hr) )
#define VYFLUXNB ( SQ(Vb)/Hb + ghalf*SQ(Hb) )
#define VYFLUXNT ( SQ(Vt)/Ht + ghalf*SQ(Ht) )
#define HNEWXFLUXMINUS ( Uxminus )
#define HNEWXFLUXPLUS ( Uxplus )
#define UNEWXFLUXMINUS ( SQ(Uxminus)/Hxminus + ghalf*SQ(Hxminus) )
#define UNEWXFLUXPLUS ( SQ(Uxplus) /Hxplus + ghalf*SQ(Hxplus) )
#define UVNEWFLUXMINUS ( Uxminus*Vxminus/Hxminus )
#define UVNEWFLUXPLUS ( Uxplus *Vxplus /Hxplus )
#define HNEWYFLUXMINUS ( Vyminus )
#define HNEWYFLUXPLUS ( Vyplus )
#define VNEWYFLUXMINUS ( SQ(Vyminus)/Hyminus + ghalf*SQ(Hyminus) )
#define VNEWYFLUXPLUS ( SQ(Vyplus) /Hyplus + ghalf*SQ(Hyplus) )
#define VUNEWFLUXMINUS ( Vyminus*Uyminus/Hyminus )
#define VUNEWFLUXPLUS ( Vyplus *Uyplus /Hyplus )
#define HXFLUXFACE (Ux)
#define UXFLUXFACE (SQ(Ux)/Hx + ghalf*SQ(Hx))
#define VXFLUXFACE (Ux*Vx/Hx)
#define HYFLUXFACE (Vy)
#define UYFLUXFACE (Vy*Uy/Hy)
#define VYFLUXFACE (SQ(Vy)/Hy + ghalf*SQ(Hy))
// XXX Added XXX
#define HXFLUXNLT ( Ult )
#define HXFLUXNRT ( Urt )
#define UXFLUXNLT ( SQ(Ult)/Hlt + ghalf*SQ(Hlt) )
#define UXFLUXNRT ( SQ(Urt)/Hrt + ghalf*SQ(Hrt) )
#define UVFLUXNLT ( Ult*Vlt/Hlt )
#define UVFLUXNRT ( Urt*Vrt/Hrt )
#define HYFLUXNBR ( Vbr )
#define HYFLUXNTR ( Vtr )
#define VUFLUXNBR ( Vbr*Ubr/Hbr )
#define VUFLUXNTR ( Vtr*Utr/Htr )
#define VYFLUXNBR ( SQ(Vbr)/Hbr + ghalf*SQ(Hbr) )
#define VYFLUXNTR ( SQ(Vtr)/Htr + ghalf*SQ(Htr) )
#define HNEWXFLUXMINUS2 ( Uxminus2 )
#define HNEWXFLUXPLUS2 ( Uxplus2 )
#define UNEWXFLUXMINUS2 ( SQ(Uxminus2)/Hxminus2 + ghalf*SQ(Hxminus2) )
#define UNEWXFLUXPLUS2 ( SQ(Uxplus2) /Hxplus2 + ghalf*SQ(Hxplus2) )
#define UVNEWFLUXMINUS2 ( Uxminus2*Vxminus2/Hxminus2 )
#define UVNEWFLUXPLUS2 ( Uxplus2 *Vxplus2 /Hxplus2 )
#define HNEWYFLUXMINUS2 ( Vyminus2 )
#define HNEWYFLUXPLUS2 ( Vyplus2 )
#define VNEWYFLUXMINUS2 ( SQ(Vyminus2)/Hyminus2 + ghalf*SQ(Hyminus2) )
#define VNEWYFLUXPLUS2 ( SQ(Vyplus2) /Hyplus2 + ghalf*SQ(Hyplus2) )
#define VUNEWFLUXMINUS2 ( Vyminus2*Uyminus2/Hyminus2 )
#define VUNEWFLUXPLUS2 ( Vyplus2 *Uyplus2 /Hyplus2 )
#define U_halfstep(deltaT, U_i, U_n, F_i, F_n, r_i, r_n, A_i, A_n, V_i, V_n) (( (( 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) )) ))
real_t U_halfstep_ORIG(// 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 dist
real_t r_n, // Next cell's (upwind's) center to face dist
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) ));
}
real_t U_halfstep_BD(// 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 ((U_i*r_n+U_n*r_i)/(r_n+r_i) + (deltaT/(r_n+r_i))*(F_n-F_i));
}
#define U_fullstep(deltaT, dr, U, F_plus, F_minus, G_plus, G_minus) (( (U - (deltaT / dr)*(F_plus - F_minus + G_plus - G_minus)) ))
real_t U_fullstep_ORIG(
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) {
return (U - (deltaT / dr)*(F_plus - F_minus + G_plus - G_minus));
}
//#define w_corrector(deltaT, dr, U_eigen, grad_half, grad_minus, grad_plus) (( HALF*(HALF*U_eigen*deltaT/dr)*(ONE-(HALF*U_eigen*deltaT/dr))*(ONE- max(MIN3(ONE, (grad_plus*grad_half/max(SQ(grad_half),EPSILON)), (grad_minus*grad_half/max(SQ(grad_half),EPSILON))), ZERO)) ))
real_t w_corrector(//_ORIG(
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(SQ(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));
}
__kernel void apply_boundary_conditions_local_cl(
const int ncells, // 0 Total number of cells
__global const char_t *celltype, // 1 Array of left neighbors
__global const int *nlft, // 2 Array of left neighbors
__global const int *nrht, // 3 Array of right neighbors
__global const int *ntop, // 4 Array of top neighbors
__global const int *nbot, // 5 Array of bottom neighbors
__global state_t *H, // 6 H array
__global state_t *U, // 7 U array
__global state_t *V) // 8 V array
{
const uint giX = get_global_id(0);
const uint tiX = get_local_id(0);
// Ensure the executing thread is not extraneous
if(giX >= ncells)
return;
char_t ctype = celltype[giX];
if (ctype == LEFT_BOUNDARY){
int nr = nrht[giX];
if (nr < (int)ncells) {
H[giX] = H[nr];
U[giX] = -U[nr];
V[giX] = V[nr];
}
}
if (ctype == RIGHT_BOUNDARY){
int nl = nlft[giX];
if (nl < (int)ncells) {
H[giX] = H[nl];
U[giX] = -U[nl];
V[giX] = V[nl];
}
}
if (ctype == BOTTOM_BOUNDARY){
int nt = ntop[giX];
if (nt < (int)ncells) {
H[giX] = H[nt];
U[giX] = U[nt];
V[giX] = -V[nt];
}
}
if (ctype == TOP_BOUNDARY){
int nb = nbot[giX];
if (nb < (int)ncells) {
H[giX] = H[nb];
U[giX] = U[nb];
V[giX] = -V[nb];
}
}
}
__kernel void apply_boundary_conditions_ghost_cl(
const int ncells, // 0 Total number of cells
__global const char_t *celltype, // 1 Array celltypes
__global const int *nlft, // 2 Array of left neighbors
__global const int *nrht, // 3 Array of right neighbors
__global const int *ntop, // 4 Array of top neighbors
__global const int *nbot, // 5 Array of bottom neighbors
__global real_t *H, // 6 H array
__global real_t *U, // 7 U array
__global real_t *V) // 8 V array
{
const unsigned int giX = get_global_id(0);
const unsigned int tiX = get_local_id(0);
// Ensure the executing thread is not extraneous
if(giX >= ncells)
return;
char_t ctype = celltype[giX];
if (ctype == LEFT_BOUNDARY){
int nr = nrht[giX];
if (nr >= (int)ncells) {
H[giX] = H[nr];
U[giX] = -U[nr];
V[giX] = V[nr];
}
}
if (ctype == RIGHT_BOUNDARY){
int nl = nlft[giX];
if (nl >= (int)ncells) {
H[giX] = H[nl];
U[giX] = -U[nl];
V[giX] = V[nl];
}
}
if (ctype == BOTTOM_BOUNDARY){
int nt = ntop[giX];
if (nt >= (int)ncells) {
H[giX] = H[nt];
U[giX] = U[nt];
V[giX] = -V[nt];
}
}
if (ctype == TOP_BOUNDARY){
int nb = nbot[giX];
if (nb >= (int)ncells) {
H[giX] = H[nb];
U[giX] = U[nb];
V[giX] = -V[nb];
}
}
}
__kernel void apply_boundary_conditions_cl(
const int ncells, // 0 Total number of cells
__global const char_t *celltype, // 1 Array of left neighbors
__global const int *nlft, // 2 Array of left neighbors
__global const int *nrht, // 3 Array of right neighbors
__global const int *ntop, // 4 Array of top neighbors
__global const int *nbot, // 5 Array of bottom neighbors
__global state_t *H, // 6 H array
__global state_t *U, // 7 U array
__global state_t *V) // 8 V array
{
const uint giX = get_global_id(0);
const uint tiX = get_local_id(0);
// Ensure the executing thread is not extraneous
if(giX >= ncells)
return;
char_t ctype = celltype[giX];
if (ctype == LEFT_BOUNDARY){
int nr = nrht[giX];
H[giX] = H[nr];
U[giX] = -U[nr];
V[giX] = V[nr];
}
if (ctype == RIGHT_BOUNDARY){
int nl = nlft[giX];
H[giX] = H[nl];
U[giX] = -U[nl];
V[giX] = V[nl];
}
if (ctype == BOTTOM_BOUNDARY){
int nt = ntop[giX];
H[giX] = H[nt];
U[giX] = U[nt];
V[giX] = -V[nt];
}
if (ctype == TOP_BOUNDARY){
int nb = nbot[giX];
H[giX] = H[nb];
U[giX] = U[nb];
V[giX] = -V[nb];
}
}
__kernel void calc_finite_difference_cl(
const int ncells, // 0 Total number of cells
const int levmx, // 1 Maximum level
__global const state_t *H, // 2
__global const state_t *U, // 3
__global const state_t *V, // 4
__global state_t *H_new, // 5
__global state_t *U_new, // 6
__global state_t *V_new, // 7
__global const int *nlft, // 8 Array of left neighbors
__global const int *nrht, // 9 Array of right neighbors
__global const int *ntop, // 10 Array of top neighbors
__global const int *nbot, // 11 Array of bottom neighbors
__global const uchar_t *level, // 12 Array of level information
const real_t deltaT, // 13 Size of time step.
__global const real_t *lev_dx, // 14
__global const real_t *lev_dy, // 15
__local state4_t *tile, // 16 Tile size in state4_t
__local int8 *itile){ // 17 Tile size in int8
/////////////////////////////////////////////
/// Get thread identification information ///
/////////////////////////////////////////////
const uint giX = get_global_id(0);
const uint tiX = get_local_id(0);
const uint ngX = get_global_size(0);
const uint ntX = get_local_size(0);
const uint group_id = get_group_id(0);
// Ensure the executing thread is not extraneous
if(giX >= ncells)
return;
/////////////////////////////////////////////
/// Set local tile & apply boundary conds ///
/////////////////////////////////////////////
setup_tile(tile, itile, ncells, H, U, V, nlft, nrht, ntop, nbot, level);
barrier(CLK_LOCAL_MEM_FENCE);
/////////////////////////////////////////////////
/// Declare all constants and local variables ///
/////////////////////////////////////////////////
const real_t g = GRAVITATIONAL_CONSTANT; // gravitational constant
const real_t ghalf = HALF*g;
// Left, right, ... left-left, right-right, ... left-top, right-top neighbor
int nl, nr, nt, nb;
int nll, nrr, ntt, nbb;
// Level
int lvl, lvl_nl, lvl_nr, lvl_nt, lvl_nb;
int lvl_nll, lvl_nrr, lvl_ntt, lvl_nbb;
// Left-top, right-top, top-right, bottom-right neighbor
int nlt, nrt, ntr, nbr;
// State variables at x-axis control volume face
real_t Hxminus, Hxplus;
real_t Uxminus, Uxplus;
real_t Vxminus, Vxplus;
// State variables at y-axis control volume face
real_t Hyminus, Hyplus;
real_t Uyminus, Uyplus;
real_t Vyminus, Vyplus;
// Variables for artificial viscosity/flux limiting
real_t wminusx_H, wminusx_U;
real_t wplusx_H, wplusx_U;
real_t wminusy_H, wminusy_V;
real_t wplusy_H, wplusy_V;
int nltl;
real_t Hll2;
int nrtr;
real_t Hrr2;
real_t Ull2;
real_t Urr2;
int ntrt;
real_t Htt2;
int nbrb;
real_t Hbb2;
real_t Vtt2;
real_t Vbb2;
real_t Hxminus2, Hxplus2;
real_t Uxminus2, Uxplus2;
real_t Vxminus2, Vxplus2;
real_t Hyminus2, Hyplus2;
real_t Uyminus2, Uyplus2;
real_t Vyminus2, Vyplus2;
real_t Hxfluxminus;
real_t Uxfluxminus;
real_t Vxfluxminus;
real_t Hxfluxplus;
real_t Uxfluxplus;
real_t Vxfluxplus;
real_t Hyfluxminus;
real_t Uyfluxminus;
real_t Vyfluxminus;
real_t Hyfluxplus;
real_t Uyfluxplus;
real_t Vyfluxplus;
// XXX Assuming square cells! XXX
// State variables and cell widths and lengths
real_t dric, drl, drr, drt, drb;
// real_t drlt, drrt, drtr, drbr;
real_t Hic, Hl, Hr, Ht, Hb;
real_t Hll, Hrr, Htt, Hbb;
real_t Uic, Ul, Ur, Ut, Ub;
real_t Ull, Urr;
real_t Vic, Vl, Vr, Vt, Vb;
real_t Vtt, Vbb;
real_t Hlt, Hrt, Htr, Hbr;
real_t Ult, Urt, Utr, Ubr;
real_t Vlt, Vrt, Vtr, Vbr;
// Local values for the state variables and cell widths and heights for the local cell as well
// as its neighboring cells
real_t dxic, dxl, dxr, dyic, dyt, dyb;
//////////////////////////
/// Set the local tile ///
//////////////////////////
int start_idx = group_id * ntX;
// int end_idx = (group_id + 1) * ntX;
lvl = levelval(tiX);
dxic = lev_dx[lvl];
dyic = lev_dy[lvl];
nl = nlftval(tiX);
nr = nrhtval(tiX);
nt = ntopval(tiX);
nb = nbotval(tiX);
// nl = nlft[ic];
// nr = nrht[ic];
// nt = ntop[ic];
// nb = nbot[ic];
dric = dxic;
Hic = Hval(tiX);
Uic = Uval(tiX);
Vic = Vval(tiX);
int glob_flag = 0;
// Storing all values associated with the left neighbors in local variables
if(nl < 0) {
nl = abs(nl+1);
lvl_nl = level[nl];
nll = nlft[nl];
Hl = H[nl];
Ul = U[nl];
Vl = V[nl];
dxl = lev_dx[level[nl]];
nlt = ntop[nl];
glob_flag = 1;
}
else {
lvl_nl = levelval(nl);
nll = nlftval(nl);
Hl = Hval(nl);
Ul = Uval(nl);
Vl = Vval(nl);
dxl = lev_dx[levelval(nl)];
nlt = ntopval(nl);
}
drl = dxl; // lev_dx[level[nl]];
if(nll < 0 || glob_flag == 1) {
if (nll < 0) nll = abs(nll+1);
lvl_nll = level[nll];
Hll = H[nll];
Ull = U[nll];
}
else {