Skip to content

Commit

Permalink
Use own wrapper class DeferredFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jul 22, 2024
1 parent 5fc0bab commit 43890f9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ set(CORE_SOURCE
src/WriteIterations.cpp
src/auxiliary/Date.cpp
src/auxiliary/Filesystem.cpp
src/auxiliary/Future.cpp
src/auxiliary/JSON.cpp
src/auxiliary/Mpi.cpp
src/backend/Attributable.cpp
Expand Down
11 changes: 7 additions & 4 deletions include/openPMD/LoadStoreChunk.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "openPMD/Dataset.hpp"
#include "openPMD/auxiliary/Future.hpp"
#include "openPMD/auxiliary/ShareRawInternal.hpp"
#include "openPMD/auxiliary/UniquePtr.hpp"

Expand Down Expand Up @@ -141,13 +142,15 @@ namespace core
enqueueStore(F &&createBuffer) -> DynamicMemoryView<T>;

template <typename T>
[[nodiscard]] auto enqueueLoad() -> std::future<std::shared_ptr<T>>;
[[nodiscard]] auto
enqueueLoad() -> auxiliary::DeferredFuture<std::shared_ptr<T>>;

template <typename T>
[[nodiscard]] auto load(EnqueuePolicy) -> std::shared_ptr<T>;

[[nodiscard]] auto enqueueLoadVariant()
-> std::future<auxiliary::detail::shared_ptr_dataset_types>;
[[nodiscard]] auto
enqueueLoadVariant() -> auxiliary::DeferredFuture<
auxiliary::detail::shared_ptr_dataset_types>;

[[nodiscard]] auto loadVariant(EnqueuePolicy)
-> auxiliary::detail::shared_ptr_dataset_types;
Expand All @@ -164,7 +167,7 @@ namespace core

auto storeChunkConfig() -> internal::LoadStoreConfigWithBuffer;

auto enqueueStore() -> std::future<void>;
auto enqueueStore() -> auxiliary::DeferredFuture<void>;

auto store(EnqueuePolicy) -> void;

Expand Down
26 changes: 26 additions & 0 deletions include/openPMD/auxiliary/Future.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "openPMD/auxiliary/TypeTraits.hpp"

#include <future>

namespace openPMD::auxiliary
{
template <typename T>
class DeferredFuture
{
using task_type = std::packaged_task<T()>;
using future_type = std::future<T>;
future_type m_future;
task_type m_task;

public:
DeferredFuture(task_type);

auto get() -> T;

[[nodiscard]] auto valid() const noexcept -> bool;

auto wait() -> void;
};
} // namespace openPMD::auxiliary
36 changes: 19 additions & 17 deletions src/LoadStoreChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ namespace core
}

template <typename T>
auto ConfigureLoadStore::enqueueLoad() -> std::future<std::shared_ptr<T>>
auto ConfigureLoadStore::enqueueLoad()
-> auxiliary::DeferredFuture<std::shared_ptr<T>>
{
auto res = m_rc.loadChunkAllocate_impl<T>(storeChunkConfig());
return std::async(
std::launch::deferred,
return auxiliary::DeferredFuture(std::packaged_task(
[res_lambda = std::move(res), rc = m_rc]() mutable {
rc.seriesFlush();
return res_lambda;
});
}));
}

template <typename T>
Expand All @@ -143,21 +143,22 @@ namespace core
{
template <typename T>
static auto call(RecordComponent &rc, internal::LoadStoreConfig cfg)
-> std::future<auxiliary::detail::shared_ptr_dataset_types>
-> auxiliary::DeferredFuture<
auxiliary::detail::shared_ptr_dataset_types>
{
auto res = rc.loadChunkAllocate_impl<T>(std::move(cfg));
return std::async(
std::launch::deferred,
return auxiliary::DeferredFuture(std::packaged_task(
[res_lambda = std::move(res), rc_lambda = rc]() mutable
-> auxiliary::detail::shared_ptr_dataset_types {
rc_lambda.seriesFlush();
return res_lambda;
});
}));
}
};

