Skip to content

Commit 2dc2c68

Browse files
committed
wip: unify common code into compiled functions
1 parent 403f467 commit 2dc2c68

5 files changed

Lines changed: 145 additions & 53 deletions

File tree

include/openPMD/LoadStoreChunk.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace core
7676
friend class compose::ConfigureLoadStore;
7777
template <typename ChildClass>
7878
friend class compose::ConfigureLoadStoreFromBuffer;
79+
friend class openPMD::RecordComponent;
7980

8081
protected:
8182
ConfigureLoadStore(RecordComponent &);
@@ -89,6 +90,21 @@ namespace core
8990

9091
auto deferFlush(Attributable &);
9192

93+
private:
94+
auto withSharedPtr_impl_mut(std::shared_ptr<void> data, Datatype)
95+
-> openPMD::ConfigureLoadStoreFromBuffer;
96+
auto
97+
withSharedPtr_impl_const(std::shared_ptr<void const> data, Datatype)
98+
-> openPMD::ConfigureStoreChunkFromBuffer;
99+
auto withUniquePtr_impl_mut(UniquePtrWithLambda<void>, Datatype)
100+
-> openPMD::ConfigureStoreChunkFromBuffer;
101+
auto withUniquePtr_impl_const(UniquePtrWithLambda<void const>, Datatype)
102+
-> openPMD::ConfigureStoreChunkFromBuffer;
103+
auto withRawPtr_impl_mut(void *data, Datatype)
104+
-> openPMD::ConfigureLoadStoreFromBuffer;
105+
auto withRawPtr_impl_const(void const *data, Datatype)
106+
-> openPMD::ConfigureStoreChunkFromBuffer;
107+
92108
public:
93109
auto getOffset() -> Offset const &;
94110
auto getExtent() -> Extent const &;
@@ -99,7 +115,7 @@ namespace core
99115
template <typename T>
100116
struct shared_ptr_return_type_impl
101117
{
102-
using return_type = ConfigureLoadStoreFromBuffer;
118+
using return_type = openPMD::ConfigureLoadStoreFromBuffer;
103119
using normalize_pointer_type =
104120
std::shared_ptr<std::remove_extent_t<T>>;
105121
};
@@ -110,7 +126,7 @@ namespace core
110126
template <typename T>
111127
struct shared_ptr_return_type_impl<T const>
112128
{
113-
using return_type = ConfigureStoreChunkFromBuffer;
129+
using return_type = openPMD::ConfigureStoreChunkFromBuffer;
114130
using normalize_pointer_type =
115131
std::shared_ptr<std::remove_extent_t<T> const>;
116132
};
@@ -127,7 +143,7 @@ namespace core
127143
* simpler for unique pointers. Just remove the array extents here.
128144
*/
129145
template <typename T>
130-
using unique_ptr_return_type = ConfigureStoreChunkFromBuffer;
146+
using unique_ptr_return_type = openPMD::ConfigureStoreChunkFromBuffer;
131147
template <typename T>
132148
using unique_ptr_normalized_type =
133149
UniquePtrWithLambda<std::remove_extent_t<T>>;

include/openPMD/RecordComponent.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ class RecordComponent : public BaseRecordComponent
495495
std::shared_ptr<void>, Datatype, internal::LoadStoreConfigWithBuffer);
496496
template <typename T>
497497
std::shared_ptr<T> loadChunkAllocate_impl(internal::LoadStoreConfig);
498+
std::shared_ptr<void> loadChunkAllocate_impl(
499+
Datatype, size_t dtype_size, internal::LoadStoreConfig);
498500

499501
// clang-format off
500502
OPENPMD_protected

src/LoadStoreChunk.cpp

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,84 @@ namespace core
123123
return *m_extent;
124124
}
125125

