Skip to content

Commit 3f3cd80

Browse files
committed
Implement for normal storeChunk()
1 parent b6ccb69 commit 3f3cd80

6 files changed

Lines changed: 159 additions & 71 deletions

File tree

include/openPMD/LoadStoreChunk.hpp

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "openPMD/Dataset.hpp"
4+
#include "openPMD/auxiliary/ShareRawInternal.hpp"
45
#include "openPMD/auxiliary/UniquePtr.hpp"
56

67
#include <optional>
@@ -34,19 +35,27 @@ namespace internal
3435
Extent extent;
3536
std::optional<MemorySelection> memorySelection;
3637
};
38+
39+
struct ConfigureStoreChunkData
40+
{
41+
ConfigureStoreChunkData(RecordComponent &);
42+
43+
RecordComponent &m_rc;
44+
std::optional<Offset> m_offset;
45+
std::optional<Extent> m_extent;
46+
};
3747
} // namespace internal
3848

3949
template <typename ChildClass = void>
40-
class ConfigureStoreChunk
50+
class ConfigureStoreChunk : protected internal::ConfigureStoreChunkData
4151
{
4252
friend class RecordComponent;
53+
template <typename>
54+
friend class ConfigureStoreChunk;
4355

44-
private:
45-
RecordComponent &m_rc;
46-
std::optional<Offset> m_offset;
47-
std::optional<Extent> m_extent;
48-
56+
protected:
4957
ConfigureStoreChunk(RecordComponent &rc);
58+
ConfigureStoreChunk(ConfigureStoreChunkData &&);
5059

5160
auto dim() const -> uint8_t;
5261
auto getOffset() const -> Offset;
@@ -58,23 +67,29 @@ class ConfigureStoreChunk
5867
std::is_void_v<ChildClass>,
5968
/*then*/ ConfigureStoreChunk<void>,
6069
/*else*/ ChildClass>;
70+
template <typename T>
71+
using normalize_dataset_type =
72+
std::remove_cv_t<std::remove_extent_t<T>> const;
6173

6274
auto offset(Offset) -> return_type &;
6375
auto extent(Extent) -> return_type &;
6476

77+
// @todo rvalue references..?
6578
template <typename T>
66-
auto fromSharedPtr(
67-
std::shared_ptr<T>) && -> TypedConfigureStoreChunk<std::shared_ptr<T>>;
79+
auto fromSharedPtr(std::shared_ptr<T>)
80+
-> TypedConfigureStoreChunk<std::shared_ptr<normalize_dataset_type<T>>>;
6881
template <typename T>
6982
auto fromUniquePtr(UniquePtrWithLambda<T>)
70-
&& -> TypedConfigureStoreChunk<UniquePtrWithLambda<T>>;
83+
-> TypedConfigureStoreChunk<
84+
UniquePtrWithLambda<normalize_dataset_type<T>>>;
7185
template <typename T, typename Del>
7286
auto fromUniquePtr(std::unique_ptr<T, Del>)
73-
&& -> TypedConfigureStoreChunk<UniquePtrWithLambda<T>>;
87+
-> TypedConfigureStoreChunk<
88+
UniquePtrWithLambda<normalize_dataset_type<T>>>;
7489
template <typename T>
75-
auto fromRawPtr(T *data) && -> TypedConfigureStoreChunk<std::shared_ptr<T>>;
90+
auto fromRawPtr(T *data) -> TypedConfigureStoreChunk<std::shared_ptr<T>>;
7691
template <typename T_ContiguousContainer>
77-
auto fromContiguousContainer(T_ContiguousContainer &data) && ->
92+
auto fromContiguousContainer(T_ContiguousContainer &data) ->
7893
typename std::enable_if_t<
7994
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
8095
TypedConfigureStoreChunk<
@@ -97,7 +112,8 @@ class TypedConfigureStoreChunk
97112
using parent_t = ConfigureStoreChunk<TypedConfigureStoreChunk<Ptr_Type>>;
98113

99114
private:
100-
friend class ConfigureStoreChunk<TypedConfigureStoreChunk<Ptr_Type>>;
115+
template <typename T>
116+
friend class ConfigureStoreChunk;
101117

102118
Ptr_Type m_buffer;
103119
std::optional<MemorySelection> m_mem_select;
@@ -113,21 +129,64 @@ class TypedConfigureStoreChunk
113129
auto as_parent() & -> parent_t &;
114130
auto as_parent() const & -> parent_t const &;
115131

116-
auto enqueue() & -> void;
132+
auto enqueue() -> void;
117133
};
118134

