alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
simdized.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5/** @file This file provides functionality to transform a type into a SIMD-optimized data structure.
6 *
7 * The implementation is motivated by https://ieeexplore.ieee.org/document/11207437
8 */
9
10#pragma once
11
12#include "alpaka/Simd.hpp"
14
15namespace alpaka
16{
17
18 /** Transform a type into a SIMD-optimized data structure.
19 *
20 * A simdized value is not necessarily a data type wrapped by alpaka::Simd, it can be a structured hierarchical
21 * type where each component is a SIMD pack. This function can be specialized within the namespace of the input
22 * type and will be found via ADL.
23 * @see alpakaSimdizedInvoke should be specialized together with this function.
24 *
25 * Simdizing of structured types as shown in the code example often improves the performance compared to wrapping
26 * into a SIMD pack.
27 * @code{.cpp}
28 * // input type
29 * template<typename T>
30 * struct Pos
31 * {
32 * T x = 0;
33 * T y = 1;
34 * };
35 *
36 * // output could be
37 * template<typename T>
38 * struct Pos
39 * {
40 * alpaka::Simd<T, width> x;
41 * alpaka::Simd<T, width> y;
42 * };
43 * @endcode
44 *
45 * @tparam T_width the width of the used SIMD type
46 * @return A simdized data type where each lane replicates the given value. If `makeSimdized` is not specialized
47 * for the given type a SIMD pack wrapping the input value will be returned.
48 */
49 template<uint32_t T_width>
50 constexpr auto makeSimdized(auto&& value)
51 {
52 return Simd<ALPAKA_TYPEOF(value), T_width>::fill(value);
53 }
54
55 /** Invokes the callable object fn with the parameters args.
56 *
57 * For structured data where each component is a SIMD pack, the functor should be forwarded to the members while
58 * recursively calling alpakaSimdizedInvoke.
59 * As soon as there is no use specialization available, the recursion is terminated by the invocation of the
60 * functor with the forwarded arguments. This function can be specialized within the namespace of the argument
61 * types and will be found via ADL.
62 * @see makeSimdized should be specialized together with this function.
63 *
64 * As shown in the code snippet, alpaka assumes at least a specialization where each argument can perform the same
65 * access used within the function. It is allowed to specialize more function signatures that do not follow the
66 * rule but are useful within the user code.
67 * @code{.cpp}
68 * // A typical case of how this specialization is called is
69 * // `alpakaSimdizedInvoke(f, Pos<int>{}, Pos<alpaka::Simd<int,4>>{})`.
70 * constexpr void alpakaSimdizedInvoke(auto&& f, alpaka::concepts::SpecializationOf<Pos> auto&&... args)
71 * {
72 * // Accessing .x and .y must be supported by all arguments.
73 * alpakaSimdizedInvoke(ALPAKA_FORWARD(f), ALPAKA_FORWARD(args).x...);
74 * alpakaSimdizedInvoke(ALPAKA_FORWARD(f), ALPAKA_FORWARD(args).y...);
75 * }
76 * @endcode
77 *
78 * @param fn Callable object to which the arguments will be forwarded.
79 * @param args Arguments forwarded to fn.
80 */
81 constexpr void alpakaSimdizedInvoke(auto&& fn, auto&&... args)
82 {
84 }
85} // namespace alpaka
#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
alpaka'S function interface
Definition fn.hpp:38
main alpaka namespace.
Definition alpaka.hpp:76
constexpr void alpakaSimdizedInvoke(auto &&fn, auto &&... args)
Invokes the callable object fn with the parameters args.
Definition simdized.hpp:81
constexpr auto makeSimdized(auto &&value)
Transform a type into a SIMD-optimized data structure.
Definition simdized.hpp:50
Simd vector.
Definition Simd.hpp:78