Skip to content

Commit

Permalink
Prepare class structure for load_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed May 22, 2024
1 parent 5f45fa0 commit 727cbbc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 66 deletions.
75 changes: 50 additions & 25 deletions include/openPMD/LoadStoreChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
namespace openPMD
{
class RecordComponent;
template <typename Ptr_Type>
template <typename Ptr_Type, typename ChildClass>
class ConfigureStoreChunkFromBuffer;
template <typename Ptr_Type>
class ConfigureLoadStoreFromBuffer;
template <typename T>
class DynamicMemoryView;

Expand Down Expand Up @@ -67,30 +69,28 @@ class ConfigureLoadStore : protected internal::ConfigureStoreChunkData
auto offset(Offset) -> return_type &;
auto extent(Extent) -> return_type &;

template <typename T>
using shared_ptr_return_type = ConfigureLoadStoreFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>;
template <typename T>
using unique_ptr_return_type = ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>,
void>;

// @todo rvalue references..?
template <typename T>
auto fromSharedPtr(std::shared_ptr<T>)
-> ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>;
auto fromSharedPtr(std::shared_ptr<T>) -> shared_ptr_return_type<T>;
template <typename T>
auto fromUniquePtr(UniquePtrWithLambda<T>)
-> ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>>;
auto fromUniquePtr(UniquePtrWithLambda<T>) -> unique_ptr_return_type<T>;
template <typename T, typename Del>
auto fromUniquePtr(std::unique_ptr<T, Del>)
-> ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>>;
auto fromUniquePtr(std::unique_ptr<T, Del>) -> unique_ptr_return_type<T>;
template <typename T>
auto
fromRawPtr(T *data) -> ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>;
auto fromRawPtr(T *data) -> shared_ptr_return_type<T>;
template <typename T_ContiguousContainer>
auto fromContiguousContainer(T_ContiguousContainer &data) ->
typename std::enable_if_t<
auto fromContiguousContainer(T_ContiguousContainer &data)
-> std::enable_if_t<
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<
typename T_ContiguousContainer::value_type> const>>>;
shared_ptr_return_type<typename T_ContiguousContainer::value_type>>;

template <typename T>
[[nodiscard]] auto enqueueStore() -> DynamicMemoryView<T>;
Expand All @@ -100,13 +100,19 @@ class ConfigureLoadStore : protected internal::ConfigureStoreChunkData
[[nodiscard]] auto enqueueStore(F &&createBuffer) -> DynamicMemoryView<T>;
};

template <typename Ptr_Type>
template <typename Ptr_Type, typename ChildClass = void>
class ConfigureStoreChunkFromBuffer
: public ConfigureLoadStore<ConfigureStoreChunkFromBuffer<Ptr_Type>>
: public ConfigureLoadStore<std::conditional_t<
std::is_void_v<ChildClass>,
/*then*/ ConfigureStoreChunkFromBuffer<Ptr_Type, void>,
/*else*/ ChildClass>>
{
public:
using parent_t =
ConfigureLoadStore<ConfigureStoreChunkFromBuffer<Ptr_Type>>;
using return_type = std::conditional_t<
std::is_void_v<ChildClass>,
/*then*/ ConfigureStoreChunkFromBuffer<Ptr_Type, void>,
/*else*/ ChildClass>;
using parent_t = ConfigureLoadStore<return_type>;

private:
template <typename T>
Expand All @@ -115,19 +121,38 @@ class ConfigureStoreChunkFromBuffer
Ptr_Type m_buffer;
std::optional<MemorySelection> m_mem_select;

ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&);

auto storeChunkConfig() const -> internal::StoreChunkConfigFromBuffer;

protected:
ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&);

public:
auto memorySelection(MemorySelection) -> ConfigureStoreChunkFromBuffer &;
auto memorySelection(MemorySelection) -> return_type &;

auto as_parent() && -> parent_t &&;
auto as_parent() & -> parent_t &;
auto as_parent() const & -> parent_t const &;

auto enqueueStore() -> void;
};

template <typename Ptr_Type>
class ConfigureLoadStoreFromBuffer
: public ConfigureStoreChunkFromBuffer<
Ptr_Type,
ConfigureLoadStoreFromBuffer<Ptr_Type>>
{
using parent_t = ConfigureStoreChunkFromBuffer<
Ptr_Type,
ConfigureLoadStoreFromBuffer<Ptr_Type>>;
template <typename>
friend class ConfigureLoadStore;
ConfigureLoadStoreFromBuffer(
Ptr_Type buffer, typename parent_t::parent_t &&);

public:
auto enqueueLoad() -> void;
};
} // namespace openPMD

#include "openPMD/LoadStoreChunk.tpp"
30 changes: 11 additions & 19 deletions include/openPMD/LoadStoreChunk.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,64 @@

