Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
SyclAccelerator.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/* SyclAccelerator.cc (C) 2000-2024 */
9/* */
10/* Backend 'SYCL' pour les accélérateurs. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/accelerator/sycl/SyclAccelerator.h"
15#include "arcane/accelerator/sycl/internal/SyclAcceleratorInternal.h"
16
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/TraceInfo.h"
20#include "arcane/utils/NotSupportedException.h"
21#include "arcane/utils/FatalErrorException.h"
22#include "arcane/utils/IMemoryAllocator.h"
23
24#include <iostream>
25
26namespace Arcane::Accelerator::Sycl
27{
28
29using namespace Arccore;
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34std::unique_ptr<sycl::queue> global_default_queue;
35namespace
36{
37 sycl::queue& _defaultQueue()
38 {
39 return *global_default_queue;
40 }
41} // namespace
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
50{
51 public:
52
55 {}
56
57 bool hasRealloc(MemoryAllocationArgs) const override { return true; }
59 {
60 sycl::queue& q = _defaultQueue();
61 void* out = nullptr;
62 _allocate(&out, new_size, args, q);
63 if (!out)
64 ARCANE_FATAL("Can not allocate memory size={0}", new_size);
65 Int64 a = reinterpret_cast<Int64>(out);
66 if ((a % 128) != 0)
67 ARCANE_FATAL("Bad alignment for SYCL allocator: offset={0}", (a % 128));
68 return { out, new_size };
69 }
71 {
72 sycl::queue& q = _defaultQueue();
74 q.submit([&](sycl::handler& cgh) {
75 cgh.memcpy(a.baseAddress(), current_ptr.baseAddress(), current_ptr.size());
76 });
77 q.wait();
78
80 return a;
81 }
83 {
84 sycl::queue& q = _defaultQueue();
85 _deallocate(ptr.baseAddress(), args, q);
86 }
87
88 protected:
89
90 virtual void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) = 0;
91 virtual void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) = 0;
92};
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
99{
100 protected:
101
102 void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) override
103 {
104 *ptr = sycl::malloc_shared(new_size, q);
105 }
106 void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) override
107 {
108 sycl::free(ptr, q);
109 }
110 eMemoryResource memoryResource() const override { return eMemoryResource::UnifiedMemory; }
111};
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
118{
119 protected:
120
121 void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) override
122 {
123 // TODO: Faire host-pinned
124 *ptr = sycl::malloc_host(new_size, q);
125 }
126 void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) override
127 {
128 sycl::free(ptr, q);
129 }
130 eMemoryResource memoryResource() const override { return eMemoryResource::HostPinned; }
131};
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
138{
139 protected:
140
141 void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) override
142 {
143 *ptr = sycl::malloc_device(new_size, q);
144 }
145 void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) override
146 {
147 sycl::free(ptr, q);
148 }
149 eMemoryResource memoryResource() const override { return eMemoryResource::Device; }
150};
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155namespace
156{
157 UnifiedMemorySyclMemoryAllocator unified_memory_sycl_memory_allocator;
158 HostPinnedSyclMemoryAllocator host_pinned_sycl_memory_allocator;
159 DeviceSyclMemoryAllocator device_sycl_memory_allocator;
160} // namespace
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
164
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
170namespace Arcane::Accelerator
171{
172
174getSyclMemoryAllocator()
175{
176 return &unified_memory_sycl_memory_allocator;
177}
178
180getSyclDeviceMemoryAllocator()
181{
182 return &device_sycl_memory_allocator;
183}
184
186getSyclUnifiedMemoryAllocator()
187{
188 return &unified_memory_sycl_memory_allocator;
189}
190
192getSyclHostPinnedMemoryAllocator()
193{
194 return &host_pinned_sycl_memory_allocator;
195}
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200void Sycl::
201setSyclMemoryQueue(const sycl::queue& memory_queue)
202{
203 global_default_queue = std::make_unique<sycl::queue>(memory_queue);
204}
205
206/*---------------------------------------------------------------------------*/
207/*---------------------------------------------------------------------------*/
208
209} // namespace Arcane::Accelerator
210
211/*---------------------------------------------------------------------------*/
212/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Classe de base d'un allocateur spécifique pour 'Sycl'.
bool hasRealloc(MemoryAllocationArgs) const override
Indique si l'allocateur supporte la sémantique de realloc.
AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override
Réalloue de la mémoire pour new_size octets et retourne le pointeur.
AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override
Alloue de la mémoire pour new_size octets et retourne le pointeur.
void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override
Libère la mémoire dont l'adresse de base est ptr.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Allocateur mémoire avec alignement mémoire spécifique.
Informations sur une zone mémoire allouée.
Classe contenant des informations pour spécialiser les allocations.
Espace de nom pour l'utilisation des accélérateurs.
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
std::int64_t Int64
Type entier signé sur 64 bits.
eMemoryResource
Liste des ressources mémoire disponibles.