alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Once.hpp
Go to the documentation of this file.
1/* Copyright 2024 Andrea Bocci
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/acc/Tag.hpp"
11#include "alpaka/idx/Traits.hpp"
12#include "alpaka/vec/Vec.hpp"
13
14#include <type_traits>
15
16namespace alpaka
17{
18
19 /* oncePerGrid
20 *
21 * `oncePerGrid(acc)` returns true for a single thread within the kernel execution grid.
22 *
23 * Usually the condition is true for block 0 and thread 0, but these indices should not be relied upon.
24 */
25
26 template<typename TAcc, typename = std::enable_if_t<isAccelerator<TAcc>>>
27 ALPAKA_FN_ACC inline constexpr bool oncePerGrid(TAcc const& acc)
28 {
29 using Dim = alpaka::Dim<TAcc>;
30 using Idx = alpaka::Idx<TAcc>;
32
33 // Workaround for a weird bug in oneAPI 2024.x targetting the CPU backend and FPGA emulator.
34 if constexpr(accMatchesTags<TAcc, TagCpuSycl, TagFpgaSyclIntel>)
35 {
36 // SYCL accelerator specific code
37 return acc.m_item_workdiv.get_global_linear_id() == 0;
38 }
39
40 return getIdx<Grid, Threads>(acc) == Vec::zeros();
41 }
42
43 /* oncePerBlock
44 *
45 * `oncePerBlock(acc)` returns true for a single thread within the block.
46 *
47 * Usually the condition is true for thread 0, but this index should not be relied upon.
48 */
49
50 template<typename TAcc, typename = std::enable_if_t<isAccelerator<TAcc>>>
51 ALPAKA_FN_ACC inline constexpr bool oncePerBlock(TAcc const& acc)
52 {
53 return getIdx<Block, Threads>(acc) == Vec<Dim<TAcc>, Idx<TAcc>>::zeros();
54 }
55
56} // namespace alpaka
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition Vec.hpp:99
#define ALPAKA_FN_ACC
All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_F...
Definition Common.hpp:38
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
ALPAKA_FN_ACC constexpr bool oncePerGrid(TAcc const &acc)
Definition Once.hpp:27
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
ALPAKA_FN_ACC constexpr bool oncePerBlock(TAcc const &acc)
Definition Once.hpp:51