Skip to content

Commit

Permalink
Don't use std::packaged_task
Browse files Browse the repository at this point in the history
Intel compilers having trouble
  • Loading branch information
franzpoeschel committed Sep 3, 2024
1 parent 1fe4534 commit bd7b378
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 54 deletions.
6 changes: 3 additions & 3 deletions include/openPMD/LoadStoreChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ namespace core

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

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

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

[[nodiscard]] auto loadVariant(EnqueuePolicy)
Expand All @@ -167,7 +167,7 @@ namespace core

auto storeChunkConfig() -> internal::LoadStoreConfigWithBuffer;

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

auto store(EnqueuePolicy) -> void;

Expand Down
13 changes: 5 additions & 8 deletions include/openPMD/auxiliary/Future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

#include "openPMD/auxiliary/TypeTraits.hpp"

#include <future>
#include <functional>

namespace openPMD::auxiliary
{
template <typename T>
class DeferredFuture
class DeferredComputation
{
using task_type = std::packaged_task<T()>;
using future_type = std::future<T>;
future_type m_future;
using task_type = std::function<T()>;
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
42 changes: 21 additions & 21 deletions src/LoadStoreChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,14 @@ namespace core

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

template <typename T>
Expand All @@ -144,24 +143,25 @@ namespace core
{
template <typename T>
static auto call(RecordComponent &rc, internal::LoadStoreConfig cfg)
-> auxiliary::DeferredFuture<
-> auxiliary::DeferredComputation<
auxiliary::detail::shared_ptr_dataset_types>
{
auto res = rc.loadChunkAllocate_impl<T>(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<VisitorEnqueueLoadVariant>(this->storeChunkConfig());
Expand Down Expand Up @@ -208,14 +208,14 @@ namespace core

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

template <typename Ptr_Type>
Expand Down Expand Up @@ -305,7 +305,7 @@ template class compose::ConfigureLoadStore<ConfigureLoadStore>;
-> DynamicMemoryView<dtype>; \
template auto core::ConfigureLoadStore::enqueueLoad() \
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
-> auxiliary::DeferredFuture<std::shared_ptr<dtype>>; \
-> auxiliary::DeferredComputation<std::shared_ptr<dtype>>; \
template auto core::ConfigureLoadStore::load(EnqueuePolicy) \
->std::shared_ptr<dtype>;
// clang-format on
Expand Down
43 changes: 21 additions & 22 deletions src/auxiliary/Future.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "openPMD/auxiliary/Future.hpp"
#include "openPMD/LoadStoreChunk.hpp"

#include <iostream>
#include <memory>
#include <stdexcept>

// comment

Expand All @@ -11,41 +13,38 @@ namespace openPMD::auxiliary
{

template <typename T>
DeferredFuture<T>::DeferredFuture(task_type task)
: m_future(task.get_future()), m_task(std::move(task))
DeferredComputation<T>::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 <typename T>
auto DeferredFuture<T>::get() -> T
auto DeferredComputation<T>::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 <typename T>
auto DeferredFuture<T>::valid() const noexcept -> bool
auto DeferredComputation<T>::valid() const noexcept -> bool
{
return m_future.valid();
return m_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>;
template class DeferredComputation<void>;
template class DeferredComputation<auxiliary::detail::shared_ptr_dataset_types>;
// clang-format off
#define INSTANTIATE_FUTURE(dtype) \
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
template class DeferredFuture<std::shared_ptr<dtype>>;
template class DeferredComputation<std::shared_ptr<dtype>>;
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_FUTURE)
#undef INSTANTIATE_FUTURE
// clang-format on
Expand Down

0 comments on commit bd7b378

Please sign in to comment.