alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
transformReduce.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
10
11namespace alpaka::onHost
12{
13 /** Transform the input data with the given function and accumulate the results into a scalar value.
14 *
15 * transformFn can be a lambda function if all arguments are specialized. This fully specialized functor must
16 * mostly wrapped by @see ScalarFunc. Generic lambdas are for some backends e.g. CUDA/HIP not supported. A lambda
17 * must be of the following form and should capture arguments only by copy.
18 *
19 * @code{.cpp}
20 * [] ALPAKA_FN_ACC(){};
21 * @endcode
22 *
23 * @param queue The queue to execute the transformation.
24 * @param exec The executor to use for the kernel execution.
25 * @param neutralElement The neutral element in respect to binaryReduceFn.
26 * @param out MdSpan for the result. The value_type must be equal to neutralElement and the result of the binary
27 * reduce functor type. The result is written to the first element of the output data.
28 * @param binaryReduceFn Reduce binary functor, the functor operation must be transitive and commutative.
29 * The atomic operation atomic::alpakaAtomicInvoke(ReduceFnType, onAcc::concepts::Acc, auto* destination,auto
30 * source) must be overloaded. The functor execution order is not specified. The functor should support Simd
31 * packages, if not you can enforce the element wise execution by wrapping into
32 * @see ScalarFunc.
33 * @param transformFn The function to apply to each element of the input data.
34 * The functor should support Simd packages. If not you can enforce the element wise execution by wrapping into
35 * ScalarFunc. If you would like to support stencil executions wrapp fn into StencilFunc. StencilFunc is
36 * getting all arguments as SimdPtr. If StencilFunc is used you should take care to not read outside of valid
37 * memory ranges by using sub-views to your input and output data. Optionally a transformFn can have an accelerator
38 * as first argument.
39 * @param in The input data to transform, all input data is passed to fn. transformFn must support as many
40 * arguments as input data is provided. An optional argument for the accelerator is support as first argument if
41 * needed.
42 *
43 * examples for a identity unary transform functor:
44 * @code{.cpp}
45 * struct Foo {
46 * constexpr auto operator()(onAcc::concepts::Acc auto const&, concepts::SimdPtr auto const& a) const {
47 * return a.load();
48 * }
49 * };
50 * struct Bar {
51 * constexpr auto operator()(concepts::SimdPtr auto const& a) const {
52 * return a.load();
53 * }
54 * };
55 * @endcode
56 *
57 * @{
58 */
59 template<typename DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
60 inline void transformReduce(
63 DataType const& neutralElement,
65 auto&& binaryReduceFn,
66 auto&& transformFn,
68 requires(std::same_as<DataType, alpaka::trait::GetValueType_t<ALPAKA_TYPEOF(out)>>)
69 {
70 if constexpr(exec == alpaka::exec::anyExecutor)
71 {
73 queue,
74 defaultExecutor(queue.getDevice()),
75 neutralElement,
76 out,
77 ALPAKA_FORWARD(binaryReduceFn),
78 ALPAKA_FORWARD(transformFn),
79 ALPAKA_FORWARD(in)...);
80 }
81 else
83 queue,
84 exec,
85 neutralElement,
86 out,
87 ALPAKA_FORWARD(binaryReduceFn),
88 ALPAKA_FORWARD(transformFn),
89 ALPAKA_FORWARD(in)...);
90 }
91
92 /**
93 * An available default executor will be selected automatically. The default executor is the executor with the most
94 * parallelism/performance.
95 */
96 template<typename DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
97 inline void transformReduce(
99 DataType const& neutralElement,
101 auto&& binaryReduceFn,
102 auto&& transformFn,
104 requires(std::same_as<DataType, alpaka::trait::GetValueType_t<ALPAKA_TYPEOF(out)>>)
105 {
107 queue,
108 defaultExecutor(queue.getDevice()),
109 neutralElement,
110 out,
111 ALPAKA_FORWARD(binaryReduceFn),
112 ALPAKA_FORWARD(transformFn),
113 ALPAKA_FORWARD(in)...);
114 }
115
116 /** @} */
117} // namespace alpaka::onHost
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
#define ALPAKA_FORWARD(instance)
Perfectly forward an instance as argument.
Definition common.hpp:148
Concept to check for an executor.
Definition trait.hpp:133
Interface concept for objects describing multidimensional memory access.
Definition IMdSpan.hpp:91
constexpr AnyExecutor anyExecutor
Automatic executor selection.
Definition executor.hpp:33
void transformReduce(auto const &queue, alpaka::concepts::Executor auto const exec, T_DataType const &neutralElement, alpaka::concepts::IMdSpan auto out, auto &&reduceFn, auto &&transformFn, auto &&in0, alpaka::concepts::IDataSource auto &&... in)
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
constexpr auto defaultExecutor(internal::concepts::DeviceHandle auto deviceHandle)
Select a default executor for the given device.
Definition trait.hpp:169
void transformReduce(Queue< T_Device, T_QueueKind > const &queue, alpaka::concepts::Executor auto const exec, DataType const &neutralElement, alpaka::concepts::IMdSpan auto out, auto &&binaryReduceFn, auto &&transformFn, alpaka::concepts::IDataSource auto &&... in)
Transform the input data with the given function and accumulate the results into a scalar value.
typename GetValueType< T >::type GetValueType_t
Definition trait.hpp:65