135+
template <typename ChildClass>
136+
template <typename T>
137+
auto ConfigureStoreChunk<ChildClass>::fromSharedPtr(std::shared_ptr<T> data)
138+
-> TypedConfigureStoreChunk<std::shared_ptr<normalize_dataset_type<T>>>
139+
{
140+
if (!data)
141+
{
142+
throw std::runtime_error(
143+
"Unallocated pointer passed during chunk store.");
144+
}
145+
return TypedConfigureStoreChunk<std::shared_ptr<normalize_dataset_type<T>>>(
146+
std::static_pointer_cast<normalize_dataset_type<T>>(std::move(data)),
147+
{std::move(*this)});
148+
}
149+
template <typename ChildClass>
150+
template <typename T>
151+
auto ConfigureStoreChunk<ChildClass>::fromUniquePtr(UniquePtrWithLambda<T> data)
152+
-> TypedConfigureStoreChunk<UniquePtrWithLambda<normalize_dataset_type<T>>>
153+
{
154+
if (!data)
155+
{
156+
throw std::runtime_error(
157+
"Unallocated pointer passed during chunk store.");
158+
}
159+
return TypedConfigureStoreChunk<
160+
UniquePtrWithLambda<normalize_dataset_type<T>>>(
161+
std::move(data).template static_cast_<normalize_dataset_type<T>>(),
162+
{std::move(*this)});
163+
}
164+
template <typename ChildClass>
165+
template <typename T>
166+
auto ConfigureStoreChunk<ChildClass>::fromRawPtr(T *data)
167+
-> TypedConfigureStoreChunk<std::shared_ptr<T>>
168+
{
169+
if (!data)
170+
{
171+
throw std::runtime_error(
172+
"Unallocated pointer passed during chunk store.");
173+
}
174+
return TypedConfigureStoreChunk<std::shared_ptr<T>>(
175+
auxiliary::shareRaw(data), {std::move(*this)});
176+
}
177+
119178
template <typename ChildClass>
120179
template <typename T, typename Del>
121180
auto ConfigureStoreChunk<ChildClass>::fromUniquePtr(
122181
std::unique_ptr<T, Del> data)
123-
&& -> TypedConfigureStoreChunk<UniquePtrWithLambda<T>>
182+
-> TypedConfigureStoreChunk<UniquePtrWithLambda<normalize_dataset_type<T>>>
124183
{
125184
return fromUniquePtr(UniquePtrWithLambda<T>(std::move(data)));
126185
}
127186
template <typename ChildClass>
128187
template <typename T_ContiguousContainer>
129188
auto ConfigureStoreChunk<ChildClass>::fromContiguousContainer(
130-
T_ContiguousContainer &data) && ->
189+
T_ContiguousContainer &data) ->
131190
typename std::enable_if_t<
132191
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
133192
TypedConfigureStoreChunk<

include/openPMD/RecordComponent.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,10 @@ class RecordComponent : public BaseRecordComponent
473473
*/
474474
RecordComponent &makeEmpty(Dataset d);
475475

476-
void storeChunk(
477-
auxiliary::WriteBuffer buffer, Datatype datatype, Offset o, Extent e);
476+
void storeChunk_impl(
477+
auxiliary::WriteBuffer buffer,
478+
Datatype datatype,
479+
internal::StoreChunkConfigFromBuffer);
478480

479481
template <typename T>
480482
DynamicMemoryView<T> storeChunkSpan_impl(internal::StoreChunkConfig);

include/openPMD/RecordComponent.tpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "openPMD/auxiliary/UniquePtr.hpp"
3232

3333
#include <memory>
34+
#include <optional>
3435
#include <type_traits>
3536

3637
namespace openPMD
@@ -187,17 +188,11 @@ template <typename T>
187188
inline void
188189
RecordComponent::storeChunk(std::shared_ptr<T> data, Offset o, Extent e)
189190
{
190-
if (!data)
191-
throw std::runtime_error(
192-
"Unallocated pointer passed during chunk store.");
193-
Datatype dtype = determineDatatype(data);
194-
195-
/* std::static_pointer_cast correctly reference-counts the pointer */
196-
storeChunk(
197-
auxiliary::WriteBuffer(std::static_pointer_cast<void const>(data)),
198-
dtype,
199-
std::move(o),
200-
std::move(e));
191+
prepareStoreChunk()
192+
.offset(std::move(o))
193+
.extent(std::move(e))
194+
.fromSharedPtr(std::move(data))
195+
.enqueue();
201196
}
202197

203198
template <typename T>
@@ -209,11 +204,10 @@ RecordComponent::storeChunk(UniquePtrWithLambda<T> data, Offset o, Extent e)
209204
"Unallocated pointer passed during chunk store.");
210205
Datatype dtype = determineDatatype<>(data);
211206

