Skip to content

Commit ddcdfc9

Browse files
committed
Prepare class structure for load_chunk
1 parent 5f45fa0 commit ddcdfc9

File tree

4 files changed

+103
-66
lines changed

4 files changed

+103
-66
lines changed

include/openPMD/LoadStoreChunk.hpp

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
namespace openPMD
1212
{
1313
class RecordComponent;
14-
template <typename Ptr_Type>
14+
template <typename Ptr_Type, typename ChildClass>
1515
class ConfigureStoreChunkFromBuffer;
16+
template <typename Ptr_Type>
17+
class ConfigureLoadStoreFromBuffer;
1618
template <typename T>
1719
class DynamicMemoryView;
1820

@@ -67,46 +69,53 @@ class ConfigureLoadStore : protected internal::ConfigureStoreChunkData
6769
auto offset(Offset) -> return_type &;
6870
auto extent(Extent) -> return_type &;
6971

72+
template <typename T>
73+
using shared_ptr_return_type = ConfigureLoadStoreFromBuffer<
74+
std::shared_ptr<normalize_dataset_type<T> const>>;
75+
template <typename T>
76+
using unique_ptr_return_type = ConfigureStoreChunkFromBuffer<
77+
UniquePtrWithLambda<normalize_dataset_type<T>>,
78+
void>;
79+
7080
// @todo rvalue references..?
7181
template <typename T>
72-
auto fromSharedPtr(std::shared_ptr<T>)
73-
-> ConfigureStoreChunkFromBuffer<
74-
std::shared_ptr<normalize_dataset_type<T> const>>;
82+
auto fromSharedPtr(std::shared_ptr<T>) -> shared_ptr_return_type<T>;
7583
template <typename T>
76-
auto fromUniquePtr(UniquePtrWithLambda<T>)
77-
-> ConfigureStoreChunkFromBuffer<
78-
UniquePtrWithLambda<normalize_dataset_type<T>>>;
84+
auto fromUniquePtr(UniquePtrWithLambda<T>) -> unique_ptr_return_type<T>;
7985
template <typename T, typename Del>
80-
auto fromUniquePtr(std::unique_ptr<T, Del>)
81-
-> ConfigureStoreChunkFromBuffer<
82-
UniquePtrWithLambda<normalize_dataset_type<T>>>;
86+
auto fromUniquePtr(std::unique_ptr<T, Del>) -> unique_ptr_return_type<T>;
8387
template <typename T>
84-
auto
85-
fromRawPtr(T *data) -> ConfigureStoreChunkFromBuffer<
86-
std::shared_ptr<normalize_dataset_type<T> const>>;
88+
auto fromRawPtr(T *data) -> shared_ptr_return_type<T>;
8789
template <typename T_ContiguousContainer>
88-
auto fromContiguousContainer(T_ContiguousContainer &data) ->
89-
typename std::enable_if_t<
90+
auto fromContiguousContainer(T_ContiguousContainer &data)
91+
-> std::enable_if_t<
9092
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
91-
ConfigureStoreChunkFromBuffer<
92-
std::shared_ptr<normalize_dataset_type<
93-
typename T_ContiguousContainer::value_type> const>>>;
93+
shared_ptr_return_type<typename T_ContiguousContainer::value_type>>;
9494

9595
template <typename T>
9696
[[nodiscard]] auto enqueueStore() -> DynamicMemoryView<T>;
9797
// definition for this one is in RecordComponent.tpp since it needs the
9898
// definition of class RecordComponent.
9999
template <typename T, typename F>
100100
[[nodiscard]] auto enqueueStore(F &&createBuffer) -> DynamicMemoryView<T>;
101+
102+
template <typename T>
103+
[[nodiscard]] auto enqueueLoad() -> std::shared_ptr<T>;
101104
};
102105

103-
template <typename Ptr_Type>
106+
template <typename Ptr_Type, typename ChildClass = void>
104107
class ConfigureStoreChunkFromBuffer
105-
: public ConfigureLoadStore<ConfigureStoreChunkFromBuffer<Ptr_Type>>
108+
: public ConfigureLoadStore<std::conditional_t<
109+
std::is_void_v<ChildClass>,
110+
/*then*/ ConfigureStoreChunkFromBuffer<Ptr_Type, void>,
111+
/*else*/ ChildClass>>
106112
{
107113
public:
108-
using parent_t =
109-
ConfigureLoadStore<ConfigureStoreChunkFromBuffer<Ptr_Type>>;
114+
using return_type = std::conditional_t<
115+
std::is_void_v<ChildClass>,
116+
/*then*/ ConfigureStoreChunkFromBuffer<Ptr_Type, void>,
117+
/*else*/ ChildClass>;
118+
using parent_t = ConfigureLoadStore<return_type>;
110119

111120
private:
112121
template <typename T>
@@ -115,19 +124,38 @@ class ConfigureStoreChunkFromBuffer
115124
Ptr_Type m_buffer;
116125
std::optional<MemorySelection> m_mem_select;
117126

118-
ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&);
119-
120127
auto storeChunkConfig() const -> internal::StoreChunkConfigFromBuffer;
121128

