alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
transform.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 write the result to the output data.
14 *
15 * fn can be a lambda function if all arguments are specialized. This fully specialized functor must mostly wrapped
16 * by @see ScalarFunc. Generic lambdas are for some backends e.g. CUDA/HIP not supported. A lambda must be of the
17 * 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 out The output data to write the result to.
26 * @param fn The function to apply to each element of the input data.
27 * The functor should support Simd packages. If not you can enforce the element wise execution by wrapping into
28 * @see ScalarFunc. If you would like to support stencil executions wrapp fn into @see StencilFunc. StencilFunc is
29 * getting all arguments as @see SimdPtr. If StencilFunc is used you should take care to not read outside of valid
30 * memory ranges by using sub-views to your input and output data. Optionally a fn can have a accelerator as first
31 * argument.
32 * @param in The input data to transform, all input data is passed to fn.
33 *
34 * examples for a identity unary transform functor:
35 * @code{.cpp}
36 * struct Foo {
37 * constexpr auto operator()(onAcc::concepts::Acc auto const&, concepts::SimdPtr auto const& a) const {
38 * return a.load();
39 * }
40 * };
41 * struct Bar {
42 * constexpr auto operator()(concepts::SimdPtr auto const& a) const {
43 * return a.load();
44 * }
45 * };
46 * @endcode
47 *
48 * @{
49 */
50 template<typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
51 inline void transform(
55 auto&& fn,
57 {
58 if constexpr(exec == alpaka::exec::anyExecutor)
59 {
61 queue,
62 defaultExecutor(queue.getDevice()),
63 ALPAKA_FORWARD(out),
65 ALPAKA_FORWARD(in)...);
66 }
67 else
69 }
70
71 /**
72 * An available default executor will be selected automatically. The default executor is an executor with most
73 * parallelism/performance.
74 */
75 template<typename T_Device, alpaka::concepts::QueueKind T_QueueKind>
76 inline void transform(
79 auto&& fn,
81 {
83 queue,
84 defaultExecutor(queue.getDevice()),
85 ALPAKA_FORWARD(out),
87 ALPAKA_FORWARD(in)...);
88 }
89
90 /** @} */
91} // 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
Interface concept for objects describing multidimensional memory access.
Definition IMdSpan.hpp:91
constexpr AnyExecutor anyExecutor
Automatic executor selection.
Definition executor.hpp:33
alpaka'S function interface
Definition fn.hpp:38
void transform(auto const &queue, alpaka::concepts::Executor auto const exec, alpaka::concepts::IMdSpan auto &&out, auto &&fn, alpaka::concepts::IDataSource auto &&... in)
Definition transform.hpp:83
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 transform(Queue< T_Device, T_QueueKind > const &queue, alpaka::concepts::Executor auto const exec, alpaka::concepts::IMdSpan auto &&out, auto &&fn, alpaka::concepts::IDataSource auto &&... in)
Transform the input data with the given function and write the result to the output data.
Definition transform.hpp:51