Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Arcane::MemoryUtils Namespace Reference

Namespace for memory and allocator management functions. More...

Functions

template<typename DataType>
Int32 checkResizeArrayWithCapacity (Array< DataType > &array, Int64 new_size, bool force_resize)
 Resizes an array by adding a memory reserve.
template<typename DataType>
Int32 checkResizeArrayWithCapacity (Array< DataType > &array, Int64 new_size)
 Resizes an array by adding a memory reserve.
IMemoryAllocatorgetAcceleratorHostMemoryAllocator ()
 Specific allocator for accelerators.
eMemoryResource getDefaultDataMemoryResource ()
 Memory resource used by the default allocator for data.
eMemoryResource getMemoryResourceFromName (const String &name)
 Returns the memory resource by its name.
IMemoryAllocatorgetDefaultDataAllocator ()
 Default allocator for data.
MemoryAllocationOptions getDefaultDataAllocator (eMemoryLocationHint hint)
 Default allocator for data with expected location information.
IMemoryAllocatorgetDeviceOrHostAllocator ()
 Returns the host or device allocator.
MemoryAllocationOptions getAllocatorForMostlyReadOnlyData ()
 Default allocator for mostly read-only data.
MemoryAllocationOptions getAllocationOptions (eMemoryResource mem_resource)
 Default allocation for the resource mem_resource.
IMemoryAllocatorgetAllocator (eMemoryResource mem_resource)
 Default allocator for the resource mem_resource.
IMemoryPoolgetMemoryPoolOrNull (eMemoryResource mem_resource)
 Memory pool for the resource mem_resource.
void copy (MutableMemoryView destination, eMemoryResource destination_mem, ConstMemoryView source, eMemoryResource source_mem, const RunQueue *queue=nullptr)
 Copies source to destination using the queue queue.
void copy (MutableMemoryView destination, ConstMemoryView source, const RunQueue *queue=nullptr)
 Copies source to destination using the queue queue.
template<typename DataType>
void copy (Span< DataType > destination, Span< const DataType > source, const RunQueue *queue=nullptr)
 Copies source to destination using the queue queue.
template<typename DataType>
void copy (SmallSpan< DataType > destination, SmallSpan< const DataType > source, const RunQueue *queue=nullptr)
 Copies source to destination using the queue queue.
void copyHostWithIndexedSource (MutableMemoryView destination, ConstMemoryView source, Span< const Int32 > indexes)
 Copies data on the host with indirection.
void copyWithIndexedSource (MutableMemoryView destination, ConstMemoryView source, SmallSpan< const Int32 > indexes, RunQueue *run_queue=nullptr)
 Copies data on the host with indirection.
void copyHost (MutableMemoryView destination, ConstMemoryView source)
 Copies the data from source into destination.
void copyHostWithIndexedDestination (MutableMemoryView destination, ConstMemoryView source, Span< const Int32 > indexes)
 Copies indexed data from v into the instance.
void copyWithIndexedDestination (MutableMemoryView destination, ConstMemoryView source, SmallSpan< const Int32 > indexes, RunQueue *run_queue=nullptr)
 Memory copy with indirection.
void fillIndexed (MutableMemoryView destination, ConstMemoryView source, SmallSpan< const Int32 > indexes, const RunQueue *run_queue=nullptr)
 Fills an indexed memory region with a value.
void fill (MutableMemoryView destination, ConstMemoryView source, const RunQueue *run_queue=nullptr)
 Fills a memory region with a value.
void copyWithIndexedSource (MutableMemoryView destination, ConstMultiMemoryView source, SmallSpan< const Int32 > indexes, RunQueue *run_queue=nullptr)
 Copies indexed data from source into destination.
void copyWithIndexedDestination (MutableMultiMemoryView destination, ConstMemoryView source, SmallSpan< const Int32 > indexes, RunQueue *run_queue=nullptr)
 Copies indexed elements of destination with data from source.
void fillIndexed (MutableMultiMemoryView destination, ConstMemoryView source, SmallSpan< const Int32 > indexes, RunQueue *run_queue=nullptr)
 Fills indexed elements of destination with data source.
