Arcane  v4.1.7.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
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/* Allocateur mémoire utilisant la classe MachineShMemWinBase. */
11/*---------------------------------------------------------------------------*/
12
13#include "arcane/core/internal/MachineShMemWinMemoryAllocator.h"
14
15#include "arcane/utils/FatalErrorException.h"
16#include "arcane/utils/ITraceMng.h"
17
18#include "arcane/core/IParallelMng.h"
19#include "arcane/core/MachineShMemWinBase.h"
20
21#include "arccore/common/AllocatedMemoryInfo.h"
22
23#include <cstring>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34MachineShMemWinMemoryAllocator::
35MachineShMemWinMemoryAllocator(IParallelMng* pm)
36: m_pm(pm)
37{}
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42AllocatedMemoryInfo MachineShMemWinMemoryAllocator::
43allocate(MemoryAllocationArgs, Int64 new_size)
44{
45 // Si la taille est égal à zéro, comme on a une création collective, on doit
46 // vérifier si la taille est égal à zéro pour tous.
47 if (m_pm->reduce(MessagePassing::ReduceMax, new_size) <= 0) {
48 return { nullptr, 0 };
49 }
50
51 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
52 const Int64 new_size_with_offset = offset + new_size;
53
54#ifdef ARCANE_DEBUG_ALLOCATOR
55 m_pm->traceMng()->debug() << "(1/2) MachineShMemWinMemoryAllocator::allocate"
56 << " -- ptr.size() : " << new_size
57 << " -- offset : " << offset
58 << " -- win_size (offset+ptr.size()) : " << new_size_with_offset;
59#endif
60
61 auto* win_ptr = new MachineShMemWinBase(m_pm, new_size_with_offset, 1);
62
63 std::byte* addr_base = win_ptr->segmentView().data();
64 std::byte* addr_after_offset = addr_base + offset;
65
66 std::memcpy(addr_base, &win_ptr, offset);
67
68#ifdef ARCANE_DEBUG_ALLOCATOR
69 m_pm->traceMng()->debug() << "(2/2) MachineShMemWinMemoryAllocator::allocate"
70 << " -- addr_base : " << addr_base
71 << " -- addr_after_offset : " << addr_after_offset;
72#endif
73
74 return { addr_after_offset, new_size };
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
80AllocatedMemoryInfo MachineShMemWinMemoryAllocator::
81reallocate(MemoryAllocationArgs, AllocatedMemoryInfo current_ptr, Int64 new_size)
82{
83 if (current_ptr.baseAddress() == nullptr) {
84 return allocate({}, new_size);
85 }
86
87 MachineShMemWinBase* win = _windowBase(current_ptr);
88
89 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
90
91 const Int64 new_size_with_offset = offset + new_size;
92
93 const Int64 d_old_size = win->segmentView().size();
94 std::byte* d_old_addr_base = win->segmentView().data();
95
96 win->resize(new_size_with_offset);
97
98 std::byte* addr_base = win->segmentView().data();
99 std::byte* addr_after_offset = addr_base + offset;
100
101#ifdef ARCANE_DEBUG_ALLOCATOR
102 m_pm->traceMng()->debug() << "MachineShMemWinMemoryAllocator::reallocate"
103 << " -- old_size : " << d_old_size
104 << " -- old_addr_base : " << d_old_addr_base
105 << " -- new ptr.size() : " << new_size
106 << " -- offset : " << offset
107 << " -- win_size (offset+ptr.size()) : " << new_size_with_offset
108 << " -- addr_base : " << addr_base
109 << " -- addr_after_offset : " << addr_after_offset;
110#endif
111
112 return { addr_after_offset, new_size };
113}
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
117
118void MachineShMemWinMemoryAllocator::
120{
121 // Grâce au allocate(), on est sûr que tout le monde a un nullptr (pas
122 // besoin de vérifier avec une réduction).
123 if (ptr.baseAddress() == nullptr) {
124 return;
125 }
126
127 MachineShMemWinBase* win_ptr = _windowBase(ptr);
128
129#ifdef ARCANE_DEBUG_ALLOCATOR
130 m_pm->traceMng()->debug() << "MachineShMemWinMemoryAllocator::deallocate"
131 << " -- ptr.size() : " << ptr.size()
132 << " -- win_size (offset+ptr.size()) : " << win_ptr->segmentView().size()
133 << " -- addr_base : " << win_ptr->segmentView().data();
134#endif
135
136 delete win_ptr;
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142ConstArrayView<Int32> MachineShMemWinMemoryAllocator::
143machineRanks(AllocatedMemoryInfo ptr)
144{
145 return _windowBase(ptr)->machineRanks();
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151void MachineShMemWinMemoryAllocator::
152barrier(AllocatedMemoryInfo ptr)
153{
154 _windowBase(ptr)->barrier();
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160Span<std::byte> MachineShMemWinMemoryAllocator::
161segmentView(AllocatedMemoryInfo ptr)
162{
163 const Span<std::byte> view = _windowBase(ptr)->segmentView();
164 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
165 return view.subSpan(offset, view.size() - offset);
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171Span<std::byte> MachineShMemWinMemoryAllocator::
172segmentView(AllocatedMemoryInfo ptr, Int32 rank)
173{
174 const Span<std::byte> view = _windowBase(ptr)->segmentView(rank);
175 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
176 return view.subSpan(offset, view.size() - offset);
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182MachineShMemWinBase* MachineShMemWinMemoryAllocator::
183_windowBase(AllocatedMemoryInfo ptr)
184{
185 constexpr Int64 offset = sizeof(MachineShMemWinBase*);
186
187 std::byte* addr_after_offset = static_cast<std::byte*>(ptr.baseAddress());
188 std::byte* addr_base = addr_after_offset - offset;
189
190 MachineShMemWinBase* win_ptr = *reinterpret_cast<MachineShMemWinBase**>(addr_base);
191
192#ifdef ARCANE_DEBUG_ALLOCATOR
193 std::cout << "MachineShMemWinMemoryAllocator::_windowBase"
194 << " -- ptr.size() : " << ptr.size()
195 << " -- offset : " << offset
196 << " -- addr_base : " << addr_base
197 << " -- addr_after_offset : " << addr_after_offset << std::endl;
198#endif
199
200#if 0 //def ARCANE_CHECK
201 {
202 Int64 size_obj = win_ptr->segmentView().size();
203 if (size_obj != offset + ptr.size()) {
204 // std::cout << "ERROR MachineShMemWinMemoryAllocator::_windowBase"
205 // << " -- win_size : " << size_obj << std::endl;
206 ARCANE_FATAL("ptr error");
207 }
208 }
209#endif
210
211 return win_ptr;
212}
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217} // End namespace Arcane
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Informations sur une zone mémoire allouée.
void * baseAddress() const
Adresse du début de la zone allouée.
Int64 size() const
Taille en octets de la zone mémoire utilisée. (-1) si inconnue.
Vue constante d'un tableau de type T.
constexpr Integer size() const noexcept
Nombre d'éléments du tableau.
Interface du gestionnaire de parallélisme pour un sous-domaine.
Classe permettant de créer une fenêtre mémoire partagée entre les sous-domaines d'un même noeud.
void resize(Int64 new_nb_elem_segment)
Méthode permettant de redimensionner notre segment.
Span< std::byte > segmentView()
Méthode permettant d'obtenir une vue sur notre segment.
AllocatedMemoryInfo allocate(MemoryAllocationArgs, Int64 new_size) override
Alloue de la mémoire pour new_size octets et retourne le pointeur.
Classe contenant des informations pour spécialiser les allocations.
constexpr __host__ __device__ pointer data() const noexcept
Pointeur sur le début de la vue.
Definition Span.h:537
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:325
@ ReduceMax
Maximum des valeurs.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int64_t Int64
Type entier signé sur 64 bits.