-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpargrid.lyx
1162 lines (815 loc) · 20.9 KB
/
pargrid.lyx
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
#LyX 1.6.5 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass article
\use_default_options true
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Title
ParGrid Basics
\end_layout
\begin_layout Section
Parallel Mesh
\end_layout
\begin_layout Standard
Computational meshes are often Cartesian rectangular cuboids (
\begin_inset Quotes eld
\end_inset
shoeboxes
\begin_inset Quotes erd
\end_inset
), although this needs not be the case.
In parallel simulations the mesh (simulation domain) is somehow partitioned
(decomposed) amongst
\begin_inset Formula $N$
\end_inset
processes.
Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
shows an example of mesh partitioning in two-dimensional case.
\end_layout
\begin_layout Standard
\begin_inset Float figure
placement h
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename partitioning.png
width 90text%
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Left) Example of mesh partitioning to four processes (colours).
Right) Processes need to allocate buffer cells (uncoloured) for cells stored
on other processes.
\begin_inset CommandInset label
LatexCommand label
name "fig:partitioning"
\end_inset
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
In parallel simulations it is perhaps easiest to think of cells that are
connected to other cells
\begin_inset Foot
status open
\begin_layout Plain Layout
Note:
\begin_inset Quotes eld
\end_inset
cell
\begin_inset Quotes erd
\end_inset
here should be understood as the smallest unit of parallelization.
Parallel cell may actually represent a small Cartesian patch/block of cells.
\end_layout
\end_inset
instead of a mesh.
For example, in Cartesian mesh each cell is connected to eight surrounding
cells in two-dimensional case, and 26 cells in three-dimensional case (Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:cell-connectivity"
\end_inset
).
Some of the neighbouring cells are local to the process, the rest are remote
neighbours hosted on other process(es) (uncoloured cells in Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
).
Some of the neighbours may not exists -- the cells at the boundary of the
simulation domain have one or more missing neighbours.
Cells with at least one missing neighbour are often flagged as
\begin_inset Quotes eld
\end_inset
ghost cells
\begin_inset Quotes erd
\end_inset
(Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
).
\end_layout
\begin_layout Standard
In ParGrid parlance ghost cells are called
\series bold
exterior cells
\series default
, while cells whose neighbours exist are called
\series bold
interior cells
\series default
.
Each process has zero or more
\series bold
local cells
\series default
(coloured cells in Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
), and keeps a copy of necessary amount of
\series bold
remote cells
\series default
(uncoloured cells in Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
).
Each process considers processes that host one or more remote neighbours
as their
\series bold
neighbouring processes
\series default
.
For example, in Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:partitioning"
\end_inset
red and orange processes consider blue and green processes as their neighbourin
g processes.
Red and orange process are not neighbours.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\align center
\begin_inset Tabular
<lyxtabular version="3" rows="1" columns="2">
<features>
<column alignment="center" valignment="top" width="0">
<column alignment="center" valignment="top" width="0">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename cell_connectivity.png
width 20text%
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Graphics
filename fdm_example.png
width 20text%
\end_inset
\end_layout
\end_inset
</cell>
</row>
</lyxtabular>
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Left) Each cell is connected to eight (26) cells in two(three)-dimensional
case.
Some of the neighbours may not exist.
Right) In finite difference advection equation data on marked (face)neighbours
are needed to propagate the gray cell.
The required cell data defines the transfer stencil of this particular
algorithm.
\begin_inset CommandInset label
LatexCommand label
name "fig:cell-connectivity"
\end_inset
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Cells are identified by unique
\series bold
global ID
\series default
numbers, i.e.
all processes agree on these numbers.
For example, in a regular Cartesian grid cells are identified by their
\begin_inset Formula $(i,j,k)$
\end_inset
indices.
Each
\begin_inset Formula $(i,j,k)$
\end_inset
tuple corresponds to a unique value (index into an array
\begin_inset Foot
status open
\begin_layout Plain Layout
In C/C++ index = k*ysize*xsize+j*xsize+i.
\end_layout
\end_inset
), which can be used as global IDs.
In ParGrid processes are only aware of their local cells, and of local
cells' neighbours, some of which reside on other processes.
Each process stores the cells (and associated data) in regular arrays that
are accessed with
\series bold
local IDs
\series default
; local IDs are simply indices into arrays.
There is also a mapping between local and global IDs.
Usually one only needs to care about the local IDs.
\end_layout
\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open
\begin_layout Plain Layout
\align center
\begin_inset Graphics
filename neighbours.png
width 3cm
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
In ParGrid the neighbour type IDs are indices into a
\begin_inset Formula $3\times3\times3$
\end_inset
block of cells, centered at the considered cell (drawn in black).
Type ID is calculated as
\begin_inset Formula $\mathrm{k}\cdot9+\mathrm{j}\cdot3+\mathrm{i}$
\end_inset
, where (i,j,k) are the indices starting from the bottom lower left corner.
The considered cell has indices (1,1,1), corresponding to neighbour type
ID 13.
\begin_inset CommandInset label
LatexCommand label
name "fig:neighbour type IDs"
\end_inset
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
In order to correctly identify neighbour cells each cell has a neighbour
list, which is an array of size 27
\begin_inset Foot
status open
\begin_layout Plain Layout
The size of neighbour list is defined in
\family typewriter
\size scriptsize
pargrid::N_neighbours
\size footnotesize
.
\end_layout
\end_inset
that lists the local IDs of its neighbours
\begin_inset Foot
status open
\begin_layout Plain Layout
ParGrid only stores cells' immediate neighbours.
If one needs data from more distant neighbours, then each (parallel) cell
must contain a patch/block of cells.
\end_layout
\end_inset
.
Neighbours' local IDs are stored in particular order that tells which of
the 27 cells the local ID refers to (see Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:neighbour type IDs"
\end_inset
)
\begin_inset Foot
status open
\begin_layout Plain Layout
A cell is also considered to be its own neighbour.
\end_layout
\end_inset
.
The indices into neighbour list can be computed with
\family typewriter
\size footnotesize
pargrid::calcNeighbourTypeID
\family default
\size default
method that, instead of (i,j,k) index of the neighbour, takes the offset
relative to the cell in question as parameters.
For example, -x neighbour has offset
\begin_inset Formula $(-1,0,0)$
\end_inset
, and +x,+y,+z neighbour has offset
\begin_inset Formula $(+1,+1,+1)$
\end_inset
.
A non-existing neighbour has local ID (and global ID) value equal to
\family typewriter
\size footnotesize
pargrid::invalidCellID()
\family default
\size default
.
Algorithm
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:accessing neighbours"
\end_inset
shows how neighbour local IDs are obtained with ParGrid.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
placement h
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\begin_inset listings
lstparams "basicstyle={\scriptsize\ttfamily}"
inline false
status open
\begin_layout Plain Layout
// Get neighbours of a cell with local ID localID
\end_layout
\begin_layout Plain Layout
const pargrid::CellID* const nbrs = pargrid.getCellNeighbourIDs(localID);
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Get local ID of (-1,0,0) neighbour
\end_layout
\begin_layout Plain Layout
pargrid::CellID nbrID = nbrs[pargrid.calcNeighbourTypeID(-1,0,0)];
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Example of how to access cell's neighbour(s).
\begin_inset CommandInset label
LatexCommand label
name "alg:accessing neighbours"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Before discussing data defined on cells let's consider a particular (bad)
implementation of two-dimensional advection equation
\begin_inset Formula $\partial_{t}f+\mathbf{V}\cdot\nabla f=0$
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Formula \begin{equation}
\frac{f_{i,j}^{n+1}-f_{i,j}^{n}}{\Delta t}=-V_{x}\frac{f_{i+1,j}^{n}-f_{i-1,j}^{n}}{2\Delta x}-V_{y}\frac{f_{i,j+1}^{n}-f_{i,j-1}^{n}}{2\Delta y}.\end{equation}
\end_inset
Here
\begin_inset Formula $n$
\end_inset
refers to the time step, and
\begin_inset Formula $i$
\end_inset
and
\begin_inset Formula $j$
\end_inset
are cell indices in a Cartesian mesh.
Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:cell-connectivity"
\end_inset
(right panel) illustrates the neighbour data that are needed to propagate
a cell forward in time.
In this example only the four out of eight neighbours are needed.
Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig:cell-connectivity"
\end_inset
(right panel) defines the
\series bold
stencil
\series default
of this algorithm -- in order to solve the gray cell correctly, the data
on the marked cells must be up-to-date, i.e.
in sync with neighbouring processes.
It is usually quite inefficient to sync data on all neighbours if only,
say, face neighbours are needed.
Algorithm
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:stencil-definition"
\end_inset
shows how one can define a new stencil with ParGrid
\begin_inset Foot
status open
\begin_layout Plain Layout
ParGrid always has one stencil with ID 0 (zero) that transfers all data.
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
placement h
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\begin_inset listings
lstparams "basicstyle={\scriptsize\ttfamily}"
inline false
status open
\begin_layout Plain Layout
// define cells whose data needs to be received
\end_layout
\begin_layout Plain Layout
vector<pargrid::NeighbourID> nbrTypeIDs;
\end_layout
\begin_layout Plain Layout
nbrTypeIDs.push_back(pargrid.calcNeighbourTypeID(-1,0,0));
\end_layout
\begin_layout Plain Layout
nbrTypeIDs.push_back(pargrid.calcNeighbourTypeID(+1,0,0));
\end_layout
\begin_layout Plain Layout
nbrTypeIDs.push_back(pargrid.calcNeighbourTypeID(0,-1,0));
\end_layout
\begin_layout Plain Layout
nbrTypeIDs.push_back(pargrid.calcNeighbourTypeID(0,+1,0));
\end_layout
\begin_layout Plain Layout
int stencilID =
\end_layout
\begin_layout Plain Layout
pargrid.addStencil(pargrid::localToRemoteUpdates,nbrTypeIDs);
\end_layout
\begin_layout Plain Layout
if (newStencilID < 0) cerr << "ERROR" << endl;
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Example of how to define a (transfer) stencil with ParGrid.
Here only data on x/y face neighbours is to be synchronized.
\begin_inset CommandInset label
LatexCommand label
name "alg:stencil-definition"
\end_inset
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
How to define cell data? In ParGrid there are two approaches to this, old
and new.
If possible one should use the (new) method described here.
Typically one need several data values per cell.
For example, in hydrodynamical simulations
\begin_inset Formula $(\rho,\rho v_{x},\rho v_{y},\rho v_{z},U)$
\end_inset
need to be stored, i.e.
five floating point values per cell.
Algorithm
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:user data definition"
\end_inset
shows an example how to define parallel data arrays with ParGrid.
Note that it is entirely up to the user how the data should be understood,
i.e.
is the data stored as cell average, or on particular cell node/face/edge.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\begin_inset listings
lstparams "basicstyle={\scriptsize\ttfamily}"
inline false
status open
\begin_layout Plain Layout
// Define a double data array that has five values per cell
\end_layout
\begin_layout Plain Layout
string name = "hydro";
\end_layout
\begin_layout Plain Layout
unsigned int userDataID = pargrid.addUserData<double>(name,5);
\end_layout
\begin_layout Plain Layout
if (userDataID == pargrid.invalidDataID())
\end_layout
\begin_layout Plain Layout
cerr << "ERROR" << endl;
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Two ways to access the user data
\end_layout
\begin_layout Plain Layout
char* ptr1 = pargrid.getUserData(userDataID);
\end_layout
\begin_layout Plain Layout
char* ptr2 = pargrid.getUserData(name);
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Set values for data on cell with local ID localID
\end_layout
\begin_layout Plain Layout
double* data = reinterpret_cast<double*>(ptr1);
\end_layout
\begin_layout Plain Layout
data[localID*5+0] = rho;
\end_layout
\begin_layout Plain Layout
data[localID*5+1] = rhovx;
\end_layout
\begin_layout Plain Layout
data[localID*5+2] = rhovy;
\end_layout
\begin_layout Plain Layout
data[localID*5+3] = rhovz;
\end_layout
\begin_layout Plain Layout
data[localID*5+4] = energy;
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Example of how to define parallel data arrays with ParGrid.
The example also shows how to access the created arrays.
\begin_inset CommandInset label
LatexCommand label
name "alg:user data definition"
\end_inset
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Definition of stencil or data arrays is not enough to make the magic happen.
One also needs to tell ParGrid which stencil(s) are used to transfer the
data array(s)
\begin_inset Foot
status open
\begin_layout Plain Layout
It is also possible to sync the same data array using several different
stencils.
\end_layout
\end_inset
.
This is done by adding
\series bold
transfers
\series default
to ParGrid.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\begin_inset listings
lstparams "basicstyle={\scriptsize\ttfamily}"
inline false
status open
\begin_layout Plain Layout
unsigned int transferID = 3;
\end_layout
\begin_layout Plain Layout
bool result
\end_layout
\begin_layout Plain Layout
= pargrid.addUserDataTransfer(userDataID,stencilID,transferID,false);
\end_layout
\begin_layout Plain Layout
if (result == false) cerr << "ERROR" << endl;
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Example of how to connect a user-defined parallel data array to a stencil.
\begin_inset CommandInset label
LatexCommand label
name "alg:transfer"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Cells whose every neighbour in the stencil are local are called
\series bold
inner cells
\series default
.
Cells that have remote neighbours in the stencil are called
\series bold
boundary cells
\series default
\begin_inset Foot
status open
\begin_layout Plain Layout
Unfortunately English language has quite limited vocabulary in this matter.
\end_layout
\end_inset
.
Boundary cells cannot be propagated until the data on (local copies of)
remote cells has been synchronized with neighbouring processes.
Typically one first starts the neighbour data sync, propagates inner cells
while waiting for sync to complete, and finally propagates boundary cells.
Algorithm
\begin_inset CommandInset ref
LatexCommand ref
reference "alg:propagation loop"
\end_inset
shows how this is done with ParGrid.
\end_layout
\begin_layout Standard
\begin_inset Float algorithm
placement h
wide false
sideways false