alpaka
Abstraction Library for Parallel Kernel Acceleration
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"
8 #include "alpaka/acc/Traits.hpp"
10 #include "alpaka/idx/Accessors.hpp"
11 #include "alpaka/idx/Traits.hpp"
12 #include "alpaka/vec/Vec.hpp"
13 
14 #include <type_traits>
15 
16 namespace 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>;
31  using Vec = alpaka::Vec<Dim, Idx>;
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 constexpr ALPAKA_FN_HOST_ACC auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition: Vec.hpp:126
#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
constexpr ALPAKA_FN_ACC bool oncePerGrid(TAcc const &acc)
Definition: Once.hpp:27
constexpr ALPAKA_FN_ACC bool oncePerBlock(TAcc const &acc)
Definition: Once.hpp:51
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19