Skip to content

Commit

Permalink
Better document reopening options
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 7, 2024
1 parent 966f22a commit 447ef86
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion include/openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace adios_defs
{
Create,
Open,
Reopen
ReopenFileThatWeCreated
};
} // namespace adios_defs

Expand Down
5 changes: 3 additions & 2 deletions include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,11 @@ class ADIOS2IOHandlerImpl

enum class IfFileNotOpen : char
{
ReopenImplicitly,
OpenImplicitly,
CreateImplicitly,
ThrowError
ThrowError,
ReopenFileThatWeCreated,
ReopenFileFoundOnDisk = OpenImplicitly,
};

detail::ADIOS2File &
Expand Down
16 changes: 14 additions & 2 deletions include/openPMD/IO/IOTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,21 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::OPEN_FILE>
new Parameter<Operation::OPEN_FILE>(std::move(*this)));
}

// Needed for reopening files in file-based Iteration encoding when using
// R/W-mode in ADIOS2. Files can only be opened for reading XOR writing,
// so R/W mode in file-based encoding can only operate at the granularity
// of files in ADIOS2. The frontend needs to tell us if we should reopen
// a file for continued reading (WasFoundOnDisk) or for continued writing
// (WasCreatedByUs).
enum class Reopen
{
WasCreatedByUs,
WasFoundOnDisk,
NoReopen
};

std::string name = "";
// true <-> file was previously created and is now opened again
bool reopen = false;
Reopen reopen = Reopen::NoReopen;
using ParsePreference = internal::ParsePreference;
std::shared_ptr<ParsePreference> out_parsePreference =
std::make_shared<ParsePreference>(ParsePreference::UpFront);
Expand Down
3 changes: 3 additions & 0 deletions include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ namespace internal
* READ_WRITE mode when re-opening a closed file in file-based encoding:
* A file that existed previously is re-opened in Read mode and will
* not support updating its contents.
* (Note that this is NOT a restriction of re-opening, this is
* fundamentally a restriction of R/W in ADIOS2. Files can be
* written XOR read.)
* A file that we created anew is re-opened in Append mode to continue
* writing data to it. Using `adios2.engine.parameters.FlattenSteps =
* "ON"` is recommended in this case.
Expand Down
26 changes: 18 additions & 8 deletions src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,12 +931,22 @@ void ADIOS2IOHandlerImpl::openFile(
writable->written = true;
writable->abstractFilePosition = std::make_shared<ADIOS2FilePosition>();

auto how_to_open = [&]() {
switch (parameters.reopen)
{
case Parameter<Operation::OPEN_FILE>::Reopen::WasCreatedByUs:
return IfFileNotOpen::ReopenFileThatWeCreated;
case Parameter<Operation::OPEN_FILE>::Reopen::WasFoundOnDisk:
return IfFileNotOpen::ReopenFileFoundOnDisk;
case Parameter<Operation::OPEN_FILE>::Reopen::NoReopen:
return IfFileNotOpen::OpenImplicitly;
}
return IfFileNotOpen::ThrowError; // Unreachable
}();

// enforce opening the file
// lazy opening is deathly in parallel situations
auto &fileData = getFileData(
file,
parameters.reopen ? IfFileNotOpen::ReopenImplicitly
: IfFileNotOpen::OpenImplicitly);
auto &fileData = getFileData(file, how_to_open);
*parameters.out_parsePreference = fileData.parsePreference;
m_dirty.emplace(std::move(file));
}
Expand Down Expand Up @@ -1567,7 +1577,7 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2AccessMode(
case adios_defs::OpenFileAs::Create:
return adios2::Mode::Write;
case adios_defs::OpenFileAs::Open:
case adios_defs::OpenFileAs::Reopen:
case adios_defs::OpenFileAs::ReopenFileThatWeCreated:
return adios2::Mode::Append;
}
break;
Expand Down Expand Up @@ -1609,7 +1619,7 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2AccessMode(
#else
return adios2::Mode::Read;
#endif
case adios_defs::OpenFileAs::Reopen:
case adios_defs::OpenFileAs::ReopenFileThatWeCreated:
/* In order to write new data to an Iteration that was
* created and closed previously, the only applicable access
* mode is Append mode, ideally in conjunction with
Expand Down Expand Up @@ -1726,8 +1736,8 @@ detail::ADIOS2File &ADIOS2IOHandlerImpl::getFileData(
using OF = adios_defs::OpenFileAs;
switch (flag)
{
case IfFileNotOpen::ReopenImplicitly:
return OF::Reopen;
case IfFileNotOpen::ReopenFileThatWeCreated:
return OF::ReopenFileThatWeCreated;
case IfFileNotOpen::OpenImplicitly:
return OF::Open;
case IfFileNotOpen::CreateImplicitly:
Expand Down
16 changes: 14 additions & 2 deletions src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2741,14 +2741,26 @@ void Series::openIteration(IterationIndex_t index, Iteration &iteration)
// open the iteration's file again
Parameter<Operation::OPEN_FILE> fOpen;
fOpen.name = iterationFilename(index);
fOpen.reopen = oldStatus == CL::Closed &&
using R = Parameter<Operation::OPEN_FILE>::Reopen;
if (oldStatus != CL::Closed)
{
fOpen.reopen = R::NoReopen;
}
else if (
// The filename only gets emplaced in there if we found it on the
// file system, otherwise it's generated by iterationFilename().
// This helps us distinguish which iterations were created by us and
// which ones existed already. This is important for ADIOS2 which
// can open an iteration for Appending XOR for reading.
series.m_iterationFilenames.find(index) ==
series.m_iterationFilenames.end();
series.m_iterationFilenames.end())
{
fOpen.reopen = R::WasCreatedByUs;
}
else
{
fOpen.reopen = R::WasFoundOnDisk;
}
IOHandler()->enqueue(IOTask(this, fOpen));

/* open base path */
Expand Down

0 comments on commit 447ef86

Please sign in to comment.