Skip to content

Commit 243b38d

Browse files
committed
FEAT: get grid to grid message passing to work
1 parent a10c794 commit 243b38d

File tree

1 file changed

+145
-19
lines changed

1 file changed

+145
-19
lines changed

src/grid_match.cpp

Lines changed: 145 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,27 @@ bool exchange_information(int64_t *nPointsToPass,
6262
return true;
6363
}
6464

65-
bool grid_match(Grid gGrid,
66-
Grid mGrid,
65+
// -----------------------------------------------------------------------------
66+
// This function:
67+
// on the requesting information side:
68+
// - figures out which processor each point of the other grid is on
69+
// - counts the points for each processor
70+
// - exchanges how many points to pass for each processor
71+
// - makes lists of coordinates to send to each processor
72+
// - sends those lists
73+
// on the interpolator side:
74+
// - builds interpolators for the requested information
75+
// -----------------------------------------------------------------------------
76+
77+
bool grid_match(Grid &gGrid,
78+
Grid &mGrid,
6779
Quadtree gQuadtree,
6880
Quadtree mQuadtree) {
6981

82+
std::string function = "grid_match";
83+
static int iFunction = -1;
84+
report.enter(function, iFunction);
85+
7086
// Let's do magnetic to geographic first:
7187

7288
int64_t iX, mnX = mGrid.get_nX();
@@ -86,6 +102,7 @@ bool grid_match(Grid gGrid,
86102

87103
// This is not the most efficient way to do this, but the first pass, let's
88104
// just count how many points we need to send to the other processors:
105+
mGrid.gridToGridMap.set_size(mnX, mnY, mnZ);
89106
for (iX = mGCs; iX < mnX - mGCs; iX++) {
90107
for (iY = mGCs; iY < mnY - mGCs; iY++) {
91108
for (iZ = mGCs; iZ < mnZ - mGCs; iZ++) {
@@ -103,6 +120,7 @@ bool grid_match(Grid gGrid,
103120
if (jNode < 0 || jNode >= nGrids) {
104121
std::cout << "out of bounds!!! " << jNode << "\n";
105122
}
123+
mGrid.gridToGridMap(iX, iY, iZ) = jNode;
106124
nPointsToPass[jNode] = nPointsToPass[jNode]+1;
107125
/* std::cout << "lon, lat, node: " << lon*cRtoD << " "
108126
<< lat*cRtoD << " "
@@ -115,13 +133,13 @@ bool grid_match(Grid gGrid,
115133
}
116134
}
117135
}
118-
std::cout << "made it here: " << iProc << "\n";
119136
MPI_Barrier(aether_comm);
120137

121-
for (jNode = 0; jNode < nGrids ; jNode++)
122-
std::cout << "nPtsToPass : " << iProc << " " << nPointsToPass[jNode] << "\n";
123-
124-
std::cout << "sending number of points :\n";
138+
if (report.test_verbose(3)) {
139+
for (jNode = 0; jNode < nGrids ; jNode++)
140+
std::cout << "nPtsToPass : " << iProc << " " << nPointsToPass[jNode] << "\n";
141+
std::cout << "sending number of points :\n";
142+
}
125143

126144
// This section sends the number of points that need to be transfered to each processor.
127145
// Then the processor saves the number of points, so it can be remembered, and both the
@@ -135,27 +153,23 @@ bool grid_match(Grid gGrid,
135153
nPointsToReceive[jNode] = nPointsDummy[iGrid];
136154
}
137155

138-
MPI_Barrier(aether_comm);
139-
140-
for (jNode = 0; jNode < nGrids ; jNode++) {
141-
std::cout << "nPtsToReceive : " << iProc << " " << jNode << " " << nPointsToReceive[jNode] << "\n";
142-
MPI_Barrier(aether_comm);
156+
if (report.test_verbose(3)) {
157+
for (jNode = 0; jNode < nGrids ; jNode++) {
158+
std::cout << "nPtsToReceive : " << iProc << " " << jNode << " " << nPointsToReceive[jNode] << "\n";
159+
}
143160
}
144161

145162
// Now we need to create an array of send points and an array of receive points.
146163
std::vector<precision_t *> latsToPass(nGrids);
147164
std::vector<precision_t *> lonsToPass(nGrids);
148165
std::vector<precision_t *> altsToPass(nGrids);
149-
for (jNode = 0; jNode < nGrids ; jNode++) {
150-
latsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
151-
lonsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
152-
altsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
153-
}
154-
155166
std::vector<precision_t *> latsToInterTo(nGrids);
156167
std::vector<precision_t *> lonsToInterTo(nGrids);
157168
std::vector<precision_t *> altsToInterTo(nGrids);
158169
for (jNode = 0; jNode < nGrids ; jNode++) {
170+
latsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
171+
lonsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
172+
altsToPass[jNode] = static_cast<precision_t*>(malloc(nPointsToPass[jNode] * sizeof(precision_t)));
159173
latsToInterTo[jNode] = static_cast<precision_t*>(malloc(nPointsToReceive[jNode] * sizeof(precision_t)));
160174
lonsToInterTo[jNode] = static_cast<precision_t*>(malloc(nPointsToReceive[jNode] * sizeof(precision_t)));
161175
altsToInterTo[jNode] = static_cast<precision_t*>(malloc(nPointsToReceive[jNode] * sizeof(precision_t)));
@@ -186,11 +200,123 @@ bool grid_match(Grid gGrid,
186200
}
187201
}
188202
bool didWork;
203+
// Pass first coordinate (lons)
204+
didWork = exchange_information(nPointsToPass,
205+
lonsToPass,
206+
nPointsToReceive,
207+
lonsToInterTo);
208+
// Pass second coordinate (lats)
189209
didWork = exchange_information(nPointsToPass,
190210
latsToPass,
191211
nPointsToReceive,
192212
latsToInterTo);
213+
// Pass third coordinate (alts):
214+
didWork = exchange_information(nPointsToPass,
215+
altsToPass,
216+
nPointsToReceive,
217+
altsToInterTo);
193218

219+
if (report.test_verbose(2)) {
220+
for (jNode = 0; jNode < nGrids ; jNode++) {
221+
std::cout << "Received the following points from iGrid = " << jNode << "\n";
222+
std::cout << " -> points received : " << nPointsToReceive[jNode] << "\n";
223+
for (int64_t iPt = 0; iPt < nPointsToReceive[jNode]; iPt++)
224+
std::cout << " -> " << iPt << " "
225+
<< lonsToInterTo[jNode][iPt] << " "
226+
<< latsToInterTo[jNode][iPt] << " "
227+
<< altsToInterTo[jNode][iPt] << "\n";
228+
}
229+
}
194230

195-
return true;
231+
struct grid_to_grid_t oneGrid;
232+
233+
int64_t nPts;
234+
for (jNode = 0; jNode < nGrids ; jNode++) {
235+
// These are backwards now, since we will switch sender and reciever:
236+
oneGrid.nPts = nPointsToReceive[jNode];
237+
oneGrid.nPtsReceive = nPointsToPass[jNode];
238+
oneGrid.iProcTo = iMember * nGrids + jNode;
239+
if (report.test_verbose(2))
240+
std::cout << "Making interpolation coefficients for : " << jNode
241+
<< "; points : " << oneGrid.nPts << "\n";
242+
if (oneGrid.nPts > 0) {
243+
// Interpolation function takes vectors,
244+
// so transfer these arrays to vectors:
245+
std::vector<precision_t> Lons(oneGrid.nPts);
246+
std::vector<precision_t> Lats(oneGrid.nPts);
247+
std::vector<precision_t> Alts(oneGrid.nPts);
248+
for (int64_t iPt = 0; iPt < oneGrid.nPts; iPt++) {
249+
Lons[iPt] = lonsToInterTo[jNode][iPt];
250+
Lats[iPt] = latsToInterTo[jNode][iPt];
251+
Alts[iPt] = altsToInterTo[jNode][iPt];
252+
}
253+
oneGrid.interpCoefs = gGrid.get_interpolation_coefs(Lons, Lats, Alts);
254+
}
255+
gGrid.gridToGridCoefs.push_back(oneGrid);
256+
}
257+
258+
report.exit(function);
259+
return didWork;
196260
}
261+
262+
bool get_data_from_other_grid(Grid &gGrid,
263+
Grid &mGrid,
264+
arma_cube &gData,
265+
arma_cube &mData) {
266+
267+
std::string function = "get_data_from_other_grid";
268+
static int iFunction = -1;
269+
report.enter(function, iFunction);
270+
271+
int64_t jNode, iPt;
272+
std::vector<precision_t *> dataToSend(nGrids);
273+
std::vector<precision_t *> dataToReceive(nGrids);
274+
int64_t *nPointsToSend = static_cast<int64_t*>(malloc(nGrids * sizeof(int64_t)));
275+
int64_t *nPointsToReceive = static_cast<int64_t*>(malloc(nGrids * sizeof(int64_t)));
276+
277+
for (jNode = 0; jNode < nGrids ; jNode++) {
278+
if (report.test_verbose(2))
279+
std::cout << "nPts : " << jNode << " " << gGrid.gridToGridCoefs[jNode].nPts << "\n";
280+
nPointsToSend[jNode] = gGrid.gridToGridCoefs[jNode].nPts;
281+
nPointsToReceive[jNode] = gGrid.gridToGridCoefs[jNode].nPtsReceive;
282+
dataToSend[jNode] = static_cast<precision_t*>(malloc(gGrid.gridToGridCoefs[jNode].nPts * sizeof(precision_t)));
283+
dataToReceive[jNode] = static_cast<precision_t*>(malloc(gGrid.gridToGridCoefs[jNode].nPtsReceive * sizeof(precision_t)));
284+
std::vector<precision_t> values = gGrid.get_interpolation_values(gData, gGrid.gridToGridCoefs[jNode].interpCoefs);
285+
286+
for (iPt = 0; iPt < gGrid.gridToGridCoefs[jNode].nPts; iPt++) {
287+
dataToSend[jNode][iPt] = values[iPt];
288+
if (report.test_verbose(2))
289+
std::cout << "datatosend : " << iPt << " " << dataToSend[jNode][iPt] << "\n";
290+
}
291+
}
292+
bool didWork = exchange_information(nPointsToSend,
293+
dataToSend,
294+
nPointsToReceive,
295+
dataToReceive);
296+
int64_t iX, mnX = mGrid.get_nX();
297+
int64_t iY, mnY = mGrid.get_nY();
298+
int64_t iZ, mnZ = mGrid.get_nZ();
299+
int64_t mGCs = mGrid.get_nGCs();
300+
std::vector<int64_t> iCounter(nGrids);
301+
for (jNode = 0; jNode < nGrids ; jNode++)
302+
iCounter[jNode] = 0;
303+
304+
for (iX = mGCs; iX < mnX - mGCs; iX++) {
305+
for (iY = mGCs; iY < mnY - mGCs; iY++) {
306+
for (iZ = mGCs; iZ < mnZ - mGCs; iZ++) {
307+
jNode = mGrid.gridToGridMap(iX, iY, iZ);
308+
if (report.test_verbose(2)) {
309+
std::cout << "unpacking point : " << iX << " " << iY << " " << iZ << " " << jNode << " "
310+
<< iCounter[jNode] << " " << dataToReceive[jNode][iCounter[jNode]] << "\n";
311+
}
312+
313+
mData(iX, iY, iZ) = dataToReceive[jNode][iCounter[jNode]];
314+
iCounter[jNode] = iCounter[jNode]+1;
315+
}
316+
}
317+
}
318+
319+
report.exit(function);
320+
return true;
321+
322+
}

0 commit comments

Comments
 (0)