126+
auto ConfigureLoadStore::withSharedPtr_impl_mut(
127+
std::shared_ptr<void> data, Datatype datatype)
128+
-> openPMD::ConfigureLoadStoreFromBuffer
129+
{
130+
if (!data)
131+
{
132+
throw std::runtime_error(
133+
"Unallocated pointer passed during chunk store.");
134+
}
135+
return openPMD::ConfigureLoadStoreFromBuffer(
136+
auxiliary::WriteBuffer(std::move(data)),
137+
datatype,
138+
{std::move(*this)});
139+
}
140+
auto ConfigureLoadStore::withSharedPtr_impl_const(
141+
std::shared_ptr<void const> data, Datatype datatype)
142+
-> openPMD::ConfigureStoreChunkFromBuffer
143+
{
144+
if (!data)
145+
{
146+
throw std::runtime_error(
147+
"Unallocated pointer passed during chunk store.");
148+
}
149+
return openPMD::ConfigureStoreChunkFromBuffer(
150+
auxiliary::WriteBuffer(std::move(data)),
151+
datatype,
152+
{std::move(*this)});
153+
}
154+
126155
template <typename T>
127156
auto ConfigureLoadStore::withSharedPtr(std::shared_ptr<T> data)
128157
-> shared_ptr_return_type<T>
129158
{
130159
using T_decayed = std::remove_cv_t<std::remove_extent_t<T>>;
160+
constexpr auto dtype = determineDatatype<T_decayed>();
161+
if constexpr (std::is_const_v<T>)
162+
{
163+
return withSharedPtr_impl_const(data, dtype);
164+
}
165+
else
166+
{
167+
return withSharedPtr_impl_mut(data, dtype);
168+
}
169+
}
170+
171+
auto ConfigureLoadStore::withUniquePtr_impl_mut(
172+
UniquePtrWithLambda<void> data, Datatype dtype)
173+
-> openPMD::ConfigureStoreChunkFromBuffer
174+
175+
{
176+
if (!data)
177+
{
178+
throw std::runtime_error(
179+
"Unallocated pointer passed during chunk store.");
180+
}
181+
182+
return openPMD::ConfigureStoreChunkFromBuffer(
183+
auxiliary::WriteBuffer(std::move(data)), dtype, {std::move(*this)});
184+
}
185+
auto ConfigureLoadStore::withUniquePtr_impl_const(
186+
UniquePtrWithLambda<void const> data, Datatype dtype)
187+
-> openPMD::ConfigureStoreChunkFromBuffer
188+
189+
{
131190
if (!data)
132191
{
133192
throw std::runtime_error(
134193
"Unallocated pointer passed during chunk store.");
135194
}
136-
return shared_ptr_return_type<T>(
195+
196+
void const *raw_ptr = data.get();
197+
return openPMD::ConfigureStoreChunkFromBuffer(
137198
auxiliary::WriteBuffer(
138-
std::static_pointer_cast<
139-
std::conditional_t<std::is_const_v<T>, void const, void>>(
140-
std::move(data))),
141-
determineDatatype<T_decayed>(),
199+
std::shared_ptr<void const>(
200+
raw_ptr,
201+
[data_lambda =
202+
std::move(data)](auto const *) { /* no-op */ })),
203+
dtype,
142204
{std::move(*this)});
143205
}
144206
template <typename T>
@@ -147,50 +209,63 @@ namespace core
147209

