Arcane  v3.14.10.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};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
117{
118 protected:
119
120 void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) override
121 {
122 // TODO: Faire host-pinned
123 *ptr = sycl::malloc_host(new_size, q);
124 }
125 void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) override
126 {
127 sycl::free(ptr, q);
128 }
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
136{
137 protected:
138
139 void _allocate(void** ptr, size_t new_size, MemoryAllocationArgs, sycl::queue& q) override
140 {
141 *ptr = sycl::malloc_device(new_size, q);
142 }
143 void _deallocate(void* ptr, MemoryAllocationArgs, sycl::queue& q) override
144 {
145 sycl::free(ptr, q);
146 }
147};
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152namespace
153{
157} // namespace
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167namespace Arcane::Accelerator
168{
169
171getSyclMemoryAllocator()
172{
174}
175
177getSyclDeviceMemoryAllocator()
178{
179 return &device_sycl_memory_allocator;
180}
181
183getSyclUnifiedMemoryAllocator()
184{
185 return &unified_memory_sycl_memory_allocator;
186}
187
189getSyclHostPinnedMemoryAllocator()
190{
191 return &host_pinned_sycl_memory_allocator;
192}
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197void Sycl::
198setSyclMemoryQueue(const sycl::queue& memory_queue)
199{
200 global_default_queue = std::make_unique<sycl::queue>(memory_queue);
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206} // namespace Arcane::Accelerator
207
208/*---------------------------------------------------------------------------*/
209/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
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.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
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.