Arcane  v3.15.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MemoryResourceMng.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/* MemoryResourceMng.cc (C) 2000-2024 */
9/* */
10/* Gestion des ressources mémoire pour les CPU et accélérateurs. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/internal/MemoryResourceMng.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/MemoryView.h"
20#include "arcane/utils/MemoryAllocator.h"
22#include "arcane/utils/internal/MemoryUtilsInternal.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace
34{
35 inline bool _isHost(eMemoryRessource r)
36 {
37 // Si on sait pas, considère qu'on est accessible de puis l'hôte.
38 if (r == eMemoryResource::Unknown)
39 return true;
40 if (r == eMemoryResource::Host || r == eMemoryResource::UnifiedMemory || r == eMemoryResource::HostPinned)
41 return true;
42 return false;
43 }
44
45} // namespace
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
51: public IMemoryCopier
52{
53 public:
54
55 void copy(ConstMemoryView from, eMemoryResource from_mem,
56 MutableMemoryView to, eMemoryResource to_mem,
57 [[maybe_unused]] const RunQueue* queue) override
58 {
59 // Sans support accélérateur, on peut juste faire un 'memcpy' si la mémoire
60 // est accessible depuis le CPU
61
62 if (!_isHost(from_mem))
63 ARCANE_FATAL("Source buffer is not accessible from host and no copier provided (location={0})",
64 from_mem);
65
66 if (!_isHost(to_mem))
67 ARCANE_FATAL("Destination buffer is not accessible from host and no copier provided (location={0})",
68 to_mem);
69
70 to.copyHost(from);
71 }
72};
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77MemoryResourceMng::
78MemoryResourceMng()
79: m_default_memory_copier(new DefaultHostMemoryCopier())
80, m_copier(m_default_memory_copier.get())
81{
82 // Par défaut on utilise l'allocateur CPU. Les allocateurs spécifiques pour
83 // les accélérateurs seront positionnés lorsqu'on aura choisi le runtime
84 // accélérateur
86 setAllocator(eMemoryRessource::Host, a);
87}
88
89/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
91
92int MemoryResourceMng::
93_checkValidResource(eMemoryResource r)
94{
95 int x = (int)r;
96 if (x <= 0 || x >= Arccore::ARCCORE_NB_MEMORY_RESOURCE)
97 ARCANE_FATAL("Invalid value '{0}'. Valid range is '1' to '{1}'", x, Arccore::ARCCORE_NB_MEMORY_RESOURCE - 1);
98 return x;
99}
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
104IMemoryAllocator* MemoryResourceMng::
105getAllocator(eMemoryResource r, bool throw_if_not_found)
106{
107 int x = _checkValidResource(r);
108 IMemoryAllocator* a = m_allocators[x];
109
110 // Si pas d'allocateur spécifique et qu'on n'est pas sur accélérateur,
111 // utilise platform::getAcceleratorHostMemoryAllocator().
112 if (!a && !m_is_accelerator) {
113 if (r == eMemoryResource::UnifiedMemory || r == eMemoryResource::HostPinned) {
115 if (!a)
116 a = m_allocators[(int)eMemoryResource::Host];
117 }
118 }
119
120 if (!a && throw_if_not_found)
121 ARCANE_FATAL("Allocator for resource '{0}' is not available", r);
122
123 return a;
124}
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
129IMemoryAllocator* MemoryResourceMng::
130getAllocator(eMemoryResource r)
131{
132 return getAllocator(r, true);
133}
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138void MemoryResourceMng::
139setAllocator(eMemoryResource r, IMemoryAllocator* allocator)
140{
141 int x = _checkValidResource(r);
142 m_allocators[x] = allocator;
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148void MemoryResourceMng::
149copy(ConstMemoryView from, eMemoryResource from_mem,
150 MutableMemoryView to, eMemoryResource to_mem, const RunQueue* queue)
151{
152 Int64 from_size = from.bytes().size();
153 Int64 to_size = to.bytes().size();
154 if (from_size > to_size)
155 ARCANE_FATAL("Destination copy is too small (to_size={0} from_size={1})", to_size, from_size);
156
157 m_copier->copy(from, from_mem, to, to_mem, queue);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163void MemoryResourceMng::
164genericCopy(ConstMemoryView from, MutableMemoryView to)
165{
166 IMemoryResourceMng* mrm = MemoryUtils::getDataMemoryResourceMng();
167 eMemoryResource mem_type = eMemoryResource::Unknown;
168 mrm->_internal()->copy(from, mem_type, to, mem_type, nullptr);
169}
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
174} // End namespace Arcane
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Fonctions de gestion mémoire et des allocateurs.
File d'exécution pour un accélérateur.
static AlignedMemoryAllocator * Simd()
Allocateur garantissant l'alignement pour utiliser la vectorisation sur la plateforme cible.
IMemoryAllocator * getDefaultDataAllocator()
Allocateur par défaut pour les données.
IMemoryAllocator * getAllocator(eMemoryResource mem_resource)
Allocateur par défaut pour la ressource mem_resource.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Arccore::eMemoryResource eMemoryRessource
Typedef pour la version Arcane historique (avec 2's')
Definition UtilsTypes.h:269
eMemoryResource
Liste des ressources mémoire disponibles.