Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
AcceleratorCore.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* AcceleratorCore.cc (C) 2000-2024 */
9/* */
10/* Déclarations générales pour le support des accélérateurs. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/FatalErrorException.h"
15
16#include "arcane/accelerator/core/internal/AcceleratorCoreGlobalInternal.h"
17#include "arcane/accelerator/core/internal/IRunnerRuntime.h"
18
19#include "arcane/accelerator/core/DeviceInfoList.h"
20#include "arcane/accelerator/core/PointerAttribute.h"
21#include "arcane/accelerator/core/ViewBuildInfo.h"
22#include "arcane/accelerator/core/RunCommand.h"
23#include "arcane/accelerator/core/RunQueue.h"
24
25#include <iostream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42namespace Arcane::Accelerator
43{
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48namespace
49{
50 bool global_is_using_cuda_runtime = false;
51 impl::IRunnerRuntime* global_cuda_runqueue_runtime = nullptr;
52 bool global_is_using_hip_runtime = false;
53 impl::IRunnerRuntime* global_hip_runqueue_runtime = nullptr;
54 bool global_is_using_sycl_runtime = false;
55 impl::IRunnerRuntime* global_sycl_runqueue_runtime = nullptr;
56} // namespace
57
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
61extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT bool impl::
62isUsingCUDARuntime()
63{
64 return global_is_using_cuda_runtime;
65}
66
67extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
68setUsingCUDARuntime(bool v)
69{
70 global_is_using_cuda_runtime = v;
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
77extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT impl::IRunnerRuntime* impl::
78getCUDARunQueueRuntime()
79{
80 return global_cuda_runqueue_runtime;
81}
82
84extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
85setCUDARunQueueRuntime(IRunnerRuntime* v)
86{
87 global_cuda_runqueue_runtime = v;
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT bool impl::
94isUsingHIPRuntime()
95{
96 return global_is_using_hip_runtime;
97}
98
99extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
100setUsingHIPRuntime(bool v)
101{
102 global_is_using_hip_runtime = v;
103}
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
109extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT impl::IRunnerRuntime* impl::
110getHIPRunQueueRuntime()
111{
112 return global_hip_runqueue_runtime;
113}
114
116extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
117setHIPRunQueueRuntime(impl::IRunnerRuntime* v)
118{
119 global_hip_runqueue_runtime = v;
120}
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
125extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT bool impl::
126isUsingSYCLRuntime()
127{
128 return global_is_using_sycl_runtime;
129}
130
131extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
132setUsingSYCLRuntime(bool v)
133{
134 global_is_using_sycl_runtime = v;
135}
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT impl::IRunnerRuntime* impl::
141getSYCLRunQueueRuntime()
142{
143 return global_hip_runqueue_runtime;
144}
145
146extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT void impl::
147setSYCLRunQueueRuntime(impl::IRunnerRuntime* v)
148{
149 global_hip_runqueue_runtime = v;
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
156extern "C++" ARCANE_ACCELERATOR_CORE_EXPORT
157std::ostream&
159{
160 switch (exec_policy) {
162 o << "None";
163 break;
165 o << "Sequential";
166 break;
168 o << "Thread";
169 break;
171 o << "CUDA";
172 break;
174 o << "HIP";
175 break;
177 o << "SYCL";
178 break;
179 }
180 return o;
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186std::ostream& operator<<(std::ostream& o, const DeviceId& device_id)
187{
188 o << device_id.asInt32();
189 return o;
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
195std::ostream&
196operator<<(std::ostream& o, ePointerMemoryType mem_type)
197{
198 switch (mem_type) {
199 case ePointerMemoryType::Unregistered:
200 o << "Unregistered";
201 break;
202 case ePointerMemoryType::Host:
203 o << "Host";
204 break;
205 case ePointerMemoryType::Device:
206 o << "Device";
207 break;
208 case ePointerMemoryType::Managed:
209 o << "Managed";
210 break;
211 }
212 return o;
213}
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218extern "C++" impl::IRunnerRuntime* impl::
219getAcceleratorRunnerRuntime()
220{
221 if (isUsingCUDARuntime())
222 return getCUDARunQueueRuntime();
223 if (isUsingHIPRuntime())
224 return getHIPRunQueueRuntime();
225 if (isUsingSYCLRuntime())
226 return getSYCLRunQueueRuntime();
227 return nullptr;
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233ePointerAccessibility impl::RuntimeStaticInfo::
234getPointerAccessibility(eExecutionPolicy policy, const void* ptr, PointerAttribute* ptr_attr)
235{
236 // Regarde si le pointeur est accessible pour la politique d'exécution donnée.
237 // Le seul cas où on peut le savoir exactement est si on a un runtime
238 // accélérateur et que la valeur retournée par getPointeAttribute() est valide.
239 if (policy == eExecutionPolicy::None)
241 IRunnerRuntime* r = getAcceleratorRunnerRuntime();
242 if (!r)
244 PointerAttribute attr;
245 r->getPointerAttribute(attr, ptr);
246 if (ptr_attr) {
247 *ptr_attr = attr;
248 }
249 if (attr.isValid()) {
250 if (isAcceleratorPolicy(policy))
251 return attr.devicePointer() ? ePointerAccessibility::Yes : ePointerAccessibility::No;
252 else {
253 if (attr.memoryType() == ePointerMemoryType::Unregistered)
255 return attr.hostPointer() ? ePointerAccessibility::Yes : ePointerAccessibility::No;
256 }
257 }
259}
260
261/*---------------------------------------------------------------------------*/
262/*---------------------------------------------------------------------------*/
263
264void impl::RuntimeStaticInfo::
265checkPointerIsAcccessible(eExecutionPolicy policy, const void* ptr,
266 const char* name, const TraceInfo& ti)
267{
268 // Le pointeur nul est toujours accessible.
269 if (!ptr)
270 return;
271 PointerAttribute ptr_attr;
272 ePointerAccessibility a = getPointerAccessibility(policy, ptr, &ptr_attr);
273 if (a == ePointerAccessibility::No) {
274 auto s = String::format("Pointer 'addr={0}' ({1}) is not accessible "
275 "for this execution policy ({2}).\n PointerInfo: {3}",
276 ptr, name, policy, ptr_attr);
277
278 throw FatalErrorException(ti, s);
279 }
280}
281
282/*---------------------------------------------------------------------------*/
283/*---------------------------------------------------------------------------*/
284
285extern "C++" ePointerAccessibility
287{
288 return impl::RuntimeStaticInfo::getPointerAccessibility(policy, ptr, ptr_attr);
289}
290
291extern "C++" void impl::
292arcaneCheckPointerIsAccessible(eExecutionPolicy policy, const void* ptr,
293 const char* name, const TraceInfo& ti)
294{
295 return impl::RuntimeStaticInfo::checkPointerIsAcccessible(policy, ptr, name, ti);
296}
297
298/*---------------------------------------------------------------------------*/
299/*---------------------------------------------------------------------------*/
300
301std::ostream&
302operator<<(std::ostream& o, const PointerAttribute& a)
303{
304 o << "(mem_type=" << a.memoryType() << ", ptr=" << a.originalPointer()
305 << ", host_ptr=" << a.hostPointer()
306 << ", device_ptr=" << a.devicePointer() << " device=" << a.device() << ")";
307 return o;
308}
309
310/*---------------------------------------------------------------------------*/
311/*---------------------------------------------------------------------------*/
312
314ViewBuildInfo(const RunQueue& queue)
315: m_queue_impl(queue._internalImpl())
316{}
317
318/*---------------------------------------------------------------------------*/
319/*---------------------------------------------------------------------------*/
320
322ViewBuildInfo(const RunQueue* queue)
323: m_queue_impl(queue->_internalImpl())
324{}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
331: m_queue_impl(command._internalQueueImpl())
332{
333}
334
335/*---------------------------------------------------------------------------*/
336/*---------------------------------------------------------------------------*/
337
338} // namespace Arcane::Accelerator
339
340/*---------------------------------------------------------------------------*/
341/*---------------------------------------------------------------------------*/
Identifiant d'un composant du système.
Definition DeviceId.h:33
Informations sur une adresse mémoire.
Gestion d'une commande sur accélérateur.
File d'exécution pour un accélérateur.
ViewBuildInfo(const RunQueue &queue)
Créé instance associée a la file queue.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Espace de nom pour l'utilisation des accélérateurs.
ePointerAccessibility getPointerAccessibility(eExecutionPolicy policy, const void *ptr, PointerAttribute *ptr_attr)
Accessibilité de l'adresse ptr pour une politique d'exécutionpolicy.
std::ostream & operator<<(std::ostream &o, eExecutionPolicy exec_policy)
Affiche le nom de la politique d'exécution.
ePointerAccessibility
Informations d'accessibilité d'une adresse mémoire.
ePointerMemoryType
Type de mémoire pour un pointeur.
eExecutionPolicy
Politique d'exécution pour un Runner.
@ SYCL
Politique d'exécution utilisant l'environnement SYCL.
@ HIP
Politique d'exécution utilisant l'environnement HIP.
@ None
Aucune politique d'exécution.
@ CUDA
Politique d'exécution utilisant l'environnement CUDA.
@ Sequential
Politique d'exécution séquentielle.
@ Thread
Politique d'exécution multi-thread.
bool isAcceleratorPolicy(eExecutionPolicy exec_policy)
Indique si exec_policy correspond à un accélérateur.