Skip to content

Commit 240e25f

Browse files
committed
Move joinedDim logic into middle-end for extendDataset
1 parent c7f896a commit 240e25f

File tree

8 files changed

+60
-22
lines changed

8 files changed

+60
-22
lines changed

include/openPMD/Dataset.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ class Dataset
8686
bool empty() const;
8787

8888
std::optional<size_t> joinedDimension() const;
89+
static std::optional<size_t> joinedDimension(Extent const &);
8990
};
9091
} // namespace openPMD

include/openPMD/IO/IOTask.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,15 @@ template <>
405405
struct OPENPMDAPI_EXPORT Parameter<Operation::EXTEND_DATASET>
406406
: public AbstractParameter
407407
{
408-
Parameter() = default;
408+
Parameter(Extent e) : joinedDimension(Dataset::joinedDimension(e))
409+
{
410+
this->extent = std::move(e);
411+
}
412+
413+
// default constructor, but callsites need to explicitly acknowledge that
414+
// joined dimensions will not be automatically configured when using it
415+
Parameter(I_dont_want_to_use_joined_dimensions_t)
416+
{}
409417
Parameter(Parameter &&) = default;
410418
Parameter(Parameter const &) = default;
411419
Parameter &operator=(Parameter &&) = default;
@@ -418,6 +426,7 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::EXTEND_DATASET>
418426
}
419427

420428
Extent extent = {};
429+
std::optional<size_t> joinedDimension;
421430
};
422431

423432
template <>

src/Dataset.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ bool Dataset::empty() const
6868
}
6969

