Skip to content

Commit 6560077

Browse files
committed
Use magic number instead of API call
1 parent 8d6c352 commit 6560077

File tree

6 files changed

+95
-54
lines changed

6 files changed

+95
-54
lines changed

include/openPMD/Dataset.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "openPMD/Datatype.hpp"
2424

25+
#include <limits>
2526
#include <memory>
2627
#include <optional>
2728
#include <string>
@@ -38,6 +39,11 @@ class Dataset
3839
friend class RecordComponent;
3940

4041
public:
42+
enum : std::uint64_t
43+
{
44+
JOINED_DIMENSION = std::numeric_limits<std::uint64_t>::max()
45+
};
46+
4147
Dataset(Datatype, Extent, std::string options = "{}");
4248

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

5964
bool empty() const;
65+
66+
std::optional<size_t> joinedDimension() const;
6067
};
6168
} // namespace openPMD

src/Dataset.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121
#include "openPMD/Dataset.hpp"
22+
#include "openPMD/Error.hpp"
2223

2324
#include <cstddef>
2425
#include <iostream>
@@ -30,7 +31,11 @@ Dataset::Dataset(Datatype d, Extent e, std::string options_in)
3031
, dtype{d}
3132
, rank{static_cast<uint8_t>(e.size())}
3233
, options{std::move(options_in)}
33-
{}
34+
{
35+
// Call this in order to have early error message in case of wrong
36+
// specification of joined dimensions
37+
joinedDimension();
38+
}
3439

3540
Dataset::Dataset(Extent e) : Dataset(Datatype::UNDEFINED, std::move(e))
3641
{}
@@ -52,14 +57,38 @@ Dataset &Dataset::extend(Extent newExtents)
5257

5358
bool Dataset::empty() const
5459
{
60+
auto jd = joinedDimension();
5561
for (size_t i = 0; i < extent.size(); ++i)
5662
{
57-
if (extent[i] == 0 &&
58-
(!joinedDimension.has_value() || joinedDimension.value() != i))
63+
if (extent[i] == 0 && (!jd.has_value() || jd.value() != i))
5964
{
6065
return true;
6166
}
6267
}
6368
return false;
6469
}
70+
71+
std::optional<size_t> Dataset::joinedDimension() const
72+
{
73+
std::optional<size_t> res;
74+
for (size_t i = 0; i < extent.size(); ++i)
75+
{
76+
if (extent[i] == JOINED_DIMENSION)
77+
{
78+
if (res.has_value())
79+
{
80+
throw error::WrongAPIUsage(
81+
"Must specify JOINED_DIMENSION at most once (found at "
82+
"indices " +
83+
std::to_string(res.value()) + " and " + std::to_string(i) +
84+
")");
85+
}
86+
else
87+
{
88+
res = i;
89+
}
90+
}
91+
}
92+
return res;
93+
}
6594
} // namespace openPMD

