alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
concurrent.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
9
10namespace alpaka::onHost
11{
12 /** Execute an n-nary function on each element of all input data.
13 *
14 * Concurrent is quite equal to a for-each algorithm with the difference that the functor is allowed to write to
15 * any argument. So it allows to implement a transform with a free number of output arguments.
16 *
17 * @param queue The queue to execute the transformation.
18 * @param exec The executor to use for the kernel execution.
19 * @param extents multi dimensional or scalar number of elements
20 * @param fn The function to apply to each element of the input data.
21 * The functor should support @see SimdPtr and therefore can be used for stencil evaluations.
22 * It is not required to wrapp the functor with @see StencilFunc.
23 * If a stencil lookup is executed you should take care to not read outside of valid memory ranges
24 * by using sub-views to your input/output data. Optionally, a function can have an accelerator as its first
25 * argument.
26 * @param inOut The input/output data, all data is passed to fn.
27 *
28 * examples for a unary add one functor:
29 * @code{.cpp}
30 * struct Foo {
31 * constexpr auto operator()(onAcc::concepts::Acc auto const&, concepts::SimdPtr auto const& a) const {
32 * a = a.load() + 1;
33 * }
34 * };
35 * struct Bar {
36 * constexpr auto operator()(concepts::SimdPtr auto const& a) const {
37 * a = a.load() + 1;
38 * }
39 * };
40 * @endcode
41 *
42 * @{
43 */
44 template<typename T_DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
45 inline void concurrent(
48 alpaka::concepts::VectorOrScalar auto const& extents,
49 auto&& fn,
50 alpaka::concepts::IDataSource auto&&... inOut)
51 {
52 if constexpr(exec == alpaka::exec::anyExecutor)
53 {
54 internal::concurrent<T_DataType>(
55 queue,
56 defaultExecutor(queue.getDevice()),
57 extents,
59 ALPAKA_FORWARD(inOut)...);
60 }
61 else
62 internal::concurrent<T_DataType>(queue, exec, extents, ALPAKA_FORWARD(fn), ALPAKA_FORWARD(inOut)...);
63 }
64
65 /**
66 * An available default executor will be selected automatically. The default executor is an executor with most
67 * parallelism/performance.
68 */
69 template<typename T_DataType, typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
70 inline void concurrent(
72 alpaka::concepts::VectorOrScalar auto const& extents,
73 auto&& fn,
74 alpaka::concepts::IDataSource auto&&... inOut)
75 {
76 internal::concurrent<T_DataType>(
77 queue,
78 defaultExecutor(queue.getDevice()),
79 extents,
81 ALPAKA_FORWARD(inOut)...);
82 }
83
84 /** @} */
85} // namespace alpaka::onHost
#define ALPAKA_FORWARD(instance)
Perfectly forward an instance as argument.
Definition common.hpp:148
Concept to check for an executor.
Definition trait.hpp:133
Concept to check if a type is a vector or scalar variable.
Definition Vec.hpp:65
constexpr AnyExecutor anyExecutor
Automatic executor selection.
Definition executor.hpp:33
alpaka'S function interface
Definition fn.hpp:38
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
void concurrent(Queue< T_Device, T_QueueKind > const &queue, alpaka::concepts::Executor auto const exec, alpaka::concepts::VectorOrScalar auto const &extents, auto &&fn, alpaka::concepts::IDataSource auto &&... inOut)
Execute an n-nary function on each element of all input data.
constexpr auto defaultExecutor(internal::concepts::DeviceHandle auto deviceHandle)
Select a default executor for the given device.
Definition trait.hpp:169