Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MemoryUtils.cc
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/* MemoryUtils.cc (C) 2000-2025 */
9/* */
10/* Fonctions utilitaires de gestion mémoire. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/MemoryAllocator.h"
18#include "arcane/utils/IMemoryRessourceMng.h"
19#include "arcane/utils/String.h"
20#include "arccore/common/internal/IMemoryResourceMngInternal.h"
21#include "arcane/utils/internal/MemoryUtilsInternal.h"
22#include "arcane/utils/internal/MemoryResourceMng.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26/*!
27 * \file MemoryUtils.h
28 * \brief Fonctions de gestion mémoire et des allocateurs.
29 */
30/*!
31 * \namespace Arcane::MemoryUtils
32 * \brief Espace de noms pour les fonctions de gestion mémoire et des allocateurs.
33 */
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace
44{
45 IMemoryAllocator* global_accelerator_host_memory_allocator = nullptr;
46 MemoryResourceMng global_default_data_memory_resource_mng;
47 IMemoryRessourceMng* global_data_memory_resource_mng = nullptr;
48 eMemoryResource global_data_memory_resource = eMemoryResource::Host;
49} // namespace
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54eMemoryResource MemoryUtils::
55getDefaultDataMemoryResource()
56{
57 return global_data_memory_resource;
58}
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
63void MemoryUtils::
64setDefaultDataMemoryResource(eMemoryResource v)
65{
66 global_data_memory_resource = v;
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
72eMemoryResource MemoryUtils::
73getMemoryResourceFromName(const String& name)
74{
75 eMemoryResource v = eMemoryResource::Unknown;
76 if (name.null())
77 return v;
78 if (name == "Device")
79 v = eMemoryResource::Device;
80 else if (name == "Host")
81 v = eMemoryResource::Host;
82 else if (name == "HostPinned")
83 v = eMemoryResource::HostPinned;
84 else if (name == "UnifiedMemory")
85 v = eMemoryResource::UnifiedMemory;
86 else
87 ARCANE_FATAL("Invalid name '{0}' for memory resource. Valid names are "
88 "'Device', 'Host', 'HostPinned' or 'UnifieMemory'.",
89 name);
90 return v;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96IMemoryRessourceMng* MemoryUtils::
97setDataMemoryResourceMng(IMemoryRessourceMng* mng)
98{
100 IMemoryRessourceMng* old = global_data_memory_resource_mng;
101 global_data_memory_resource_mng = mng;
102 return old;
103}
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108IMemoryRessourceMng* MemoryUtils::
109getDataMemoryResourceMng()
110{
111 IMemoryRessourceMng* a = global_data_memory_resource_mng;
112 if (!a)
113 return &global_default_data_memory_resource_mng;
114 return a;
115}
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120IMemoryAllocator* MemoryUtils::
121getDefaultDataAllocator()
122{
123 return getDataMemoryResourceMng()->getAllocator(getDefaultDataMemoryResource());
124}
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
129IMemoryAllocator* MemoryUtils::
130getDeviceOrHostAllocator()
131{
132 IMemoryRessourceMng* mrm = getDataMemoryResourceMng();
133 IMemoryAllocator* a = mrm->getAllocator(eMemoryResource::Device, false);
134 if (a)
135 return a;
136 return mrm->getAllocator(eMemoryResource::Host);
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142MemoryAllocationOptions MemoryUtils::
143getDefaultDataAllocator(eMemoryLocationHint hint)
144{
145 return MemoryAllocationOptions(getDefaultDataAllocator(), hint);
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151IMemoryAllocator* MemoryUtils::
152getAllocator(eMemoryRessource mem_resource)
153{
154 return getDataMemoryResourceMng()->getAllocator(mem_resource);
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160MemoryAllocationOptions MemoryUtils::
161getAllocationOptions(eMemoryRessource mem_resource)
162{
163 return MemoryAllocationOptions(getAllocator(mem_resource));
164}
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169MemoryAllocationOptions MemoryUtils::
170getAllocatorForMostlyReadOnlyData()
171{
172 return getDefaultDataAllocator(eMemoryLocationHint::HostAndDeviceMostlyRead);
173}
174
175/*---------------------------------------------------------------------------*/
176/*---------------------------------------------------------------------------*/
177
178IMemoryAllocator* MemoryUtils::
180{
181 return global_accelerator_host_memory_allocator;
182}
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187IMemoryAllocator* MemoryUtils::
188setAcceleratorHostMemoryAllocator(IMemoryAllocator* a)
189{
190 IMemoryAllocator* old = global_accelerator_host_memory_allocator;
191 global_accelerator_host_memory_allocator = a;
192 return old;
193}
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
197
198Int64 MemoryUtils::impl::
199computeCapacity(Int64 size)
200{
201 double d_size = static_cast<double>(size);
202 double d_new_capacity = d_size * 1.8;
203 if (size > 5000000)
204 d_new_capacity = d_size * 1.2;
205 else if (size > 500000)
206 d_new_capacity = d_size * 1.5;
207 return static_cast<Int64>(d_new_capacity);
208}
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213void MemoryUtils::
214copy(MutableMemoryView destination, eMemoryRessource destination_mem,
215 ConstMemoryView source, eMemoryRessource source_mem, const RunQueue* queue)
216{
217 IMemoryRessourceMng* mrm = getDataMemoryResourceMng();
218 mrm->_internal()->copy(source, destination_mem, destination, source_mem, queue);
219}
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
224} // End namespace Arcane
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
#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.
Vue constante sur une zone mémoire contigue contenant des éléments de taille fixe.
Vue modifiable sur une zone mémoire contigue contenant des éléments de taille fixe.
Chaîne de caractères unicode.
bool null() const
Retourne true si la chaîne est nulle.
Definition String.cc:305
IMemoryAllocator * getAcceleratorHostMemoryAllocator()
Allocateur spécifique pour les accélérateurs.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int64_t Int64
Type entier signé sur 64 bits.
Arcane::eMemoryResource eMemoryRessource
Typedef pour la version Arcane historique (avec 2's')