12#ifndef ARCCORE_COMMON_ACCELERATOR_INTERNAL_ACCELERATORMEMORYALLOCATORBASE_H
13#define ARCCORE_COMMON_ACCELERATOR_INTERNAL_ACCELERATORMEMORYALLOCATORBASE_H
17#include "arccore/common/accelerator/CommonAcceleratorGlobal.h"
19#include "arccore/base/String.h"
20#include "arccore/base/FatalErrorException.h"
22#include "arccore/common/AllocatedMemoryInfo.h"
23#include "arccore/common/AlignedMemoryAllocator.h"
24#include "arccore/common/internal/MemoryPool.h"
26#include "arccore/common/accelerator/internal/MemoryTracer.h"
53 void initialize(
Int64 block_size,
bool do_block_alloc)
61 void dumpStats(std::ostream& ostr,
const String& name);
67 return wanted_capacity;
76 Int64 orig_capacity = wanted_capacity;
77 Int64 new_size = orig_capacity * element_size;
79 Int64 nb_iter = 4 + (4096 / block_size);
80 for (
Int64 i = 0; i < nb_iter; ++i) {
81 if (new_size >= (4 * block_size))
86 new_size = _computeNextMultiple(new_size, block_size);
87 wanted_capacity = new_size / element_size;
88 if (wanted_capacity < orig_capacity)
89 wanted_capacity = orig_capacity;
90 return wanted_capacity;
93 void notifyDoAllocate(
void* ptr)
97 uintptr_t addr =
reinterpret_cast<uintptr_t
>(ptr);
120 Int64 new_n = n / multiple;
121 if ((n % multiple) != 0)
123 return (new_n * multiple);
132class ARCCORE_COMMON_EXPORT AcceleratorMemoryAllocatorBase
133:
public AlignedMemoryAllocator
138 using BaseClass = AlignedMemoryAllocator;
153 :
public IMemoryPoolAllocator
157 virtual void doMemoryCopy(
void* destination,
const void* source,
Int64 size) = 0;
174 void* out = m_sub_allocator->allocateMemory(new_size);
175 m_block_wrapper.notifyDoAllocate(out);
178 ARCCORE_FATAL(
"Bad alignment for Accelerator allocator: offset={0}", (a % 128));
179 m_tracer.traceAllocate(out, new_size, args);
180 _applyHint(out, new_size, args);
181 return { out, new_size };
186 void* ptr = mem_info.baseAddress();
187 size_t mem_size = mem_info.capacity();
188 if (m_use_memory_pool)
189 _removeHint(ptr, mem_size, args);
193 m_tracer.traceDeallocate(mem_info, args);
194 m_sub_allocator->freeMemory(ptr, mem_size);
200 return m_block_wrapper.adjustedCapacity(wanted_capacity, element_size);
206 m_direct_sub_allocator->doMemoryCopy(destination.baseAddress(), source.baseAddress(), source.size());
208 IMemoryPool* memoryPool() {
return &m_memory_pool; }
212 virtual void _applyHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
214 virtual void _removeHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
215 [[maybe_unused]] MemoryAllocationArgs args) {}
219 Impl::MemoryTracerWrapper m_tracer;
220 std::unique_ptr<IUnderlyingAllocator> m_direct_sub_allocator;
221 Arcane::Impl::MemoryPool m_memory_pool;
222 IMemoryPoolAllocator* m_sub_allocator =
nullptr;
223 bool m_use_memory_pool =
false;
224 String m_allocator_name;
225 std::atomic<Int32> m_nb_reallocate = 0;
226 std::atomic<Int64> m_reallocate_size = 0;
227 Int32 m_print_level = 0;
228 BlockAllocatorWrapper m_block_wrapper;
233 void _doInitializeUVM(
bool default_use_memory_pool =
false);
235 void _doInitializeHostPinned(
bool default_use_memory_pool =
false);
237 void _doInitializeDevice(
bool default_use_memory_pool =
false);
241 void _setTraceLevel(Int32 v) { m_tracer.setTraceLevel(v); }
246 void _setUseMemoryPool(
bool is_used);
#define ARCCORE_FATAL(...)
Macro envoyant une exception FatalErrorException.
MemoryPoolFlags
Liste des flags pour le pool mémoire à activer.
eMemoryResource memoryResource() const final
Ressource mémoire fournie par l'allocateur.
bool hasRealloc(MemoryAllocationArgs) const final
Indique si l'allocateur supporte la sémantique de realloc.
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.
void copyMemory(MemoryAllocationArgs args, AllocatedMemoryInfo destination, AllocatedMemoryInfo source) final
Copie la mémoire entre deux zones.
Int64 adjustedCapacity(MemoryAllocationArgs args, Int64 wanted_capacity, Int64 element_size) const final
Ajuste la capacité suivant la taille d'élément.
Classe commune pour gérer l'allocation par bloc.
std::atomic< Int32 > m_nb_allocate
Nombre d'allocations.
Int64 m_block_size
Taille d'un bloc. L'allocation sera un multiple de cette taille.
bool m_do_block_allocate
Indique si l'allocation en utilisant m_block_size.
std::atomic< Int32 > m_nb_unaligned_allocate
Nombre d'allocations non alignées.
Int64 adjustedCapacity(MemoryAllocationArgs args, Int64 wanted_capacity, Int64 element_size) const override
Ajuste la capacité suivant la taille d'élément.
Informations sur une zone mémoire allouée.
Interface d'un pool mémoire.
Interface du gestionnaire de traces.
Interface d'un allocateur pour un MemoryPool.
Classe contenant des informations pour spécialiser les allocations.
Chaîne de caractères unicode.
Espace de nom pour l'utilisation des accélérateurs.
std::int64_t Int64
Type entier signé sur 64 bits.
eMemoryResource
Liste des ressources mémoire disponibles.