alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
reduce.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 /** accumulate the results into a scalar value.
14 *
15 * @param queue The queue to execute the transformation.
16 * @param exec The executor to use for the kernel execution.
17 * @param neutralElement The neutral element in respect to binaryReduceFn.
18 * @param out MdSpan for the result. The value_type must be equal to neutralElement and the result of the binary
19 * reduce functor type. The result is written to the first element of the output data.
20 * @param binaryReduceFn Reduce binary functor, the functor operation must be transitive and commutative.
21 * The atomic operation atomic::alpakaAtomicInvoke(ReduceFnType, onAcc::concepts::Acc, auto* destination,auto
22 * source) must be overloaded. The functor execution order is not specified. The functor should support Simd
23 * packages, if not you can enforce the element wise execution by wrapping into ScalarFunc.
24 * @param in The input data which should be reduced.
25 *
26 * @{
27 */
28 template<typename DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
29 inline void reduce(
32 DataType const& neutralElement,
34 auto&& binaryReduceFn,
35 auto&& in) requires(std::same_as<DataType, alpaka::trait::GetValueType_t<ALPAKA_TYPEOF(out)>>)
36 {
37 if constexpr(exec == alpaka::exec::anyExecutor)
38 {
39 internal::transformReduce(
40 queue,
41 defaultExecutor(queue.getDevice()),
42 neutralElement,
43 out,
44 ALPAKA_FORWARD(binaryReduceFn),
45 std::identity{},
46 ALPAKA_FORWARD(in));
47 }
48 else
49 internal::transformReduce(
50 queue,
51 exec,
52 neutralElement,
53 out,
54 ALPAKA_FORWARD(binaryReduceFn),
55 std::identity{},
56 ALPAKA_FORWARD(in));
57 }
58
59 /**
60 * An available default executor will be selected automatically. The default executor is an executor with most
61 * parallelism/performance.
62 */
63 template<typename DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
64 inline void reduce(
66 DataType const& neutralElement,
68 auto&& binaryReduceFn,
70 requires(std::same_as<DataType, alpaka::trait::GetValueType_t<ALPAKA_TYPEOF(out)>>)
71 {
72 reduce(
73 queue,
74 defaultExecutor(queue.getDevice()),
75 neutralElement,
76 out,
77 ALPAKA_FORWARD(binaryReduceFn),
78 ALPAKA_FORWARD(in));
79 }
80
81 /** @} */
82} // 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
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 reduce(Queue< T_Device, T_QueueKind > const &queue, alpaka::concepts::Executor auto const exec, DataType const &neutralElement, alpaka::concepts::IMdSpan auto out, auto &&binaryReduceFn, auto &&in)
accumulate the results into a scalar value.
Definition reduce.hpp:29
typename GetValueType< T >::type GetValueType_t
Definition trait.hpp:65