void fill (MutableMultiMemoryView destination, ConstMemoryView source, RunQueue *run_queue=nullptr)
 Fills elements of destination with the value source.

Detailed Description

Namespace for memory and allocator management functions.

Function Documentation

◆ checkResizeArrayWithCapacity() [1/2]

template<typename DataType>
Int32 Arcane::MemoryUtils::checkResizeArrayWithCapacity ( Array< DataType > & array,
Int64 new_size )
inline

Resizes an array by adding a memory reserve.

This call is equivalent to checkResizeArrayWithCapacity(array, new_size, false).

Definition at line 75 of file arcane/src/arcane/utils/MemoryUtils.h.

References checkResizeArrayWithCapacity().

◆ checkResizeArrayWithCapacity() [2/2]

template<typename DataType>
Int32 Arcane::MemoryUtils::checkResizeArrayWithCapacity ( Array< DataType > & array,
Int64 new_size,
bool force_resize )
inline

Resizes an array by adding a memory reserve.

The array array is resized only if new_size is greater than the current size of the array or if force_resize is true.

If the array is resized, an additional capacity is reserved to prevent reallocating every time.

Return values
2if reallocation occurred via reserve()
1if resizing occurred without reallocation.
0if no operation took place.

Definition at line 52 of file arcane/src/arcane/utils/MemoryUtils.h.

References Arcane::AbstractArray< T >::capacity(), Arcane::AbstractArray< T >::largeSize(), Arcane::Array< T >::reserve(), and Arcane::Array< T >::resize().

Referenced by Arcane::ItemGroupImpl::addItems(), Arcane::MeshUtils::checkResizeArray(), and checkResizeArrayWithCapacity().

◆ copy() [1/4]

void Arcane::MemoryUtils::copy ( MutableMemoryView destination,
ConstMemoryView source,
const RunQueue * queue = nullptr )
inline

Copies source to destination using the queue queue.

Definition at line 188 of file arccore/src/common/arccore/common/MemoryUtils.h.

References copy(), and Arcane::Unknown.

◆ copy() [2/4]

void Arcane::MemoryUtils::copy ( MutableMemoryView destination,
eMemoryResource destination_mem,
ConstMemoryView source,
eMemoryResource source_mem,
const RunQueue * queue = nullptr )

Copies source to destination using the queue queue.

It is possible to specify the memory resource where the source and destination are located. If they are unknown, it is preferable to use the overload copy(MutableMemoryView destination, ConstMemoryView source, const RunQueue* queue).

Definition at line 229 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::IMemoryResourceMng::_internal().

Referenced by Arcane::ItemGroupImpl::addItems(), copy(), copy(), copy(), and Arcane::ItemVector::ItemVector().

◆ copy() [3/4]

template<typename DataType>
void Arcane::MemoryUtils::copy ( SmallSpan< DataType > destination,
SmallSpan< const DataType > source,
const RunQueue * queue = nullptr )
inline

Copies source to destination using the queue queue.

Definition at line 212 of file arccore/src/common/arccore/common/MemoryUtils.h.

References copy().

◆ copy() [4/4]

template<typename DataType>
void Arcane::MemoryUtils::copy ( Span< DataType > destination,
Span< const DataType > source,
const RunQueue * queue = nullptr )
inline

Copies source to destination using the queue queue.

Definition at line 199 of file arccore/src/common/arccore/common/MemoryUtils.h.

References Arcane::asBytes(), Arcane::asWritableBytes(), and copy().

◆ copyHost()

void Arcane::MemoryUtils::copyHost ( MutableMemoryView destination,
ConstMemoryView source )

Copies the data from source into destination.

Uses std::memmove for the copy.

Precondition
source.bytes.size() >= destination.bytes.size()

Definition at line 240 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References ARCCORE_CHECK_POINTER, ARCCORE_FATAL, Arcane::ConstMemoryView::bytes(), and Arcane::MutableMemoryView::bytes().

◆ copyHostWithIndexedDestination()

void Arcane::MemoryUtils::copyHostWithIndexedDestination ( MutableMemoryView destination,
ConstMemoryView source,
Span< const Int32 > indexes )