src/backend/BaseRecordComponent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ std::optional<size_t> BaseRecordComponent::joinedDimension() const
7070
auto &rc = get();
7171
if (rc.m_dataset.has_value())
7272
{
73-
return rc.m_dataset.value().joinedDimension;
73+
return rc.m_dataset.value().joinedDimension();
7474
}
7575
else
7676
{

src/binding/python/Dataset.cpp

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,50 @@ using namespace openPMD;
3131

3232
void init_Dataset(py::module &m)
3333
{
34-
py::class_<Dataset>(m, "Dataset")
34+
auto pyDataset =
35+
py::class_<Dataset>(m, "Dataset")
36+
.def(
37+
py::init<Datatype, Extent>(),
38+
py::arg("dtype"),
39+
py::arg("extent"))
40+
.def(py::init<Extent>(), py::arg("extent"))
41+
.def(
42+
py::init([](py::dtype dt, Extent e) {
43+
auto const d = dtype_from_numpy(dt);
44+
return new Dataset{d, e};
45+
}),
46+
py::arg("dtype"),
47+
py::arg("extent"))
48+
.def(
49+
py::init<Datatype, Extent, std::string>(),
50+
py::arg("dtype"),
51+
py::arg("extent"),
52+
py::arg("options"))
53+
.def(
54+
py::init([](py::dtype dt, Extent e, std::string options) {
55+
auto const d = dtype_from_numpy(dt);
56+
return new Dataset{d, e, std::move(options)};
57+
}),
58+
py::arg("dtype"),
59+
py::arg("extent"),
60+
py::arg("options"))
3561

36-
.def(py::init<Datatype, Extent>(), py::arg("dtype"), py::arg("extent"))
37-
.def(py::init<Extent>(), py::arg("extent"))
38-
.def(
39-
py::init([](py::dtype dt, Extent e) {
40-
auto const d = dtype_from_numpy(dt);
41-
return new Dataset{d, e};
42-
}),
43-
py::arg("dtype"),
44-
py::arg("extent"))
45-
.def(
46-
py::init<Datatype, Extent, std::string>(),
47-
py::arg("dtype"),
48-
py::arg("extent"),
49-
py::arg("options"))
50-
.def(
51-
py::init([](py::dtype dt, Extent e, std::string options) {
52-
auto const d = dtype_from_numpy(dt);
53-
return new Dataset{d, e, std::move(options)};
54-
}),
55-
py::arg("dtype"),
56-
py::arg("extent"),
57-
py::arg("options"))
62+
.def(
63+
"__repr__",
64+
[](const Dataset &d) {
65+
return "<openPMD.Dataset of rank '" +
66+
std::to_string(d.rank) + "'>";
67+
})
5868

59-
.def(
60-
"__repr__",
61-
[](const Dataset &d) {
62-
return "<openPMD.Dataset of rank '" + std::to_string(d.rank) +
63-
"'>";
64-
})
65-
66-
.def_readonly("extent", &Dataset::extent)
67-
.def("extend", &Dataset::extend)
68-
.def_readonly("rank", &Dataset::rank)
69-
.def_property_readonly(
70-
"dtype", [](const Dataset &d) { return dtype_to_numpy(d.dtype); })
71-
.def_readwrite("options", &Dataset::options)
72-
.def_readwrite("joined_dimension", &Dataset::joinedDimension);
69+
.def_property_readonly(
70+
"joined_dimension", &Dataset::joinedDimension)
71+
.def_readonly("extent", &Dataset::extent)
72+
.def("extend", &Dataset::extend)
73+
.def_readonly("rank", &Dataset::rank)
74+
.def_property_readonly(
75+
"dtype",
76+
[](const Dataset &d) { return dtype_to_numpy(d.dtype); })
77+
.def_readwrite("options", &Dataset::options);
78+
pyDataset.attr("JOINED_DIMENSION") =
79+
py::int_(uint64_t(Dataset::JOINED_DIMENSION));
7380
}

test/ParallelIOTest.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,8 @@ void joined_dim(std::string const &ext)
17881788

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

1791-
Dataset numParticlesDS(determineDatatype<patchType>(), {0});
1792-
numParticlesDS.joinedDimension = 0;
1791+
Dataset numParticlesDS(
1792+
determineDatatype<patchType>(), {Dataset::JOINED_DIMENSION});
17931793
auto numParticles =
17941794
it.particles["e"]
17951795
.particlePatches["numParticles"][RecordComponent::SCALAR];
@@ -1801,8 +1801,8 @@ void joined_dim(std::string const &ext)
18011801

18021802
auto patchOffset = it.particles["e"].particlePatches["offset"]["x"];
18031803
auto patchExtent = it.particles["e"].particlePatches["extent"]["x"];
1804-
Dataset particlePatchesDS(determineDatatype<float>(), {0});
1805-
particlePatchesDS.joinedDimension = 0;
1804+
Dataset particlePatchesDS(
1805+
determineDatatype<float>(), {Dataset::JOINED_DIMENSION});
18061806
patchOffset.resetDataset(particlePatchesDS);
18071807
patchExtent.resetDataset(particlePatchesDS);
18081808

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

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

18271826
size_t counter = 0;

test/SerialIOTest.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7229,8 +7229,8 @@ void joined_dim(std::string const &ext)
72297229

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

7232-
Dataset numParticlesDS(determineDatatype<patchType>(), {0});
7233-
numParticlesDS.joinedDimension = 0;
7232+
Dataset numParticlesDS(
7233+
determineDatatype<patchType>(), {Dataset::JOINED_DIMENSION});
72347234
auto numParticles =
72357235
it.particles["e"]
72367236
.particlePatches["numParticles"][RecordComponent::SCALAR];
@@ -7242,8 +7242,8 @@ void joined_dim(std::string const &ext)
72427242

72437243
auto patchOffset = it.particles["e"].particlePatches["offset"]["x"];
72447244
auto patchExtent = it.particles["e"].particlePatches["extent"]["x"];
7245-
Dataset particlePatchesDS(determineDatatype<float>(), {0});
7246-
particlePatchesDS.joinedDimension = 0;
7245+
Dataset particlePatchesDS(
7246+
determineDatatype<float>(), {Dataset::JOINED_DIMENSION});
72477247
patchOffset.resetDataset(particlePatchesDS);
72487248
patchExtent.resetDataset(particlePatchesDS);
72497249

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

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

72677266
size_t counter = 0;

0 commit comments

Comments
 (0)