Skip to content

Commit 989984c

Browse files
committed
FEAT: message passing on multiple grids + iDir = 4
1 parent fc39fa0 commit 989984c

File tree

1 file changed

+103
-24
lines changed

1 file changed

+103
-24
lines changed

src/exchange_messages.cpp

Lines changed: 103 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ bool Ions::exchange_old(Grid &grid) {
105105
// 1 - top
106106
// 2 - left
107107
// 3 - bottom
108+
// 4 - vertical (k direction) for closed field lines
108109
//
109110
// cells (assume gc = 2):
110111
// 0 1 | 2 3 4 ... n-gc*2 n-gc-1 | n-gc n-1
@@ -124,6 +125,7 @@ bool pack_border(const arma_cube &value,
124125

125126
int64_t iXstart, iXend;
126127
int64_t iYstart, iYend;
128+
int64_t iZstart, iZend;
127129

128130
// ----------------------------
129131
// left / right message passing
@@ -159,8 +161,22 @@ bool pack_border(const arma_cube &value,
159161
}
160162
}
161163

164+
// ----------------------------
165+
// k-dir (only top)
166+
if (iDir == 4) {
167+
iXstart = nG;
168+
iXend = nX - nG;
169+
iYstart = nG;
170+
iYend = nY - nG;
171+
iZstart = nZ - nG;
172+
iZend = nZ;
173+
} else {
174+
iZstart = nG;
175+
iZend = nZ - nG;
176+
}
177+
162178
try {
163-
for (int64_t iZ = nG; iZ < nZ - nG; iZ++) {
179+
for (int64_t iZ = iZstart; iZ < iZend; iZ++) {
164180
for (int64_t iY = iYstart; iY < iYend; iY++) {
165181
for (int64_t iX = iXstart; iX < iXend; iX++) {
166182
packed[*iCounter] = value(iX, iY, iZ);
@@ -186,6 +202,7 @@ bool pack_border(const arma_cube &value,
186202
// 1 - top
187203
// 2 - left
188204
// 3 - bottom
205+
// 4 - k-dir top
189206
// DoReverseX and DoReverseY are because packing always happens from
190207
// lower left to upper right, while face we are unpacking too may
191208
// have a different (left - right and up - down) geometry
@@ -210,7 +227,8 @@ bool unpack_border(arma_cube &value,
210227

211228
int64_t iXstart, iXend;
212229
int64_t iYstart, iYend;
213-
int64_t xInc = 1, yInc = 1;
230+
int64_t iZstart, iZend;
231+
int64_t xInc = 1, yInc = 1, zInc = 1;
214232

215233
int64_t iXOff = 0;
216234
int64_t nCx = nX - 2 * nG;
@@ -261,10 +279,26 @@ bool unpack_border(arma_cube &value,
261279
}
262280
}
263281

282+
if (iDir == 4) {
283+
iXstart = nG;
284+
iXend = nX - nG;
285+
iYstart = nG;
286+
iYend = nY - nG;
287+
// need to reverse direction, since packing is from the bottom up,
288+
// which means unpacking should be from the top down
289+
iZend = nZ - nG;
290+
iZstart = nZ;
291+
zInc = -1;
292+
} else {
293+
iZstart = nG;
294+
iZend = nZ - nG;
295+
}
296+
264297
try {
265298
int64_t iXp, iYp;
266299

267-
for (int64_t iZ = nG; iZ < nZ - nG; iZ++) {
300+
for (int64_t iZ = iZstart; iZ < iZend; iZ += zInc) {
301+
268302
if (XbecomesY) {
269303
for (int64_t iX = iXstart; iX < iXend; iX += xInc) {
270304
iXp = iX;
@@ -311,7 +345,6 @@ bool unpack_border(arma_cube &value,
311345
} catch (...) {
312346
DidWork = false;
313347
}
314-
315348
return DidWork;
316349
}
317350

@@ -440,6 +473,12 @@ bool Grid::send_one_var_one_face(int64_t iFace) {
440473

441474
bool DidWork = true;
442475

476+
if (report.test_verbose(4))
477+
std::cout << "in send_one_var_one_face : " << iFace << " from: " <<
478+
iProc << " to: " <<
479+
interchangesOneVar[iFace].iProc_to << " tag: " <<
480+
interchangesOneVar[iFace].iTag << "\n";
481+
443482
MPI_Isend(interchangesOneVar[iFace].buffer,
444483
interchangesOneVar[iFace].iSizeTotal,
445484
MPI_BYTE,
@@ -480,6 +519,13 @@ bool Grid::receive_one_var_one_face(int64_t iFace) {
480519

481520
bool DidWork = true;
482521

522+
if (report.test_verbose(4))
523+
std::cout << "in receive_one_var_one_face : " << iFace << " from: " <<
524+
iProc << " to: " <<
525+
interchangesOneVar[iFace].iProc_to << " tag: " <<
526+
interchangesOneVar[iFace].iTag << "\n";
527+
528+
483529
MPI_Recv(interchangesOneVar[iFace].rbuffer,
484530
interchangesOneVar[iFace].iSizeTotal,
485531
MPI_BYTE,
@@ -508,23 +554,32 @@ Grid::messages_struct Grid::make_new_interconnection(int64_t iDir,
508554

509555
int64_t nPtsX = nGCs * (nY - nGCs * 2) * (nZ - nGCs * 2);
510556
int64_t nPtsY = nGCs * (nX - nGCs * 2) * (nZ - nGCs * 2);
557+
int64_t nPtsZ = nGCs * (nX - nGCs * 2) * (nY - nGCs * 2);
511558

512559
new_inter.iFace = iDir;
513560
new_inter.DoReverseX = DoReverseX;
514561
new_inter.DoReverseY = DoReverseY;
515562
new_inter.IsPole = IsPole;
516563
new_inter.XbecomesY = XbecomesY;
517564

565+
// Along i axis (left or right):
518566
if (iDir == 0 || iDir == 2) {
519567
new_inter.iSizeTotal = nVars * nPtsX * sizeof(precision_t);
520568
new_inter.index.set_size(nGCs, nY);
521569
new_inter.ratio.set_size(nGCs, nY);
522-
} else {
570+
}
571+
572+
// Along j axis (up or down):
573+
if (iDir == 1 || iDir == 3) {
523574
new_inter.iSizeTotal = nVars * nPtsY * sizeof(precision_t);
524575
new_inter.index.set_size(nGCs, nX);
525576
new_inter.ratio.set_size(nGCs, nX);
526577
}
527578

579+
// Along K axis (up only for now):
580+
if (iDir == 4)
581+
new_inter.iSizeTotal = nVars * nPtsZ * sizeof(precision_t);
582+
528583
new_inter.buffer = static_cast<precision_t*>(malloc(new_inter.iSizeTotal));
529584
new_inter.rbuffer = static_cast<precision_t*>(malloc(new_inter.iSizeTotal));
530585

@@ -538,7 +593,6 @@ Grid::messages_struct Grid::make_new_interconnection(int64_t iDir,
538593
return new_inter;
539594
}
540595

541-
542596
/*
543597
// -----------------------------------------------------------------------------
544598
// Exchange messages for the NEUTRALS:
@@ -772,6 +826,15 @@ bool Neutrals::exchange_really_old(Grid &grid) {
772826

773827
// -----------------------------------------------------------------------------
774828
// Initialize interfaces between horizontal sides on a grid
829+
// Directions:
830+
// 0 = + i (right)
831+
// 1 = + j (up)
832+
// 2 = - i (left)
833+
// 3 = - j (down)
834+
// 4 = + k (vertical) - only along closed dipole field lines
835+
// For the cubesphere grid:
836+
// iRoot = 4 is the south polar region
837+
// iRoot = 5 is the north polar region
775838
// -----------------------------------------------------------------------------
776839

777840
bool exchange_sides_init(Grid &grid, int64_t nVarsToPass) {
@@ -911,6 +974,24 @@ bool exchange_sides_init(Grid &grid, int64_t nVarsToPass) {
911974
ReverseY,
912975
XbecomesY));
913976

977+
if (grid.get_IsDipole() && grid.get_IsClosed()) {
978+
// This operates in the N/S (j or Y) direction:
979+
ReverseX = false;
980+
ReverseY = false;
981+
IsPole = false;
982+
XbecomesY = false;
983+
grid.interchangesOneVar.push_back(
984+
grid.make_new_interconnection(4,
985+
nVarsToPass,
986+
grid.iProcZ,
987+
grid.edge_Z,
988+
IsPole,
989+
ReverseX,
990+
ReverseY,
991+
XbecomesY));
992+
993+
}
994+
914995
report.exit(function);
915996
return DidWork;
916997
}
@@ -942,30 +1023,31 @@ bool exchange_one_var(Grid &grid,
9421023

9431024
bool DidWork = true;
9441025

945-
int iTag, iDir;
1026+
int iTag, iDir, nDir;
9461027
int iSpecies;
9471028

948-
static int64_t iX, nX = grid.get_nX();
949-
static int64_t iY, nY = grid.get_nY();
950-
static int64_t iZ, nZ = grid.get_nZ();
1029+
static int64_t nX = grid.get_nX();
1030+
static int64_t nY = grid.get_nY();
1031+
static int64_t nZ = grid.get_nZ();
9511032
static int64_t nG = grid.get_nGCs();
952-
static int64_t nPtsX = nG * (nY - nG * 2) * (nZ - nG * 2);
953-
static int64_t nPtsY = nG * (nX - nG * 2) * (nZ - nG * 2);
954-
static bool IsFirstTime = true;
9551033
static arma_cube var_scgc;
9561034

9571035
static int64_t nVarsToPass = 1;
9581036

959-
if (IsFirstTime) {
1037+
if (!grid.isExchangeInitialized) {
9601038
DidWork = exchange_sides_init(grid, nVarsToPass);
961-
var_scgc.set_size(nX, nY, nX);
962-
IsFirstTime = false;
1039+
grid.isExchangeInitialized = true;
9631040
}
1041+
var_scgc.set_size(nX, nY, nZ);
9641042

9651043
int64_t iP;
9661044
precision_t oneSign = 1.0;
9671045

968-
for (int iDir = 0; iDir < 4; iDir++) {
1046+
nDir = 4;
1047+
if (grid.get_IsDipole() && grid.get_IsClosed())
1048+
nDir++;
1049+
1050+
for (int iDir = 0; iDir < nDir; iDir++) {
9691051
if (report.test_verbose(4))
9701052
std::cout << "packing one var : " << iDir << " " << iProc
9711053
<< " " << grid.interchangesOneVar[iDir].iProc_to
@@ -991,29 +1073,29 @@ bool exchange_one_var(Grid &grid,
9911073
}
9921074

9931075
// Send all faces asynchronously:
994-
for (int iDir = 0; iDir < 4; iDir++) {
1076+
for (int iDir = 0; iDir < nDir; iDir++) {
9951077
if (grid.interchangesOneVar[iDir].iProc_to >= 0) {
9961078
report.print(4, "Sending one face");
9971079
DidWork = grid.send_one_var_one_face(iDir);
9981080
}
9991081
}
10001082

10011083
// Receive all faces asynchronously:
1002-
for (int iDir = 0; iDir < 4; iDir++) {
1084+
for (int iDir = 0; iDir < nDir; iDir++) {
10031085
if (grid.interchangesOneVar[iDir].iProc_to >= 0) {
10041086
report.print(4, "Receiving one face");
10051087
DidWork = grid.receive_one_var_one_face(iDir);
10061088
}
10071089
}
10081090

10091091
// Wait for messages to get there:
1010-
for (int iDir = 0; iDir < 4; iDir++) {
1092+
for (int iDir = 0; iDir < nDir; iDir++) {
10111093
if (grid.interchangesOneVar[iDir].iProc_to >= 0)
10121094
MPI_Wait(&grid.interchangesOneVar[iDir].requests, MPI_STATUS_IGNORE);
10131095
}
10141096

10151097
// Unpack all faces:
1016-
for (int iDir = 0; iDir < 4; iDir++) {
1098+
for (int iDir = 0; iDir < nDir; iDir++) {
10171099
if (grid.interchangesOneVar[iDir].iProc_to >= 0) {
10181100
iP = 0;
10191101
report.print(4, "Unpacking Border");
@@ -1029,9 +1111,6 @@ bool exchange_one_var(Grid &grid,
10291111
}
10301112
}
10311113

1032-
// Wait for all processors to be done.
1033-
MPI_Barrier(aether_comm);
1034-
10351114
// If this is a cubesphere grid, interpolate ghostcells to their proper location
10361115
//if (grid.IsCubeSphereGrid & grid.gcInterpolationSet) {
10371116
// report.print(3, "Interpolating Ghostcells to Proper Location");

0 commit comments

Comments
 (0)