Copies indexed data from v into the instance.

The operation is equivalent to the following pseudo-code:

Int64 n = indexes.size();
for( Int64 i=0; i<n; ++i )
destination[indexes[i]] = source[i];
std::int64_t Int64
Signed integer type of 64 bits.
Precondition
destination.datatypeSize() == source.datatypeSize();
destination.nbElement() >= indexes.size();

Definition at line 262 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References copyWithIndexedDestination(), and Arcane::SpanImpl< T, SizeType, Extent >::smallView().

◆ copyHostWithIndexedSource()

void Arcane::MemoryUtils::copyHostWithIndexedSource ( MutableMemoryView destination,
ConstMemoryView source,
Span< const Int32 > indexes )

Copies data on the host with indirection.

Copies the data from source into destination indexed by indexes

The operation is equivalent to the following pseudo-code:

Int64 n = indexes.size();
for( Int64 i=0; i<n; ++i )
destination[i] = source[indexes[i]];
Precondition
destination.datatypeSize() == source.datatypeSize();
source.nbElement() >= indexes.size();

Definition at line 325 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References copyWithIndexedSource(), and Arcane::SpanImpl< T, SizeType, Extent >::smallView().

◆ copyWithIndexedDestination() [1/2]

void Arcane::MemoryUtils::copyWithIndexedDestination ( MutableMemoryView destination,
ConstMemoryView source,
SmallSpan< const Int32 > indexes,
RunQueue * run_queue = nullptr )

Memory copy with indirection.

Copies the data from source into destination for the indices specified by indexes.

The operation is equivalent to the following pseudo-code:

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i )
destination[indexes[i]] = source[i];
std::int32_t Int32
Signed integer type of 32 bits.

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();
destination.nbElement() >= indexes.size();

Definition at line 272 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::MutableMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMemoryView::datatypeSize(), and Arcane::SpanImpl< T, SizeType, Extent >::size().

Referenced by copyHostWithIndexedDestination().

◆ copyWithIndexedDestination() [2/2]

void Arcane::MemoryUtils::copyWithIndexedDestination ( MutableMultiMemoryView destination,
ConstMemoryView source,
SmallSpan< const Int32 > indexes,
RunQueue * run_queue = nullptr )

Copies indexed elements of destination with data from source.

The operation is equivalent to the following pseudo-code:

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i ){
Int32 index0 = indexes[ (i*2) ];
Int32 index1 = indexes[ (i*2)+1 ];
destination[index0][index1] = source[i];
}

The array indexes must have a size that is a multiple of 2. Even values are used to index the first array and odd values the second.

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == v.datatypeSize();
source.nbElement() >= indexes.size();

Definition at line 358 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMultiMemoryView::datatypeSize(), Arcane::SpanImpl< T, SizeType, Extent >::size(), and Arcane::MutableMultiMemoryView::views().

◆ copyWithIndexedSource() [1/2]

void Arcane::MemoryUtils::copyWithIndexedSource ( MutableMemoryView destination,
ConstMemoryView source,
SmallSpan< const Int32 > indexes,
RunQueue * run_queue = nullptr )

Copies data on the host with indirection.

Copies the data from source into destination indexed by indexes

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i )
destination[i] = source[indexes[i]];

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();
source.nbElement() >= indexes.size();

Definition at line 335 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::MutableMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMemoryView::datatypeSize(), and Arcane::SpanImpl< T, SizeType, Extent >::size().

Referenced by copyHostWithIndexedSource().

◆ copyWithIndexedSource() [2/2]

void Arcane::MemoryUtils::copyWithIndexedSource ( MutableMemoryView destination,
ConstMultiMemoryView source,
SmallSpan< const Int32 > indexes,
RunQueue * run_queue = nullptr )

Copies indexed data from source into destination.

The operation is equivalent to the following pseudo-code:

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i ){
Int32 index0 = indexes[ (i*2) ];
Int32 index1 = indexes[ (i*2)+1 ];
destination[i] = source[index0][index1];
}

