Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MemoryResourceMng.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MemoryResourceMng.cc (C) 2000-2025 */
9/* */
10/* Memory resource management for CPUs and accelerators. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/internal/MemoryResourceMng.h"
15
16#include "arccore/base/FatalErrorException.h"
17#include "arccore/base/PlatformUtils.h"
18#include "arccore/base/MemoryView.h"
19#include "arccore/common/Array.h"
20#include "arccore/common/AlignedMemoryAllocator.h"
22#include "arccore/common/internal/MemoryUtilsInternal.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace
33{
34 bool _isHost(eMemoryResource r)
35 {
36 // If unknown, assume it is accessible from the host.
38 return true;
40 return true;
41 return false;
42 }
43
44} // namespace
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
50: public IMemoryCopier
51{
52 public:
53
56 [[maybe_unused]] const RunQueue* queue) override
57 {
58 // Without accelerator support, we can just perform a 'memcpy' if the memory
59 // is accessible from the CPU
60
61 if (!_isHost(from_mem))
62 ARCCORE_FATAL("Source buffer is not accessible from host and no copier provided (location={0})",
63 from_mem);
64
65 if (!_isHost(to_mem))
66 ARCCORE_FATAL("Destination buffer is not accessible from host and no copier provided (location={0})",
67 to_mem);
68
69 MemoryUtils::copyHost(to, from);
70 }
71};
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76MemoryResourceMng::
77MemoryResourceMng()
78: m_default_memory_copier(new DefaultHostMemoryCopier())
79, m_copier(m_default_memory_copier.get())
80{
81 // By default, we use the CPU allocator. Specific allocators for
82 // accelerators will be set when the accelerator runtime is chosen
83 IMemoryAllocator* a = AlignedMemoryAllocator::Simd();
84 setAllocator(eMemoryResource::Host, a);
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90int MemoryResourceMng::
91_checkValidResource(eMemoryResource r)
92{
93 int x = (int)r;
94 if (x <= 0 || x >= ARCCORE_NB_MEMORY_RESOURCE)
95 ARCCORE_FATAL("Invalid value '{0}'. Valid range is '1' to '{1}'", x, ARCCORE_NB_MEMORY_RESOURCE - 1);
96 return x;
97}
98
99/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
101
103getAllocator(eMemoryResource r, bool throw_if_not_found)
104{
105 int x = _checkValidResource(r);
107
108 // If no specific allocator is found and we are not on an accelerator,
109 // use Platform::getAcceleratorHostMemoryAllocator().
110 if (!a && !m_is_accelerator) {
113 a = m_allocators[static_cast<int>(mem)];
114 if (!a)
115 a = m_allocators[static_cast<int>(eMemoryResource::Host)];
116 }
117 }
118
119 if (!a && throw_if_not_found)
120 ARCCORE_FATAL("Allocator for resource '{0}' is not available", r);
121
122 return a;
123}
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
139{
140 int x = _checkValidResource(r);
141 return m_memory_pools[x];
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
149{
150 int x = _checkValidResource(r);
151 m_allocators[x] = allocator;
152}
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
159{
160 int x = _checkValidResource(r);
161 m_memory_pools[x] = pool;
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167void MemoryResourceMng::
168copy(ConstMemoryView from, eMemoryResource from_mem,
169 MutableMemoryView to, eMemoryResource to_mem, const RunQueue* queue)
170{
171 Int64 from_size = from.bytes().size();
172 Int64 to_size = to.bytes().size();
173 if (from_size > to_size)
174 ARCCORE_FATAL("Destination copy is too small (to_size={0} from_size={1})", to_size, from_size);
175
176 m_copier->copy(from, from_mem, to, to_mem, queue);
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
184{
187 mrm->_internal()->copy(from, mem_type, to, mem_type, nullptr);
188}
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
193} // namespace Arcane
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Memory management utility functions.
static AlignedMemoryAllocator * Simd()
Allocator guaranteeing alignment to use vectorization on the target platform.
Constant view on a contiguous memory region containing fixed-size elements.
constexpr SpanType bytes() const
View in byte form.
void copy(ConstMemoryView from, eMemoryResource from_mem, MutableMemoryView to, eMemoryResource to_mem, const RunQueue *queue) override
Copies the data from from to to with the queue queue.
Interface for memory copies with accelerator support.
virtual void copy(ConstMemoryView from, eMemoryResource from_mem, MutableMemoryView to, eMemoryResource to_mem, const RunQueue *queue)=0
Copies the data from from to to with the queue queue.
Interface of a memory pool.
Definition IMemoryPool.h:32
Memory resource management for CPUs and accelerators.
virtual IMemoryResourceMngInternal * _internal()=0
Internal interface.
FixedArray< IMemoryPool *, ARCCORE_NB_MEMORY_RESOURCE > m_memory_pools
List of memory pools.
void setAllocator(eMemoryResource r, IMemoryAllocator *allocator) override
Sets the allocator for resource r.
static void genericCopy(ConstMemoryView from, MutableMemoryView to)
Generic copy using platform::getDataMemoryRessourceMng().
FixedArray< IMemoryAllocator *, ARCCORE_NB_MEMORY_RESOURCE > m_allocators
List of allocators.
IMemoryPool * getMemoryPoolOrNull(eMemoryResource r) override
Memory pool for resource r.
IMemoryAllocator * getAllocator(eMemoryResource r) override
Memory allocator for resource r.
void setMemoryPool(eMemoryResource r, IMemoryPool *pool) override
Sets the memory pool for resource r.
Mutable view on a contiguous memory region containing fixed-size elements.
constexpr SpanType bytes() const
View in byte form.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
IMemoryRessourceMng * getDataMemoryResourceMng()
Memory resource manager for data.
void copyHost(MutableMemoryView destination, ConstMemoryView source)
Copies the data from source into destination.
eMemoryResource getDefaultDataMemoryResource()
Memory resource used by the default allocator for data.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
eMemoryResource
List of available memory resources.
@ HostPinned
Allocates on the host.
@ Unknown
Unknown or uninitialized value.
@ Host
Allocates on the host.
@ UnifiedMemory
Allocates using unified memory.