148210
{
149211
using T_decayed = std::remove_cv_t<std::remove_extent_t<T>>;
150-
if (!data)
151-
{
152-
throw std::runtime_error(
153-
"Unallocated pointer passed during chunk store.");
154-
}
212+
constexpr auto dtype = determineDatatype<T_decayed>();
155213
if constexpr (std::is_const_v<T>)
156214
{
157-
void const *raw_ptr = data.get();
158-
return unique_ptr_return_type<T>(
159-
auxiliary::WriteBuffer(
160-
std::shared_ptr<void const>(
161-
raw_ptr,
162-
[data_lambda =
163-
std::move(data)](auto const *) { /* no-op */ })),
164-
determineDatatype<T_decayed>(),
165-
{std::move(*this)});
215+
return withUniquePtr_impl_const(
216+
std::move(data).template static_cast_<void const>(), dtype);
166217
}
167218
else
168219
{
169-
return unique_ptr_return_type<T>(
170-
auxiliary::WriteBuffer(
171-
std::move(data).template static_cast_<void>()),
172-
determineDatatype<T_decayed>(),
173-
{std::move(*this)});
220+
return withUniquePtr_impl_mut(
221+
std::move(data).template static_cast_<void>(), dtype);
174222
}
175223
}
176-
template <typename T>
177-
auto ConfigureLoadStore::withRawPtr(T *data) -> shared_ptr_return_type<T>
224+
225+
auto ConfigureLoadStore::withRawPtr_impl_mut(void *data, Datatype dtype)
226+
-> openPMD::ConfigureLoadStoreFromBuffer
178227
{
179-
using T_decayed = std::remove_cv_t<std::remove_extent_t<T>>;
180228
if (!data)
181229
{
182230
throw std::runtime_error(
183231
"Unallocated pointer passed during chunk store.");
184232
}
185-
return shared_ptr_return_type<T>(
186-
auxiliary::WriteBuffer(
187-
std::static_pointer_cast<
188-
std::conditional_t<std::is_const_v<T>, void const, void>>(
189-
auxiliary::shareRaw(data))),
190-
determineDatatype<T_decayed>(),
233+
return openPMD::ConfigureLoadStoreFromBuffer(
234+
auxiliary::WriteBuffer(auxiliary::shareRaw(data)),
235+
dtype,
236+
{std::move(*this)});
237+
}
238+
239+
auto
240+
ConfigureLoadStore::withRawPtr_impl_const(void const *data, Datatype dtype)
241+
-> openPMD::ConfigureStoreChunkFromBuffer
242+
{
243+
if (!data)
244+
{
245+
throw std::runtime_error(
246+
"Unallocated pointer passed during chunk store.");
247+
}
248+
return openPMD::ConfigureStoreChunkFromBuffer(
249+
auxiliary::WriteBuffer(auxiliary::shareRaw(data)),
250+
dtype,
191251
{std::move(*this)});
192252
}
193253

254+
template <typename T>
255+
auto ConfigureLoadStore::withRawPtr(T *data) -> shared_ptr_return_type<T>
256+
{
257+
using T_decayed = std::remove_cv_t<std::remove_extent_t<T>>;
258+
constexpr auto dtype = determineDatatype<T_decayed>();
259+
if constexpr (std::is_const_v<T>)
260+
{
261+
return withRawPtr_impl_const(data, dtype);
262+
}
263+
else
264+
{
265+
return withRawPtr_impl_mut(data, dtype);
266+
}
267+
}
268+
194269
template <typename T>
195270
auto ConfigureLoadStore::enqueueStore() -> DynamicMemoryView<T>
196271
{

src/RecordComponent.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,15 @@ template <typename T_with_extent>
229229
std::shared_ptr<T_with_extent>
230230
RecordComponent::loadChunkAllocate_impl(internal::LoadStoreConfig cfg)
231231
{
232-
using T = std::remove_extent_t<T_with_extent>;
233-
// static_assert(!std::is_same_v<T, std::string>, "EVIL");
232+
using T = std::remove_cv_t<std::remove_extent_t<T_with_extent>>;
233+
auto res = loadChunkAllocate_impl(
234+
determineDatatype<T>(), sizeof(T), std::move(cfg));
235+
return std::static_pointer_cast<T_with_extent>(res);
236+
}
237+
238+
std::shared_ptr<void> RecordComponent::loadChunkAllocate_impl(
239+
Datatype dtype, size_t dtype_size, internal::LoadStoreConfig cfg)
240+
{
234241
auto [o, e] = std::move(cfg);
235242

236243
size_t numPoints = 1;
@@ -239,25 +246,16 @@ RecordComponent::loadChunkAllocate_impl(internal::LoadStoreConfig cfg)
239246
numPoints *= val;
240247
}
241248

242-
#if (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 11000) || \
243-
(defined(__apple_build_version__) && __clang_major__ < 14)
244-
auto newData = std::shared_ptr<T_with_extent>(
245-
new T[numPoints], [](T *p) { delete[] p; });
249+
auto newData =
250+
std::shared_ptr<void>(new char[numPoints * dtype_size], [](void *p) {
251+
delete[] (static_cast<char *>(p));
252+
});
246253
prepareLoadStore()
247254
.offset(std::move(o))
248255
.extent(std::move(e))
249-
.withSharedPtr(newData)
256+
.withSharedPtr_impl_mut(newData, dtype)
250257
.load(EnqueuePolicy::Defer);
251258
return newData;
252-
#else
253-
auto newData = std::shared_ptr<T[]>(new T[numPoints]);
254-
prepareLoadStore()
255-
.offset(std::move(o))
256-
.extent(std::move(e))
257-
.withSharedPtr(newData)
258-
.load(EnqueuePolicy::Defer);
259-
return std::static_pointer_cast<T_with_extent>(std::move(newData));
260-
#endif
261259
}
262260

263261
RecordComponent::RecordComponent() : BaseRecordComponent(NoInit())

src/auxiliary/UniquePtr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace auxiliary
5858

5959
OPENPMD_FOREACH_DATASET_DATATYPE(
6060
OPENPMD_INSTANTIATE_WITH_AND_WITHOUT_EXTENT)
61-
OPENPMD_INSTANTIATE(void)
61+
OPENPMD_INSTANTIATE(void) OPENPMD_INSTANTIATE(void const)
6262
#undef OPENPMD_INSTANTIATE
6363
#undef OPENPMD_INSTANTIATE_WITH_AND_WITHOUT_EXTENT
6464

@@ -108,6 +108,7 @@ OPENPMD_FOREACH_NONVECTOR_DATATYPE(OPENPMD_INSTANTIATE_WITH_AND_WITHOUT_EXTENT)
108108
// Instantiate this directly, do not instantiate the
109109
// `std::unique_ptr<void>`-based constructor.
110110
template class UniquePtrWithLambda<void>;
111+
template class UniquePtrWithLambda<void const>;
111112
#undef OPENPMD_INSTANTIATE
112113
#undef OPENPMD_INSTANTIATE_WITH_AND_WITHOUT_EXTENT
113114
#undef OPENPMD_ARRAY

0 commit comments

Comments
 (0)