The array indexes must have a size that is a multiple of 2. Even values are used to index the first array and odd values the second.

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();
destination.nbElement() >= indexes.size();

Definition at line 401 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::MutableMemoryView::bytes(), Arcane::ConstMultiMemoryView::datatypeSize(), Arcane::MutableMemoryView::datatypeSize(), Arcane::SpanImpl< T, SizeType, Extent >::size(), and Arcane::ConstMultiMemoryView::views().

◆ fill() [1/2]

void Arcane::MemoryUtils::fill ( MutableMemoryView destination,
ConstMemoryView source,
const RunQueue * run_queue = nullptr )

Fills a memory region with a value.

Fills the values of the memory region destination with the value of the memory region source. source must have a single value. The memory region source must be accessible from the host.

The operation is equivalent to the following pseudo-code:

Int32 n = nbElement();
for( Int32 i=0; i<n; ++i )
destination[i] = source[0];

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();

Definition at line 311 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::MutableMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMemoryView::datatypeSize(), and Arcane::SpanImpl< T, SizeType, Extent >::fill().

◆ fill() [2/2]

void Arcane::MemoryUtils::fill ( MutableMultiMemoryView destination,
ConstMemoryView source,
RunQueue * run_queue = nullptr )

Fills elements of destination with the value source.

source must have a single value. It must be accessible from the host.

The operation is equivalent to the following pseudo-code:

Int32 n = nbElement();
for( Int32 i=0; i<n; ++i ){
Int32 index0 = (i*2);
Int32 index1 = (i*2)+1;
destination[index0][index1] = source[0];
}

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();

Definition at line 390 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMultiMemoryView::datatypeSize(), and Arcane::MutableMultiMemoryView::views().

◆ fillIndexed() [1/2]

void Arcane::MemoryUtils::fillIndexed ( MutableMemoryView destination,
ConstMemoryView source,
SmallSpan< const Int32 > indexes,
const RunQueue * run_queue = nullptr )

Fills an indexed memory region with a value.

Fills the indices indexes of the memory region destination with the value of the memory region source. source must have a single value. The memory region source must be accessible from the host.

The operation is equivalent to the following pseudo-code:

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i )
destination[indexes[i]] = source[0];

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();
destination.nbElement() >= indexes.size();

Definition at line 292 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::MutableMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMemoryView::datatypeSize(), Arcane::SpanImpl< T, SizeType, Extent >::fill(), and Arcane::SpanImpl< T, SizeType, Extent >::size().

◆ fillIndexed() [2/2]

void Arcane::MemoryUtils::fillIndexed ( MutableMultiMemoryView destination,
ConstMemoryView source,
SmallSpan< const Int32 > indexes,
RunQueue * run_queue = nullptr )

Fills indexed elements of destination with data source.

source must have a single value. This value will be used to fill the values of the instance at the indices specified by indexes. It must be accessible from the host.

The operation is equivalent to the following pseudo-code:

Int32 n = indexes.size();
for( Int32 i=0; i<n; ++i ){
Int32 index0 = indexes[ (i*2) ];
Int32 index1 = indexes[ (i*2)+1 ];
destination[index0][index1] = source[0];
}

If run_queue is not null, it will be used for the copy.

Precondition
destination.datatypeSize() == source.datatypeSize();
destination.nbElement() >= indexes.size();

Definition at line 374 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::ConstMemoryView::bytes(), Arcane::ConstMemoryView::datatypeSize(), Arcane::MutableMultiMemoryView::datatypeSize(), Arcane::SpanImpl< T, SizeType, Extent >::size(), and Arcane::MutableMultiMemoryView::views().

◆ getAcceleratorHostMemoryAllocator()

IMemoryAllocator * Arcane::MemoryUtils::getAcceleratorHostMemoryAllocator ( )

◆ getAllocationOptions()

MemoryAllocationOptions Arcane::MemoryUtils::getAllocationOptions ( eMemoryResource mem_resource)

Default allocation for the resource mem_resource.

