alpaka
Abstraction Library for Parallel Kernel Acceleration
WorkDivMembers.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, Matthias Werner, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
9 #include "alpaka/idx/Traits.hpp"
10 #include "alpaka/vec/Vec.hpp"
12 
13 #include <iosfwd>
14 
15 namespace alpaka
16 {
17  //! A basic class holding the work division as grid block extent, block thread and thread element extent.
18  template<typename TDim, typename TIdx>
19  class WorkDivMembers : public interface::Implements<ConceptWorkDiv, WorkDivMembers<TDim, TIdx>>
20  {
21  public:
23 
24  //! Accepts different alpaka vector types and takes the last TDim number of items.
26  template<typename TGridBlockExtent, typename TBlockThreadExtent, typename TThreadElemExtent>
28  TGridBlockExtent const& gridBlockExtent = TGridBlockExtent(),
29  TBlockThreadExtent const& blockThreadExtent = TBlockThreadExtent(),
30  TThreadElemExtent const& threadElemExtent = TThreadElemExtent())
31  : m_gridBlockExtent(getExtentVecEnd<TDim>(gridBlockExtent))
32  , m_blockThreadExtent(getExtentVecEnd<TDim>(blockThreadExtent))
33  , m_threadElemExtent(getExtentVecEnd<TDim>(threadElemExtent))
34  {
35  }
36 
37  //! \brief Accepts single specific type and is called without explicit template parameters.
40  alpaka::Vec<TDim, TIdx> const& gridBlockExtent,
41  alpaka::Vec<TDim, TIdx> const& blockThreadExtent,
42  alpaka::Vec<TDim, TIdx> const& elemExtent)
43  : m_gridBlockExtent(gridBlockExtent)
44  , m_blockThreadExtent(blockThreadExtent)
45  , m_threadElemExtent(elemExtent)
46  {
47  }
48 
54  {
55  }
56 
58  template<typename TWorkDiv>
59  ALPAKA_FN_HOST_ACC explicit WorkDivMembers(TWorkDiv const& other)
60  : m_gridBlockExtent(subVecEnd<TDim>(getWorkDiv<Grid, Blocks>(other)))
61  , m_blockThreadExtent(subVecEnd<TDim>(getWorkDiv<Block, Threads>(other)))
62  , m_threadElemExtent(subVecEnd<TDim>(getWorkDiv<Thread, Elems>(other)))
63  {
64  }
65 
67  auto operator=(WorkDivMembers const&) -> WorkDivMembers& = default;
68  auto operator=(WorkDivMembers&&) -> WorkDivMembers& = default;
69 
71  template<typename TWorkDiv>
73  {
74  m_gridBlockExtent = subVecEnd<TDim>(getWorkDiv<Grid, Blocks>(other));
75  m_blockThreadExtent = subVecEnd<TDim>(getWorkDiv<Block, Threads>(other));
76  m_threadElemExtent = subVecEnd<TDim>(getWorkDiv<Thread, Elems>(other));
77  return *this;
78  }
79 
80  ALPAKA_FN_HOST_ACC friend constexpr auto operator==(WorkDivMembers const& a, WorkDivMembers const& b) -> bool
81  {
82  return a.m_gridBlockExtent == b.m_gridBlockExtent && a.m_blockThreadExtent == b.m_blockThreadExtent
83  && a.m_threadElemExtent == b.m_threadElemExtent;
84  }
85 
86  ALPAKA_FN_HOST friend auto operator<<(std::ostream& os, WorkDivMembers const& workDiv) -> std::ostream&
87  {
88  return os << "{gridBlockExtent: " << workDiv.m_gridBlockExtent
89  << ", blockThreadExtent: " << workDiv.m_blockThreadExtent
90  << ", threadElemExtent: " << workDiv.m_threadElemExtent << "}";
91  }
92 
93  public:
97  };
98 
99  //! Deduction guide for the constructor which can be called without explicit template type parameters
101  template<typename TDim, typename TIdx>
103  alpaka::Vec<TDim, TIdx> const& gridBlockExtent,
104  alpaka::Vec<TDim, TIdx> const& blockThreadExtent,
106 
107  namespace trait
108  {
109  //! The WorkDivMembers dimension get trait specialization.
110  template<typename TDim, typename TIdx>
111  struct DimType<WorkDivMembers<TDim, TIdx>>
112  {
113  using type = TDim;
114  };
115 
116  //! The WorkDivMembers idx type trait specialization.
117  template<typename TDim, typename TIdx>
118  struct IdxType<WorkDivMembers<TDim, TIdx>>
119  {
120  using type = TIdx;
121  };
122 
123  //! The WorkDivMembers grid block extent trait specialization.
124  template<typename TDim, typename TIdx>
125  struct GetWorkDiv<WorkDivMembers<TDim, TIdx>, origin::Grid, unit::Blocks>
126  {
127  //! \return The number of blocks in each dimension of the grid.
130  {
131  return workDiv.m_gridBlockExtent;
132  }
133  };
134 
135  //! The WorkDivMembers block thread extent trait specialization.
136  template<typename TDim, typename TIdx>
137  struct GetWorkDiv<WorkDivMembers<TDim, TIdx>, origin::Block, unit::Threads>
138  {
139  //! \return The number of threads in each dimension of a block.
142  {
143  return workDiv.m_blockThreadExtent;
144  }
145  };
146 
147  //! The WorkDivMembers thread element extent trait specialization.
148  template<typename TDim, typename TIdx>
149  struct GetWorkDiv<WorkDivMembers<TDim, TIdx>, origin::Thread, unit::Elems>
150  {
151  //! \return The number of elements in each dimension of a thread.
154  {
155  return workDiv.m_threadElemExtent;
156  }
157  };
158  } // namespace trait
159 } // namespace alpaka
A basic class holding the work division as grid block extent, block thread and thread element extent.
ALPAKA_FN_HOST friend auto operator<<(std::ostream &os, WorkDivMembers const &workDiv) -> std::ostream &
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto operator=(TWorkDiv const &other) -> WorkDivMembers< TDim, TIdx > &
auto operator=(WorkDivMembers &&) -> WorkDivMembers &=default
ALPAKA_FN_HOST_ACC WorkDivMembers()=delete
Vec< TDim, TIdx > m_blockThreadExtent
Vec< TDim, TIdx > m_gridBlockExtent
Vec< TDim, TIdx > m_threadElemExtent
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC WorkDivMembers(TWorkDiv const &other)
ALPAKA_FN_HOST_ACC constexpr friend auto operator==(WorkDivMembers const &a, WorkDivMembers const &b) -> bool
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC WorkDivMembers(alpaka::Vec< TDim, TIdx > const &gridBlockExtent, alpaka::Vec< TDim, TIdx > const &blockThreadExtent, alpaka::Vec< TDim, TIdx > const &elemExtent)
Accepts single specific type and is called without explicit template parameters.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC WorkDivMembers(TGridBlockExtent const &gridBlockExtent=TGridBlockExtent(), TBlockThreadExtent const &blockThreadExtent=TBlockThreadExtent(), TThreadElemExtent const &threadElemExtent=TThreadElemExtent())
Accepts different alpaka vector types and takes the last TDim number of items.
auto operator=(WorkDivMembers const &) -> WorkDivMembers &=default
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC WorkDivMembers(WorkDivMembers const &other)
WorkDivMembers(WorkDivMembers &&)=default
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
#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
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getWorkDiv(TWorkDiv const &workDiv) -> Vec< Dim< TWorkDiv >, Idx< TWorkDiv >>
Get the extent requested.
Definition: Traits.hpp:33
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC WorkDivMembers(alpaka::Vec< TDim, TIdx > const &gridBlockExtent, alpaka::Vec< TDim, TIdx > const &blockThreadExtent, alpaka::Vec< TDim, TIdx > const &elemExtent) -> WorkDivMembers< TDim, TIdx >
Deduction guide for the constructor which can be called without explicit template type parameters.
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto subVecEnd(TVec const &vec)
Definition: Traits.hpp:66
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getExtentVecEnd(T const &object={}) -> Vec< TDim, Idx< T >>
Definition: Traits.hpp:78
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
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getWorkDiv(WorkDivMembers< TDim, TIdx > const &workDiv) -> Vec< TDim, TIdx >
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getWorkDiv(WorkDivMembers< TDim, TIdx > const &workDiv) -> Vec< TDim, TIdx >
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getWorkDiv(WorkDivMembers< TDim, TIdx > const &workDiv) -> Vec< TDim, TIdx >
The work div trait.
Definition: Traits.hpp:27
The idx type trait.
Definition: Traits.hpp:25