Skip to content

Commit

Permalink
[oneDPL] future code cleanup and API fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDvorskiy committed Dec 9, 2024
1 parent 8d2717c commit 56968d6
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,35 +701,40 @@ struct __deferrable_mode
{
};

//A contract for future class: <sycl::event or other event, a value, sycl::buffers..., or __usm_host_or_buffer_storage>
//Impl details: inheritance (private) instead of aggregation for enabling the empty base optimization.
template <typename _Event, typename... _Args>
class __future : private std::tuple<_Args...>
// An overload of __wait_and_get_value for 'sycl::buffer'
template <typename _Event, typename _T>
constexpr auto
__wait_and_get_value(_Event&&, const sycl::buffer<_T>& __buf, std::size_t __idx = 0)
{
_Event __my_event;
//according to a contract, returned value is one-element sycl::buffer
return __buf.get_host_access(sycl::read_only)[__idx];
}

template <typename _T>
constexpr auto
__wait_and_get_value(const sycl::buffer<_T>& __buf)
{
//according to a contract, returned value is one-element sycl::buffer
return __buf.get_host_access(sycl::read_only)[0];
}
// An overload of __wait_and_get_value for '__result_and_scratch_storage'
template <typename _Event, typename _ExecutionPolicy, typename _T>
constexpr auto
__wait_and_get_value(_Event&& __event, const __result_and_scratch_storage<_ExecutionPolicy, _T>& __storage, std::size_t __idx = 0)
{
return __storage.__wait_and_get_value(__my_event, __idx);
}

template <typename _ExecutionPolicy, typename _T>
constexpr auto
__wait_and_get_value(const __result_and_scratch_storage<_ExecutionPolicy, _T>& __storage)
{
return __storage.__wait_and_get_value(__my_event);
}
template <typename _Event, typename _T>
constexpr auto
__wait_and_get_value(_Event&& __event, const _T& __val, std::size_t)
{
__event.wait_and_throw();
return __val;
}

template <typename _T>
constexpr auto
__wait_and_get_value(const _T& __val)
{
wait();
return __val;
}
//A contract for 'future' class:
//* The first argument is an event, which has 'wait_and_throw' method
//* The second and the following argument a trivial type T or RAII storage.
//* The 'future' class extends the lifetime for such RAII objects.
//* Impl details: inheritance (private) instead of aggregation for enabling the empty base optimization.
template <typename _Event, typename... _Args>
class __future : private std::tuple<_Args...>
{
_Event __my_event;

public:
__future(_Event __e, _Args... __args) : std::tuple<_Args...>(__args...), __my_event(__e) {}
Expand Down Expand Up @@ -764,13 +769,14 @@ class __future : private std::tuple<_Args...>
#endif
}

template <std::size_t _Idx = 0>
auto
get()
get(std::size_t __idx = 0)
{
if constexpr (sizeof...(_Args) > 0)
{
auto& __val = std::get<0>(*this);
return __wait_and_get_value(__val);
auto& __val = std::get<_Idx>(*this);
return __wait_and_get_value(__val, __idx);
}
else
wait();
Expand Down

0 comments on commit 56968d6

Please sign in to comment.