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 virtual void _applyHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
208 virtual void _removeHint([[maybe_unused]]
void* ptr, [[maybe_unused]]
size_t new_size,
213 impl::MemoryTracerWrapper m_tracer;
214 std::unique_ptr<IUnderlyingAllocator> m_direct_sub_allocator;
216 IMemoryPoolAllocator* m_sub_allocator =
nullptr;
217 bool m_use_memory_pool =
false;
218 String m_allocator_name;
219 std::atomic<Int32> m_nb_reallocate = 0;
220 std::atomic<Int64> m_reallocate_size = 0;
221 Int32 m_print_level = 0;
222 BlockAllocatorWrapper m_block_wrapper;
227 void _doInitializeUVM(
bool default_use_memory_pool =
false);
229 void _doInitializeHostPinned(
bool default_use_memory_pool =
false);
231 void _doInitializeDevice(
bool default_use_memory_pool =
false);
235 void _setTraceLevel(Int32 v) { m_tracer.setTraceLevel(v); }
240 void _setUseMemoryPool(
bool is_used);
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.