14#include "arccore/common/accelerator/internal/RunCommandImpl.h"
15#include "arccore/common/accelerator/internal/AcceleratorCoreGlobalInternal.h"
17#include "arccore/base/FatalErrorException.h"
18#include "arccore/base/ForLoopTraceInfo.h"
19#include "arccore/base/PlatformUtils.h"
20#include "arccore/base/Convert.h"
21#include "arccore/base/ConcurrencyBase.h"
23#include "arccore/common/accelerator/Runner.h"
24#include "arccore/common/accelerator/internal/IRunQueueEventImpl.h"
25#include "arccore/common/accelerator/internal/IRunQueueStream.h"
26#include "arccore/common/accelerator/internal/IRunnerRuntime.h"
27#include "arccore/common/accelerator/internal/RunQueueImpl.h"
28#include "arccore/common/accelerator/internal/ReduceMemoryImpl.h"
29#include "arccore/common/accelerator/internal/RunnerImpl.h"
34namespace Arcane::Accelerator::impl
41RunCommandImpl(RunQueueImpl* queue)
43, m_use_accelerator(impl::isAcceleratorPolicy(queue->runner()->executionPolicy()))
65 while (!m_reduce_memory_pool.empty()) {
66 delete m_reduce_memory_pool.top();
67 m_reduce_memory_pool.pop();
74IRunQueueEventImpl* RunCommandImpl::
77 if (m_use_sequential_timer_event)
78 return getSequentialRunQueueRuntime()->createEventImplWithTimer();
79 return runner()->_createEventWithTimer();
96 if (m_use_accelerator && !ProfilingRegistry::hasProfiling())
97 m_use_sequential_timer_event =
true;
99 m_start_event = _createEvent();
100 m_stop_event = _createEvent();
102 if (
auto v = Convert::Type<Int32>::tryParseFromEnvironment(
"ARCANE_ACCELERATOR_ALLOW_REUSE_COMMAND",
true))
103 m_is_allow_reuse_command = (v.value() != 0);
109RunCommandImpl* RunCommandImpl::
110create(RunQueueImpl* r)
112 RunCommandImpl* c = r->_internalCreateOrGetRunCommandImpl();
123notifyBeginLaunchKernel()
125 if (m_has_been_launched) {
126 if (!m_is_allow_reuse_command)
127 ARCCORE_FATAL(
"Command has already been launched. You can not re-use the same command.\n"
128 " You can temporarily allow it if you set environment variable\n"
129 " ARCANE_ACCELERATOR_ALLOW_REUSE_COMMAND to 1\n");
131 IRunQueueStream* stream = internalStream();
132 stream->notifyBeginLaunchKernel(*
this);
134 m_has_been_launched =
true;
135 if (m_use_profiling) {
136 m_start_event->recordQueue(stream);
137 m_begin_time = platform::getRealTimeNS();
138 m_loop_one_exec_stat_ptr = &m_loop_one_exec_stat;
139 m_loop_one_exec_stat.setBeginTime(m_begin_time);
151notifyEndLaunchKernel()
153 IRunQueueStream* stream = internalStream();
156 m_stop_event->recordQueue(stream);
157 stream->notifyEndLaunchKernel(*
this);
158 m_queue->_addRunningCommand(
this);
171notifyLaunchKernelSyclEvent(
void* sycl_event_ptr)
173 IRunQueueStream* stream = internalStream();
174 stream->_setSyclLastCommandEvent(sycl_event_ptr);
178 m_start_event->recordQueue(stream);
191notifyEndExecuteKernel()
194 if (!m_has_been_launched)
197 Int64 diff_time_ns = 0;
198 if (m_use_profiling){
199 diff_time_ns = m_stop_event->elapsedTime(m_start_event);
200 runner()->addTime((
double)diff_time_ns / 1.0e9);
203 ForLoopOneExecStat* exec_info = m_loop_one_exec_stat_ptr;
205 exec_info->setEndTime(m_begin_time + diff_time_ns);
207 ForLoopTraceInfo flti(traceInfo(), kernelName());
208 ProfilingRegistry::_threadLocalForLoopInstance()->merge(*exec_info, flti);
218 m_kernel_name = String();
219 m_trace_info = TraceInfo();
220 m_nb_thread_per_block = 0;
221 m_use_profiling = ProfilingRegistry::hasProfiling();
222 m_parallel_loop_options = ConcurrencyBase::defaultParallelLoopOptions();
224 m_loop_one_exec_stat.reset();
225 m_loop_one_exec_stat_ptr =
nullptr;
226 m_has_been_launched =
false;
227 m_has_living_run_command =
false;
228 m_may_be_put_in_pool =
false;
229 m_shared_memory_size = 0;
235IReduceMemoryImpl* RunCommandImpl::
236getOrCreateReduceMemoryImpl()
238 ReduceMemoryImpl* p = _getOrCreateReduceMemoryImpl();
240 m_active_reduce_memory_list.insert(p);
249releaseReduceMemoryImpl(ReduceMemoryImpl* p)
251 auto x = m_active_reduce_memory_list.find(p);
252 if (x == m_active_reduce_memory_list.end())
253 ARCCORE_FATAL(
"ReduceMemoryImpl in not in active list");
254 m_active_reduce_memory_list.erase(x);
255 m_reduce_memory_pool.push(p);
261IRunQueueStream* RunCommandImpl::
262internalStream()
const
264 return m_queue->_internalStream();
270RunnerImpl* RunCommandImpl::
273 return m_queue->runner();
279ReduceMemoryImpl* RunCommandImpl::
280_getOrCreateReduceMemoryImpl()
284 if (!m_use_accelerator)
287 auto& pool = m_reduce_memory_pool;
290 ReduceMemoryImpl* p = pool.top();
294 return new ReduceMemoryImpl(
this);
303_notifyDestroyRunCommand()
307 if (!m_has_been_launched || m_may_be_put_in_pool)
308 m_queue->_putInCommandPool(
this);