From 65600775ab1b5233d0986a69e209c97994eca0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Tue, 11 Apr 2023 12:08:58 +0200 Subject: [PATCH] Use magic number instead of API call --- include/openPMD/Dataset.hpp | 9 +++- src/Dataset.cpp | 35 +++++++++++-- src/backend/BaseRecordComponent.cpp | 2 +- src/binding/python/Dataset.cpp | 81 ++++++++++++++++------------- test/ParallelIOTest.cpp | 11 ++-- test/SerialIOTest.cpp | 11 ++-- 6 files changed, 95 insertions(+), 54 deletions(-) diff --git a/include/openPMD/Dataset.hpp b/include/openPMD/Dataset.hpp index 0c79ed9909..0032888541 100644 --- a/include/openPMD/Dataset.hpp +++ b/include/openPMD/Dataset.hpp @@ -22,6 +22,7 @@ #include "openPMD/Datatype.hpp" +#include #include #include #include @@ -38,6 +39,11 @@ class Dataset friend class RecordComponent; public: + enum : std::uint64_t + { + JOINED_DIMENSION = std::numeric_limits::max() + }; + Dataset(Datatype, Extent, std::string options = "{}"); /** @@ -53,9 +59,10 @@ class Dataset Extent extent; Datatype dtype; uint8_t rank; - std::optional joinedDimension; std::string options = "{}"; //!< backend-dependent JSON configuration bool empty() const; + + std::optional joinedDimension() const; }; } // namespace openPMD diff --git a/src/Dataset.cpp b/src/Dataset.cpp index f3682c4528..19e67313cf 100644 --- a/src/Dataset.cpp +++ b/src/Dataset.cpp @@ -19,6 +19,7 @@ * If not, see . */ #include "openPMD/Dataset.hpp" +#include "openPMD/Error.hpp" #include #include @@ -30,7 +31,11 @@ Dataset::Dataset(Datatype d, Extent e, std::string options_in) , dtype{d} , rank{static_cast(e.size())} , options{std::move(options_in)} -{} +{ + // Call this in order to have early error message in case of wrong + // specification of joined dimensions + joinedDimension(); +} Dataset::Dataset(Extent e) : Dataset(Datatype::UNDEFINED, std::move(e)) {} @@ -52,14 +57,38 @@ Dataset &Dataset::extend(Extent newExtents) bool Dataset::empty() const { + auto jd = joinedDimension(); for (size_t i = 0; i < extent.size(); ++i) { - if (extent[i] == 0 && - (!joinedDimension.has_value() || joinedDimension.value() != i)) + if (extent[i] == 0 && (!jd.has_value() || jd.value() != i)) { return true; } } return false; } + +std::optional Dataset::joinedDimension() const +{ + std::optional res; + for (size_t i = 0; i < extent.size(); ++i) + { + if (extent[i] == JOINED_DIMENSION) + { + if (res.has_value()) + { + throw error::WrongAPIUsage( + "Must specify JOINED_DIMENSION at most once (found at " + "indices " + + std::to_string(res.value()) + " and " + std::to_string(i) + + ")"); + } + else + { + res = i; + } + } + } + return res; +} } // namespace openPMD diff --git a/src/backend/BaseRecordComponent.cpp b/src/backend/BaseRecordComponent.cpp index 873759bee2..d1a8d33d16 100644 --- a/src/backend/BaseRecordComponent.cpp +++ b/src/backend/BaseRecordComponent.cpp @@ -70,7 +70,7 @@ std::optional BaseRecordComponent::joinedDimension() const auto &rc = get(); if (rc.m_dataset.has_value()) { - return rc.m_dataset.value().joinedDimension; + return rc.m_dataset.value().joinedDimension(); } else { diff --git a/src/binding/python/Dataset.cpp b/src/binding/python/Dataset.cpp index ca7a7099a9..8dee67024f 100644 --- a/src/binding/python/Dataset.cpp +++ b/src/binding/python/Dataset.cpp @@ -31,43 +31,50 @@ using namespace openPMD; void init_Dataset(py::module &m) { - py::class_(m, "Dataset") + auto pyDataset = + py::class_(m, "Dataset") + .def( + py::init(), + py::arg("dtype"), + py::arg("extent")) + .def(py::init(), py::arg("extent")) + .def( + py::init([](py::dtype dt, Extent e) { + auto const d = dtype_from_numpy(dt); + return new Dataset{d, e}; + }), + py::arg("dtype"), + py::arg("extent")) + .def( + py::init(), + py::arg("dtype"), + py::arg("extent"), + py::arg("options")) + .def( + py::init([](py::dtype dt, Extent e, std::string options) { + auto const d = dtype_from_numpy(dt); + return new Dataset{d, e, std::move(options)}; + }), + py::arg("dtype"), + py::arg("extent"), + py::arg("options")) - .def(py::init(), py::arg("dtype"), py::arg("extent")) - .def(py::init(), py::arg("extent")) - .def( - py::init([](py::dtype dt, Extent e) { - auto const d = dtype_from_numpy(dt); - return new Dataset{d, e}; - }), - py::arg("dtype"), - py::arg("extent")) - .def( - py::init(), - py::arg("dtype"), - py::arg("extent"), - py::arg("options")) - .def( - py::init([](py::dtype dt, Extent e, std::string options) { - auto const d = dtype_from_numpy(dt); - return new Dataset{d, e, std::move(options)}; - }), - py::arg("dtype"), - py::arg("extent"), - py::arg("options")) + .def( + "__repr__", + [](const Dataset &d) { + return ""; + }) - .def( - "__repr__", - [](const Dataset &d) { - return ""; - }) - - .def_readonly("extent", &Dataset::extent) - .def("extend", &Dataset::extend) - .def_readonly("rank", &Dataset::rank) - .def_property_readonly( - "dtype", [](const Dataset &d) { return dtype_to_numpy(d.dtype); }) - .def_readwrite("options", &Dataset::options) - .def_readwrite("joined_dimension", &Dataset::joinedDimension); + .def_property_readonly( + "joined_dimension", &Dataset::joinedDimension) + .def_readonly("extent", &Dataset::extent) + .def("extend", &Dataset::extend) + .def_readonly("rank", &Dataset::rank) + .def_property_readonly( + "dtype", + [](const Dataset &d) { return dtype_to_numpy(d.dtype); }) + .def_readwrite("options", &Dataset::options); + pyDataset.attr("JOINED_DIMENSION") = + py::int_(uint64_t(Dataset::JOINED_DIMENSION)); } diff --git a/test/ParallelIOTest.cpp b/test/ParallelIOTest.cpp index ce183ce1af..f2889a2e8a 100644 --- a/test/ParallelIOTest.cpp +++ b/test/ParallelIOTest.cpp @@ -1788,8 +1788,8 @@ void joined_dim(std::string const &ext) auto it = s.writeIterations()[100]; - Dataset numParticlesDS(determineDatatype(), {0}); - numParticlesDS.joinedDimension = 0; + Dataset numParticlesDS( + determineDatatype(), {Dataset::JOINED_DIMENSION}); auto numParticles = it.particles["e"] .particlePatches["numParticles"][RecordComponent::SCALAR]; @@ -1801,8 +1801,8 @@ void joined_dim(std::string const &ext) auto patchOffset = it.particles["e"].particlePatches["offset"]["x"]; auto patchExtent = it.particles["e"].particlePatches["extent"]["x"]; - Dataset particlePatchesDS(determineDatatype(), {0}); - particlePatchesDS.joinedDimension = 0; + Dataset particlePatchesDS( + determineDatatype(), {Dataset::JOINED_DIMENSION}); patchOffset.resetDataset(particlePatchesDS); patchExtent.resetDataset(particlePatchesDS); @@ -1820,8 +1820,7 @@ void joined_dim(std::string const &ext) } auto epx = it.particles["e"]["position"]["x"]; - Dataset ds(determineDatatype(), {1}); - ds.joinedDimension = 0; + Dataset ds(determineDatatype(), {Dataset::JOINED_DIMENSION}); epx.resetDataset(ds); size_t counter = 0; diff --git a/test/SerialIOTest.cpp b/test/SerialIOTest.cpp index 3035e73d83..f0b6f9557e 100644 --- a/test/SerialIOTest.cpp +++ b/test/SerialIOTest.cpp @@ -7229,8 +7229,8 @@ void joined_dim(std::string const &ext) auto it = s.writeIterations()[100]; - Dataset numParticlesDS(determineDatatype(), {0}); - numParticlesDS.joinedDimension = 0; + Dataset numParticlesDS( + determineDatatype(), {Dataset::JOINED_DIMENSION}); auto numParticles = it.particles["e"] .particlePatches["numParticles"][RecordComponent::SCALAR]; @@ -7242,8 +7242,8 @@ void joined_dim(std::string const &ext) auto patchOffset = it.particles["e"].particlePatches["offset"]["x"]; auto patchExtent = it.particles["e"].particlePatches["extent"]["x"]; - Dataset particlePatchesDS(determineDatatype(), {0}); - particlePatchesDS.joinedDimension = 0; + Dataset particlePatchesDS( + determineDatatype(), {Dataset::JOINED_DIMENSION}); patchOffset.resetDataset(particlePatchesDS); patchExtent.resetDataset(particlePatchesDS); @@ -7260,8 +7260,7 @@ void joined_dim(std::string const &ext) } auto epx = it.particles["e"]["position"]["x"]; - Dataset ds(determineDatatype(), {1}); - ds.joinedDimension = 0; + Dataset ds(determineDatatype(), {Dataset::JOINED_DIMENSION}); epx.resetDataset(ds); size_t counter = 0;