auto ConfigureLoadStore::enqueueLoadVariant()
-> std::future<auxiliary::detail::shared_ptr_dataset_types>
-> auxiliary::DeferredFuture<
auxiliary::detail::shared_ptr_dataset_types>
{
return m_rc.visit<VisitorEnqueueLoadVariant>(this->storeChunkConfig());
}
Expand Down Expand Up @@ -202,16 +203,15 @@ namespace core
}

template <typename Ptr_Type>
auto
ConfigureStoreChunkFromBuffer<Ptr_Type>::enqueueStore() -> std::future<void>
auto ConfigureStoreChunkFromBuffer<Ptr_Type>::enqueueStore()
-> auxiliary::DeferredFuture<void>
{
this->m_rc.storeChunk_impl(
asWriteBuffer(std::move(m_buffer)),
determineDatatype<auxiliary::IsPointer_t<Ptr_Type>>(),
storeChunkConfig());
return std::async(
std::launch::deferred,
[rc_lambda = m_rc]() mutable -> void { rc_lambda.seriesFlush(); });
return auxiliary::DeferredFuture(std::packaged_task(
[rc_lambda = m_rc]() mutable -> void { rc_lambda.seriesFlush(); }));
}

template <typename Ptr_Type>
Expand Down Expand Up @@ -300,7 +300,7 @@ template class compose::ConfigureLoadStore<ConfigureLoadStore>;
template auto core::ConfigureLoadStore::enqueueStore() \
-> DynamicMemoryView<dtype>; \
template auto core::ConfigureLoadStore::enqueueLoad() \
-> std::future<std::shared_ptr<dtype>>; \
-> auxiliary::DeferredFuture<std::shared_ptr<dtype>>; \
template auto core::ConfigureLoadStore::load(EnqueuePolicy) \
->std::shared_ptr<dtype>;
// clang-format on
Expand All @@ -310,13 +310,15 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES)
#undef INSTANTIATE_METHOD_TEMPLATES

/* clang-format would destroy the NOLINT comments */
//// clang-format off
// clang-format off
#define INSTANTIATE_HALF(pointer_type) \
template class ConfigureStoreChunkFromBuffer<pointer_type>; \
template class core::ConfigureStoreChunkFromBuffer<pointer_type>; \
template class compose::ConfigureLoadStore< \
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
ConfigureStoreChunkFromBuffer<pointer_type>>; \
template class compose::ConfigureStoreChunkFromBuffer< \
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
ConfigureStoreChunkFromBuffer<pointer_type>>;
// clang-format on

Expand All @@ -325,7 +327,7 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES)
#define INSTANTIATE_FULL(pointer_type) \
INSTANTIATE_HALF(pointer_type) \
template class ConfigureLoadStoreFromBuffer<pointer_type>; \
template class core::ConfigureLoadStoreFromBuffer<pointer_type>; \
template class core::ConfigureLoadStoreFromBuffer<pointer_type>; \
template class compose::ConfigureLoadStore< \
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
ConfigureLoadStoreFromBuffer<pointer_type>>; \
Expand Down
51 changes: 51 additions & 0 deletions src/auxiliary/Future.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "openPMD/auxiliary/Future.hpp"
#include "openPMD/LoadStoreChunk.hpp"

#include <memory>

// comment

#include "openPMD/DatatypeMacros.hpp"

namespace openPMD::auxiliary
{

template <typename T>
DeferredFuture<T>::DeferredFuture(task_type task)
: m_future(task.get_future()), m_task(std::move(task))
{}

template <typename T>
auto DeferredFuture<T>::get() -> T
{
if (m_future.valid())
{
m_task();
} // else get() was already called, propagate the std::future behavior
return m_future.get();
}

template <typename T>
auto DeferredFuture<T>::valid() const noexcept -> bool
{
return m_future.valid();
}

template <typename T>
auto DeferredFuture<T>::wait() -> void
{
if (!m_task.valid())
{
m_task();
}
}

template class DeferredFuture<void>;
template class DeferredFuture<auxiliary::detail::shared_ptr_dataset_types>;
#define INSTANTIATE_FUTURE(dtype) \
template class DeferredFuture<std::shared_ptr<dtype>>;
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_FUTURE)
#undef INSTANTIATE_FUTURE
} // namespace openPMD::auxiliary

#include "openPMD/UndefDatatypeMacros.hpp"

0 comments on commit 43890f9

Please sign in to comment.