212-
storeChunk(
207+
storeChunk_impl(
213208
auxiliary::WriteBuffer{std::move(data).template static_cast_<void>()},
214209
dtype,
215-
std::move(o),
216-
std::move(e));
210+
{std::move(o), std::move(e), std::nullopt});
217211
}
218212

219213
template <typename T, typename Del>

include/openPMD/benchmark/mpi/MPIBenchmark.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ MPIBenchmark<DatasetFillerProvider>::runBenchmark(int rootThread)
239239
}
240240
for (Datatype dt : datatypes)
241241
{
242-
switchType<BenchmarkExecution<Clock>>(dt, exec, res, rootThread);
242+
switchDatasetType<BenchmarkExecution<Clock>>(dt, exec, res, rootThread);
243243
}
244244

245245
return res;

src/LoadStoreChunk.cpp

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
11

22

33
#include "openPMD/LoadStoreChunk.hpp"
4+
#include "openPMD/Datatype.hpp"
45
#include "openPMD/RecordComponent.hpp"
56
#include "openPMD/Span.hpp"
7+
#include "openPMD/auxiliary/Memory.hpp"
8+
#include "openPMD/auxiliary/ShareRawInternal.hpp"
9+
#include "openPMD/auxiliary/TypeTraits.hpp"
610
#include "openPMD/auxiliary/UniquePtr.hpp"
711

812
// comment to keep clang-format from reordering
913
#include "openPMD/DatatypeMacros.hpp"
1014

15+
#include <memory>
1116
#include <stdexcept>
1217

1318
namespace openPMD
1419
{
20+
21+
namespace internal
22+
{
23+
ConfigureStoreChunkData::ConfigureStoreChunkData(RecordComponent &rc)
24+
: m_rc(rc)
25+
{}
26+
} // namespace internal
27+
28+
namespace
29+
{
30+
template <typename T>
31+
auto asWriteBuffer(std::shared_ptr<T> &&ptr) -> auxiliary::WriteBuffer
32+
{
33+
/* std::static_pointer_cast correctly reference-counts the pointer */
34+
return auxiliary::WriteBuffer(
35+
std::static_pointer_cast<void const>(std::move(ptr)));
36+
}
37+
template <typename T>
38+
auto asWriteBuffer(UniquePtrWithLambda<T> &&ptr) -> auxiliary::WriteBuffer
39+
{
40+
return auxiliary::WriteBuffer{
41+
std::move(ptr).template static_cast_<void const>()};
42+
}
43+
} // namespace
44+
1545
template <typename ChildClass>
1646
ConfigureStoreChunk<ChildClass>::ConfigureStoreChunk(RecordComponent &rc)
17-
: m_rc(rc)
47+
: ConfigureStoreChunkData(rc)
48+
{}
49+
50+
template <typename ChildClass>
51+
ConfigureStoreChunk<ChildClass>::ConfigureStoreChunk(
52+
internal::ConfigureStoreChunkData &&data)
53+
: ConfigureStoreChunkData(std::move(data))
1854
{}
1955

2056
template <typename ChildClass>
@@ -67,14 +103,14 @@ template <typename ChildClass>
67103
auto ConfigureStoreChunk<ChildClass>::extent(Extent extent) -> return_type &
68104
{
69105
m_extent = std::move(extent);
70-
return *this;
106+
return *static_cast<return_type *>(this);
71107
}
72108

73109
template <typename ChildClass>
74110
auto ConfigureStoreChunk<ChildClass>::offset(Offset offset) -> return_type &
75111
{
76112
m_offset = std::move(offset);
77-
return *this;
113+
return *static_cast<return_type *>(this);
78114
}
79115

80116
template <typename ChildClass>
@@ -84,28 +120,6 @@ auto ConfigureStoreChunk<ChildClass>::enqueue() -> DynamicMemoryView<T>
84120
return m_rc.storeChunkSpan_impl<T>(storeChunkConfig());
85121
}
86122

87-
template <typename ChildClass>
88-
template <typename T>
89-
auto ConfigureStoreChunk<ChildClass>::fromSharedPtr(
90-
std::shared_ptr<T>) && -> TypedConfigureStoreChunk<std::shared_ptr<T>>
91-
{
92-
throw std::runtime_error("Unimplemented!");
93-
}
94-
template <typename ChildClass>
95-
template <typename T>
96-
auto ConfigureStoreChunk<ChildClass>::fromUniquePtr(UniquePtrWithLambda<T>)
97-
&& -> TypedConfigureStoreChunk<UniquePtrWithLambda<T>>
98-
{
99-
throw std::runtime_error("Unimplemented!");
100-
}
101-
template <typename ChildClass>
102-
template <typename T>
103-
auto ConfigureStoreChunk<ChildClass>::fromRawPtr(
104-
T *) && -> TypedConfigureStoreChunk<std::shared_ptr<T>>
105-
{
106-
throw std::runtime_error("Unimplemented!");
107-
}
108-
109123
template <typename Ptr_Type>
110124
TypedConfigureStoreChunk<Ptr_Type>::TypedConfigureStoreChunk(
111125
Ptr_Type buffer, parent_t &&parent)
@@ -128,14 +142,25 @@ auto TypedConfigureStoreChunk<Ptr_Type>::as_parent() const & -> parent_t const &
128142
return *this;
129143
}
130144

