DPC++ Runtime
Runtime libraries for oneAPI DPC++
reduction.hpp
Go to the documentation of this file.
1 //==---------------- reduction.hpp - SYCL reduction ------------*- C++ -*---==//
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 
12 
14 
16 namespace sycl {
17 
20 template <typename T, typename AllocatorT, typename BinaryOperation>
21 std::enable_if_t<has_known_identity<BinaryOperation, T>::value,
22  ext::oneapi::detail::reduction_impl<
23  T, BinaryOperation, 0, 1,
24  ext::oneapi::detail::default_reduction_algorithm<
25  false, access::placeholder::true_t, 1>>>
26 reduction(buffer<T, 1, AllocatorT> Var, handler &CGH, BinaryOperation,
27  const property_list &PropList = {}) {
29  PropList.has_property<property::reduction::initialize_to_identity>();
30  return {Var, CGH, InitializeToIdentity};
31 }
32 
37 template <typename T, typename AllocatorT, typename BinaryOperation>
38 std::enable_if_t<!has_known_identity<BinaryOperation, T>::value,
39  ext::oneapi::detail::reduction_impl<
40  T, BinaryOperation, 0, 1,
41  ext::oneapi::detail::default_reduction_algorithm<
42  false, access::placeholder::true_t, 1>>>
44  const property_list &PropList = {}) {
45  // TODO: implement reduction that works even when identity is not known.
46  (void)PropList;
47  throw runtime_error("Identity-less reductions with unknown identity are not "
48  "supported yet.",
49  PI_ERROR_INVALID_VALUE);
50 }
51 
55 template <typename T, typename BinaryOperation>
56 std::enable_if_t<has_known_identity<BinaryOperation, T>::value,
57  ext::oneapi::detail::reduction_impl<
58  T, BinaryOperation, 0, 1,
59  ext::oneapi::detail::default_reduction_algorithm<
60  true, access::placeholder::false_t, 1>>>
61 reduction(T *Var, BinaryOperation, const property_list &PropList = {}) {
63  PropList.has_property<property::reduction::initialize_to_identity>();
64  return {Var, InitializeToIdentity};
65 }
66 
72 template <typename T, typename BinaryOperation>
73 std::enable_if_t<!has_known_identity<BinaryOperation, T>::value,
74  ext::oneapi::detail::reduction_impl<
75  T, BinaryOperation, 0, 1,
76  ext::oneapi::detail::default_reduction_algorithm<
77  true, access::placeholder::false_t, 1>>>
78 reduction(T *, BinaryOperation, const property_list &PropList = {}) {
79  // TODO: implement reduction that works even when identity is not known.
80  (void)PropList;
81  throw runtime_error("Identity-less reductions with unknown identity are not "
82  "supported yet.",
83  PI_ERROR_INVALID_VALUE);
84 }
85 
89 template <typename T, typename AllocatorT, typename BinaryOperation>
90 ext::oneapi::detail::reduction_impl<
91  T, BinaryOperation, 0, 1,
92  ext::oneapi::detail::default_reduction_algorithm<
93  false, access::placeholder::true_t, 1>>
94 reduction(buffer<T, 1, AllocatorT> Var, handler &CGH, const T &Identity,
95  BinaryOperation Combiner, const property_list &PropList = {}) {
97  PropList.has_property<property::reduction::initialize_to_identity>();
98  return {Var, CGH, Identity, Combiner, InitializeToIdentity};
99 }
100 
104 template <typename T, typename BinaryOperation>
105 ext::oneapi::detail::reduction_impl<
106  T, BinaryOperation, 0, 1,
107  ext::oneapi::detail::default_reduction_algorithm<
108  true, access::placeholder::false_t, 1>>
109 reduction(T *Var, const T &Identity, BinaryOperation Combiner,
110  const property_list &PropList = {}) {
111  bool InitializeToIdentity =
112  PropList.has_property<property::reduction::initialize_to_identity>();
113  return {Var, Identity, Combiner, InitializeToIdentity};
114 }
115 
116 #if __cplusplus >= 201703L
117 template <typename T, size_t Extent, typename BinaryOperation>
121 std::enable_if_t<Extent != dynamic_extent &&
122  has_known_identity<BinaryOperation, T>::value,
123  ext::oneapi::detail::reduction_impl<
124  T, BinaryOperation, 1, Extent,
125  ext::oneapi::detail::default_reduction_algorithm<
126  true, access::placeholder::false_t, 1>>>
127 reduction(span<T, Extent> Span, BinaryOperation,
128  const property_list &PropList = {}) {
129  bool InitializeToIdentity =
130  PropList.has_property<property::reduction::initialize_to_identity>();
131  return {Span, InitializeToIdentity};
132 }
133 
139 template <typename T, size_t Extent, typename BinaryOperation>
140 std::enable_if_t<Extent != dynamic_extent &&
141  !has_known_identity<BinaryOperation, T>::value,
142  ext::oneapi::detail::reduction_impl<
143  T, BinaryOperation, 1, Extent,
144  ext::oneapi::detail::default_reduction_algorithm<
145  true, access::placeholder::false_t, 1>>>
146 reduction(span<T, Extent>, BinaryOperation,
147  const property_list &PropList = {}) {
148  // TODO: implement reduction that works even when identity is not known.
149  (void)PropList;
150  throw runtime_error("Identity-less reductions with unknown identity are not "
151  "supported yet.",
152  PI_ERROR_INVALID_VALUE);
153 }
154 
158 template <typename T, size_t Extent, typename BinaryOperation>
160  ext::oneapi::detail::reduction_impl<
161  T, BinaryOperation, 1, Extent,
162  ext::oneapi::detail::default_reduction_algorithm<
163  true, access::placeholder::false_t, 1>>>
164 reduction(span<T, Extent> Span, const T &Identity, BinaryOperation Combiner,
165  const property_list &PropList = {}) {
166  bool InitializeToIdentity =
167  PropList.has_property<property::reduction::initialize_to_identity>();
168  return {Span, Identity, Combiner, InitializeToIdentity};
169 }
170 #endif
171 
172 } // namespace sycl
173 } // __SYCL_INLINE_NAMESPACE(cl)
T
sycl
Definition: invoke_simd.hpp:68
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
cl::sycl::buffer
Defines a shared array that can be used by kernels in queues.
Definition: buffer.hpp:78
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
reduction.hpp
cl::sycl::handler
Command group handler class.
Definition: handler.hpp:362
cl::sycl::detail::InitializeToIdentity
@ InitializeToIdentity
Definition: property_helper.hpp:34
cl::sycl::ext::oneapi::reduction
std::enable_if_t< detail::IsKnownIdentityOp< T, BinaryOperation >::value, detail::reduction_impl< T, BinaryOperation, 0, 1, detail::default_reduction_algorithm< true, access::placeholder::false_t, 1 > > > reduction(T *VarPtr, BinaryOperation)
Creates and returns an object implementing the reduction functionality.
Definition: reduction.hpp:2624
cl::sycl::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
known_identity.hpp
cl::sycl::ext::intel::detail::dynamic_extent
constexpr size_t dynamic_extent
Definition: matrix-aot-amx.hpp:50
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12