Throws an exception if no allocator is available for the resource (for example, if eMemoryResource::Device is requested and there is no support for accelerators.

The eMemoryResource::UnifiedMemory resource is always available. If no accelerator runtime is loaded, then it is equivalent to eMemoryResource::Host.

Definition at line 211 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References getAllocator().

◆ getAllocator()

IMemoryAllocator * Arcane::MemoryUtils::getAllocator ( eMemoryResource mem_resource)

Default allocator for the resource mem_resource.

Throws an exception if no allocator is available for the resource mem_resource.

See also
getAllocationOptions().

Definition at line 202 of file arccore/src/common/arccore/common/MemoryUtils.cc.

Referenced by getAllocationOptions().

◆ getAllocatorForMostlyReadOnlyData()

MemoryAllocationOptions Arcane::MemoryUtils::getAllocatorForMostlyReadOnlyData ( )

Default allocator for mostly read-only data.

This call is equivalent to getDefaultDataAllocator(eMemoryLocationHint::HostAndDeviceMostlyRead).

Definition at line 193 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References getDefaultDataAllocator(), and Arcane::HostAndDeviceMostlyRead.

◆ getDefaultDataAllocator() [1/2]

IMemoryAllocator * Arcane::MemoryUtils::getDefaultDataAllocator ( )

Default allocator for data.

The default allocator for data is an allocator that allows access to the memory region both by the host and the accelerator.

It is possible to retrieve the associated memory resource via getDefaultDataMemoryResource();

This call is equivalent to getAllocator(getDefaultDataMemoryResource()).

It is guaranteed that the alignment is at least that returned by AlignedMemoryAllocator::Simd().

Definition at line 162 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References getDefaultDataMemoryResource().

Referenced by getAllocatorForMostlyReadOnlyData(), getDefaultDataAllocator(), and Arcane::platform::getDefaultDataAllocator().

◆ getDefaultDataAllocator() [2/2]

MemoryAllocationOptions Arcane::MemoryUtils::getDefaultDataAllocator ( eMemoryLocationHint hint)

Default allocator for data with expected location information.

This function returns the allocator of getDefaulDataAllocator() but adds the memory management information specified by hint.

Definition at line 184 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References getDefaultDataAllocator().

◆ getDefaultDataMemoryResource()

eMemoryResource Arcane::MemoryUtils::getDefaultDataMemoryResource ( )

Memory resource used by the default allocator for data.

By default, if an accelerator runtime is initialized, the associated resource is eMemoryResource::UnifiedMemory. Otherwise, it is eMemoryResource::Host.

See also
getDefaultDataAllocator();

Definition at line 76 of file arccore/src/common/arccore/common/MemoryUtils.cc.

Referenced by getDefaultDataAllocator().

◆ getDeviceOrHostAllocator()

IMemoryAllocator * Arcane::MemoryUtils::getDeviceOrHostAllocator ( )

Returns the host or device allocator.

If an accelerator runtime is initialized, the returned allocator allows allocation using the default accelerator memory (eMemoryResource::Device). Otherwise, it uses the host allocator (eMemoryResource::Host).

Definition at line 171 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References Arcane::Device, Arcane::IMemoryResourceMng::getAllocator(), and Arcane::Host.

◆ getMemoryPoolOrNull()

IMemoryPool * Arcane::MemoryUtils::getMemoryPoolOrNull ( eMemoryResource mem_resource)

Memory pool for the resource mem_resource.

Returns nullptr if no memory pool is available for the resource mem_resource.

Definition at line 220 of file arccore/src/common/arccore/common/MemoryUtils.cc.

◆ getMemoryResourceFromName()

eMemoryResource Arcane::MemoryUtils::getMemoryResourceFromName ( const String & name)

Returns the memory resource by its name.

The name corresponds to the name of the enumeration value (e.g., 'Device' for eMemoryResource::Device.

If name is null, returns eMemoryResource::Unknown. If name does not correspond to a valid value, throws an exception.

Definition at line 85 of file arccore/src/common/arccore/common/MemoryUtils.cc.

References ARCCORE_FATAL, Arcane::Device, Arcane::Host, Arcane::HostPinned, Arcane::String::null(), Arcane::UnifiedMemory, and Arcane::Unknown.