12#ifndef ARCANE_ACCELERATOR_CORE_INTERNAL_ACCELERATORMEMORYALLOCATORBASE_H
13#define ARCANE_ACCELERATOR_CORE_INTERNAL_ACCELERATORMEMORYALLOCATORBASE_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"
25#include "arcane/utils/internal/MemoryPool.h"
27#include "arcane/accelerator/core/internal/MemoryTracer.h"
54 void initialize(
Int64 block_size,
bool do_block_alloc)
62 void dumpStats(std::ostream& ostr,
const String& name);
68 return wanted_capacity;
77 Int64 orig_capacity = wanted_capacity;
78 Int64 new_size = orig_capacity * element_size;
80 Int64 nb_iter = 4 + (4096 / block_size);
81 for (
Int64 i = 0; i < nb_iter; ++i) {
82 if (new_size >= (4 * block_size))
87 new_size = _computeNextMultiple(new_size, block_size);
88 wanted_capacity = new_size / element_size;
89 if (wanted_capacity < orig_capacity)
90 wanted_capacity = orig_capacity;
91 return wanted_capacity;
94 void notifyDoAllocate(
void* ptr)
98 uintptr_t addr =
reinterpret_cast<uintptr_t
>(ptr);
121 Int64 new_n = n / multiple;
122 if ((n % multiple) != 0)
124 return (new_n * multiple);
133class ARCANE_ACCELERATOR_CORE_EXPORT AcceleratorMemoryAllocatorBase
134:
public AlignedMemoryAllocator
139 using BaseClass = AlignedMemoryAllocator;
154 :
public IMemoryPoolAllocator
158 virtual void doMemoryCopy(
void* destination,
const void* source,
Int64 size) = 0;
175 void* out = m_sub_allocator->allocateMemory(new_size);
176 m_block_wrapper.notifyDoAllocate(out);
179 ARCANE_FATAL(
"Bad alignment for Accelerator allocator: offset={0}", (a % 128));
180 m_tracer.traceAllocate(out, new_size, args);
181 _applyHint(out, new_size, args);
182 return { out, new_size };
187 void* ptr = mem_info.baseAddress();
188 size_t mem_size = mem_info.capacity();
189 if (m_use_memory_pool)
190 _removeHint(ptr, mem_size, args);
194 m_tracer.traceDeallocate(mem_info, args);
195 m_sub_allocator->freeMemory(ptr, mem_size);
201 return m_block_wrapper.adjustedCapacity(wanted_capacity, element_size);
207 virtual void _applyHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
209 virtual void _removeHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
214 impl::MemoryTracerWrapper m_tracer;
215 std::unique_ptr<IUnderlyingAllocator> m_direct_sub_allocator;
217 IMemoryPoolAllocator* m_sub_allocator =
nullptr;
218 bool m_use_memory_pool =
false;
219 String m_allocator_name;
220 std::atomic<Int32> m_nb_reallocate = 0;
221 std::atomic<Int64> m_reallocate_size = 0;
222 Int32 m_print_level = 0;
223 BlockAllocatorWrapper m_block_wrapper;
228 void _doInitializeUVM();
230 void _doInitializeHostPinned();
232 void _doInitializeDevice();
236 void _setTraceLevel(Int32 v) { m_tracer.setTraceLevel(v); }
241 void _setUseMemoryPool(
bool is_used);
#define ARCANE_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.
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 du gestionnaire de traces.
Classe contenant des informations pour spécialiser les allocations.
Chaîne de caractères unicode.
Interface d'un allocateur pour un MemoryPool.
Classe pour gérer une liste de zone allouées.
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.