alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
interface.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7/** @file
8 *
9 * On some constexpr function signatures `ALPAKA_FN_HOST_ACC` is required for CUDA;
10 * otherwise a `__host__` function called from a `__host__ __device__` context
11 * triggers a warning and the generated code is wrong.
12 */
13
14#include "alpaka/Vec.hpp"
15#include "alpaka/concepts.hpp"
17#include "alpaka/mem/MdSpan.hpp"
23
24/** functionality which is usable on the accelerator compute device from within a kernel. */
25namespace alpaka::onAcc
26{
27 namespace concepts
28 {
29 template<typename T>
30 concept IdxMapping = internal::trait::isIdxMapping_v<T>;
31
32 template<typename T>
33 concept IdxTraversing = internal::trait::isIdxTraversing_v<T>;
34 } // namespace concepts
35
36 /**@{
37 * @name range-based loop indexable index container
38 */
39
40 /** Creates an index container
41 *
42 * The index data type is deduced from the supplied range.
43 * The traversal policy (`T_Traverse`) defines how the next valid index is found for a worker and
44 * defaults to @c traverse::Flat.
45 * The mapping policy (`T_IdxLayout`) defines how the index is mapped to worker threads and defaults to
46 * @c layout::Optimized.
47 *
48 * @param workGroup Description of the participating thread group. More than one
49 * thread can have the same index within the group; all workers
50 * with the same id obtain the same index as result.
51 * @param range Index range description.
52 * @param traverse Policy describing how the next value can be found.
53 * @param idxLayout Policy describing how real worker threads will be mapped to the range.
54 * @return A index container that can be used in a range-based for loop.
55 */
56 template<concepts::IdxTraversing T_Traverse = traverse::Flat, concepts::IdxMapping T_IdxLayout = layout::Optimized>
58 auto const& acc,
59 auto const workGroup,
60 auto const range,
61 T_Traverse traverse = T_Traverse{},
62 T_IdxLayout idxLayout = T_IdxLayout{})
63 {
64 return internal::MakeIter::
65 Op<void, ALPAKA_TYPEOF(acc), ALPAKA_TYPEOF(DomainSpec{workGroup, range}), T_Traverse, T_IdxLayout>{}(
66 acc,
67 DomainSpec{workGroup, range},
69 idxLayout);
70 }
71
72 /** Specialization for an index container with a given boundary direction of the volume described by the range.
73 */
74 template<concepts::IdxTraversing T_Traverse = traverse::Flat, concepts::IdxMapping T_IdxLayout = layout::Optimized>
76 auto const& acc,
77 auto const workGroup,
78 auto const range,
80 T_Traverse traverse = T_Traverse{},
81 T_IdxLayout idxLayout = T_IdxLayout{})
82 {
83 static_assert(ALPAKA_TYPEOF(bd)::dim() == ALPAKA_TYPEOF(range)::dim());
84 auto const subRange = makeDirectionSubRange(range, bd);
85 return internal::MakeIter::
86 Op<void, ALPAKA_TYPEOF(acc), ALPAKA_TYPEOF(DomainSpec{workGroup, subRange}), T_Traverse, T_IdxLayout>{}(
87 acc,
88 DomainSpec{workGroup, subRange},
90 idxLayout);
91 }
92
93 /** Creates an index container
94 *
95 * The traversal policy (`T_Traverse`) defines how the next valid index is found for a worker and
96 * defaults to @c traverse::Flat.
97 * The mapping policy (`T_IdxLayout`) defines how the index is mapped to worker threads and defaults to
98 * @c layout::Optimized.
99 *
100 * @tparam T_ScalarIdxType scalar index type sed for the indices inside the iterator
101 * @param workGroup Description of the participating thread group. More than one
102 * thread can have the same index within the group; all workers
103 * with the same id obtain the same index as result.
104 * @param range Index range description.
105 * @param traverse Policy describing how the next value can be found.
106 * @param idxLayout Policy describing how real worker threads will be mapped to the range.
107 * @return A index container that can be used in a range-based for loop.
108 */
109 template<
110 typename T_ScalarIdxType,
114 auto const& acc,
115 auto const workGroup,
116 auto const range,
117 T_Traverse traverse = T_Traverse{},
118 T_IdxLayout idxLayout = T_IdxLayout{})
119 {
120 return internal::MakeIter::Op<
121 T_ScalarIdxType,
122 ALPAKA_TYPEOF(acc),
123 ALPAKA_TYPEOF(DomainSpec{workGroup, range}),
124 T_Traverse,
125 T_IdxLayout>{}(acc, DomainSpec{workGroup, range}, traverse, idxLayout);
126 }
127
128 ///@cond NO_HTML
129 /** Specialisation for one-dimensional ranges. */
130 template<
133 ALPAKA_FN_HOST_ACC constexpr auto makeIdxMap(
134 auto const& acc,
135 auto const workGroup,
136 alpaka::concepts::IdxRange auto const range,
137 T_Traverse traverse = T_Traverse{},
138 T_IdxLayout idxLayout = T_IdxLayout{}) requires(ALPAKA_TYPEOF(range)::dim() == 1u)
139 {
140 return internal::MakeIter::
141 Op<void, ALPAKA_TYPEOF(acc), ALPAKA_TYPEOF(DomainSpec{workGroup, range}), T_Traverse, T_IdxLayout>{}(
142 acc,
143 DomainSpec{workGroup, range},
144 traverse,
145 idxLayout);
146 }
147
148 /** Specialisation for one-dimensional ranges. */
149 template<
150 typename T_ScalarIdxType,
153 ALPAKA_FN_HOST_ACC constexpr auto makeIdxMap(
154 auto const& acc,
155 auto const workGroup,
156 alpaka::concepts::IdxRange auto const range,
157 T_Traverse traverse = T_Traverse{},
158 T_IdxLayout idxLayout = T_IdxLayout{}) requires(ALPAKA_TYPEOF(range)::dim() == 1u)
159 {
160 return internal::MakeIter::Op<
161 T_ScalarIdxType,
162 ALPAKA_TYPEOF(acc),
163 ALPAKA_TYPEOF(DomainSpec{workGroup, range}),
164 T_Traverse,
165 T_IdxLayout>{}(acc, DomainSpec{workGroup, range}, traverse, idxLayout);
166 }
167
168 ///@endcond NO_HTML
169 /** @} */
170} // namespace alpaka::onAcc
#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_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
Concept checking whether T is a boundary direction.
functionality which is usable on the accelerator compute device from within a kernel.
Definition executor.hpp:38
ALPAKA_FN_HOST_ACC constexpr auto makeIdxMap(auto const &acc, auto const workGroup, auto const range, T_Traverse traverse=T_Traverse{}, T_IdxLayout idxLayout=T_IdxLayout{})
Creates an index container.
Definition interface.hpp:57
constexpr auto makeDirectionSubRange(auto const range, alpaka::BoundaryDirection< T_dim, T_LowHaloVec, T_UpHaloVec > const &boundaryDir)
Definition IdxRange.hpp:136
The index layout will automatically selected based on the executor.
Definition layout.hpp:27
Linearize the index domain for traversing.
Definition traverse.hpp:22
Traversing the index domain with MD-tiles.
Definition traverse.hpp:42