alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
functor.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
8
9#include <type_traits>
10#include <utility>
11
12namespace alpaka
13{
14 /** Marks a functor which supports SimdPtr as arguments
15 *
16 * Wrapping a functor or lambda with this class to signal support for SimdPtr.
17 * A stencil functor can be used to write stencil operations within a transform call.
18 */
19 template<typename T_Func>
20 struct StencilFunc : T_Func
21 {
22 using Functor = T_Func;
23
24 constexpr StencilFunc(auto&& func) : T_Func{ALPAKA_FORWARD(func)}
25 {
26 }
27 };
28
29 template<typename T_Func>
31
32 /** Marks a functor that can only be executed with scalar types and not SIMD packages.
33 *
34 * The functor will be executed element wise for SIMD packages due to methods used which prevent using SIMD
35 * packages directly.
36 */
37 template<typename T_Func>
38 struct ScalarFunc : T_Func
39 {
40 using Functor = T_Func;
41
42 constexpr ScalarFunc(auto&& func) : T_Func{ALPAKA_FORWARD(func)}
43 {
44 }
45 };
46
47 template<typename T_Func>
49
50 /** Execute the functor with or without an accelerator as first argument
51 *
52 * The functor is not allowed to have both possible signatures.
53 *
54 * @{
55 */
56 template<typename T_Acc, typename T_Functor, typename... T_Args>
57 requires std::invocable<T_Functor, T_Acc, T_Args...>
58 inline constexpr auto callFunctor(T_Acc const& acc, T_Functor&& functor, T_Args&&... args)
59 {
60 return functor(acc, std::forward<T_Args>(args)...);
61 }
62
63 template<typename T_Acc, typename T_Functor, typename... T_Args>
64 requires std::invocable<T_Functor, T_Args...>
65 inline constexpr auto callFunctor(T_Acc const&, T_Functor&& functor, T_Args&&... args)
66 {
67 return functor(std::forward<T_Args>(args)...);
68 }
69
70 /** @} */
71} // namespace alpaka
#define ALPAKA_FN_HOST_ACC
All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_F...
Definition common.hpp:32
#define ALPAKA_FORWARD(instance)
Perfectly forward an instance as argument.
Definition common.hpp:148
main alpaka namespace.
Definition alpaka.hpp:76
ALPAKA_FN_HOST_ACC StencilFunc(T_Func &&) -> StencilFunc< T_Func >
ALPAKA_FN_HOST_ACC ScalarFunc(T_Func &&) -> ScalarFunc< T_Func >
constexpr auto callFunctor(T_Acc const &acc, T_Functor &&functor, T_Args &&... args)
Execute the functor with or without an accelerator as first argument.
Definition functor.hpp:58
Marks a functor that can only be executed with scalar types and not SIMD packages.
Definition functor.hpp:39
constexpr ScalarFunc(auto &&func)
Definition functor.hpp:42
Marks a functor which supports SimdPtr as arguments.
Definition functor.hpp:21
constexpr StencilFunc(auto &&func)
Definition functor.hpp:24