14#include "arccore/common/accelerator/internal/RunQueueImpl.h"
17#include "arccore/common/SmallArray.h"
19#include "arccore/common/accelerator/internal/IRunnerRuntime.h"
20#include "arccore/common/accelerator/internal/IRunQueueStream.h"
21#include "arccore/common/accelerator/internal/RunCommandImpl.h"
22#include "arccore/common/accelerator/internal/RunnerImpl.h"
23#include "arccore/common/accelerator/internal/IRunQueueEventImpl.h"
25#include "arccore/common/accelerator/Runner.h"
26#include "arccore/common/accelerator/DeviceId.h"
27#include "arccore/common/accelerator/RunQueueEvent.h"
28#include "arccore/common/accelerator/KernelLaunchArgs.h"
30#include <unordered_set>
36namespace Arcane::Accelerator::Impl
47 explicit Lock(RunQueueImpl* p)
49 if (p->m_use_pool_mutex) {
50 m_mutex = p->m_pool_mutex.get();
61 Lock(
const Lock&) =
delete;
62 Lock& operator=(
const Lock&) =
delete;
66 std::mutex* m_mutex =
nullptr;
74: m_runner_impl(runner_impl)
75, m_execution_policy(runner_impl->executionPolicy())
76, m_runtime(runner_impl->runtime())
77, m_queue_stream(m_runtime->createStream(bi))
88 delete m_queue_stream;
101 std::unordered_set<RunCommandImpl*> command_set;
105 if (command_set.find(c) != command_set.end())
106 std::cerr <<
"Command is present several times in the command pool\n";
107 command_set.insert(c);
109 RunCommand::_internalDestroyImpl(c);
118_destroy(RunQueueImpl* q)
120 q->_freeCommandsInPool();
139 if (!_internalStream()->_barrierNoException()) {
143 std::cerr <<
"WARNING: Error in internal accelerator barrier\n";
146 m_runner_impl->_internalPutRunQueueImplInPool(
this);
148 RunQueueImpl::_destroy(
this);
156_setDefaultMemoryRessource()
166MemoryAllocationOptions RunQueueImpl::
167allocationOptions()
const
170 Int16 device_id =
static_cast<Int16>(m_runner_impl->deviceId().asInt32());
171 opt.setDevice(device_id);
179isAutoPrefetchCommand()
const
181 return m_runner_impl->isAutoPrefetchCommand();
188copyMemory(
const MemoryCopyArgs& args)
const
190 _internalStream()->copyMemory(args);
197prefetchMemory(
const MemoryPrefetchArgs& args)
const
199 _internalStream()->prefetchMemory(args);
206recordEvent(RunQueueEvent& event)
208 auto* p =
event._internalEventImpl();
209 return p->recordQueue(_internalStream());
216waitEvent(RunQueueEvent& event)
218 auto* p =
event._internalEventImpl();
219 return p->waitForEvent(_internalStream());
225RunQueueImpl* RunQueueImpl::
228 return _reset(r->_internalCreateOrGetRunQueueImpl());
234RunQueueImpl* RunQueueImpl::
235create(RunnerImpl* r,
const RunQueueBuildInfo& bi)
237 return _reset(r->_internalCreateOrGetRunQueueImpl(bi));
243RunCommandImpl* RunQueueImpl::
244_internalCreateOrGetRunCommandImpl()
246 RunCommandImpl* p =
nullptr;
257 p = RunCommand::_internalCreateImpl(
this);
274 if (m_use_pool_mutex) {
285 for (RunCommandImpl* p : command_list) {
286 p->notifyEndExecuteKernel();
290 for (RunCommandImpl* p : command_list) {
297 p->notifyEndExecuteKernel();
340_putInCommandPool(RunCommandImpl* p)
355 _internalStream()->barrier();
373 p->_setDefaultMemoryRessource();
381setConcurrentCommandCreation(
bool v)
383 m_use_pool_mutex = v;
384 if (!m_pool_mutex.get())
385 m_pool_mutex = std::make_unique<std::mutex>();
392dumpStats(std::ostream& ostr)
const
Memory management utility functions.
Implementation of a command for accelerator.
bool m_may_be_put_in_pool
Indicates if the command can be returned to the pool associated with the RunQueue.
bool m_has_living_run_command
Indicates if a RunCommand has a reference to this instance.
Lock for the RunCommand pool in multi-thread.
eMemoryResource m_memory_ressource
Default memory resource.
void _checkPutCommandInPoolNoLock(RunCommandImpl *p)
Returns the command to the pool if possible.
UniqueArray< RunCommandImpl * > m_active_run_command_list
List of running commands.
void _internalFreeRunningCommands()
Frees running commands.
bool m_is_async
Indicates if the queue is asynchronous.
static RunQueueImpl * _reset(RunQueueImpl *p)
Resets the implementation.
std::stack< RunCommandImpl * > m_run_command_pool
Command pool.
void _internalBarrier()
Blocks until all commands are finished.
Information to create a RunQueue.
void add(ConstReferenceType val)
Adds element val to the end of the array.
1D data array with pre-allocated stack buffer.
bool isAcceleratorPolicy(eExecutionPolicy exec_policy)
Indicates if exec_policy corresponds to an accelerator.
MemoryAllocationOptions getAllocationOptions(eMemoryResource mem_resource)
Default allocation for the resource mem_resource.
eMemoryResource getDefaultDataMemoryResource()
Memory resource used by the default allocator for data.
std::int16_t Int16
Signed integer type of 16 bits.
bool arccoreIsCheck()
True if in check mode.
@ Host
Allocates on the host.
std::int32_t Int32
Signed integer type of 32 bits.