From bd7b37801bbe1e6de0a0bca3d2fa8bcdd051c99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Thu, 1 Aug 2024 15:33:59 +0200 Subject: [PATCH] Don't use std::packaged_task Intel compilers having trouble --- include/openPMD/LoadStoreChunk.hpp | 6 ++-- include/openPMD/auxiliary/Future.hpp | 13 ++++----- src/LoadStoreChunk.cpp | 42 +++++++++++++-------------- src/auxiliary/Future.cpp | 43 ++++++++++++++-------------- 4 files changed, 50 insertions(+), 54 deletions(-) diff --git a/include/openPMD/LoadStoreChunk.hpp b/include/openPMD/LoadStoreChunk.hpp index 043d185994..18af717c0a 100644 --- a/include/openPMD/LoadStoreChunk.hpp +++ b/include/openPMD/LoadStoreChunk.hpp @@ -143,13 +143,13 @@ namespace core template [[nodiscard]] auto - enqueueLoad() -> auxiliary::DeferredFuture>; + enqueueLoad() -> auxiliary::DeferredComputation>; template [[nodiscard]] auto load(EnqueuePolicy) -> std::shared_ptr; [[nodiscard]] auto - enqueueLoadVariant() -> auxiliary::DeferredFuture< + enqueueLoadVariant() -> auxiliary::DeferredComputation< auxiliary::detail::shared_ptr_dataset_types>; [[nodiscard]] auto loadVariant(EnqueuePolicy) @@ -167,7 +167,7 @@ namespace core auto storeChunkConfig() -> internal::LoadStoreConfigWithBuffer; - auto enqueueStore() -> auxiliary::DeferredFuture; + auto enqueueStore() -> auxiliary::DeferredComputation; auto store(EnqueuePolicy) -> void; diff --git a/include/openPMD/auxiliary/Future.hpp b/include/openPMD/auxiliary/Future.hpp index 6c5cebd822..d40f7372fb 100644 --- a/include/openPMD/auxiliary/Future.hpp +++ b/include/openPMD/auxiliary/Future.hpp @@ -2,25 +2,22 @@ #include "openPMD/auxiliary/TypeTraits.hpp" -#include +#include namespace openPMD::auxiliary { template -class DeferredFuture +class DeferredComputation { - using task_type = std::packaged_task; - using future_type = std::future; - future_type m_future; + using task_type = std::function; task_type m_task; + bool m_valid = false; public: - DeferredFuture(task_type); + DeferredComputation(task_type); auto get() -> T; [[nodiscard]] auto valid() const noexcept -> bool; - - auto wait() -> void; }; } // namespace openPMD::auxiliary diff --git a/src/LoadStoreChunk.cpp b/src/LoadStoreChunk.cpp index cb1a922e24..6d6d57386f 100644 --- a/src/LoadStoreChunk.cpp +++ b/src/LoadStoreChunk.cpp @@ -114,15 +114,14 @@ namespace core template auto ConfigureLoadStore::enqueueLoad() - -> auxiliary::DeferredFuture> + -> auxiliary::DeferredComputation> { auto res = m_rc.loadChunkAllocate_impl(storeChunkConfig()); - return auxiliary::DeferredFuture>( - std::packaged_task()>( - [res_lambda = std::move(res), rc = m_rc]() mutable { - rc.seriesFlush(); - return res_lambda; - })); + return auxiliary::DeferredComputation>( + [res_lambda = std::move(res), rc = m_rc]() mutable { + rc.seriesFlush(); + return res_lambda; + }); } template @@ -144,24 +143,25 @@ namespace core { template static auto call(RecordComponent &rc, internal::LoadStoreConfig cfg) - -> auxiliary::DeferredFuture< + -> auxiliary::DeferredComputation< auxiliary::detail::shared_ptr_dataset_types> { auto res = rc.loadChunkAllocate_impl(std::move(cfg)); - return auxiliary::DeferredFuture< + return auxiliary::DeferredComputation< auxiliary::detail::shared_ptr_dataset_types>( - std::packaged_task< - auxiliary::detail::shared_ptr_dataset_types()>( - [res_lambda = std::move(res), rc_lambda = rc]() mutable - -> auxiliary::detail::shared_ptr_dataset_types { - rc_lambda.seriesFlush(); - return res_lambda; - })); + + [res_lambda = std::move(res), rc_lambda = rc]() mutable + -> auxiliary::detail::shared_ptr_dataset_types { + std::cout << "Flushing Series from Future" << std::endl; + rc_lambda.seriesFlush(); + std::cout << "Flushed Series from Future" << std::endl; + return res_lambda; + }); } }; auto ConfigureLoadStore::enqueueLoadVariant() - -> auxiliary::DeferredFuture< + -> auxiliary::DeferredComputation< auxiliary::detail::shared_ptr_dataset_types> { return m_rc.visit(this->storeChunkConfig()); @@ -208,14 +208,14 @@ namespace core template auto ConfigureStoreChunkFromBuffer::enqueueStore() - -> auxiliary::DeferredFuture + -> auxiliary::DeferredComputation { this->m_rc.storeChunk_impl( asWriteBuffer(std::move(m_buffer)), determineDatatype>(), storeChunkConfig()); - return auxiliary::DeferredFuture(std::packaged_task( - [rc_lambda = m_rc]() mutable -> void { rc_lambda.seriesFlush(); })); + return auxiliary::DeferredComputation( + [rc_lambda = m_rc]() mutable -> void { rc_lambda.seriesFlush(); }); } template @@ -305,7 +305,7 @@ template class compose::ConfigureLoadStore; -> DynamicMemoryView; \ template auto core::ConfigureLoadStore::enqueueLoad() \ /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ - -> auxiliary::DeferredFuture>; \ + -> auxiliary::DeferredComputation>; \ template auto core::ConfigureLoadStore::load(EnqueuePolicy) \ ->std::shared_ptr; // clang-format on diff --git a/src/auxiliary/Future.cpp b/src/auxiliary/Future.cpp index 5699bb894f..944fe63504 100644 --- a/src/auxiliary/Future.cpp +++ b/src/auxiliary/Future.cpp @@ -1,7 +1,9 @@ #include "openPMD/auxiliary/Future.hpp" #include "openPMD/LoadStoreChunk.hpp" +#include #include +#include // comment @@ -11,41 +13,38 @@ namespace openPMD::auxiliary { template -DeferredFuture::DeferredFuture(task_type task) - : m_future(task.get_future()), m_task(std::move(task)) +DeferredComputation::DeferredComputation(task_type task) + : m_task([wrapped_task = std::move(task), this]() { + if (!this->m_valid) + { + throw std::runtime_error( + "[DeferredComputation] No valid state. Probably already " + "computed."); + } + this->m_valid = false; + return std::move(wrapped_task)(); + }) + , m_valid(true) {} template -auto DeferredFuture::get() -> T +auto DeferredComputation::get() -> T { - if (m_future.valid()) - { - m_task(); - } // else get() was already called, propagate the std::future behavior - return m_future.get(); + return m_task(); } template -auto DeferredFuture::valid() const noexcept -> bool +auto DeferredComputation::valid() const noexcept -> bool { - return m_future.valid(); + return m_valid; } -template -auto DeferredFuture::wait() -> void -{ - if (!m_task.valid()) - { - m_task(); - } -} - -template class DeferredFuture; -template class DeferredFuture; +template class DeferredComputation; +template class DeferredComputation; // clang-format off #define INSTANTIATE_FUTURE(dtype) \ /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ - template class DeferredFuture>; + template class DeferredComputation>; OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_FUTURE) #undef INSTANTIATE_FUTURE // clang-format on