Arcane  v3.16.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/*---------------------------------------------------------------------------*/
48class SyclMemoryAllocatorBase
49: public Arccore::AlignedMemoryAllocator3
50{
51 public:
52
53 SyclMemoryAllocatorBase()
54 : AlignedMemoryAllocator3(128)
55 {}
56
57 bool hasRealloc(MemoryAllocationArgs) const override { return true; }
58 AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override
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 }
70 AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override
71 {
72 sycl::queue& q = _defaultQueue();
73 AllocatedMemoryInfo a = allocate(args, new_size);
74 q.submit([&](sycl::handler& cgh) {
75 cgh.memcpy(a.baseAddress(), current_ptr.baseAddress(), current_ptr.size());
76 });
77 q.wait();
78
79 deallocate(args, current_ptr);
80 return a;
81 }
82 void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override
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
98: public SyclMemoryAllocatorBase
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 }
111};
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
117: public SyclMemoryAllocatorBase
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 }
131};
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
137: public SyclMemoryAllocatorBase
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 }
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
179Arccore::IMemoryAllocator* Sycl::
180getSyclDeviceMemoryAllocator()
181{
182 return &device_sycl_memory_allocator;
183}
184
185Arccore::IMemoryAllocator* Sycl::
186getSyclUnifiedMemoryAllocator()
187{
188 return &unified_memory_sycl_memory_allocator;
189}
190
191Arccore::IMemoryAllocator* Sycl::
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.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Informations sur une zone mémoire allouée.
void * baseAddress() const
Adresse du début de la zone allouée.
Int64 size() const
Taille en octets de la zone mémoire utilisée. (-1) si inconnue.
Classe contenant des informations pour spécialiser les allocations.
Espace de nom pour l'utilisation des accélérateurs.
eMemoryResource
Liste des ressources mémoire disponibles.
@ HostPinned
Alloue sur l'hôte.
@ UnifiedMemory
Alloue en utilisant la mémoire unifiée.
@ Device
Alloue sur le device.