alpaka
Abstraction Library for Parallel Kernel Acceleration
QueueGenericThreadsBlocking.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/dev/Traits.hpp"
11 #include "alpaka/wait/Traits.hpp"
12 
13 #include <atomic>
14 #include <memory>
15 #include <mutex>
16 
17 namespace alpaka
18 {
19  template<typename TDev>
20  class EventGenericThreads;
21 
22  namespace generic
23  {
24  namespace detail
25  {
26 #if BOOST_COMP_CLANG
27 // avoid diagnostic warning: "has no out-of-line virtual method definitions; its vtable will be emitted in every
28 // translation unit [-Werror,-Wweak-vtables]" https://stackoverflow.com/a/29288300
29 # pragma clang diagnostic push
30 # pragma clang diagnostic ignored "-Wweak-vtables"
31 #endif
32  //! The CPU device queue implementation.
33  template<typename TDev>
35 #if BOOST_COMP_CLANG
36 # pragma clang diagnostic pop
37 #endif
38  {
39  public:
40  explicit QueueGenericThreadsBlockingImpl(TDev dev) noexcept
41  : m_dev(std::move(dev))
43  {
44  }
45 
49 
51  {
52  alpaka::enqueue(*this, ev);
53  }
54 
55  void wait(EventGenericThreads<TDev> const& ev) final
56  {
57  alpaka::wait(*this, ev);
58  }
59 
60  public:
61  TDev const m_dev; //!< The device this queue is bound to.
62  std::mutex mutable m_mutex;
63  std::atomic<bool> m_bCurrentlyExecutingTask;
64  };
65  } // namespace detail
66  } // namespace generic
67 
68  //! The CPU device queue.
69  template<typename TDev>
71  : public concepts::Implements<ConceptCurrentThreadWaitFor, QueueGenericThreadsBlocking<TDev>>
72  , public concepts::Implements<ConceptQueue, QueueGenericThreadsBlocking<TDev>>
73  , public concepts::Implements<ConceptGetDev, QueueGenericThreadsBlocking<TDev>>
74  {
75  public:
76  explicit QueueGenericThreadsBlocking(TDev const& dev)
77  : m_spQueueImpl(std::make_shared<generic::detail::QueueGenericThreadsBlockingImpl<TDev>>(dev))
78  {
80 
81  dev.registerQueue(m_spQueueImpl);
82  }
83 
84  auto operator==(QueueGenericThreadsBlocking<TDev> const& rhs) const -> bool
85  {
86  return (m_spQueueImpl == rhs.m_spQueueImpl);
87  }
88 
89  auto operator!=(QueueGenericThreadsBlocking<TDev> const& rhs) const -> bool
90  {
91  return !((*this) == rhs);
92  }
93 
94  public:
95  std::shared_ptr<generic::detail::QueueGenericThreadsBlockingImpl<TDev>> m_spQueueImpl;
96  };
97 
98  namespace trait
99  {
100  //! The CPU blocking device queue device type trait specialization.
101  template<typename TDev>
103  {
104  using type = TDev;
105  };
106 
107  //! The CPU blocking device queue device get trait specialization.
108  template<typename TDev>
110  {
111  ALPAKA_FN_HOST static auto getDev(QueueGenericThreadsBlocking<TDev> const& queue) -> TDev
112  {
113  return queue.m_spQueueImpl->m_dev;
114  }
115  };
116 
117  //! The CPU blocking device queue event type trait specialization.
118  template<typename TDev>
120  {
122  };
123 
124  //! The CPU blocking device queue enqueue trait specialization.
125  //! This default implementation for all tasks directly invokes the function call operator of the task.
126  template<typename TDev, typename TTask>
128  {
129  ALPAKA_FN_HOST static auto enqueue(QueueGenericThreadsBlocking<TDev>& queue, TTask const& task) -> void
130  {
131  std::lock_guard<std::mutex> lk(queue.m_spQueueImpl->m_mutex);
132 
133  queue.m_spQueueImpl->m_bCurrentlyExecutingTask = true;
134 
135  task();
136 
137  queue.m_spQueueImpl->m_bCurrentlyExecutingTask = false;
138  }
139  };
140 
141  //! The CPU blocking device queue test trait specialization.
142  template<typename TDev>
144  {
145  ALPAKA_FN_HOST static auto empty(QueueGenericThreadsBlocking<TDev> const& queue) -> bool
146  {
147  return !queue.m_spQueueImpl->m_bCurrentlyExecutingTask;
148  }
149  };
150 
151  //! The CPU blocking device queue thread wait trait specialization.
152  //!
153  //! Blocks execution of the calling thread until the queue has finished processing all previously requested
154  //! tasks (kernels, data copies, ...)
155  template<typename TDev>
157  {
159  {
160  std::lock_guard<std::mutex> lk(queue.m_spQueueImpl->m_mutex);
161  }
162  };
163  } // namespace trait
164 } // namespace alpaka
165 
#define ALPAKA_DEBUG_FULL_LOG_SCOPE
Definition: Debug.hpp:62
std::shared_ptr< generic::detail::QueueGenericThreadsBlockingImpl< TDev > > m_spQueueImpl
auto operator==(QueueGenericThreadsBlocking< TDev > const &rhs) const -> bool
auto operator!=(QueueGenericThreadsBlocking< TDev > const &rhs) const -> bool
auto operator=(QueueGenericThreadsBlockingImpl< TDev > const &) -> QueueGenericThreadsBlockingImpl< TDev > &=delete
void wait(EventGenericThreads< TDev > const &ev) final
waiting for the event
QueueGenericThreadsBlockingImpl(QueueGenericThreadsBlockingImpl< TDev > const &)=delete
void enqueue(EventGenericThreads< TDev > &ev) final
enqueue the event
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
The alpaka accelerator library.
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition: Traits.hpp:47
ALPAKA_FN_HOST auto wait(TAwaited const &awaited) -> void
Waits the thread for the completion of the given awaited action to complete.
Definition: Traits.hpp:34
Tag used in class inheritance hierarchies that describes that a specific concept (TConcept) is implem...
Definition: Concepts.hpp:15
static ALPAKA_FN_HOST auto currentThreadWaitFor(QueueGenericThreadsBlocking< TDev > const &queue) -> void
The thread wait trait.
Definition: Traits.hpp:21
The device type trait.
Definition: Traits.hpp:23
static ALPAKA_FN_HOST auto empty(QueueGenericThreadsBlocking< TDev > const &queue) -> bool
The queue empty trait.
Definition: Traits.hpp:31
static ALPAKA_FN_HOST auto enqueue(QueueGenericThreadsBlocking< TDev > &queue, TTask const &task) -> void
The queue enqueue trait.
Definition: Traits.hpp:27
The event type trait.
Definition: Traits.hpp:17
static ALPAKA_FN_HOST auto getDev(QueueGenericThreadsBlocking< TDev > const &queue) -> TDev
The device get trait.
Definition: Traits.hpp:27