Skip to content

Commit 9e75d58

Browse files
committed
feat: improve error message for missing partition.nc file
1 parent c5f36a8 commit 9e75d58

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

core/src/ModelMetadata.cpp

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -163,57 +163,66 @@ void ModelMetadata::readNeighbourData(netCDF::NcFile& ncFile)
163163

164164
void ModelMetadata::getPartitionMetadata(std::string partitionFile)
165165
{
166-
netCDF::NcFile ncFile(partitionFile, netCDF::NcFile::read);
167-
int sizes = ncFile.getDim("L").getSize();
168-
int nBoxes = ncFile.getDim("P").getSize();
169-
auto& modelMPI = ModelMPI::getInstance();
170-
auto mpiSize = modelMPI.getSize();
171-
if (nBoxes != mpiSize) {
172-
std::string errorMsg = "Number of MPI ranks " + std::to_string(mpiSize) + " <> "
173-
+ std::to_string(nBoxes) + "\n";
174-
throw std::runtime_error(errorMsg);
175-
}
176-
if (!globalExtentX) {
177-
globalExtentX = ncFile.getDim("NX").getSize();
178-
} else if (globalExtentX != ncFile.getDim("NX").getSize()) {
179-
throw std::runtime_error("ModelMetadata: Inconsistent global x-extent between "
180-
"partition and input files.");
181-
}
182-
if (!globalExtentY) {
183-
globalExtentY = ncFile.getDim("NY").getSize();
184-
} else if (globalExtentX != ncFile.getDim("NY").getSize()) {
185-
throw std::runtime_error("ModelMetadata: Inconsistent global y-extent between "
186-
"partition and input files.");
187-
}
188-
netCDF::NcGroup bboxGroup(ncFile.getGroup(bboxName));
166+
try {
167+
netCDF::NcFile ncFile(partitionFile, netCDF::NcFile::read);
168+
int sizes = ncFile.getDim("L").getSize();
169+
int nBoxes = ncFile.getDim("P").getSize();
170+
auto& modelMPI = ModelMPI::getInstance();
171+
auto mpiSize = modelMPI.getSize();
172+
if (nBoxes != mpiSize) {
173+
std::string errorMsg = "Number of MPI ranks " + std::to_string(mpiSize) + " <> "
174+
+ std::to_string(nBoxes) + "\n";
175+
throw std::runtime_error(errorMsg);
176+
}
177+
if (!globalExtentX) {
178+
globalExtentX = ncFile.getDim("NX").getSize();
179+
} else if (globalExtentX != ncFile.getDim("NX").getSize()) {
180+
throw std::runtime_error("ModelMetadata: Inconsistent global x-extent between "
181+
"partition and input files.");
182+
}
183+
if (!globalExtentY) {
184+
globalExtentY = ncFile.getDim("NY").getSize();
185+
} else if (globalExtentX != ncFile.getDim("NY").getSize()) {
186+
throw std::runtime_error("ModelMetadata: Inconsistent global y-extent between "
187+
"partition and input files.");
188+
}
189+
netCDF::NcGroup bboxGroup(ncFile.getGroup(bboxName));
189190

190-
std::vector<size_t> rank(1, modelMPI.getRank());
191-
bboxGroup.getVar("domain_x").getVar(rank, &localCornerX);
192-
bboxGroup.getVar("domain_y").getVar(rank, &localCornerY);
193-
bboxGroup.getVar("domain_extent_x").getVar(rank, &localExtentX);
194-
bboxGroup.getVar("domain_extent_y").getVar(rank, &localExtentY);
191+
std::vector<size_t> rank(1, modelMPI.getRank());
192+
bboxGroup.getVar("domain_x").getVar(rank, &localCornerX);
193+
bboxGroup.getVar("domain_y").getVar(rank, &localCornerY);
194+
bboxGroup.getVar("domain_extent_x").getVar(rank, &localExtentX);
195+
bboxGroup.getVar("domain_extent_y").getVar(rank, &localExtentY);
195196

196-
readNeighbourData(ncFile);
197+
readNeighbourData(ncFile);
197198

198-
// cornerHaloRecv doesn't need to be read because it can be easily calculated.
199-
for (auto corner : corners) {
200-
if (cornerRanks[corner].size()) {
201-
cornerHaloRecv[corner].resize(1);
202-
cornerHaloRecv[corner][0] = 2 * (localExtentX + localExtentY) + corner;
203-
}
204-
if (cornerRanksPeriodic[corner].size()) {
205-
cornerHaloRecvPeriodic[corner].resize(1);
206-
cornerHaloRecvPeriodic[corner][0] = 2 * (localExtentX + localExtentY) + corner;
199+
// cornerHaloRecv doesn't need to be read because it can be easily calculated.
200+
for (auto corner : corners) {
201+
if (cornerRanks[corner].size()) {
202+
cornerHaloRecv[corner].resize(1);
203+
cornerHaloRecv[corner][0] = 2 * (localExtentX + localExtentY) + corner;
204+
}
205+
if (cornerRanksPeriodic[corner].size()) {
206+
cornerHaloRecvPeriodic[corner].resize(1);
207+
cornerHaloRecvPeriodic[corner][0] = 2 * (localExtentX + localExtentY) + corner;
208+
}
207209
}
208-
}
209-
210-
// gather rank extents in X & Y direction for all processes
211-
rankExtentsX.resize(modelMPI.getSize(), 0);
212-
rankExtentsY.resize(modelMPI.getSize(), 0);
213-
MPI_Allgather(&localExtentX, 1, MPI_INT, rankExtentsX.data(), 1, MPI_INT, modelMPI.getComm());
214-
MPI_Allgather(&localExtentY, 1, MPI_INT, rankExtentsY.data(), 1, MPI_INT, modelMPI.getComm());
215210

216-
ncFile.close();
211+
// gather rank extents in X & Y direction for all processes
212+
rankExtentsX.resize(modelMPI.getSize(), 0);
213+
rankExtentsY.resize(modelMPI.getSize(), 0);
214+
MPI_Allgather(
215+
&localExtentX, 1, MPI_INT, rankExtentsX.data(), 1, MPI_INT, modelMPI.getComm());
216+
MPI_Allgather(
217+
&localExtentY, 1, MPI_INT, rankExtentsY.data(), 1, MPI_INT, modelMPI.getComm());
218+
219+
ncFile.close();
220+
} catch (netCDF::exceptions::NcException& e) {
221+
std::cerr << "Failed to open partition file [" << partitionFile << "] :: " << e.what()
222+
<< std::endl;
223+
auto& modelMPI = ModelMPI::getInstance();
224+
MPI_Abort(modelMPI.getComm(), 1);
225+
}
217226
}
218227

219228
int ModelMetadata::getLocalCornerX() const { return localCornerX; }

0 commit comments

Comments
 (0)