Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MeshMaterialSynchronizeBuffer.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/* MeshMaterialSynchronizeBuffer.cc (C) 2000-2024 */
9/* */
10/* Buffer management for synchronizing material variables. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/materials/IMeshMaterialSynchronizeBuffer.h"
15
16#include "arcane/utils/UniqueArray.h"
17#include "arcane/utils/PlatformUtils.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane::Materials
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29class MultiBufferMeshMaterialSynchronizeBuffer
31{
32 public:
33
34 struct BufferInfo
35 {
36 explicit BufferInfo(IMemoryAllocator* allocator)
37 : m_send_buffer(allocator)
38 , m_receive_buffer(allocator)
39 {
40 }
41
42 void reset()
43 {
44 m_send_size = 0;
45 m_receive_size = 0;
46 m_send_buffer.clear();
47 m_receive_buffer.clear();
48 }
49 Int32 m_send_size = 0;
50 Int32 m_receive_size = 0;
51 UniqueArray<Byte> m_send_buffer;
52 UniqueArray<Byte> m_receive_buffer;
53 };
54
55 public:
56
57 explicit MultiBufferMeshMaterialSynchronizeBuffer(IMemoryAllocator* allocator)
58 : m_default_buffer_info(allocator)
59 {
60 }
61
62 Int32 nbRank() const override { return m_nb_rank; }
63 void setNbRank(Int32 nb_rank) override
64 {
65 m_nb_rank = nb_rank;
66 m_buffer_infos.resize(nb_rank, m_default_buffer_info);
67 for (auto& x : m_buffer_infos)
68 x.reset();
69 }
70 Span<Byte> sendBuffer(Int32 index) override
71 {
72 return m_buffer_infos[index].m_send_buffer;
73 }
74 void setSendBufferSize(Int32 index, Int32 new_size) override
75 {
76 m_buffer_infos[index].m_send_size = new_size;
77 }
79 {
80 return m_buffer_infos[index].m_receive_buffer;
81 }
82 void setReceiveBufferSize(Int32 index, Int32 new_size) override
83 {
84 m_buffer_infos[index].m_receive_size = new_size;
85 }
86 void allocate() override
87 {
88 m_total_size = 0;
89 for (auto& x : m_buffer_infos) {
90 x.m_send_buffer.resize(x.m_send_size);
91 x.m_receive_buffer.resize(x.m_receive_size);
92 m_total_size += x.m_send_size + x.m_receive_size;
93 }
94 }
95 Int64 totalSize() const override { return m_total_size; }
96
97 public:
98
99 Int32 m_nb_rank = 0;
100 Int64 m_total_size = 0;
101 BufferInfo m_default_buffer_info;
102 UniqueArray<BufferInfo> m_buffer_infos;
103};
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108class OneBufferMeshMaterialSynchronizeBuffer
110{
111 public:
112
114 {
115 void reset()
116 {
117 m_send_size = 0;
118 m_receive_size = 0;
119 m_send_index = 0;
120 m_receive_index = 0;
121 }
122
123 Span<Byte> sendBuffer(Span<Byte> full_buffer) const
124 {
125 return full_buffer.subspan(m_send_index, m_send_size);
126 }
127 Span<Byte> receiveBuffer(Span<Byte> full_buffer) const
128 {
129 return full_buffer.subspan(m_receive_index, m_receive_size);
130 }
131
132 Int32 m_send_size = 0;
133 Int32 m_receive_size = 0;
134 Int64 m_send_index = 0;
135 Int64 m_receive_index = 0;
136 };
137
138 public:
139
140 explicit OneBufferMeshMaterialSynchronizeBuffer(IMemoryAllocator* allocator)
141 : m_buffer(allocator)
142 {}
143
144 public:
145
146 Int32 nbRank() const override { return m_nb_rank; }
147 void setNbRank(Int32 nb_rank) override
148 {
149 m_nb_rank = nb_rank;
150 m_buffer_infos.resize(nb_rank);
151 for (auto& x : m_buffer_infos)
152 x.reset();
153 }
155 {
156 return m_buffer_infos[index].sendBuffer(m_buffer);
157 }
158 void setSendBufferSize(Int32 index, Int32 new_size) override
159 {
160 m_buffer_infos[index].m_send_size = new_size;
161 }
163 {
164 return m_buffer_infos[index].receiveBuffer(m_buffer);
165 }
166 void setReceiveBufferSize(Int32 index, Int32 new_size) override
167 {
168 m_buffer_infos[index].m_receive_size = new_size;
169 }
170 void allocate() override
171 {
172 Int64 total_send_size = 0;
173 Int64 total_receive_size = 0;
174 for (auto& x : m_buffer_infos) {
175 total_send_size += x.m_send_size;
176 total_receive_size += x.m_receive_size;
177 }
178 m_buffer.resize(total_send_size + total_receive_size);
179 Int64 send_index = 0;
180 Int64 receive_index = total_send_size;
181 for (auto& x : m_buffer_infos) {
182 x.m_send_index = send_index;
183 x.m_receive_index = receive_index;
184 send_index += x.m_send_size;
185 receive_index += x.m_receive_size;
186 }
187 }
188 Int64 totalSize() const override { return m_buffer.largeSize(); }
189
190 public:
191
192 Int32 m_nb_rank = 0;
193 UniqueArray<BufferInfo> m_buffer_infos;
194 UniqueArray<Byte> m_buffer;
195};
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200namespace impl
201{
203 makeMultiBufferMeshMaterialSynchronizeBufferRef(eMemoryRessource memory_ressource)
204 {
205 auto* a = MemoryUtils::getAllocator(memory_ressource);
208 }
209
210 extern "C++" ARCANE_MATERIALS_EXPORT Ref<IMeshMaterialSynchronizeBuffer>
211 makeMultiBufferMeshMaterialSynchronizeBufferRef()
212 {
213 return makeMultiBufferMeshMaterialSynchronizeBufferRef(eMemoryRessource::Host);
214 }
215
216 extern "C++" ARCANE_MATERIALS_EXPORT Ref<IMeshMaterialSynchronizeBuffer>
217 makeOneBufferMeshMaterialSynchronizeBufferRef(eMemoryRessource memory_ressource)
218 {
219 auto* a = MemoryUtils::getAllocator(memory_ressource);
220 auto* v = new OneBufferMeshMaterialSynchronizeBuffer(a);
222 }
223} // namespace impl
224
225/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
227
228} // End namespace Arcane::Materials
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
Memory and allocator management functions.
void setReceiveBufferSize(Int32 index, Int32 new_size) override
Sets the number of elements for the i-th receive buffer.
Span< Byte > sendBuffer(Int32 index) override
Send buffer for the i-th buffer.
void setNbRank(Int32 nb_rank) override
Sets the number of ranks. This invalidates the send and receive buffers.
Int64 totalSize() const override
Total size allocated for the buffers.
void setSendBufferSize(Int32 index, Int32 new_size) override
Sets the number of elements for the i-th send buffer.
Span< Byte > receiveBuffer(Int32 index) override
Send buffer for the i-th buffer.
Span< Byte > receiveBuffer(Int32 index) override
Send buffer for the i-th buffer.
Int64 totalSize() const override
Total size allocated for the buffers.
Span< Byte > sendBuffer(Int32 index) override
Send buffer for the i-th buffer.
void setReceiveBufferSize(Int32 index, Int32 new_size) override
Sets the number of elements for the i-th receive buffer.
void setNbRank(Int32 nb_rank) override
Sets the number of ranks. This invalidates the send and receive buffers.
void setSendBufferSize(Int32 index, Int32 new_size) override
Sets the number of elements for the i-th send buffer.
Reference to an instance.
View of an array of elements of type T.
Definition Span.h:635
constexpr __host__ __device__ Span< T, DynExtent > subspan(Int64 abegin, Int64 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:724
1D data vector with value semantics (STL style).
Always enables tracing in Arcane parts concerning materials.
class ARCANE_MATERIALS_EXPORT(64) SimdMatVarIndex
SIMD indexer on a component.
IMemoryAllocator * getAllocator(eMemoryResource mem_resource)
Default allocator for the resource mem_resource.
std::int64_t Int64
Signed integer type of 64 bits.
Arcane::eMemoryResource eMemoryRessource
Typedef for the historical Arcane version (with 2's').
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.