Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
RunCommandLaunchInfo.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* RunCommandLaunchInfo.cc (C) 2000-2025 */
9/* */
10/* Informations pour l'exécution d'une 'RunCommand'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/accelerator/core/RunCommandLaunchInfo.h"
15
16#include "arcane/utils/CheckedConvert.h"
17
18#include "arcane/accelerator/core/RunCommand.h"
19#include "arcane/accelerator/core/internal/RunQueueImpl.h"
20#include "arcane/accelerator/core/NativeStream.h"
21#include "arcane/accelerator/core/internal/IRunnerRuntime.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane::Accelerator::impl
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32RunCommandLaunchInfo::
33RunCommandLaunchInfo(RunCommand& command, Int64 total_loop_size)
34: m_command(command)
35, m_total_loop_size(total_loop_size)
36{
37 m_queue_impl = m_command._internalQueueImpl();
38 m_exec_policy = m_queue_impl->executionPolicy();
39
40 // Le calcul des informations de kernel n'est utile que sur accélérateur
41 if (isAcceleratorPolicy(m_exec_policy)) {
42 m_kernel_launch_args = _computeKernelLaunchArgs();
43 m_command._allocateReduceMemory(m_kernel_launch_args.nbBlockPerGrid());
44 }
45}
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50RunCommandLaunchInfo::
51~RunCommandLaunchInfo()
52{
53 // Notifie de la fin de lancement du noyau. Normalement, cela est déjà fait
54 // sauf s'il y a eu une exception pendant le lancement du noyau de calcul.
55 _doEndKernelLaunch();
56}
57
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
61void RunCommandLaunchInfo::
62beginExecute()
63{
64 if (m_has_exec_begun)
65 ARCANE_FATAL("beginExecute() has already been called");
66 m_has_exec_begun = true;
67 m_command._internalNotifyBeginLaunchKernel();
68 if (m_exec_policy == eExecutionPolicy::Thread)
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
80void RunCommandLaunchInfo::
81endExecute()
82{
83 if (!m_has_exec_begun)
84 ARCANE_FATAL("beginExecute() has to be called before endExecute()");
85 _doEndKernelLaunch();
86}
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90
91void RunCommandLaunchInfo::
92_doEndKernelLaunch()
93{
94 if (m_is_notify_end_kernel_done)
95 return;
96 m_is_notify_end_kernel_done = true;
97 m_command._internalNotifyEndLaunchKernel();
98
99 impl::RunQueueImpl* q = m_queue_impl;
100 if (!q->isAsync())
101 q->_internalBarrier();
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107NativeStream RunCommandLaunchInfo::
108_internalNativeStream()
109{
110 return m_command._internalNativeStream();
111}
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
118KernelLaunchArgs RunCommandLaunchInfo::
119_computeKernelLaunchArgs() const
120{
121 int threads_per_block = m_command.nbThreadPerBlock();
122 if (threads_per_block<=0)
123 threads_per_block = 256;
124 Int64 big_b = (m_total_loop_size + threads_per_block - 1) / threads_per_block;
125 int blocks_per_grid = CheckedConvert::toInt32(big_b);
126 return { blocks_per_grid, threads_per_block };
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
132ParallelLoopOptions RunCommandLaunchInfo::
133computeParallelLoopOptions() const
134{
135 ParallelLoopOptions opt = m_command.parallelLoopOptions();
136 const bool use_dynamic_compute = true;
137 // Calcule une taille de grain par défaut si cela n'est pas renseigné dans
138 // les options. Par défaut on fait en sorte de faire un nombre d'itérations
139 // égale à 2 fois le nombre de threads utilisés.
140 if (use_dynamic_compute && opt.grainSize() == 0) {
141 Int32 nb_thread = opt.maxThread();
142 if (nb_thread <= 0)
143 nb_thread = TaskFactory::nbAllowedThread();
144 if (nb_thread <= 0)
145 nb_thread = 1;
146 Int32 grain_size = static_cast<Int32>((double)m_total_loop_size / (nb_thread * 2.0));
147 opt.setGrainSize(grain_size);
148 }
149 return opt;
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
159void RunCommandLaunchInfo::
160_computeLoopRunInfo()
161{
162 ForLoopTraceInfo lti(m_command.traceInfo(), m_command.kernelName());
163 m_loop_run_info = ForLoopRunInfo(computeParallelLoopOptions(), lti);
164 m_loop_run_info.setExecStat(m_command._internalCommandExecStat());
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
178KernelLaunchArgs RunCommandLaunchInfo::
179_threadBlockInfo(const void* func, Int32 shared_memory_size) const
180{
181 return m_queue_impl->_internalRuntime()->computeKernalLaunchArgs(m_kernel_launch_args, func, totalLoopSize(), shared_memory_size);
182}
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187void RunCommandLaunchInfo::
188_addSyclEvent(void* sycl_event_ptr)
189{
190 m_command._internalNotifyBeginLaunchKernelSyclEvent(sycl_event_ptr);
191}
192
193/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
195
196} // End namespace Arcane::Accelerator
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Arguments pour lancer un kernel.
Int64 totalLoopSize() const
Taille totale de la boucle.
ParallelLoopOptions computeParallelLoopOptions() const
Calcule et retourne les informations pour les boucles multi-thread.
void _computeLoopRunInfo()
Calcule la valeur de m_loop_run_info.
File d'exécution pour accélérateur.
void _internalBarrier()
Bloque jusqu'à ce que toutes les commandes soient terminées.
Informations d'exécution d'une boucle.
Informations de trace pour une boucle 'for'.
Options d'exécution d'une boucle parallèle en multi-thread.
Integer grainSize() const
Taille d'un intervalle d'itération.
Int32 maxThread() const
Nombre maximal de threads autorisés.
void setGrainSize(Integer v)
Positionne la taille (approximative) d'un intervalle d'itération.
static Int32 nbAllowedThread()
Nombre de threads utilisés au maximum pour gérer les tâches.
@ Thread
Politique d'exécution multi-thread.
bool isAcceleratorPolicy(eExecutionPolicy exec_policy)
Indique si exec_policy correspond à un accélérateur.
Int32 toInt32(Int64 v)
Converti un Int64 en un Int32.
std::int64_t Int64
Type entier signé sur 64 bits.
std::int32_t Int32
Type entier signé sur 32 bits.