alpaka
Abstraction Library for Parallel Kernel Acceleration
IdxBtOmp.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Axel Huebl, Benjamin Worpitz, Matthias Werner, Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Assert.hpp"
10 #include "alpaka/idx/MapIdx.hpp"
11 #include "alpaka/idx/Traits.hpp"
12 #include "alpaka/vec/Vec.hpp"
14 
15 #ifdef _OPENMP
16 
17 # include <omp.h>
18 
19 namespace alpaka
20 {
21  namespace bt
22  {
23  //! The OpenMP accelerator index provider.
24  template<typename TDim, typename TIdx>
25  class IdxBtOmp : public interface::Implements<ConceptIdxBt, IdxBtOmp<TDim, TIdx>>
26  {
27  };
28  } // namespace bt
29 
30  namespace trait
31  {
32  //! The OpenMP accelerator index dimension get trait specialization.
33  template<typename TDim, typename TIdx>
34  struct DimType<bt::IdxBtOmp<TDim, TIdx>>
35  {
36  using type = TDim;
37  };
38 
39  //! The OpenMP accelerator block thread index get trait specialization.
40  template<typename TDim, typename TIdx>
41  struct GetIdx<bt::IdxBtOmp<TDim, TIdx>, origin::Block, unit::Threads>
42  {
43  //! \return The index of the current thread in the block.
44  template<typename TWorkDiv>
45  static auto getIdx(bt::IdxBtOmp<TDim, TIdx> const& /* idx */, TWorkDiv const& workDiv) -> Vec<TDim, TIdx>
46  {
47  // We assume that the thread id is positive.
48  ALPAKA_ASSERT_ACC(::omp_get_thread_num() >= 0);
49  // \TODO: Would it be faster to precompute the index and cache it inside an array?
50  return mapIdx<TDim::value>(
51  Vec<DimInt<1u>, TIdx>(static_cast<TIdx>(::omp_get_thread_num())),
52  getWorkDiv<Block, Threads>(workDiv));
53  }
54  };
55 
56  template<typename TIdx>
57  struct GetIdx<bt::IdxBtOmp<DimInt<1u>, TIdx>, origin::Block, unit::Threads>
58  {
59  //! \return The index of the current thread in the block.
60  template<typename TWorkDiv>
61  static auto getIdx(bt::IdxBtOmp<DimInt<1u>, TIdx> const& /* idx */, TWorkDiv const&)
62  -> Vec<DimInt<1u>, TIdx>
63  {
64  return Vec<DimInt<1u>, TIdx>(static_cast<TIdx>(::omp_get_thread_num()));
65  }
66  };
67 
68  //! The OpenMP accelerator block thread index idx type trait specialization.
69  template<typename TDim, typename TIdx>
70  struct IdxType<bt::IdxBtOmp<TDim, TIdx>>
71  {
72  using type = TIdx;
73  };
74  } // namespace trait
75 } // namespace alpaka
76 
77 #endif
#define ALPAKA_ASSERT_ACC(...)
ALPAKA_ASSERT_ACC is an assert-like macro.
Definition: Assert.hpp:52
The OpenMP accelerator index provider.
Definition: IdxBtOmp.hpp:26
The alpaka accelerator library.
std::integral_constant< std::size_t, N > DimInt
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition: Interface.hpp:15
The dimension getter type trait.
Definition: Traits.hpp:14
static auto getIdx(bt::IdxBtOmp< DimInt< 1u >, TIdx > const &, TWorkDiv const &) -> Vec< DimInt< 1u >, TIdx >
Definition: IdxBtOmp.hpp:61
static auto getIdx(bt::IdxBtOmp< TDim, TIdx > const &, TWorkDiv const &workDiv) -> Vec< TDim, TIdx >
Definition: IdxBtOmp.hpp:45
The index get trait.
Definition: Traits.hpp:42
The idx type trait.
Definition: Traits.hpp:25