Skip to content

Commit

Permalink
Set padding according to MPI rank
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Sep 5, 2023
1 parent d8773dd commit e9dcbc0
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,24 @@ auto JSONIOHandlerImpl::putJsonContents(
};

#if openPMD_HAVE_MPI
auto num_digits = [](unsigned n) -> unsigned {
constexpr auto max = std::numeric_limits<unsigned>::max();
unsigned base_10 = 1;
unsigned res = 1;
while (base_10 < max)
{
base_10 *= 10;
if (n / base_10 == 0)
{
return res;
}
++res;
}
return res;
};

auto parallelImplementation =
[this, &filename, &writeSingleFile](MPI_Comm comm) {
[this, &filename, &writeSingleFile, &num_digits](MPI_Comm comm) {
auto path = fullPath(*filename);
auto dirpath = path + ".parallel";
if (!auxiliary::create_directories(dirpath))
Expand All @@ -1386,10 +1402,12 @@ auto JSONIOHandlerImpl::putJsonContents(
"Failed creating directory '" + dirpath +
"' for parallel JSON output");
}
int rank = 0;
int rank = 0, size = 0;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &size);
std::stringstream subfilePath;
subfilePath << dirpath << "/mpi_rank_" << std::setw(6)
subfilePath << dirpath << "/mpi_rank_"
<< std::setw(num_digits(size - 1))
<< std::setfill('0') << rank << ".json";
writeSingleFile(subfilePath.str());
};
Expand Down

0 comments on commit e9dcbc0

Please sign in to comment.