|
| 1 | +//==------- properties.hpp - SYCL properties associated with reductions ----==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#pragma once |
| 10 | +#define SYCL_EXT_ONEAPI_REDUCTION_PROPERTIES |
| 11 | + |
| 12 | +#include <sycl/ext/oneapi/properties/property.hpp> |
| 13 | +#include <sycl/ext/oneapi/properties/property_value.hpp> |
| 14 | +#include <sycl/reduction.hpp> |
| 15 | + |
| 16 | +namespace sycl { |
| 17 | +inline namespace _V1 { |
| 18 | +namespace ext { |
| 19 | +namespace oneapi { |
| 20 | +namespace experimental { |
| 21 | + |
| 22 | +struct deterministic_key |
| 23 | + : detail::compile_time_property_key<detail::PropKind::Deterministic> { |
| 24 | + using value_t = property_value<deterministic_key>; |
| 25 | +}; |
| 26 | +inline constexpr deterministic_key::value_t deterministic; |
| 27 | + |
| 28 | +struct initialize_to_identity_key |
| 29 | + : detail::compile_time_property_key< |
| 30 | + detail::PropKind::InitializeToIdentity> { |
| 31 | + using value_t = property_value<initialize_to_identity_key>; |
| 32 | +}; |
| 33 | +inline constexpr initialize_to_identity_key::value_t initialize_to_identity; |
| 34 | + |
| 35 | +} // namespace experimental |
| 36 | +} // namespace oneapi |
| 37 | +} // namespace ext |
| 38 | + |
| 39 | +namespace detail { |
| 40 | + |
| 41 | +template <typename BinaryOperation, typename PropertyList> |
| 42 | +auto WrapOp(BinaryOperation combiner, PropertyList properties) { |
| 43 | + if constexpr (properties.template has_property< |
| 44 | + ext::oneapi::experimental::deterministic_key>()) { |
| 45 | + return DeterministicOperatorWrapper(combiner); |
| 46 | + } else { |
| 47 | + return combiner; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +template <typename T, typename BinaryOperation, typename PropertyList> |
| 52 | +void CheckReductionIdentity(PropertyList properties) { |
| 53 | + if constexpr (properties.template has_property< |
| 54 | + ext::oneapi::experimental::initialize_to_identity_key>()) { |
| 55 | + static_assert(has_known_identity_v<BinaryOperation, T>, |
| 56 | + "initialize_to_identity requires an identity value."); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +template <typename PropertyList> |
| 61 | +property_list GetReductionPropertyList(PropertyList properties) { |
| 62 | + if constexpr (properties.template has_property< |
| 63 | + ext::oneapi::experimental::initialize_to_identity_key>()) { |
| 64 | + return sycl::property::reduction::initialize_to_identity{}; |
| 65 | + } |
| 66 | + return {}; |
| 67 | +} |
| 68 | + |
| 69 | +template <typename BinaryOperation> struct DeterministicOperatorWrapper { |
| 70 | + |
| 71 | + DeterministicOperatorWrapper(BinaryOperation BinOp = BinaryOperation()) |
| 72 | + : BinOp(BinOp) {} |
| 73 | + |
| 74 | + template <typename... Args> |
| 75 | + std::invoke_result_t<BinaryOperation, Args...> operator()(Args... args) { |
| 76 | + return BinOp(std::forward<Args>(args)...); |
| 77 | + } |
| 78 | + |
| 79 | + BinaryOperation BinOp; |
| 80 | +}; |
| 81 | + |
| 82 | +template <typename BinaryOperation> |
| 83 | +struct IsDeterministicOperator<DeterministicOperatorWrapper<BinaryOperation>> |
| 84 | + : std::true_type {}; |
| 85 | + |
| 86 | +} // namespace detail |
| 87 | + |
| 88 | +template <typename BufferT, typename BinaryOperation, typename PropertyList> |
| 89 | +auto reduction(BufferT vars, handler &cgh, BinaryOperation combiner, |
| 90 | + PropertyList properties) { |
| 91 | + detail::CheckReductionIdentity<typename BufferT::value_type, BinaryOperation>( |
| 92 | + properties); |
| 93 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 94 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 95 | + return reduction(vars, cgh, WrappedOp, RuntimeProps); |
| 96 | +} |
| 97 | + |
| 98 | +template <typename T, typename BinaryOperation, typename PropertyList> |
| 99 | +auto reduction(T *var, BinaryOperation combiner, PropertyList properties) { |
| 100 | + detail::CheckReductionIdentity<T, BinaryOperation>(properties); |
| 101 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 102 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 103 | + return reduction(var, WrappedOp, RuntimeProps); |
| 104 | +} |
| 105 | + |
| 106 | +template <typename T, size_t Extent, typename BinaryOperation, |
| 107 | + typename PropertyList> |
| 108 | +auto reduction(span<T, Extent> vars, BinaryOperation combiner, |
| 109 | + PropertyList properties) { |
| 110 | + detail::CheckReductionIdentity<T, BinaryOperation>(properties); |
| 111 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 112 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 113 | + return reduction(vars, WrappedOp, RuntimeProps); |
| 114 | +} |
| 115 | + |
| 116 | +template <typename BufferT, typename BinaryOperation, typename PropertyList> |
| 117 | +auto reduction(BufferT vars, handler &cgh, |
| 118 | + const typename BufferT::value_type &identity, |
| 119 | + BinaryOperation combiner, PropertyList properties) { |
| 120 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 121 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 122 | + return reduction(vars, cgh, identity, WrappedOp, RuntimeProps); |
| 123 | +} |
| 124 | + |
| 125 | +template <typename T, typename BinaryOperation, typename PropertyList> |
| 126 | +auto reduction(T *var, const T &identity, BinaryOperation combiner, |
| 127 | + PropertyList properties) { |
| 128 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 129 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 130 | + return reduction(var, identity, WrappedOp, RuntimeProps); |
| 131 | +} |
| 132 | + |
| 133 | +template <typename T, size_t Extent, typename BinaryOperation, |
| 134 | + typename PropertyList> |
| 135 | +auto reduction(span<T, Extent> vars, const T &identity, |
| 136 | + BinaryOperation combiner, PropertyList properties) { |
| 137 | + auto WrappedOp = detail::WrapOp(combiner, properties); |
| 138 | + auto RuntimeProps = detail::GetReductionPropertyList(properties); |
| 139 | + return reduction(vars, identity, WrappedOp, RuntimeProps); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace _V1 |
| 143 | +} // namespace sycl |
0 commit comments