alpaka
Abstraction Library for Parallel Kernel Acceleration
IdxBtRefThreadIdMap.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/Traits.hpp"
11 #include "alpaka/vec/Vec.hpp"
12 
13 #include <map>
14 #include <thread>
15 
16 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_THREADS_ENABLED
17 
18 namespace alpaka
19 {
20  namespace bt
21  {
22  //! The threads accelerator index provider.
23  template<typename TDim, typename TIdx>
24  class IdxBtRefThreadIdMap : public interface::Implements<ConceptIdxBt, IdxBtRefThreadIdMap<TDim, TIdx>>
25  {
26  public:
27  using ThreadIdToIdxMap = std::map<std::thread::id, Vec<TDim, TIdx>>;
28 
30  : m_threadToIndexMap(mThreadToIndices)
31  {
32  }
33 
36 
37  public:
38  ThreadIdToIdxMap const& m_threadToIndexMap; //!< The mapping of thread id's to thread indices.
39  };
40  } // namespace bt
41 
42  namespace trait
43  {
44  //! The CPU threads accelerator index dimension get trait specialization.
45  template<typename TDim, typename TIdx>
46  struct DimType<bt::IdxBtRefThreadIdMap<TDim, TIdx>>
47  {
48  using type = TDim;
49  };
50 
51  //! The CPU threads accelerator block thread index get trait specialization.
52  template<typename TDim, typename TIdx>
53  struct GetIdx<bt::IdxBtRefThreadIdMap<TDim, TIdx>, origin::Block, unit::Threads>
54  {
55  //! \return The index of the current thread in the block.
56  template<typename TWorkDiv>
57  ALPAKA_FN_HOST static auto getIdx(
59  TWorkDiv const& /* workDiv */) -> Vec<TDim, TIdx>
60  {
61  auto const threadId = std::this_thread::get_id();
62  auto const threadEntry = idx.m_threadToIndexMap.find(threadId);
63  ALPAKA_ASSERT(threadEntry != std::end(idx.m_threadToIndexMap));
64  return threadEntry->second;
65  }
66  };
67 
68  //! The CPU threads accelerator block thread index idx type trait specialization.
69  template<typename TDim, typename TIdx>
70  struct IdxType<bt::IdxBtRefThreadIdMap<TDim, TIdx>>
71  {
72  using type = TIdx;
73  };
74  } // namespace trait
75 } // namespace alpaka
76 
77 #endif
#define ALPAKA_ASSERT(...)
The assert can be explicit disabled by defining NDEBUG.
Definition: Assert.hpp:13
The threads accelerator index provider.
ALPAKA_FN_HOST IdxBtRefThreadIdMap(ThreadIdToIdxMap const &mThreadToIndices)
std::map< std::thread::id, Vec< TDim, TIdx > > ThreadIdToIdxMap
ALPAKA_FN_HOST IdxBtRefThreadIdMap(IdxBtRefThreadIdMap const &)=delete
ALPAKA_FN_HOST auto operator=(IdxBtRefThreadIdMap const &) -> IdxBtRefThreadIdMap &=delete
ThreadIdToIdxMap const & m_threadToIndexMap
The mapping of thread id's to thread indices.
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
ALPAKA_FN_HOST auto end(TView &view) -> Iterator< TView >
Definition: Iterator.hpp:139
The alpaka accelerator library.
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 ALPAKA_FN_HOST auto getIdx(bt::IdxBtRefThreadIdMap< TDim, TIdx > const &idx, TWorkDiv const &) -> Vec< TDim, TIdx >
The index get trait.
Definition: Traits.hpp:42
The idx type trait.
Definition: Traits.hpp:25