7070
std::optional<size_t> Dataset::joinedDimension() const
71+
{
72+
return joinedDimension(extent);
73+
}
74+
75+
std::optional<size_t> Dataset::joinedDimension(Extent const &extent)
7176
{
7277
std::optional<size_t> res;
7378
for (size_t i = 0; i < extent.size(); ++i)

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,10 @@ namespace detail
873873
{
874874
template <typename T, typename... Args>
875875
static void call(
876-
adios2::IO &IO, std::string const &variable, Extent const &newShape)
876+
adios2::IO &IO,
877+
std::string const &variable,
878+
Extent const &newShape,
879+
std::optional<size_t> const &newJoinedDim)
877880
{
878881
auto var = IO.InquireVariable<T>(variable);
879882
if (!var)
@@ -888,54 +891,60 @@ namespace detail
888891
{
889892
dims.push_back(ext);
890893
}
891-
auto joinedDim = joinedDimension(var.Shape());
894+
auto oldJoinedDim = joinedDimension(var.Shape());
892895
auto make_runtime_error = [&](char const *message) {
893896
std::stringstream s;
894897
s << "[ADIOS2IOHandlerImpl::extendDataset()] " << message
895898
<< "\nNote: Variable '" << variable << "' has old shape ";
896899
auxiliary::write_vec_to_stream(s, var.Shape());
897-
if (joinedDim.has_value())
900+
if (oldJoinedDim.has_value())
898901
{
899-
s << " (joined dimension on index " << *joinedDim << ").";
902+
s << " (joined dimension on index " << *oldJoinedDim << ")";
900903
}
901904
else
902905
{
903906
s << " (no joined dimension)";
904907
}
905908
s << " and is extended to new shape ";
906-
auxiliary::write_vec_to_stream(s, newShape) << ".";
909+
auxiliary::write_vec_to_stream(s, newShape);
910+
if (newJoinedDim.has_value())
911+
{
912+
s << " (joined dimension on index " << *newJoinedDim
913+
<< ").";
914+
}
915+
else
916+
{
917+
s << " (no joined dimension).";
918+
}
907919
return std::runtime_error(s.str());
908920
};
909-
if (joinedDim.has_value() ||
921+
if (oldJoinedDim.has_value() ||
910922
var.ShapeID() == adios2::ShapeID::JoinedArray)
911923
{
912-
if (!joinedDim.has_value())
924+
if (!oldJoinedDim.has_value())
913925
{
914926
throw make_runtime_error(
915927
"Inconsistent state of variable: Has shape ID "
916928
"JoinedArray, but its shape contains no value "
917929
"adios2::JoinedDim.");
918930
}
919-
if (newShape.at(*joinedDim) != Dataset::JOINED_DIMENSION)
931+
if (newJoinedDim != oldJoinedDim)
920932
{
921933
throw make_runtime_error(
922934
"Variable was previously configured with a joined "
923935
"dimension, so the new dataset extent must keep the "
924936
"joined dimension on that index.");
925937
}
926-
dims[*joinedDim] = adios2::JoinedDim;
938+
dims[*newJoinedDim] = adios2::JoinedDim;
927939
}
928940
else
929941
{
930-
for (auto s : newShape)
942+
if (newJoinedDim.has_value())
931943
{
932-
if (s == Dataset::JOINED_DIMENSION)
933-
{
934-
throw make_runtime_error(
935-
"Variable was not previously configured with a "
936-
"joined dimension, but is now requested to change "
937-
"extent to a joined array.");
938-
}
944+
throw make_runtime_error(
945+
"Variable was not previously configured with a "
946+
"joined dimension, but is now requested to change "
947+
"extent to a joined array.");
939948
}
940949
}
941950

@@ -958,7 +967,7 @@ void ADIOS2IOHandlerImpl::extendDataset(
958967
auto &filedata = getFileData(file, IfFileNotOpen::ThrowError);
959968
Datatype dt = detail::fromADIOS2Type(filedata.m_IO.VariableType(name));
960969
switchAdios2VariableType<detail::DatasetExtender>(
961-
dt, filedata.m_IO, name, parameters.extent);
970+
dt, filedata.m_IO, name, parameters.extent, parameters.joinedDimension);
962971
}
963972

964973
void ADIOS2IOHandlerImpl::openFile(

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,12 @@ void HDF5IOHandlerImpl::extendDataset(
840840
throw std::runtime_error(
841841
"[HDF5] Extending an unwritten Dataset is not possible.");
842842

843+
if (parameters.joinedDimension.has_value())
844+
{
845+
error::throwOperationUnsupportedInBackend(
846+
"HDF5", "Joined Arrays currently only supported in ADIOS2");
847+
}
848+
843849
auto res = getFile(writable);
844850
if (!res)
845851
res = getFile(writable->parent);

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,13 @@ void JSONIOHandlerImpl::extendDataset(
711711
VERIFY_ALWAYS(
712712
access::write(m_handler->m_backendAccess),
713713
"[JSON] Cannot extend a dataset in read-only mode.")
714+
715+
if (parameters.joinedDimension.has_value())
716+
{
717+
error::throwOperationUnsupportedInBackend(
718+
"JSON", "Joined Arrays currently only supported in ADIOS2");
719+
}
720+
714721
setAndGetFilePosition(writable);
715722
refreshFileFromParent(writable);
716723
auto &j = obtainJsonContents(writable);

src/RecordComponent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ void RecordComponent::flush(
339339
}
340340
else
341341
{
342-
Parameter<Operation::EXTEND_DATASET> pExtend;
343-
pExtend.extent = rc.m_dataset.value().extent;
342+
Parameter<Operation::EXTEND_DATASET> pExtend(
343+
rc.m_dataset.value().extent);
344344
IOHandler()->enqueue(IOTask(this, std::move(pExtend)));
345345
rc.m_hasBeenExtended = false;
346346
}

src/binding/python/Dataset.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ void init_Dataset(py::module &m)
7979
})
8080

8181
.def_property_readonly(
82-
"joined_dimension", &Dataset::joinedDimension)
82+
"joined_dimension",
83+
py::overload_cast<>(&Dataset::joinedDimension, py::const_))
8384
.def_readonly("extent", &Dataset::extent)
8485
.def("extend", &Dataset::extend)
8586
.def_readonly("rank", &Dataset::rank)

0 commit comments

Comments
 (0)