-
Notifications
You must be signed in to change notification settings - Fork 0
/
lulesh.cu
5490 lines (4630 loc) · 167 KB
/
lulesh.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
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) 2010.
Lawrence Livermore National Security, LLC.
Produced at the Lawrence Livermore National Laboratory.
LLNL-CODE-461231
All rights reserved.
This file is part of LULESH, Version 1.0.
Please also read this link -- http://www.opensource.org/licenses/index.php
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 disclaimer below.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the disclaimer (as noted below)
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the LLNS/LLNL 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 COPYRIGHT HOLDERS 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 LAWRENCE LIVERMORE NATIONAL SECURITY, LLC,
THE U.S. DEPARTMENT OF ENERGY 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.
Additional BSD Notice
1. This notice is required to be provided under our contract with the U.S.
Department of Energy (DOE). This work was produced at Lawrence Livermore
National Laboratory under Contract No. DE-AC52-07NA27344 with the DOE.
2. Neither the United States Government nor Lawrence Livermore National
Security, LLC nor any of their employees, makes any warranty, express
or implied, or assumes any liability or responsibility for the accuracy,
completeness, or usefulness of any information, apparatus, product, or
process disclosed, or represents that its use would not infringe
privately-owned rights.
3. Also, reference herein to any specific commercial products, process, or
services by trade name, trademark, manufacturer or otherwise does not
necessarily constitute or imply its endorsement, recommendation, or
favoring by the United States Government or Lawrence Livermore National
Security, LLC. The views and opinions of authors expressed herein do not
necessarily state or reflect those of the United States Government or
Lawrence Livermore National Security, LLC, and shall not be used for
advertising or product endorsement purposes.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <util.h>
#include <sm_utils.inl>
#include <cuda.h>
#include <allocator.h>
#include "cuda_profiler_api.h"
#ifdef USE_MPI
#include <mpi.h>
#endif
#include <sys/time.h>
#include <unistd.h>
#include "lulesh.h"
/****************************************************/
/* Allow flexibility for arithmetic representations */
/****************************************************/
__device__ inline real4 SQRT(real4 arg) { return sqrtf(arg) ; }
__device__ inline real8 SQRT(real8 arg) { return sqrt(arg) ; }
__device__ inline real4 CBRT(real4 arg) { return cbrtf(arg) ; }
__device__ inline real8 CBRT(real8 arg) { return cbrt(arg) ; }
__device__ __host__ inline real4 FABS(real4 arg) { return fabsf(arg) ; }
__device__ __host__ inline real8 FABS(real8 arg) { return fabs(arg) ; }
__device__ inline real4 FMAX(real4 arg1,real4 arg2) { return fmaxf(arg1,arg2) ; }
__device__ inline real8 FMAX(real8 arg1,real8 arg2) { return fmax(arg1,arg2) ; }
#define MAX(a, b) ( ((a) > (b)) ? (a) : (b))
// Enzyme Autodifferentiation
//
//
template<typename... Args>
__device__
void __enzyme_autodiff(void*, Args...);
__device__ int enzyme_dup, enzyme_const, enzyme_active;
// Statement to govern whether to AD or just perform a normal forward pass -> normal-forward = no AD
#define Normal_forward 0
/* Stuff needed for boundary conditions */
/* 2 BCs on each of 6 hexahedral faces (12 bits) */
#define XI_M 0x00007
#define XI_M_SYMM 0x00001
#define XI_M_FREE 0x00002
#define XI_M_COMM 0x00004
#define XI_P 0x00038
#define XI_P_SYMM 0x00008
#define XI_P_FREE 0x00010
#define XI_P_COMM 0x00020
#define ETA_M 0x001c0
#define ETA_M_SYMM 0x00040
#define ETA_M_FREE 0x00080
#define ETA_M_COMM 0x00100
#define ETA_P 0x00e00
#define ETA_P_SYMM 0x00200
#define ETA_P_FREE 0x00400
#define ETA_P_COMM 0x00800
#define ZETA_M 0x07000
#define ZETA_M_SYMM 0x01000
#define ZETA_M_FREE 0x02000
#define ZETA_M_COMM 0x04000
#define ZETA_P 0x38000
#define ZETA_P_SYMM 0x08000
#define ZETA_P_FREE 0x10000
#define ZETA_P_COMM 0x20000
#define VOLUDER(a0,a1,a2,a3,a4,a5,b0,b1,b2,b3,b4,b5,dvdc) \
{ \
const Real_t twelfth = Real_t(1.0) / Real_t(12.0) ; \
\
dvdc= \
((a1) + (a2)) * ((b0) + (b1)) - ((a0) + (a1)) * ((b1) + (b2)) + \
((a0) + (a4)) * ((b3) + (b4)) - ((a3) + (a4)) * ((b0) + (b4)) - \
((a2) + (a5)) * ((b3) + (b5)) + ((a3) + (a5)) * ((b2) + (b5)); \
dvdc *= twelfth; \
}
/*
__device__
static
__forceinline__
void SumOverNodes(Real_t& val, volatile Real_t* smem, int cta_elem, int node) {
int tid = (cta_elem << 3) + node;
smem[tid] = val;
if (node < 4)
{
smem[tid] += smem[tid+4];
smem[tid] += smem[tid+2];
smem[tid] += smem[tid+1];
}
val = smem[(cta_elem << 3)];
}
*/
__device__
static
__forceinline__
void SumOverNodesShfl(Real_t& val) {
val += utils::shfl_xor( val, 4, 8);
val += utils::shfl_xor( val, 2, 8);
val += utils::shfl_xor( val, 1, 8);
}
__host__ __device__
static
__forceinline__
Real_t CalcElemVolume(const Real_t x0,
const Real_t x1,
const Real_t x2,
const Real_t x3,
const Real_t x4,
const Real_t x5,
const Real_t x6,
const Real_t x7,
const Real_t y0,
const Real_t y1,
const Real_t y2,
const Real_t y3,
const Real_t y4,
const Real_t y5,
const Real_t y6,
const Real_t y7,
const Real_t z0,
const Real_t z1,
const Real_t z2,
const Real_t z3,
const Real_t z4,
const Real_t z5,
const Real_t z6,
const Real_t z7)
{
Real_t twelveth = Real_t(1.0)/Real_t(12.0);
Real_t dx61 = x6 - x1;
Real_t dy61 = y6 - y1;
Real_t dz61 = z6 - z1;
Real_t dx70 = x7 - x0;
Real_t dy70 = y7 - y0;
Real_t dz70 = z7 - z0;
Real_t dx63 = x6 - x3;
Real_t dy63 = y6 - y3;
Real_t dz63 = z6 - z3;
Real_t dx20 = x2 - x0;
Real_t dy20 = y2 - y0;
Real_t dz20 = z2 - z0;
Real_t dx50 = x5 - x0;
Real_t dy50 = y5 - y0;
Real_t dz50 = z5 - z0;
Real_t dx64 = x6 - x4;
Real_t dy64 = y6 - y4;
Real_t dz64 = z6 - z4;
Real_t dx31 = x3 - x1;
Real_t dy31 = y3 - y1;
Real_t dz31 = z3 - z1;
Real_t dx72 = x7 - x2;
Real_t dy72 = y7 - y2;
Real_t dz72 = z7 - z2;
Real_t dx43 = x4 - x3;
Real_t dy43 = y4 - y3;
Real_t dz43 = z4 - z3;
Real_t dx57 = x5 - x7;
Real_t dy57 = y5 - y7;
Real_t dz57 = z5 - z7;
Real_t dx14 = x1 - x4;
Real_t dy14 = y1 - y4;
Real_t dz14 = z1 - z4;
Real_t dx25 = x2 - x5;
Real_t dy25 = y2 - y5;
Real_t dz25 = z2 - z5;
#define TRIPLE_PRODUCT(x1, y1, z1, x2, y2, z2, x3, y3, z3) \
((x1)*((y2)*(z3) - (z2)*(y3)) + (x2)*((z1)*(y3) - (y1)*(z3)) + (x3)*((y1)*(z2) - (z1)*(y2)))
// 11 + 3*14
Real_t volume =
TRIPLE_PRODUCT(dx31 + dx72, dx63, dx20,
dy31 + dy72, dy63, dy20,
dz31 + dz72, dz63, dz20) +
TRIPLE_PRODUCT(dx43 + dx57, dx64, dx70,
dy43 + dy57, dy64, dy70,
dz43 + dz57, dz64, dz70) +
TRIPLE_PRODUCT(dx14 + dx25, dx61, dx50,
dy14 + dy25, dy61, dy50,
dz14 + dz25, dz61, dz50);
#undef TRIPLE_PRODUCT
volume *= twelveth;
return volume ;
}
__host__ __device__
static
__forceinline__
Real_t CalcElemVolume(const Real_t x[8],
const Real_t y[8],
const Real_t z[8])
{
return CalcElemVolume( x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7],
y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7],
z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7]);
}
void cuda_init(int rank)
{
Int_t deviceCount, dev;
cudaDeviceProp cuda_deviceProp;
cudaSafeCall( cudaGetDeviceCount(&deviceCount) );
if (deviceCount == 0) {
fprintf(stderr, "cuda_init(): no devices supporting CUDA.\n");
exit(1);
}
dev = rank % deviceCount;
if ((dev < 0) || (dev > deviceCount-1)) {
fprintf(stderr, "cuda_init(): requested device (%d) out of range [%d,%d]\n",
dev, 0, deviceCount-1);
exit(1);
}
cudaSafeCall( cudaSetDevice(dev) );
struct cudaDeviceProp props;
cudaGetDeviceProperties(&props, dev);
char hostname[256];
gethostname(hostname, sizeof(hostname));
printf("Host %s using GPU %i: %s\n", hostname, dev, props.name);
cudaSafeCall( cudaGetDeviceProperties(&cuda_deviceProp, dev) );
if (cuda_deviceProp.major < 3) {
fprintf(stderr, "cuda_init(): This implementation of Lulesh requires device SM 3.0+.\n", dev);
exit(1);
}
#if CUDART_VERSION < 5000
fprintf(stderr,"cuda_init(): This implementation of Lulesh uses texture objects, which is requires Cuda 5.0+.\n");
exit(1);
#endif
}
void AllocateNodalPersistent(Domain* domain, size_t domNodes)
{
domain->x.resize(domNodes) ; /* coordinates */
domain->y.resize(domNodes) ;
domain->z.resize(domNodes) ;
domain->xd.resize(domNodes) ; /* velocities */
domain->yd.resize(domNodes) ;
domain->zd.resize(domNodes) ;
domain->xdd.resize(domNodes) ; /* accelerations */
domain->ydd.resize(domNodes) ;
domain->zdd.resize(domNodes) ;
domain->fx.resize(domNodes) ; /* forces */
domain->fy.resize(domNodes) ;
domain->fz.resize(domNodes) ;
domain->dfx.resize(domNodes) ; /* AD derivative of the forces */
domain->dfy.resize(domNodes) ;
domain->dfz.resize(domNodes) ;
domain->nodalMass.resize(domNodes) ; /* mass */
}
void AllocateElemPersistent(Domain* domain, size_t domElems, size_t padded_domElems)
{
domain->matElemlist.resize(domElems) ; /* material indexset */
domain->nodelist.resize(8*padded_domElems) ; /* elemToNode connectivity */
domain->lxim.resize(domElems) ; /* elem connectivity through face */
domain->lxip.resize(domElems) ;
domain->letam.resize(domElems) ;
domain->letap.resize(domElems) ;
domain->lzetam.resize(domElems) ;
domain->lzetap.resize(domElems) ;
domain->elemBC.resize(domElems) ; /* elem face symm/free-surf flag */
domain->e.resize(domElems) ; /* energy */
domain->p.resize(domElems) ; /* pressure */
domain->d_e.resize(domElems) ; /* AD derivative of energy E */
domain->q.resize(domElems) ; /* q */
domain->ql.resize(domElems) ; /* linear term for q */
domain->qq.resize(domElems) ; /* quadratic term for q */
domain->v.resize(domElems) ; /* relative volume */
domain->volo.resize(domElems) ; /* reference volume */
domain->delv.resize(domElems) ; /* m_vnew - m_v */
domain->vdov.resize(domElems) ; /* volume derivative over volume */
domain->arealg.resize(domElems) ; /* elem characteristic length */
domain->ss.resize(domElems) ; /* "sound speed" */
domain->elemMass.resize(domElems) ; /* mass */
}
void AllocateSymmX(Domain* domain, size_t size)
{
domain->symmX.resize(size) ;
}
void AllocateSymmY(Domain* domain, size_t size)
{
domain->symmY.resize(size) ;
}
void AllocateSymmZ(Domain* domain, size_t size)
{
domain->symmZ.resize(size) ;
}
void InitializeFields(Domain* domain)
{
/* Basic Field Initialization */
thrust::fill(domain->ss.begin(),domain->ss.end(),0.);
thrust::fill(domain->e.begin(),domain->e.end(),0.);
thrust::fill(domain->p.begin(),domain->p.end(),0.);
thrust::fill(domain->q.begin(),domain->q.end(),0.);
thrust::fill(domain->v.begin(),domain->v.end(),1.);
thrust::fill(domain->d_e.begin(),domain->d_e.end(),0.);
thrust::fill(domain->xd.begin(),domain->xd.end(),0.);
thrust::fill(domain->yd.begin(),domain->yd.end(),0.);
thrust::fill(domain->zd.begin(),domain->zd.end(),0.);
thrust::fill(domain->xdd.begin(),domain->xdd.end(),0.);
thrust::fill(domain->ydd.begin(),domain->ydd.end(),0.);
thrust::fill(domain->zdd.begin(),domain->zdd.end(),0.);
thrust::fill(domain->nodalMass.begin(),domain->nodalMass.end(),0.);
}
////////////////////////////////////////////////////////////////////////////////
void
Domain::SetupCommBuffers(Int_t edgeNodes)
{
// allocate a buffer large enough for nodal ghost data
maxEdgeSize = MAX(this->sizeX, MAX(this->sizeY, this->sizeZ))+1 ;
maxPlaneSize = CACHE_ALIGN_REAL(maxEdgeSize*maxEdgeSize) ;
maxEdgeSize = CACHE_ALIGN_REAL(maxEdgeSize) ;
// assume communication to 6 neighbors by default
m_rowMin = (m_rowLoc == 0) ? 0 : 1;
m_rowMax = (m_rowLoc == m_tp-1) ? 0 : 1;
m_colMin = (m_colLoc == 0) ? 0 : 1;
m_colMax = (m_colLoc == m_tp-1) ? 0 : 1;
m_planeMin = (m_planeLoc == 0) ? 0 : 1;
m_planeMax = (m_planeLoc == m_tp-1) ? 0 : 1;
#if USE_MPI
// account for face communication
Index_t comBufSize =
(m_rowMin + m_rowMax + m_colMin + m_colMax + m_planeMin + m_planeMax) *
maxPlaneSize * MAX_FIELDS_PER_MPI_COMM ;
// account for edge communication
comBufSize +=
((m_rowMin & m_colMin) + (m_rowMin & m_planeMin) + (m_colMin & m_planeMin) +
(m_rowMax & m_colMax) + (m_rowMax & m_planeMax) + (m_colMax & m_planeMax) +
(m_rowMax & m_colMin) + (m_rowMin & m_planeMax) + (m_colMin & m_planeMax) +
(m_rowMin & m_colMax) + (m_rowMax & m_planeMin) + (m_colMax & m_planeMin)) *
maxPlaneSize * MAX_FIELDS_PER_MPI_COMM ;
// account for corner communication
// factor of 16 is so each buffer has its own cache line
comBufSize += ((m_rowMin & m_colMin & m_planeMin) +
(m_rowMin & m_colMin & m_planeMax) +
(m_rowMin & m_colMax & m_planeMin) +
(m_rowMin & m_colMax & m_planeMax) +
(m_rowMax & m_colMin & m_planeMin) +
(m_rowMax & m_colMin & m_planeMax) +
(m_rowMax & m_colMax & m_planeMin) +
(m_rowMax & m_colMax & m_planeMax)) * CACHE_COHERENCE_PAD_REAL ;
this->commDataSend = new Real_t[comBufSize] ;
this->commDataRecv = new Real_t[comBufSize] ;
// pin buffers
cudaHostRegister(this->commDataSend, comBufSize*sizeof(Real_t), 0);
cudaHostRegister(this->commDataRecv, comBufSize*sizeof(Real_t), 0);
// prevent floating point exceptions
memset(this->commDataSend, 0, comBufSize*sizeof(Real_t)) ;
memset(this->commDataRecv, 0, comBufSize*sizeof(Real_t)) ;
// allocate shadow GPU buffers
cudaMalloc(&this->d_commDataSend, comBufSize*sizeof(Real_t));
cudaMalloc(&this->d_commDataRecv, comBufSize*sizeof(Real_t));
// prevent floating point exceptions
cudaMemset(this->d_commDataSend, 0, comBufSize*sizeof(Real_t));
cudaMemset(this->d_commDataRecv, 0, comBufSize*sizeof(Real_t));
#endif
}
void SetupConnectivityBC(Domain *domain, int edgeElems)
{
int domElems = domain->numElem;
Vector_h<Index_t> lxim_h(domElems);
Vector_h<Index_t> lxip_h(domElems);
Vector_h<Index_t> letam_h(domElems);
Vector_h<Index_t> letap_h(domElems);
Vector_h<Index_t> lzetam_h(domElems);
Vector_h<Index_t> lzetap_h(domElems);
/* set up elemement connectivity information */
lxim_h[0] = 0 ;
for (Index_t i=1; i<domElems; ++i) {
lxim_h[i] = i-1 ;
lxip_h[i-1] = i ;
}
lxip_h[domElems-1] = domElems-1 ;
for (Index_t i=0; i<edgeElems; ++i) {
letam_h[i] = i ;
letap_h[domElems-edgeElems+i] = domElems-edgeElems+i ;
}
for (Index_t i=edgeElems; i<domElems; ++i) {
letam_h[i] = i-edgeElems ;
letap_h[i-edgeElems] = i ;
}
for (Index_t i=0; i<edgeElems*edgeElems; ++i) {
lzetam_h[i] = i ;
lzetap_h[domElems-edgeElems*edgeElems+i] = domElems-edgeElems*edgeElems+i ;
}
for (Index_t i=edgeElems*edgeElems; i<domElems; ++i) {
lzetam_h[i] = i - edgeElems*edgeElems ;
lzetap_h[i-edgeElems*edgeElems] = i ;
}
/* set up boundary condition information */
Vector_h<Index_t> elemBC_h(domElems);
for (Index_t i=0; i<domElems; ++i) {
elemBC_h[i] = 0 ; /* clear BCs by default */
}
Index_t ghostIdx[6] ; // offsets to ghost locations
for (Index_t i=0; i<6; ++i) {
ghostIdx[i] = INT_MIN ;
}
Int_t pidx = domElems ;
if (domain->m_planeMin != 0) {
ghostIdx[0] = pidx ;
pidx += domain->sizeX*domain->sizeY ;
}
if (domain->m_planeMax != 0) {
ghostIdx[1] = pidx ;
pidx += domain->sizeX*domain->sizeY ;
}
if (domain->m_rowMin != 0) {
ghostIdx[2] = pidx ;
pidx += domain->sizeX*domain->sizeZ ;
}
if (domain->m_rowMax != 0) {
ghostIdx[3] = pidx ;
pidx += domain->sizeX*domain->sizeZ ;
}
if (domain->m_colMin != 0) {
ghostIdx[4] = pidx ;
pidx += domain->sizeY*domain->sizeZ ;
}
if (domain->m_colMax != 0) {
ghostIdx[5] = pidx ;
}
/* symmetry plane or free surface BCs */
for (Index_t i=0; i<edgeElems; ++i) {
Index_t planeInc = i*edgeElems*edgeElems ;
Index_t rowInc = i*edgeElems ;
for (Index_t j=0; j<edgeElems; ++j) {
if (domain->m_planeLoc == 0) {
elemBC_h[rowInc+j] |= ZETA_M_SYMM ;
}
else {
elemBC_h[rowInc+j] |= ZETA_M_COMM ;
lzetam_h[rowInc+j] = ghostIdx[0] + rowInc + j ;
}
if (domain->m_planeLoc == domain->m_tp-1) {
elemBC_h[rowInc+j+domElems-edgeElems*edgeElems] |=
ZETA_P_FREE;
}
else {
elemBC_h[rowInc+j+domElems-edgeElems*edgeElems] |=
ZETA_P_COMM ;
lzetap_h[rowInc+j+domElems-edgeElems*edgeElems] =
ghostIdx[1] + rowInc + j ;
}
if (domain->m_rowLoc == 0) {
elemBC_h[planeInc+j] |= ETA_M_SYMM ;
}
else {
elemBC_h[planeInc+j] |= ETA_M_COMM ;
letam_h[planeInc+j] = ghostIdx[2] + rowInc + j ;
}
if (domain->m_rowLoc == domain->m_tp-1) {
elemBC_h[planeInc+j+edgeElems*edgeElems-edgeElems] |=
ETA_P_FREE ;
}
else {
elemBC_h[planeInc+j+edgeElems*edgeElems-edgeElems] |=
ETA_P_COMM ;
letap_h[planeInc+j+edgeElems*edgeElems-edgeElems] =
ghostIdx[3] + rowInc + j ;
}
if (domain->m_colLoc == 0) {
elemBC_h[planeInc+j*edgeElems] |= XI_M_SYMM ;
}
else {
elemBC_h[planeInc+j*edgeElems] |= XI_M_COMM ;
lxim_h[planeInc+j*edgeElems] = ghostIdx[4] + rowInc + j ;
}
if (domain->m_colLoc == domain->m_tp-1) {
elemBC_h[planeInc+j*edgeElems+edgeElems-1] |= XI_P_FREE ;
}
else {
elemBC_h[planeInc+j*edgeElems+edgeElems-1] |= XI_P_COMM ;
lxip_h[planeInc+j*edgeElems+edgeElems-1] =
ghostIdx[5] + rowInc + j ;
}
}
}
domain->elemBC = elemBC_h;
domain->lxim = lxim_h;
domain->lxip = lxip_h;
domain->letam = letam_h;
domain->letap = letap_h;
domain->lzetam = lzetam_h;
domain->lzetap = lzetap_h;
}
void Domain::BuildMesh(Int_t nx, Int_t edgeNodes, Int_t edgeElems, Int_t domNodes, Int_t padded_domElems, Vector_h<Real_t> &x_h, Vector_h<Real_t> &y_h, Vector_h<Real_t> &z_h, Vector_h<Int_t> &nodelist_h)
{
Index_t meshEdgeElems = m_tp*nx ;
x_h.resize(domNodes);
y_h.resize(domNodes);
z_h.resize(domNodes);
// initialize nodal coordinates
Index_t nidx = 0 ;
Real_t tz = Real_t(1.125)*Real_t(m_planeLoc*nx)/Real_t(meshEdgeElems) ;
for (Index_t plane=0; plane<edgeNodes; ++plane) {
Real_t ty = Real_t(1.125)*Real_t(m_rowLoc*nx)/Real_t(meshEdgeElems) ;
for (Index_t row=0; row<edgeNodes; ++row) {
Real_t tx = Real_t(1.125)*Real_t(m_colLoc*nx)/Real_t(meshEdgeElems) ;
for (Index_t col=0; col<edgeNodes; ++col) {
x_h[nidx] = tx ;
y_h[nidx] = ty ;
z_h[nidx] = tz ;
++nidx ;
// tx += ds ; // may accumulate roundoff...
tx = Real_t(1.125)*Real_t(m_colLoc*nx+col+1)/Real_t(meshEdgeElems) ;
}
// ty += ds ; // may accumulate roundoff...
ty = Real_t(1.125)*Real_t(m_rowLoc*nx+row+1)/Real_t(meshEdgeElems) ;
}
// tz += ds ; // may accumulate roundoff...
tz = Real_t(1.125)*Real_t(m_planeLoc*nx+plane+1)/Real_t(meshEdgeElems) ;
}
x = x_h;
y = y_h;
z = z_h;
nodelist_h.resize(padded_domElems*8);
// embed hexehedral elements in nodal point lattice
Index_t zidx = 0 ;
nidx = 0 ;
for (Index_t plane=0; plane<edgeElems; ++plane) {
for (Index_t row=0; row<edgeElems; ++row) {
for (Index_t col=0; col<edgeElems; ++col) {
nodelist_h[0*padded_domElems+zidx] = nidx ;
nodelist_h[1*padded_domElems+zidx] = nidx + 1 ;
nodelist_h[2*padded_domElems+zidx] = nidx + edgeNodes + 1 ;
nodelist_h[3*padded_domElems+zidx] = nidx + edgeNodes ;
nodelist_h[4*padded_domElems+zidx] = nidx + edgeNodes*edgeNodes ;
nodelist_h[5*padded_domElems+zidx] = nidx + edgeNodes*edgeNodes + 1 ;
nodelist_h[6*padded_domElems+zidx] = nidx + edgeNodes*edgeNodes + edgeNodes + 1 ;
nodelist_h[7*padded_domElems+zidx] = nidx + edgeNodes*edgeNodes + edgeNodes ;
++zidx ;
++nidx ;
}
++nidx ;
}
nidx += edgeNodes ;
}
nodelist = nodelist_h;
}
Domain *NewDomain(char* argv[], Int_t numRanks, Index_t colLoc,
Index_t rowLoc, Index_t planeLoc,
Index_t nx, int tp, bool structured, Int_t nr, Int_t balance, Int_t cost)
{
Domain *domain = new Domain ;
domain->max_streams = 32;
domain->streams.resize(domain->max_streams);
for (Int_t i=0;i<domain->max_streams;i++)
cudaStreamCreate(&(domain->streams[i]));
cudaEventCreateWithFlags(&domain->time_constraint_computed,cudaEventDisableTiming);
Index_t domElems;
Index_t domNodes;
Index_t padded_domElems;
Vector_h<Index_t> nodelist_h;
Vector_h<Real_t> x_h;
Vector_h<Real_t> y_h;
Vector_h<Real_t> z_h;
if (structured)
{
domain->m_tp = tp ;
domain->m_numRanks = numRanks ;
domain->m_colLoc = colLoc ;
domain->m_rowLoc = rowLoc ;
domain->m_planeLoc = planeLoc ;
Index_t edgeElems = nx ;
Index_t edgeNodes = edgeElems+1 ;
domain->sizeX = edgeElems ;
domain->sizeY = edgeElems ;
domain->sizeZ = edgeElems ;
domain->numElem = domain->sizeX*domain->sizeY*domain->sizeZ ;
domain->padded_numElem = PAD(domain->numElem,32);
domain->numNode = (domain->sizeX+1)*(domain->sizeY+1)*(domain->sizeZ+1) ;
domain->padded_numNode = PAD(domain->numNode,32);
domElems = domain->numElem ;
domNodes = domain->numNode ;
padded_domElems = domain->padded_numElem ;
AllocateElemPersistent(domain,domElems,padded_domElems);
AllocateNodalPersistent(domain,domNodes);
domain->SetupCommBuffers(edgeNodes);
InitializeFields(domain);
domain->BuildMesh(nx, edgeNodes, edgeElems, domNodes, padded_domElems, x_h, y_h, z_h, nodelist_h);
domain->numSymmX = domain->numSymmY = domain->numSymmZ = 0;
if (domain->m_colLoc == 0)
domain->numSymmX = (edgeElems+1)*(edgeElems+1) ;
if (domain->m_rowLoc == 0)
domain->numSymmY = (edgeElems+1)*(edgeElems+1) ;
if (domain->m_planeLoc == 0)
domain->numSymmZ = (edgeElems+1)*(edgeElems+1) ;
AllocateSymmX(domain,edgeNodes*edgeNodes);
AllocateSymmY(domain,edgeNodes*edgeNodes);
AllocateSymmZ(domain,edgeNodes*edgeNodes);
/* set up symmetry nodesets */
Vector_h<Index_t> symmX_h(domain->symmX.size());
Vector_h<Index_t> symmY_h(domain->symmY.size());
Vector_h<Index_t> symmZ_h(domain->symmZ.size());
Int_t nidx = 0 ;
for (Index_t i=0; i<edgeNodes; ++i) {
Index_t planeInc = i*edgeNodes*edgeNodes ;
Index_t rowInc = i*edgeNodes ;
for (Index_t j=0; j<edgeNodes; ++j) {
if (domain->m_planeLoc == 0) {
symmZ_h[nidx] = rowInc + j ;
}
if (domain->m_rowLoc == 0) {
symmY_h[nidx] = planeInc + j ;
}
if (domain->m_colLoc == 0) {
symmX_h[nidx] = planeInc + j*edgeNodes ;
}
++nidx ;
}
}
if (domain->m_planeLoc == 0)
domain->symmZ = symmZ_h;
if (domain->m_rowLoc == 0)
domain->symmY = symmY_h;
if (domain->m_colLoc == 0)
domain->symmX = symmX_h;
SetupConnectivityBC(domain, edgeElems);
}
else
{
FILE *fp;
int ee, en;
if ((fp = fopen(argv[2], "r")) == 0) {
printf("could not open file %s\n", argv[2]) ;
exit( LFileError ) ;
}
bool fsuccess;
fsuccess = fscanf(fp, "%d %d", &ee, &en) ;
domain->numElem = Index_t(ee);
domain->padded_numElem = PAD(domain->numElem,32);
domain->numNode = Index_t(en);
domain->padded_numNode = PAD(domain->numNode,32);
domElems = domain->numElem ;
domNodes = domain->numNode ;
padded_domElems = domain->padded_numElem ;
AllocateElemPersistent(domain,domElems,padded_domElems);
AllocateNodalPersistent(domain,domNodes);
InitializeFields(domain);
/* initialize nodal coordinates */
x_h.resize(domNodes);
y_h.resize(domNodes);
z_h.resize(domNodes);
for (Index_t i=0; i<domNodes; ++i) {
double px, py, pz ;
fsuccess = fscanf(fp, "%lf %lf %lf", &px, &py, &pz) ;
x_h[i] = Real_t(px) ;
y_h[i] = Real_t(py) ;
z_h[i] = Real_t(pz) ;
}
domain->x = x_h;
domain->y = y_h;
domain->z = z_h;
/* embed hexehedral elements in nodal point lattice */
nodelist_h.resize(padded_domElems*8);
for (Index_t zidx=0; zidx<domElems; ++zidx) {
for (Index_t ni=0; ni<Index_t(8); ++ni) {
int n ;
fsuccess = fscanf(fp, "%d", &n) ;
nodelist_h[ni*padded_domElems+zidx] = Index_t(n);
}
}
domain->nodelist = nodelist_h;
/* set up face-based element neighbors */
Vector_h<Index_t> lxim_h(domElems);
Vector_h<Index_t> lxip_h(domElems);
Vector_h<Index_t> letam_h(domElems);
Vector_h<Index_t> letap_h(domElems);
Vector_h<Index_t> lzetam_h(domElems);
Vector_h<Index_t> lzetap_h(domElems);
for (Index_t i=0; i<domElems; ++i) {
int xi_m, xi_p, eta_m, eta_p, zeta_m, zeta_p ;
fsuccess = fscanf(fp, "%d %d %d %d %d %d",
&xi_m, &xi_p, &eta_m, &eta_p, &zeta_m, &zeta_p) ;
lxim_h[i] = Index_t(xi_m) ;
lxip_h[i] = Index_t(xi_p) ;
letam_h[i] = Index_t(eta_m) ;
letap_h[i] = Index_t(eta_p) ;
lzetam_h[i] = Index_t(zeta_m) ;
lzetap_h[i] = Index_t(zeta_p) ;
}
domain->lxim = lxim_h;
domain->lxip = lxip_h;
domain->letam = letam_h;
domain->letap = letap_h;
domain->lzetam = lzetam_h;
domain->lzetap = lzetap_h;
/* set up X symmetry nodeset */
fsuccess = fscanf(fp, "%d", &domain->numSymmX) ;
Vector_h<Index_t> symmX_h(domain->numSymmX);
for (Index_t i=0; i<domain->numSymmX; ++i) {
int n ;
fsuccess = fscanf(fp, "%d", &n) ;
symmX_h[i] = Index_t(n) ;
}
domain->symmX = symmX_h;
fsuccess = fscanf(fp, "%d", &domain->numSymmY) ;
Vector_h<Index_t> symmY_h(domain->numSymmY);
for (Index_t i=0; i<domain->numSymmY; ++i) {
int n ;
fsuccess = fscanf(fp, "%d", &n) ;
symmY_h[i] = Index_t(n) ;
}
domain->symmY = symmY_h;
fsuccess = fscanf(fp, "%d", &domain->numSymmZ) ;
Vector_h<Index_t> symmZ_h(domain->numSymmZ);
for (Index_t i=0; i<domain->numSymmZ; ++i) {
int n ;
fsuccess = fscanf(fp, "%d", &n) ;
symmZ_h[i] = Index_t(n) ;
}
domain->symmZ = symmZ_h;
/* set up free surface nodeset */
Index_t numFreeSurf;
fsuccess = fscanf(fp, "%d", &numFreeSurf) ;
Vector_h<Index_t> freeSurf_h(numFreeSurf);
for (Index_t i=0; i<numFreeSurf; ++i) {
int n ;
fsuccess = fscanf(fp, "%d", &n) ;
freeSurf_h[i] = Index_t(n) ;
}
printf("%c\n",fsuccess);//nothing
fclose(fp);
/* set up boundary condition information */
Vector_h<Index_t> elemBC_h(domElems);
Vector_h<Index_t> surfaceNode_h(domNodes);
for (Index_t i=0; i<domain->numElem; ++i) {
elemBC_h[i] = 0 ;
}
for (Index_t i=0; i<domain->numNode; ++i) {
surfaceNode_h[i] = 0 ;
}
for (Index_t i=0; i<domain->numSymmX; ++i) {
surfaceNode_h[symmX_h[i]] = 1 ;
}
for (Index_t i=0; i<domain->numSymmY; ++i) {
surfaceNode_h[symmY_h[i]] = 1 ;
}
for (Index_t i=0; i<domain->numSymmZ; ++i) {
surfaceNode_h[symmZ_h[i]] = 1 ;
}
for (Index_t zidx=0; zidx<domain->numElem; ++zidx) {
Int_t mask = 0 ;
for (Index_t ni=0; ni<8; ++ni) {
mask |= (surfaceNode_h[nodelist_h[ni*domain->padded_numElem+zidx]] << ni) ;
}
if ((mask & 0x0f) == 0x0f) elemBC_h[zidx] |= ZETA_M_SYMM ;
if ((mask & 0xf0) == 0xf0) elemBC_h[zidx] |= ZETA_P_SYMM ;
if ((mask & 0x33) == 0x33) elemBC_h[zidx] |= ETA_M_SYMM ;
if ((mask & 0xcc) == 0xcc) elemBC_h[zidx] |= ETA_P_SYMM ;
if ((mask & 0x99) == 0x99) elemBC_h[zidx] |= XI_M_SYMM ;
if ((mask & 0x66) == 0x66) elemBC_h[zidx] |= XI_P_SYMM ;
}
for (Index_t zidx=0; zidx<domain->numElem; ++zidx) {
if (elemBC_h[zidx] == (XI_M_SYMM | ETA_M_SYMM | ZETA_M_SYMM)) {
domain->octantCorner = zidx ;
break ;
}
}
for (Index_t i=0; i<domain->numNode; ++i) {
surfaceNode_h[i] = 0 ;
}
for (Index_t i=0; i<numFreeSurf; ++i) {
surfaceNode_h[freeSurf_h[i]] = 1 ;
}
for (Index_t zidx=0; zidx<domain->numElem; ++zidx) {
Int_t mask = 0 ;