Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MachineShMemWinMemoryAllocator.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/* MachineShMemWinMemoryAllocator.h (C) 2000-2026 */
9/* */
10/* Memory allocator using the MachineShMemWinBase class. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/internal/MachineShMemWinMemoryAllocator.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/ITraceMng.h"
18
19#include "arcane/core/IParallelMng.h"
20#include "arcane/core/MachineShMemWinBase.h"
21
22#include "arccore/common/AllocatedMemoryInfo.h"
23
24#include <cstring>
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35MachineShMemWinMemoryAllocator::
36MachineShMemWinMemoryAllocator(IParallelMng* pm)
37: m_pm(pm)
38{}
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43AllocatedMemoryInfo MachineShMemWinMemoryAllocator::
44allocate(MemoryAllocationArgs, Int64 new_size)
45{
46 // If the size is zero, since we have a collective creation, we must
47 // check if the size is zero for everyone.
48 if (m_pm->reduce(MessagePassing::ReduceMax, new_size) <= 0) {
49 return { nullptr, 0 };
50 }
51
52 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
53 const Int64 new_size_with_offset = offset + new_size;
54
55#ifdef ARCANE_DEBUG_ALLOCATOR
56 m_pm->traceMng()->debug() << "(1/2) MachineShMemWinMemoryAllocator::allocate"
57 << " -- ptr.size() : " << new_size
58 << " -- offset : " << offset
59 << " -- win_size (offset+ptr.size()) : " << new_size_with_offset;
60#endif
61
62 auto* win_ptr = new MachineShMemWinBase(m_pm, new_size_with_offset, 1);
63
64 std::byte* addr_base = win_ptr->segmentView().data();
65 std::byte* addr_after_offset = addr_base + offset;
66
67 std::memcpy(addr_base, &win_ptr, offset);
68
69#ifdef ARCANE_DEBUG_ALLOCATOR
70 m_pm->traceMng()->debug() << "(2/2) MachineShMemWinMemoryAllocator::allocate"
71 << " -- addr_base : " << addr_base
72 << " -- addr_after_offset : " << addr_after_offset;
73#endif
74
75 return { addr_after_offset, new_size };
76}
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81AllocatedMemoryInfo MachineShMemWinMemoryAllocator::
82reallocate(MemoryAllocationArgs, AllocatedMemoryInfo current_ptr, Int64 new_size)
83{
84 if (current_ptr.baseAddress() == nullptr) {
85 return allocate({}, new_size);
86 }
87
88 MachineShMemWinBase* win = _windowBase(current_ptr);
89
90 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
91
92 const Int64 new_size_with_offset = offset + new_size;
93
94 const Int64 d_old_size = win->segmentView().size();
95 std::byte* d_old_addr_base = win->segmentView().data();
96
97 win->resize(new_size_with_offset);
98
99 std::byte* addr_base = win->segmentView().data();
100 std::byte* addr_after_offset = addr_base + offset;
101
102#ifdef ARCANE_DEBUG_ALLOCATOR
103 m_pm->traceMng()->debug() << "MachineShMemWinMemoryAllocator::reallocate"
104 << " -- old_size : " << d_old_size
105 << " -- old_addr_base : " << d_old_addr_base
106 << " -- new ptr.size() : " << new_size
107 << " -- offset : " << offset
108 << " -- win_size (offset+ptr.size()) : " << new_size_with_offset
109 << " -- addr_base : " << addr_base
110 << " -- addr_after_offset : " << addr_after_offset;
111#endif
112
113 return { addr_after_offset, new_size };
114}
115
116/*---------------------------------------------------------------------------*/
117/*---------------------------------------------------------------------------*/
118
119void MachineShMemWinMemoryAllocator::
121{
122 // Thanks to allocate(), we are sure that everyone has a nullptr (no
123 // need to check with a reduction).
124 if (ptr.baseAddress() == nullptr) {
125 return;
126 }
127
128 MachineShMemWinBase* win_ptr = _windowBase(ptr);
129
130#ifdef ARCANE_DEBUG_ALLOCATOR
131 m_pm->traceMng()->debug() << "MachineShMemWinMemoryAllocator::deallocate"
132 << " -- ptr.size() : " << ptr.size()
133 << " -- win_size (offset+ptr.size()) : " << win_ptr->segmentView().size()
134 << " -- addr_base : " << win_ptr->segmentView().data();
135#endif
136
137 delete win_ptr;
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143ConstArrayView<Int32> MachineShMemWinMemoryAllocator::
144machineRanks(AllocatedMemoryInfo ptr)
145{
146 return _windowBase(ptr)->machineRanks();
147}
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152void MachineShMemWinMemoryAllocator::
153barrier(AllocatedMemoryInfo ptr)
154{
155 _windowBase(ptr)->barrier();
156}
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
161Span<std::byte> MachineShMemWinMemoryAllocator::
162segmentView(AllocatedMemoryInfo ptr)
163{
164 const Span<std::byte> view = _windowBase(ptr)->segmentView();
165 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
166 return view.subSpan(offset, view.size() - offset);
167}
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172Span<std::byte> MachineShMemWinMemoryAllocator::
173segmentView(AllocatedMemoryInfo ptr, Int32 rank)
174{
175 const Span<std::byte> view = _windowBase(ptr)->segmentView(rank);
176 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
177 return view.subSpan(offset, view.size() - offset);
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183MachineShMemWinBase* MachineShMemWinMemoryAllocator::
184_windowBase(AllocatedMemoryInfo ptr)
185{
186 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
187
188 std::byte* addr_after_offset = static_cast<std::byte*>(ptr.baseAddress());
189 std::byte* addr_base = addr_after_offset - offset;
190
191 MachineShMemWinBase* win_ptr = *reinterpret_cast<MachineShMemWinBase**>(addr_base);
192
193#ifdef ARCANE_DEBUG_ALLOCATOR
194 std::cout << "MachineShMemWinMemoryAllocator::_windowBase"
195 << " -- ptr.size() : " << ptr.size()
196 << " -- offset : " << offset
197 << " -- addr_base : " << addr_base
198 << " -- addr_after_offset : " << addr_after_offset << std::endl;
199#endif
200
201#if 0 //def ARCANE_CHECK
202 {
203 Int64 size_obj = win_ptr->segmentView().size();
204 if (size_obj != offset + ptr.size()) {
205 // std::cout << "ERROR MachineShMemWinMemoryAllocator::_windowBase"
206 // << " -- win_size : " << size_obj << std::endl;
207 ARCANE_FATAL("ptr error");
208 }
209 }
210#endif
211
212 return win_ptr;
213}
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218} // End namespace Arcane
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Information about an allocated memory region.
void * baseAddress() const
Address of the start of the allocated region.
Int64 size() const
Size in bytes of the used memory region. (-1) if unknown.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of the parallelism manager for a subdomain.
Class allowing the creation of a shared memory window between the subdomains of the same node.
void resize(Int64 new_nb_elem_segment)
Method to resize our segment.
Span< std::byte > segmentView()
Method to obtain a view of our segment.
AllocatedMemoryInfo allocate(MemoryAllocationArgs, Int64 new_size) override
Allocates memory for new_size bytes and returns the pointer.
Class containing information to specialize allocations.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.