14#include "arcane/accelerator/core/internal/AcceleratorMemoryAllocatorBase.h"
16#include "arccore/base/Convert.h"
17#include "arccore/trace/ITraceMng.h"
19#include "arcane/utils/PlatformUtils.h"
33void BlockAllocatorWrapper::
34dumpStats(std::ostream& ostr,
const String& name)
36 ostr <<
"Allocator '" << name <<
"' : nb_allocate=" <<
m_nb_allocate
47AcceleratorMemoryAllocatorBase::
48AcceleratorMemoryAllocatorBase(
const String& allocator_name, IUnderlyingAllocator* underlying_allocator)
49: AlignedMemoryAllocator3(128)
50, m_direct_sub_allocator(underlying_allocator)
51, m_memory_pool(m_direct_sub_allocator.get(), allocator_name)
52, m_sub_allocator(m_direct_sub_allocator.get())
53, m_allocator_name(allocator_name)
55 if (
auto v = Convert::Type<Int32>::tryParseFromEnvironment(
"ARCANE_ACCELERATOR_MEMORY_PRINT_LEVEL",
true))
56 m_print_level = v.value();
62void AcceleratorMemoryAllocatorBase::
63finalize(ITraceMng* tm)
65 if (m_print_level >= 1) {
66 std::ostringstream ostr;
67 if (m_use_memory_pool) {
68 m_memory_pool.dumpStats(ostr);
69 m_memory_pool.dumpFreeMap(ostr);
71 ostr <<
"Allocator '" << m_allocator_name <<
"' nb_realloc=" << m_nb_reallocate
72 <<
" realloc_copy=" << m_reallocate_size <<
"\n";
73 m_block_wrapper.dumpStats(ostr, m_allocator_name);
75 tm->info() << ostr.str();
77 std::cout << ostr.str();
80 m_memory_pool.freeCachedMemory();
91 m_reallocate_size += current_size;
92 String array_name = args.arrayName();
93 const bool do_print = (m_print_level >= 2);
95 std::cout <<
"Reallocate allocator=" << m_allocator_name
96 <<
" current_size=" << current_size
97 <<
" current_capacity=" << current_info.
capacity()
98 <<
" new_capacity=" << new_size
100 if (array_name.
null() && m_print_level >= 3) {
104 std::cout <<
" name=" << array_name;
105 if (m_print_level >= 4)
110 if (m_use_memory_pool)
111 _removeHint(current_info.
baseAddress(), current_size, args);
114 m_direct_sub_allocator->doMemoryCopy(a.baseAddress(), current_info.
baseAddress(), current_size);
120void AcceleratorMemoryAllocatorBase::
123 bool do_page_allocate =
true;
125 do_page_allocate = (v.value() != 0);
127 m_block_wrapper.initialize(page_size, do_page_allocate);
129 bool use_memory_pool =
false;
131 use_memory_pool = (v.value() &
static_cast<int>(MemoryPoolFlags::UVM)) != 0;
132 _setUseMemoryPool(use_memory_pool);
136void AcceleratorMemoryAllocatorBase::
137_doInitializeHostPinned()
139 bool use_memory_pool =
false;
141 use_memory_pool = (v.value() &
static_cast<int>(MemoryPoolFlags::HostPinned)) != 0;
142 _setUseMemoryPool(use_memory_pool);
143 m_block_wrapper.initialize(128, use_memory_pool);
147void AcceleratorMemoryAllocatorBase::
150 bool use_memory_pool =
false;
152 use_memory_pool = (v.value() &
static_cast<int>(MemoryPoolFlags::Device)) != 0;
153 _setUseMemoryPool(use_memory_pool);
154 m_block_wrapper.initialize(128, use_memory_pool);
158void AcceleratorMemoryAllocatorBase::
159_setUseMemoryPool(
bool is_used)
163 m_sub_allocator = (is_used) ? mem_pool : direct;
164 m_use_memory_pool = is_used;
168 ARCANE_FATAL(
"Invalid value '{0}' for memory pool max block size");
169 size_t block_size =
static_cast<size_t>(v.value());
170 m_memory_pool.setMaxCachedBlockSize(block_size);
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) final
Alloue de la mémoire pour new_size octets et retourne le pointeur.
void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo mem_info) final
Libère la mémoire dont l'adresse de base est ptr.
std::atomic< Int32 > m_nb_allocate
Nombre d'allocations.
std::atomic< Int32 > m_nb_unaligned_allocate
Nombre d'allocations non alignées.
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.
Int64 capacity() const
Taille en octets de la zone mémoire allouée. (-1) si inconnue.
Classe template pour convertir un type.
Classe contenant des informations pour spécialiser les allocations.
Chaîne de caractères unicode.
bool null() const
Retourne true si la chaîne est nulle.
Interface d'un allocateur pour un MemoryPool.
Espace de nom pour l'utilisation des accélérateurs.
std::int64_t Int64
Type entier signé sur 64 bits.