namespace openPMD
{

template <typename ChildClass>
template <typename T>
auto ConfigureLoadStore<ChildClass>::fromSharedPtr(std::shared_ptr<T> data)
-> ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>
-> shared_ptr_return_type<T>
{
if (!data)
{
throw std::runtime_error(
"Unallocated pointer passed during chunk store.");
}
return ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>(
return shared_ptr_return_type<T>(
std::static_pointer_cast<normalize_dataset_type<T> const>(
std::move(data)),
{std::move(*this)});
}
template <typename ChildClass>
template <typename T>
auto ConfigureLoadStore<ChildClass>::fromUniquePtr(UniquePtrWithLambda<T> data)
-> ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>>
-> unique_ptr_return_type<T>

{
if (!data)
{
throw std::runtime_error(
"Unallocated pointer passed during chunk store.");
}
return ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>>(
return unique_ptr_return_type<T>(
std::move(data).template static_cast_<normalize_dataset_type<T>>(),
{std::move(*this)});
}
template <typename ChildClass>
template <typename T>
auto ConfigureLoadStore<ChildClass>::fromRawPtr(T *data)
-> ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>
-> shared_ptr_return_type<T>
{
if (!data)
{
throw std::runtime_error(
"Unallocated pointer passed during chunk store.");
}
return ConfigureStoreChunkFromBuffer<
std::shared_ptr<normalize_dataset_type<T> const>>(
return shared_ptr_return_type<T>(
auxiliary::shareRaw(data), {std::move(*this)});
}

template <typename ChildClass>
template <typename T, typename Del>
auto ConfigureLoadStore<ChildClass>::fromUniquePtr(std::unique_ptr<T, Del> data)
-> ConfigureStoreChunkFromBuffer<
UniquePtrWithLambda<normalize_dataset_type<T>>>
-> unique_ptr_return_type<T>
{
return fromUniquePtr(UniquePtrWithLambda<T>(std::move(data)));
}
template <typename ChildClass>
template <typename T_ContiguousContainer>
auto ConfigureLoadStore<ChildClass>::fromContiguousContainer(
T_ContiguousContainer &data) ->
typename std::enable_if_t<
T_ContiguousContainer &data)
-> std::enable_if_t<
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,
ConfigureStoreChunkFromBuffer<std::shared_ptr<normalize_dataset_type<
typename T_ContiguousContainer::value_type> const>>>
shared_ptr_return_type<typename T_ContiguousContainer::value_type>>
{
if (!m_extent.has_value() && dim() == 1)
{
Expand Down
2 changes: 1 addition & 1 deletion include/openPMD/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class RecordComponent : public BaseRecordComponent
friend class MeshRecordComponent;
template <typename ChildClass>
friend class ConfigureLoadStore;
template <typename Ptr_Type>
template <typename Ptr_Type, typename ChildClass>
friend class ConfigureStoreChunkFromBuffer;

public:
Expand Down
52 changes: 31 additions & 21 deletions src/LoadStoreChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,54 +121,62 @@ auto ConfigureLoadStore<ChildClass>::enqueueStore() -> DynamicMemoryView<T>
return m_rc.storeChunkSpan_impl<T>(storeChunkConfig());
}

template <typename Ptr_Type>
ConfigureStoreChunkFromBuffer<Ptr_Type>::ConfigureStoreChunkFromBuffer(
Ptr_Type buffer, parent_t &&parent)
template <typename Ptr_Type, typename ChildClass>
ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::
ConfigureStoreChunkFromBuffer(Ptr_Type buffer, parent_t &&parent)
: parent_t(std::move(parent)), m_buffer(std::move(buffer))
{}

template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent() && -> parent_t &&
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
&& -> parent_t &&
{
return std::move(*this);
}
template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent() & -> parent_t &
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
& -> parent_t &
{
return *this;
}
template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::as_parent()
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::as_parent()
const & -> parent_t const &
{
return *this;
}

template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::storeChunkConfig() const
-> internal::StoreChunkConfigFromBuffer
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::storeChunkConfig()
const -> internal::StoreChunkConfigFromBuffer
{
return internal::StoreChunkConfigFromBuffer{
this->getOffset(), this->getExtent(), m_mem_select};
}

template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::memorySelection(
MemorySelection sel) -> ConfigureStoreChunkFromBuffer &
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::memorySelection(
MemorySelection sel) -> return_type &
{
this->m_mem_select = std::make_optional<MemorySelection>(std::move(sel));
return *this;
return *static_cast<return_type *>(this);
}

template <typename Ptr_Type>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::enqueueStore() -> void
template <typename Ptr_Type, typename ChildClass>
auto ConfigureStoreChunkFromBuffer<Ptr_Type, ChildClass>::enqueueStore() -> void
{
this->m_rc.storeChunk_impl(
asWriteBuffer(std::move(m_buffer)),
determineDatatype<auxiliary::IsPointer_t<Ptr_Type>>(),
storeChunkConfig());
}

template <typename Ptr_Type>
ConfigureLoadStoreFromBuffer<Ptr_Type>::ConfigureLoadStoreFromBuffer(
Ptr_Type buffer, typename parent_t::parent_t &&parent)
: parent_t(std::move(buffer), std::move(parent))
{}

#define INSTANTIATE_METHOD_TEMPLATES(base_class, dtype) \
template auto base_class::enqueueStore() -> DynamicMemoryView<dtype>;

Expand All @@ -181,13 +189,15 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
#undef INSTANTIATE_METHOD_TEMPLATES_FOR_BASE

#define INSTANTIATE_STORE_CHUNK_FROM_BUFFER(dtype) \
template class ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>; \
template class ConfigureStoreChunkFromBuffer< \
std::shared_ptr<dtype const>>; \
std::shared_ptr<dtype const>, \
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>; \
template class ConfigureLoadStore< \
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>; \
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>; \
INSTANTIATE_METHOD_TEMPLATES( \
ConfigureLoadStore< \
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>, \
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype const>>>, \
dtype) \
template class ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype>>; \
template class ConfigureLoadStore< \
Expand Down

0 comments on commit 727cbbc

Please sign in to comment.