129+
protected:
130+
ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&);
131+
122132
public:
123-
auto memorySelection(MemorySelection) -> ConfigureStoreChunkFromBuffer &;
133+
auto memorySelection(MemorySelection) -> return_type &;
124134

125135
auto as_parent() && -> parent_t &&;
126136
auto as_parent() & -> parent_t &;
127137
auto as_parent() const & -> parent_t const &;
128138

129139
auto enqueueStore() -> void;
130140
};
141+
142+
template <typename Ptr_Type>
143+
class ConfigureLoadStoreFromBuffer
144+
: public ConfigureStoreChunkFromBuffer<
145+
Ptr_Type,
146+
ConfigureLoadStoreFromBuffer<Ptr_Type>>
147+
{
148+
using parent_t = ConfigureStoreChunkFromBuffer<
149+
Ptr_Type,
150+
ConfigureLoadStoreFromBuffer<Ptr_Type>>;
151+
template <typename>
152+
friend class ConfigureLoadStore;
153+
ConfigureLoadStoreFromBuffer(
154+
Ptr_Type buffer, typename parent_t::parent_t &&);
155+
156+
public:
157+
auto enqueueLoad() -> void;
158+
};
131159
} // namespace openPMD
132160

133161
#include "openPMD/LoadStoreChunk.tpp"

include/openPMD/LoadStoreChunk.tpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,64 @@
44

