Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
AsyncRunQueuePool.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* AsyncRunQueuePool.h (C) 2000-2022 */
9/* */
10/* Collection of asynchronous execution queues with priority on accelerator. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_ACCELERATOR_ASYNC_RUNQUEUE_POOL_H
13#define ARCANE_ACCELERATOR_ASYNC_RUNQUEUE_POOL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/accelerator/core/Runner.h"
19#include "arcane/accelerator/core/RunQueue.h"
20#include "arcane/accelerator/core/RunQueueBuildInfo.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::Accelerator
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief Collection of asynchronous execution queues with priority on accelerator.
33 *
34 * The size of the collection is only configurable upon creation and there
35 * is a maximum size of POOL_MAX_SIZE.
36 * If the requested size is greater than this, the actual size of the
37 * collection will be POOL_MAX_SIZE.
38 * The element access operator returns the (i % poolSize())-th
39 *
40 * \warning API is currently under definition.
41 * \note Courtesy of D.Dureau from Pattern4GPU
42 */
43class AsyncRunQueuePool
44{
45 public:
46
47 //! Up to 32 queues (32 = max number of kernels executable simultaneously)
48 // TODO: Constant taken from David Dureau's code in Pattern4GPU, is this limitation necessary?
49 static constexpr Int32 POOL_MAX_SIZE = 32;
50
51 public:
52
53 AsyncRunQueuePool() = delete;
54 AsyncRunQueuePool(const AsyncRunQueuePool&) = delete;
55 AsyncRunQueuePool(AsyncRunQueuePool&&) = delete;
56 AsyncRunQueuePool& operator=(const AsyncRunQueuePool&) = delete;
57 AsyncRunQueuePool& operator=(AsyncRunQueuePool&&) = delete;
58
59 explicit AsyncRunQueuePool(Runner& runner, Int32 pool_size = POOL_MAX_SIZE,
61 : m_pool_size(std::min(pool_size, POOL_MAX_SIZE))
62 {
63 m_pool.reserve(m_pool_size);
64 for (Int32 i(0); i < m_pool_size; ++i) {
66 // TODO: can be changed by std::to_underlying in c++23 (GCC11 CLANG13 MSVC19.30)
67 bi.setPriority(static_cast<std::underlying_type_t<eRunQueuePriority>>(queues_priority));
68 auto queue_ref = makeQueueRef(runner, bi);
69 queue_ref->setAsync(true);
70 m_pool.add(queue_ref);
71 }
72 }
73
74 // TODO: Should the destructor be virtual for potential inheritance?
76 {
77 m_pool_size = 0;
78 m_pool.clear();
79 }
80
81 //! To retrieve the i % poolSize() i-th execution queue
82 inline const RunQueue& operator[](Int32 i) const
83 {
84 return *(m_pool[i % m_pool_size].get());
85 }
86
87 //! To retrieve the i % poolSize() i-th execution queue
89 {
90 return m_pool[i % m_pool_size].get();
91 }
92
93 //! Forces waiting for all RunQueues
94 void waitAll() const
95 {
96 for (auto q : m_pool)
97 q->barrier();
98 }
99
100 //! Size of the collection
101 inline Int32 poolSize() const
102 {
103 return m_pool_size;
104 }
105
106 // TODO: Should it be changed to protected for potential inheritance?
107 private:
108
110 Int32 m_pool_size;
111};
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116/*!
117 * \brief Creates a temporary queue pool associated with \a runner.
118 *
119 * The pool size is AsyncRunQueuePool::POOL_MAX_SIZE and the queues have
120 * a default priority.
121 *
122 * This call is thread-safe if runner.isConcurrentQueueCreation()==true.
123 */
126{
127 return AsyncRunQueuePool(runner);
128}
129
130/*!
131 * \brief Creates a temporary queue pool associated with \a runner.
132 *
133 * This call is thread-safe if runner.isConcurrentQueueCreation()==true.
134 */
137{
138 return AsyncRunQueuePool(runner, size, priority);
139}
140
141/*!
142 * \brief Creates a temporary queue pool associated with \a runner.
143 *
144 * The pool size is AsyncRunQueuePool::POOL_MAX_SIZE and the queues have
145 * a default priority.
146 *
147 * This call is thread-safe if runner.isConcurrentQueueCreation()==true.
148 */
151{
152 ARCANE_CHECK_POINTER(runner);
153 return AsyncRunQueuePool(*runner);
154}
155
156/*!
157 * \brief Creates a temporary queue pool associated with \a runner.
158 *
159 * This call is thread-safe if runner.isConcurrentQueueCreation()==true.
160 */
163{
164 ARCANE_CHECK_POINTER(runner);
165 return AsyncRunQueuePool(*runner, size, priority);
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171} // End namespace Arcane::Accelerator
172
173/*---------------------------------------------------------------------------*/
174/*---------------------------------------------------------------------------*/
175
176#endif
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
Collection of asynchronous execution queues with priority on accelerator.
Int32 poolSize() const
Size of the collection.
RunQueue * operator[](Int32 i)
To retrieve the i % poolSize() i-th execution queue.
static constexpr Int32 POOL_MAX_SIZE
Up to 32 queues (32 = max number of kernels executable simultaneously).
const RunQueue & operator[](Int32 i) const
To retrieve the i % poolSize() i-th execution queue.
void waitAll() const
Forces waiting for all RunQueues.
1D data vector with value semantics (STL style).
Namespace for accelerator usage.
Ref< RunQueue > makeQueueRef(const Runner &runner)
Creates a reference to a queue with the default execution policy of runner.
AsyncRunQueuePool makeAsyncQueuePool(Runner &runner)
Creates a temporary queue pool associated with runner.
eRunQueuePriority
Predefined priority levels for run queues on accelerators.
std::int32_t Int32
Signed integer type of 32 bits.