Skip to content

Commit

Permalink
Use magic number instead of API call
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Apr 12, 2023
1 parent 8d6c352 commit 6560077
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 54 deletions.
9 changes: 8 additions & 1 deletion include/openPMD/Dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "openPMD/Datatype.hpp"

#include <limits>
#include <memory>
#include <optional>
#include <string>
Expand All @@ -38,6 +39,11 @@ class Dataset
friend class RecordComponent;

public:
enum : std::uint64_t
{
JOINED_DIMENSION = std::numeric_limits<std::uint64_t>::max()
};

Dataset(Datatype, Extent, std::string options = "{}");

/**
Expand All @@ -53,9 +59,10 @@ class Dataset
Extent extent;
Datatype dtype;
uint8_t rank;
std::optional<size_t> joinedDimension;
std::string options = "{}"; //!< backend-dependent JSON configuration

bool empty() const;

std::optional<size_t> joinedDimension() const;
};
} // namespace openPMD
35 changes: 32 additions & 3 deletions src/Dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/Dataset.hpp"
#include "openPMD/Error.hpp"

#include <cstddef>
#include <iostream>
Expand All @@ -30,7 +31,11 @@ Dataset::Dataset(Datatype d, Extent e, std::string options_in)
, dtype{d}
, rank{static_cast<uint8_t>(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))
{}
Expand All @@ -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<size_t> Dataset::joinedDimension() const
{
std::optional<size_t> 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
2 changes: 1 addition & 1 deletion src/backend/BaseRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ std::optional<size_t> 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
{
Expand Down
81 changes: 44 additions & 37 deletions src/binding/python/Dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,50 @@ using namespace openPMD;

void init_Dataset(py::module &m)
{
py::class_<Dataset>(m, "Dataset")
auto pyDataset =
py::class_<Dataset>(m, "Dataset")
.def(
py::init<Datatype, Extent>(),
py::arg("dtype"),
py::arg("extent"))
.def(py::init<Extent>(), 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<Datatype, Extent, std::string>(),
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<Datatype, Extent>(), py::arg("dtype"), py::arg("extent"))
.def(py::init<Extent>(), 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<Datatype, Extent, std::string>(),
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 "<openPMD.Dataset of rank '" +
std::to_string(d.rank) + "'>";
})

.def(
"__repr__",
[](const Dataset &d) {
return "<openPMD.Dataset of rank '" + std::to_string(d.rank) +
"'>";
})

.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));
}
11 changes: 5 additions & 6 deletions test/ParallelIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,8 +1788,8 @@ void joined_dim(std::string const &ext)

auto it = s.writeIterations()[100];

Dataset numParticlesDS(determineDatatype<patchType>(), {0});
numParticlesDS.joinedDimension = 0;
Dataset numParticlesDS(
determineDatatype<patchType>(), {Dataset::JOINED_DIMENSION});
auto numParticles =
it.particles["e"]
.particlePatches["numParticles"][RecordComponent::SCALAR];
Expand All @@ -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<float>(), {0});
particlePatchesDS.joinedDimension = 0;
Dataset particlePatchesDS(
determineDatatype<float>(), {Dataset::JOINED_DIMENSION});
patchOffset.resetDataset(particlePatchesDS);
patchExtent.resetDataset(particlePatchesDS);

Expand All @@ -1820,8 +1820,7 @@ void joined_dim(std::string const &ext)
}

auto epx = it.particles["e"]["position"]["x"];
Dataset ds(determineDatatype<type>(), {1});
ds.joinedDimension = 0;
Dataset ds(determineDatatype<type>(), {Dataset::JOINED_DIMENSION});
epx.resetDataset(ds);

size_t counter = 0;
Expand Down
11 changes: 5 additions & 6 deletions test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7229,8 +7229,8 @@ void joined_dim(std::string const &ext)

auto it = s.writeIterations()[100];

Dataset numParticlesDS(determineDatatype<patchType>(), {0});
numParticlesDS.joinedDimension = 0;
Dataset numParticlesDS(
determineDatatype<patchType>(), {Dataset::JOINED_DIMENSION});
auto numParticles =
it.particles["e"]
.particlePatches["numParticles"][RecordComponent::SCALAR];
Expand All @@ -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<float>(), {0});
particlePatchesDS.joinedDimension = 0;
Dataset particlePatchesDS(
determineDatatype<float>(), {Dataset::JOINED_DIMENSION});
patchOffset.resetDataset(particlePatchesDS);
patchExtent.resetDataset(particlePatchesDS);

Expand All @@ -7260,8 +7260,7 @@ void joined_dim(std::string const &ext)
}

auto epx = it.particles["e"]["position"]["x"];
Dataset ds(determineDatatype<type>(), {1});
ds.joinedDimension = 0;
Dataset ds(determineDatatype<type>(), {Dataset::JOINED_DIMENSION});
epx.resetDataset(ds);

size_t counter = 0;
Expand Down

0 comments on commit 6560077

Please sign in to comment.