145+
template <typename Ptr_Type>
146+
auto TypedConfigureStoreChunk<Ptr_Type>::storeChunkConfig() const
147+
-> internal::StoreChunkConfigFromBuffer
148+
{
149+
return internal::StoreChunkConfigFromBuffer{
150+
this->getOffset(), this->getExtent(), m_mem_select};
151+
}
152+
153+
template <typename Ptr_Type>
154+
auto TypedConfigureStoreChunk<Ptr_Type>::enqueue() -> void
155+
{
156+
this->m_rc.storeChunk_impl(
157+
asWriteBuffer(std::move(m_buffer)),
158+
determineDatatype<auxiliary::IsPointer_t<Ptr_Type>>(),
159+
storeChunkConfig());
160+
}
161+
131162
#define INSTANTIATE_METHOD_TEMPLATES(base_class, dtype) \
132-
template auto base_class::enqueue() -> DynamicMemoryView<dtype>; \
133-
template auto base_class::fromSharedPtr(std::shared_ptr<dtype>) \
134-
&& -> TypedConfigureStoreChunk<std::shared_ptr<dtype>>; \
135-
template auto base_class::fromUniquePtr(UniquePtrWithLambda<dtype>) \
136-
&& -> TypedConfigureStoreChunk<UniquePtrWithLambda<dtype>>; \
137-
template auto base_class::fromRawPtr( \
138-
dtype *) && -> TypedConfigureStoreChunk<std::shared_ptr<dtype>>;
163+
template auto base_class::enqueue() -> DynamicMemoryView<dtype>;
139164

140165
#define INSTANTIATE_METHOD_TEMPLATES_FOR_BASE(dtype) \
141166
INSTANTIATE_METHOD_TEMPLATES(ConfigureStoreChunk<void>, dtype)
@@ -146,14 +171,19 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
146171
#undef INSTANTIATE_METHOD_TEMPLATES_FOR_BASE
147172

148173
#define INSTANTIATE_TYPED_STORE_CHUNK(dtype) \
149-
template class TypedConfigureStoreChunk<std::shared_ptr<dtype>>; \
174+
template class TypedConfigureStoreChunk<std::shared_ptr<dtype const>>; \
175+
template class ConfigureStoreChunk< \
176+
TypedConfigureStoreChunk<std::shared_ptr<dtype const>>>; \
150177
INSTANTIATE_METHOD_TEMPLATES( \
151-
ConfigureStoreChunk<TypedConfigureStoreChunk<std::shared_ptr<dtype>>>, \
178+
ConfigureStoreChunk< \
179+
TypedConfigureStoreChunk<std::shared_ptr<dtype const>>>, \
152180
dtype) \
153-
template class TypedConfigureStoreChunk<UniquePtrWithLambda<dtype>>; \
181+
template class TypedConfigureStoreChunk<UniquePtrWithLambda<dtype const>>; \
182+
template class ConfigureStoreChunk< \
183+
TypedConfigureStoreChunk<UniquePtrWithLambda<dtype const>>>; \
154184
INSTANTIATE_METHOD_TEMPLATES( \
155185
ConfigureStoreChunk< \
156-
TypedConfigureStoreChunk<UniquePtrWithLambda<dtype>>>, \
186+
TypedConfigureStoreChunk<UniquePtrWithLambda<dtype const>>>, \
157187
dtype)
158188

159189
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_TYPED_STORE_CHUNK)

src/RecordComponent.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,12 @@ void RecordComponent::readBase(bool require_unit_si)
499499
}
500500
}
501501

502-
void RecordComponent::storeChunk(
503-
auxiliary::WriteBuffer buffer, Datatype dtype, Offset o, Extent e)
502+
void RecordComponent::storeChunk_impl(
503+
auxiliary::WriteBuffer buffer,
504+
Datatype dtype,
505+
internal::StoreChunkConfigFromBuffer cfg)
504506
{
507+
auto [o, e, memorySelection] = std::move(cfg);
505508
verifyChunk(dtype, o, e);
506509

507510
Parameter<Operation::WRITE_DATASET> dWrite;

0 commit comments

Comments
 (0)