Arcane  v4.1.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
SpecificMemoryCopyList.h
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/* SpecificMemoryCopyList.h (C) 2000-2025 */
9/* */
10/* Classe template pour gérer des fonctions spécialisées de copie mémoire. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_COMMON_INTERNAL_SPECIFICMEMORYCOPYLIST_H
13#define ARCCORE_COMMON_INTERNAL_SPECIFICMEMORYCOPYLIST_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/String.h"
18#include "arccore/base/ArrayExtentsValue.h"
19#include "arccore/base/FatalErrorException.h"
20#include "arccore/common/CommonGlobal.h"
21
22#include <atomic>
23#include <vector>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane::impl
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
36class ARCCORE_COMMON_EXPORT IndexedMemoryCopyArgs
37{
38 public:
39
40 using RunQueue = Arcane::Accelerator::RunQueue;
41
42 public:
43
44 IndexedMemoryCopyArgs(SmallSpan<const Int32> indexes, Span<const std::byte> source,
45 Span<std::byte> destination, const RunQueue* run_queue)
46 : m_indexes(indexes)
47 , m_source(source)
48 , m_destination(destination)
49 , m_queue(run_queue)
50 {}
51
52 public:
53
54 SmallSpan<const Int32> m_indexes;
55 Span<const std::byte> m_source;
56 Span<std::byte> m_destination;
57 const RunQueue* m_queue = nullptr;
58};
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
66class ARCCORE_COMMON_EXPORT IndexedMultiMemoryCopyArgs
67{
68 public:
69
72 SmallSpan<const Span<const std::byte>> multi_memory,
73 Span<std::byte> destination,
74 RunQueue* run_queue)
75 : m_indexes(indexes)
76 , m_const_multi_memory(multi_memory)
77 , m_destination_buffer(destination)
78 , m_queue(run_queue)
79 {}
80
83 SmallSpan<Span<std::byte>> multi_memory,
85 RunQueue* run_queue)
86 : m_indexes(indexes)
87 , m_multi_memory(multi_memory)
88 , m_source_buffer(source)
89 , m_queue(run_queue)
90 {}
91
92 public:
93
94 SmallSpan<const Int32> m_indexes;
95 SmallSpan<const Span<const std::byte>> m_const_multi_memory;
96 SmallSpan<Span<std::byte>> m_multi_memory;
97 Span<const std::byte> m_source_buffer;
98 Span<std::byte> m_destination_buffer;
99 RunQueue* m_queue = nullptr;
100};
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
107class ARCCORE_COMMON_EXPORT ISpecificMemoryCopy
108{
109 public:
110
111 virtual ~ISpecificMemoryCopy() = default;
112
113 public:
114
115 virtual void copyFrom(const IndexedMemoryCopyArgs& args) = 0;
116 virtual void copyTo(const IndexedMemoryCopyArgs& args) = 0;
117 virtual void fill(const IndexedMemoryCopyArgs& args) = 0;
118 virtual void copyFrom(const IndexedMultiMemoryCopyArgs&) = 0;
119 virtual void copyTo(const IndexedMultiMemoryCopyArgs&) = 0;
120 virtual void fill(const IndexedMultiMemoryCopyArgs& args) = 0;
121 virtual Int32 datatypeSize() const = 0;
122};
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
129class ARCCORE_COMMON_EXPORT ISpecificMemoryCopyList
130{
131 public:
132
133 virtual void copyTo(Int32 datatype_size, const IndexedMemoryCopyArgs& args) = 0;
134 virtual void copyFrom(Int32 datatype_size, const IndexedMemoryCopyArgs& args) = 0;
135 virtual void fill(Int32 datatype_size, const IndexedMemoryCopyArgs& args) = 0;
136 virtual void copyTo(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) = 0;
137 virtual void copyFrom(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) = 0;
138 virtual void fill(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) = 0;
139};
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
152template <typename Traits>
153class SpecificMemoryCopyList
155{
156 public:
157
158 using InterfaceType = typename Traits::InterfaceType;
159 template <typename DataType, typename Extent> using SpecificType = typename Traits::template SpecificType<DataType, Extent>;
160 using RefType = typename Traits::RefType;
161
162 public:
163
164 static constexpr Int32 NB_COPIER = 128;
165
166 public:
167
168 SpecificMemoryCopyList()
169 {
170 m_copier.fill(nullptr);
171 }
172
173 ~SpecificMemoryCopyList()
174 {
176 delete copier;
177 }
178
179 public:
180
182 template <typename CopierType>
184 {
185 auto* copier = new CopierType();
186 m_copier[copier->datatypeSize()] = copier;
187 m_dynamic_copier_list.push_back(copier);
188 }
189
190 public:
191
192 void printStats()
193 {
194 std::cout << "SpecificMemory::nb_specialized=" << m_nb_specialized
195 << " nb_generic=" << m_nb_generic << "\n";
196 }
197
198 void checkValid()
199 {
200 // Vérifie que les taille sont correctes
201 for (Int32 i = 0; i < NB_COPIER; ++i) {
202 auto* x = m_copier[i];
203 if (x && (x->datatypeSize() != i))
204 ARCCORE_FATAL("Incoherent datatype size v={0} expected={1}", x->datatypeSize(), i);
205 }
206 }
207
208 private:
209
210 RefType _copier(Int32 v)
211 {
212 if (v < 0)
213 ARCCORE_FATAL("Bad value {0} for datasize", v);
214
215 InterfaceType* x = nullptr;
216 if (v < NB_COPIER)
217 x = m_copier[v];
218 if (x) {
219 if (x->datatypeSize() != v)
220 ARCCORE_FATAL("Incoherent datatype size v={0} expected={1}", x->datatypeSize(), v);
221 ++m_nb_specialized;
222 }
223 else
224 ++m_nb_generic;
225 return RefType(x, v);
226 }
227
228 public:
229
230 void copyTo(Int32 datatype_size, const IndexedMemoryCopyArgs& args) override
231 {
232 auto c = _copier(datatype_size);
233 c.copyTo(args);
234 }
235 void copyTo(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) override
236 {
237 auto c = _copier(datatype_size);
238 c.copyTo(args);
239 }
240 void copyFrom(Int32 datatype_size, const IndexedMemoryCopyArgs& args) override
241 {
242 auto c = _copier(datatype_size);
243 c.copyFrom(args);
244 }
245 void copyFrom(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) override
246 {
247 auto c = _copier(datatype_size);
248 c.copyFrom(args);
249 }
250 void fill(Int32 datatype_size, const IndexedMemoryCopyArgs& args) override
251 {
252 auto c = _copier(datatype_size);
253 c.fill(args);
254 }
255 void fill(Int32 datatype_size, const IndexedMultiMemoryCopyArgs& args) override
256 {
257 auto c = _copier(datatype_size);
258 c.fill(args);
259 }
260
261 private:
262
263 std::array<InterfaceType*, NB_COPIER> m_copier;
264 std::atomic<Int32> m_nb_specialized = 0;
265 std::atomic<Int32> m_nb_generic = 0;
267 std::vector<ISpecificMemoryCopy*> m_dynamic_copier_list;
268};
269
270/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
272
273template <typename DataType, typename Extent>
275: public ISpecificMemoryCopy
276{
277 static Int32 typeSize() { return static_cast<Int32>(sizeof(DataType)); }
278
279 public:
280
281 Int32 datatypeSize() const override { return m_extent.v * typeSize(); }
282
283 public:
284
285 Extent m_extent;
286
287 protected:
288
289 static Span<const DataType> _toTrueType(Span<const std::byte> a)
290 {
291 return { reinterpret_cast<const DataType*>(a.data()), a.size() / typeSize() };
292 }
293 static Span<DataType> _toTrueType(Span<std::byte> a)
294 {
295 return { reinterpret_cast<DataType*>(a.data()), a.size() / typeSize() };
296 }
297};
298
299/*---------------------------------------------------------------------------*/
300/*---------------------------------------------------------------------------*/
307template <typename Traits>
308class SpecificMemoryCopyRef
309{
310 template <typename DataType, typename Extent> using SpecificType = typename Traits::template SpecificType<DataType, Extent>;
311
312 public:
313
314 SpecificMemoryCopyRef(ISpecificMemoryCopy* specialized_copier, Int32 datatype_size)
315 : m_specialized_copier(specialized_copier)
316 , m_used_copier(specialized_copier)
317 {
318 m_generic_copier.m_extent.v = datatype_size;
319 if (!m_used_copier)
320 m_used_copier = &m_generic_copier;
321 }
322
323 void copyFrom(const IndexedMemoryCopyArgs& args)
324 {
325 m_used_copier->copyFrom(args);
326 }
327
328 void copyTo(const IndexedMemoryCopyArgs& args)
329 {
330 m_used_copier->copyTo(args);
331 }
332
333 void fill(const IndexedMemoryCopyArgs& args)
334 {
335 m_used_copier->fill(args);
336 }
337
338 void copyFrom(const IndexedMultiMemoryCopyArgs& args)
339 {
340 m_used_copier->copyFrom(args);
341 }
342
343 void copyTo(const IndexedMultiMemoryCopyArgs& args)
344 {
345 m_used_copier->copyTo(args);
346 }
347
348 void fill(const IndexedMultiMemoryCopyArgs& args)
349 {
350 m_used_copier->fill(args);
351 }
352
353 private:
354
355 ISpecificMemoryCopy* m_specialized_copier = nullptr;
356 SpecificType<std::byte, ExtentValue<DynExtent>> m_generic_copier;
357 ISpecificMemoryCopy* m_used_copier = nullptr;
358};
359
360/*---------------------------------------------------------------------------*/
361/*---------------------------------------------------------------------------*/
370class ARCCORE_COMMON_EXPORT GlobalMemoryCopyList
371{
372 private:
373
374 static ISpecificMemoryCopyList* default_global_copy_list;
375 static ISpecificMemoryCopyList* accelerator_global_copy_list;
376
377 public:
378
380 static ISpecificMemoryCopyList* getDefault(const RunQueue* queue);
381
392 static ISpecificMemoryCopyList* acceleratorInstance()
393 {
394 return accelerator_global_copy_list;
395 }
396};
397
398/*---------------------------------------------------------------------------*/
399/*---------------------------------------------------------------------------*/
400
401} // namespace Arcane::impl
402
403/*---------------------------------------------------------------------------*/
404/*---------------------------------------------------------------------------*/
405
406#endif
#define ARCCORE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Vue d'un tableau d'éléments de type T.
Definition Span.h:801
Vue d'un tableau d'éléments de type T.
Definition Span.h:633
Classe singleton contenant l'instance à utiliser pour les copies.
static ISpecificMemoryCopyList * getDefault(const RunQueue *queue)
Retourne l'instance par défaut pour la file queue.
static void setAcceleratorInstance(ISpecificMemoryCopyList *ptr)
Positionne l'instance par défaut pour les copies lorsqu'un runtime accélérateur est activé
Interface d'une liste d'instances de ISpecificMemoryCopy spécialisées.
Interface d'un copieur mémoire spécialisé pour une taille de donnée.
Arguments pour une copie de certains indices entre deux zones mémoire.
Arguments pour une copie de certains indices vers/depuis une zone mémoire multiple.
IndexedMultiMemoryCopyArgs(SmallSpan< const Int32 > indexes, SmallSpan< Span< std::byte > > multi_memory, Span< const std::byte > source, RunQueue *run_queue)
Constructor pour copyFrom.
IndexedMultiMemoryCopyArgs(SmallSpan< const Int32 > indexes, SmallSpan< const Span< const std::byte > > multi_memory, Span< std::byte > destination, RunQueue *run_queue)
Constructeur pour copyTo.
std::vector< ISpecificMemoryCopy * > m_dynamic_copier_list
Liste des copieurs qu'il faudra supprimer via 'delete'.
void addCopier()
Ajoute un copieur spécifique.
std::int32_t Int32
Type entier signé sur 32 bits.