55
namespace openPMD
66
{
7-
87
template <typename ChildClass>
98
template <typename T>
109
auto ConfigureLoadStore<ChildClass>::fromSharedPtr(std::shared_ptr<T> data)
11-
-> ConfigureStoreChunkFromBuffer<
12-
std::shared_ptr<normalize_dataset_type<T> const>>
10+
-> shared_ptr_return_type<T>
1311
{
1412
if (!data)
1513
{
1614
throw std::runtime_error(
1715
"Unallocated pointer passed during chunk store.");
1816
}
19-
return ConfigureStoreChunkFromBuffer<
20-
std::shared_ptr<normalize_dataset_type<T> const>>(
17+
return shared_ptr_return_type<T>(
2118
std::static_pointer_cast<normalize_dataset_type<T> const>(
2219
std::move(data)),
2320
{std::move(*this)});
2421
}
2522
template <typename ChildClass>
2623
template <typename T>
2724
auto ConfigureLoadStore<ChildClass>::fromUniquePtr(UniquePtrWithLambda<T> data)
28-
-> ConfigureStoreChunkFromBuffer<
29-
UniquePtrWithLambda<normalize_dataset_type<T>>>
25+
-> unique_ptr_return_type<T>
26+
3027
{
3128
if (!data)
3229
{
3330
throw std::runtime_error(
3431
"Unallocated pointer passed during chunk store.");
3532
}
36-
return ConfigureStoreChunkFromBuffer<
37-
UniquePtrWithLambda<normalize_dataset_type<T>>>(
33+
return unique_ptr_return_type<T>(
3834
std::move(data).template static_cast_<normalize_dataset_type<T>>(),
3935
{std::move(*this)});
4036
}
4137
template <typename ChildClass>
4238
template <typename T>
4339
auto ConfigureLoadStore<ChildClass>::fromRawPtr(T *data)
44-
-> ConfigureStoreChunkFromBuffer<
45-
std::shared_ptr<normalize_dataset_type<T> const>>
40+
-> shared_ptr_return_type<T>
4641
{
4742
if (!data)
4843
{
4944
throw std::runtime_error(
5045
"Unallocated pointer passed during chunk store.");
5146
}
52-
return ConfigureStoreChunkFromBuffer<
53-
std::shared_ptr<normalize_dataset_type<T> const>>(
47+
return shared_ptr_return_type<T>(
5448
auxiliary::shareRaw(data), {std::move(*this)});
5549
}
5650

5751
template <typename ChildClass>
5852
template <typename T, typename Del>
5953
auto ConfigureLoadStore<ChildClass>::fromUniquePtr(std::unique_ptr<T, Del> data)
60-
-> ConfigureStoreChunkFromBuffer<
61-
UniquePtrWithLambda<normalize_dataset_type<T>>>
54+
-> unique_ptr_return_type<T>
6255
{
6356
return fromUniquePtr(UniquePtrWithLambda<T>(std::move(data)));
6457
}
6558
template <typename ChildClass>
6659
template <typename T_ContiguousContainer>
6760
auto ConfigureLoadStore<ChildClass>::fromContiguousContainer(
68-
T_ContiguousContainer &data) ->
69-
typename std::enable_if_t<
61+
T_ContiguousContainer &data)
62+
-> std::enable_if_t<
7063
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
71-
ConfigureStoreChunkFromBuffer<std::shared_ptr<normalize_dataset_type<
72-
typename T_ContiguousContainer::value_type> const>>>
64+
shared_ptr_return_type<typename T_ContiguousContainer::value_type>>
7365
{
7466
if (!m_extent.has_value() && dim() == 1)
7567
{

include/openPMD/RecordComponent.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class RecordComponent : public BaseRecordComponent
135135
friend class MeshRecordComponent;
136136
template <typename ChildClass>
137137
friend class ConfigureLoadStore;
138-
template <typename Ptr_Type>
138+
template <typename Ptr_Type, typename ChildClass>
139139
friend class ConfigureStoreChunkFromBuffer;
140140

141141
public:

src/LoadStoreChunk.cpp

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,54 +121,69 @@ auto ConfigureLoadStore<ChildClass>::enqueueStore() -> DynamicMemoryView<T>
121121
return m_rc.storeChunkSpan_impl<T>(storeChunkConfig());
122122
}
123123

124-
template <typename Ptr_Type>
125-
ConfigureStoreChunkFromBuffer<Ptr_Type>::ConfigureStoreChunkFromBuffer(
126-
Ptr_Type buffer, parent_t &&parent)
124+
template <typename Ptr_Type, typename ChildClass>
125+
ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::
126+
ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&parent)
127127
: parent_t(std::move(parent)), m_buffer(std::move(buffer))
128128
{}
129129

130-
template <typename Ptr_Type>
131-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent() && -> parent_t &&
130+
template <typename Ptr_Type, typename ChildClass>
131+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
132+
&& -> parent_t &&
132133
{
133134
return std::move(*this);
134135
}
135-
template <typename Ptr_Type>
136-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent() & -> parent_t &
136+
template <typename Ptr_Type, typename ChildClass>
137+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
138+
& -> parent_t &
137139
{
138140
return *this;
139141
}
140-
template <typename Ptr_Type>
141-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent()
142+
template <typename Ptr_Type, typename ChildClass>
143+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
142144
const & -> parent_t const &
143145
{
144146
return *this;
145147
}
146148

147-
template <typename Ptr_Type>
148-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::storeChunkConfig() const
149-
-> internal::StoreChunkConfigFromBuffer
149+
template <typename Ptr_Type, typename ChildClass>
150+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::storeChunkConfig()
151+
const -> internal::StoreChunkConfigFromBuffer
150152
{
151153
return internal::StoreChunkConfigFromBuffer{
152154
this->getOffset(), this->getExtent(), m_mem_select};
153155
}
154156

155-
template <typename Ptr_Type>
156-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::memorySelection(
157-
MemorySelection sel) -> ConfigureStoreChunkFromBuffer &
157+
template <typename Ptr_Type, typename ChildClass>
158+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::memorySelection(
159+
MemorySelection sel) -> return_type &
158160
{
159161
this->m_mem_select = std::make_optional<MemorySelection>(std::move(sel));
160-
return *this;
162+
return *static_cast<return_type *>(this);
161163
}
162164

163-
template <typename Ptr_Type>
164-
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::enqueueStore() -> void
165+
template <typename Ptr_Type, typename ChildClass>
166+
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::enqueueStore() -> void
165167
{
166168
this->m_rc.storeChunk_impl(
167169
asWriteBuffer(std::move(m_buffer)),
168170
determineDatatype<auxiliary::IsPointer_t<Ptr_Type>>(),
169171
storeChunkConfig());
170172
}
171173

174+
template <typename Ptr_Type>
175+
ConfigureLoadStoreFromBuffer<Ptr_Type>::ConfigureLoadStoreFromBuffer(
176+
Ptr_Type buffer, typename parent_t::parent_t &&parent)
177+
: parent_t(std::move(buffer), std::move(parent))
178+
{
179+
static_assert(
180+
std::is_same_v<
181+
Ptr_Type,
182+
std::shared_ptr<typename Ptr_Type::element_type>>,
183+
"ConfigureLoadStoreFromBuffer must be instantiated with a shared_ptr "
184+
"type.");
185+
}
186+
172187
#define INSTANTIATE_METHOD_TEMPLATES(base_class, dtype) \
173188
template auto base_class::enqueueStore() -> DynamicMemoryView<dtype>;
174189

@@ -181,13 +196,15 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
181196
#undef INSTANTIATE_METHOD_TEMPLATES_FOR_BASE
182197

183198
#define INSTANTIATE_STORE_CHUNK_FROM_BUFFER(dtype) \
199+
template class ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>; \
184200
template class ConfigureStoreChunkFromBuffer< \
185-
std::shared_ptr<dtype const>>; \
201+
std::shared_ptr<dtype const>, \
202+
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>; \
186203
template class ConfigureLoadStore< \
187-
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>; \
204+
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>; \
188205
INSTANTIATE_METHOD_TEMPLATES( \
189206
ConfigureLoadStore< \
190-
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>, \
207+
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>, \
191208
dtype) \
192209
template class ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype>>; \
193210
template class ConfigureLoadStore< \

0 commit comments

Comments
 (0)