alpaka
Abstraction Library for Parallel Kernel Acceleration
Accessors.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Axel Huebl, Benjamin Worpitz, Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
11 #include "alpaka/dim/Traits.hpp"
12 #include "alpaka/idx/Traits.hpp"
13 #include "alpaka/vec/Vec.hpp"
15 
16 #include <utility>
17 
18 namespace alpaka
19 {
20  //! Get the indices requested.
22  template<typename TOrigin, typename TUnit, typename TIdx, typename TWorkDiv>
23  ALPAKA_FN_HOST_ACC auto getIdx(TIdx const& idx, TWorkDiv const& workDiv) -> Vec<Dim<TWorkDiv>, Idx<TIdx>>
24  {
26  }
27 
28  //! Get the indices requested.
30  template<typename TOrigin, typename TUnit, typename TIdxWorkDiv>
31  ALPAKA_FN_HOST_ACC auto getIdx(TIdxWorkDiv const& idxWorkDiv) -> Vec<Dim<TIdxWorkDiv>, Idx<TIdxWorkDiv>>
32  {
33  return trait::GetIdx<TIdxWorkDiv, TOrigin, TUnit>::getIdx(idxWorkDiv, idxWorkDiv);
34  }
35 
36  namespace trait
37  {
38  //! The grid block index get trait specialization for classes with IdxGbBase member type.
39  template<typename TIdxGb>
40  struct GetIdx<TIdxGb, origin::Grid, unit::Blocks>
41  {
43 
44  //! \return The index of the current thread in the grid.
46  template<typename TWorkDiv>
47  ALPAKA_FN_HOST_ACC static auto getIdx(TIdxGb const& idx, TWorkDiv const& workDiv)
49  {
51  }
52  };
53 
54  //! The block thread index get trait specialization for classes with IdxBtBase member type.
55  template<typename TIdxBt>
56  struct GetIdx<TIdxBt, origin::Block, unit::Threads>
57  {
59 
60  //! \return The index of the current thread in the grid.
62  template<typename TWorkDiv>
63  ALPAKA_FN_HOST_ACC static auto getIdx(TIdxBt const& idx, TWorkDiv const& workDiv)
65  {
67  }
68  };
69 
70  //! The grid thread index get trait specialization.
71  template<typename TIdx>
72  struct GetIdx<TIdx, origin::Grid, unit::Threads>
73  {
74  //! \return The index of the current thread in the grid.
76  template<typename TWorkDiv>
77  ALPAKA_FN_HOST_ACC static auto getIdx(TIdx const& idx, TWorkDiv const& workDiv)
78  {
79  return alpaka::getIdx<origin::Grid, unit::Blocks>(idx, workDiv)
80  * getWorkDiv<origin::Block, unit::Threads>(workDiv)
81  + alpaka::getIdx<origin::Block, unit::Threads>(idx, workDiv);
82  }
83  };
84  } // namespace trait
85 
86  //! Get the index of the first element this thread computes.
88  template<typename TIdxWorkDiv, typename TGridThreadIdx, typename TThreadElemExtent>
90  [[maybe_unused]] TIdxWorkDiv const& idxWorkDiv,
91  TGridThreadIdx const& gridThreadIdx,
92  TThreadElemExtent const& threadElemExtent) -> Vec<Dim<TIdxWorkDiv>, Idx<TIdxWorkDiv>>
93  {
94  return gridThreadIdx * threadElemExtent;
95  }
96 
97  //! Get the index of the first element this thread computes.
99  template<typename TIdxWorkDiv, typename TGridThreadIdx>
100  ALPAKA_FN_HOST_ACC auto getIdxThreadFirstElem(TIdxWorkDiv const& idxWorkDiv, TGridThreadIdx const& gridThreadIdx)
102  {
103  auto const threadElemExtent(alpaka::getWorkDiv<alpaka::Thread, alpaka::Elems>(idxWorkDiv));
104  return getIdxThreadFirstElem(idxWorkDiv, gridThreadIdx, threadElemExtent);
105  }
106 
107  //! Get the index of the first element this thread computes.
109  template<typename TIdxWorkDiv>
110  ALPAKA_FN_HOST_ACC auto getIdxThreadFirstElem(TIdxWorkDiv const& idxWorkDiv)
112  {
113  auto const gridThreadIdx(alpaka::getIdx<alpaka::Grid, alpaka::Threads>(idxWorkDiv));
114  return getIdxThreadFirstElem(idxWorkDiv, gridThreadIdx);
115  }
116 } // namespace alpaka
A n-dimensional vector.
Definition: Vec.hpp:38
#define ALPAKA_FN_HOST_ACC
Definition: Common.hpp:39
#define ALPAKA_NO_HOST_ACC_WARNING
Disable nvcc warning: 'calling a host function from host device function.' Usage: ALPAKA_NO_HOST_ACC_...
Definition: Common.hpp:82
typename detail::ImplementationBaseType< TInterface, TDerived >::type ImplementationBase
Returns the type that implements the given interface in the inheritance hierarchy.
Definition: Interface.hpp:66
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getIdx(TIdxWorkDiv const &idxWorkDiv) -> Vec< Dim< TIdxWorkDiv >, Idx< TIdxWorkDiv >>
Get the indices requested.
Definition: Accessors.hpp:31
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getIdx(TIdx const &idx, TWorkDiv const &workDiv) -> Vec< Dim< TWorkDiv >, Idx< TIdx >>
Get the indices requested.
Definition: Accessors.hpp:23
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getIdxThreadFirstElem([[maybe_unused]] TIdxWorkDiv const &idxWorkDiv, TGridThreadIdx const &gridThreadIdx, TThreadElemExtent const &threadElemExtent) -> Vec< Dim< TIdxWorkDiv >, Idx< TIdxWorkDiv >>
Get the index of the first element this thread computes.
Definition: Accessors.hpp:89
interface::ImplementationBase< ConceptIdxBt, TIdxBt > ImplementationBase
Definition: Accessors.hpp:58
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getIdx(TIdxBt const &idx, TWorkDiv const &workDiv) -> Vec< Dim< ImplementationBase >, Idx< ImplementationBase >>
Definition: Accessors.hpp:63
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getIdx(TIdxGb const &idx, TWorkDiv const &workDiv) -> Vec< Dim< ImplementationBase >, Idx< ImplementationBase >>
Definition: Accessors.hpp:47
interface::ImplementationBase< ConceptIdxGb, TIdxGb > ImplementationBase
Definition: Accessors.hpp:42
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getIdx(TIdx const &idx, TWorkDiv const &workDiv)
Definition: Accessors.hpp:77
The index get trait.
Definition: Traits.hpp:42