Arcane  4.2.3.0
User documentation
Loading...
Searching...
No Matches
SerializerExtension.h
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/* SerializerExtension.h (C) 2000-2026 */
9/* */
10/* Extension of the serializer with support of multidimensional arrays. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_SERIALIZE_SERIALIZEREXTENSION_H
13#define ARCCORE_SERIALIZE_SERIALIZEREXTENSION_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/serialize/ISerializer.h"
18
19#include <array>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30/*!
31 * \brief Class that extends the functionalities of the ISerializer.
32 *
33 * This class allows you to serialize multidimensionnal arrays or String arrays.
34 * It uses methods of the classical ISerializer. You can destroy this class
35 * between steps of serialization, it doesn't store any datas.
36 */
37class ARCCORE_SERIALIZE_EXPORT SerializerExtension
38{
39 public:
40
41 /*!
42 * \brief Constructor
43 * \param serializer The classical serializer.
44 */
46 : m_serializer(serializer)
47 {}
48
49 public:
50
51 //! Reserve for a view of \a values elements
52 template <class Type>
53 void reserveSpan(Span2<const Type> values) const;
54
55 //! Reserve for a view of \a values elements
56 template <class Type, class Extents>
57 void reserveSpan(MDSpan<const Type, Extents> values) const
58 requires(Extents::rank() <= 4);
59
60 //! Reserve to save the number of elements and the \a values elements
61 template <class Type>
62 void reserveArray(Span2<const Type> values) const;
63
64 //! Reserve to save the number of elements and the \a values elements
65 template <class Type, class Extents>
66 void reserveArray(MDSpan<const Type, Extents> values) const
67 requires(Extents::rank() <= 4);
68
69 //! Reserve to save the number of elements and the \a values elements
70 void reserveArray(Span<const String> values) const;
71
72 public:
73
74 //! Add the array \a values
75 template <class Type>
76 void putSpan(Span2<const Type> values) const;
77
78 //! Add the array \a values
79 template <class Type, class Extents>
80 void putSpan(MDSpan<const Type, Extents> values) const
81 requires(Extents::rank() <= 4);
82
83 //! Save the number of elements and the \a values elements
84 template <class Type>
85 void putArray(Span2<const Type> values) const;
86
87 //! Save the number of elements and the \a values elements
88 template <class Type, class Extents>
89 void putArray(MDSpan<const Type, Extents> values) const
90 requires(Extents::rank() <= 4);
91
92 //! Save the number of elements and the \a values elements
93 void putArray(Span<const String> values) const;
94
95 public:
96
97 //! Retrieve the array \a values
98 template <class Type>
99 void getSpan(Span2<Type> values) const;
100
101 //! Retrieve the array \a values
102 template <class Type, class Extents>
103 void getSpan(MDSpan<Type, Extents> values) const
104 requires(Extents::rank() <= 4);
105
106 //! Resize and fill \a values
107 template <class Type>
108 void getArray(Array2<Type>& values) const;
109
110 //! Resize and fill \a values
111 template <class Type, class Extents>
112 void getArray(NumArray<Type, Extents>& values) const
113 requires(Extents::rank() <= 4);
114
115 //! Resize and fill \a values
116 void getArray(Array<String>& values) const;
117
118 private:
119
120 ISerializer* m_serializer;
121};
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126template <class Type>
128reserveSpan(Span2<const Type> values) const
129{
130 Span<const Type> span_1d(values.data(), values.dim1Size() * values.dim2Size());
131 m_serializer->reserveSpan(span_1d);
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137template <class Type, class Extents>
140requires(Extents::rank() <= 4)
141{
142 m_serializer->reserveSpan(values.to1DSpan());
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148template <class Type>
151{
152 m_serializer->reserveInt64(2);
153 this->reserveSpan(values);
154}
155
156/*---------------------------------------------------------------------------*/
157/*---------------------------------------------------------------------------*/
158
159template <class Type, class Extents>
162requires(Extents::rank() <= 4)
163{
164 constexpr Int32 rank = Extents::rank();
165 m_serializer->reserveInt64(rank);
166 m_serializer->reserveSpan(values.to1DSpan());
167}
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172template <class Type>
174putSpan(Span2<const Type> values) const
175{
176 Span<const Type> span_1d(values.data(), values.dim1Size() * values.dim2Size());
177 m_serializer->putSpan(span_1d);
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183template <class Type, class Extents>
186requires(Extents::rank() <= 4)
187{
188 m_serializer->putSpan(values.to1DSpan());
189}
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
193
194template <class Type>
196putArray(Span2<const Type> values) const
197{
198 m_serializer->putInt64(values.dim1Size());
199 m_serializer->putInt64(values.dim2Size());
200 this->putSpan(values);
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206template <class Type, class Extents>
209requires(Extents::rank() <= 4)
210{
211 constexpr Int32 rank = Extents::rank();
212 // TODO : Add method in ArrayExtents to get an std::array to remove
213 // all 'requires()' of this class and simplify this method.
214 if constexpr (rank >= 1) {
215 m_serializer->putInt64(values.extent0());
216 }
217 if constexpr (rank >= 2) {
218 m_serializer->putInt64(values.extent1());
219 }
220 if constexpr (rank >= 3) {
221 m_serializer->putInt64(values.extent2());
222 }
223 if constexpr (rank >= 4) {
224 m_serializer->putInt64(values.extent3());
225 }
226 m_serializer->putSpan(values.to1DSpan());
227}
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
231
232template <class Type>
234getSpan(Span2<Type> values) const
235{
236 Span<Type> span_1d(values.data(), values.dim1Size() * values.dim2Size());
237 m_serializer->getSpan(span_1d);
238}
239
240/*---------------------------------------------------------------------------*/
241/*---------------------------------------------------------------------------*/
242
243template <class Type, class Extents>
245getSpan(MDSpan<Type, Extents> values) const
246requires(Extents::rank() <= 4)
247{
248 Span<Type> values_1d = values.to1DSpan();
249 m_serializer->getSpan(values_1d);
250}
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
254
255template <class Type>
257getArray(Array2<Type>& values) const
258{
259 Int64 size1 = m_serializer->getInt64();
260 Int64 size2 = m_serializer->getInt64();
261
262 values.resize(size1, size2);
263 m_serializer->getSpan(values.to1DSpan());
264}
265
266/*---------------------------------------------------------------------------*/
267/*---------------------------------------------------------------------------*/
268
269template <class Type, class Extents>
272requires(Extents::rank() <= 4)
273{
274 using IndexType = Extents::ExtentIndexType;
275
276 {
277 constexpr Int32 rank = Extents::rank();
278 std::array<IndexType, rank> size{};
279 for (Int32 i = 0; i < rank; ++i) {
280 size[i] = static_cast<IndexType>(m_serializer->getInt64());
281 }
282 MDIndex<rank, IndexType> mdi(size);
283 values.resizeDestructive(mdi);
284 }
285
286 m_serializer->getSpan(values.to1DSpan());
287}
288
289/*---------------------------------------------------------------------------*/
290/*---------------------------------------------------------------------------*/
291
292} // namespace Arcane
293
294/*---------------------------------------------------------------------------*/
295/*---------------------------------------------------------------------------*/
296
297#endif
Class representing a classic 2D array.
Span< DataType > to1DSpan()
View of the array as a 1D array.
void resize(Int64 new_size)
Resizes only the first dimension, leaving the second dimension unchanged.
Base class for 1D data vectors.
Base class for multidimensional views.
Multi-dimensional arrays for numerical types accessible on accelerators.
void getSpan(Span2< Type > values) const
Retrieve the array values.
void reserveArray(Span2< const Type > values) const
Reserve to save the number of elements and the values elements.
SerializerExtension(ISerializer *serializer)
Constructor.
void putArray(Span2< const Type > values) const
Save the number of elements and the values elements.
void reserveSpan(Span2< const Type > values) const
Reserve for a view of values elements.
void getArray(Array2< Type > &values) const
Resize and fill values.
void putSpan(Span2< const Type > values) const
Add the array values.
constexpr __host__ __device__ ElementType * data()
Pointer to the allocated memory.
Definition Span2.h:192
constexpr __host__ __device__ SizeType dim2Size() const
Number of elements in the second dimension.
Definition Span2.h:118
constexpr __host__ __device__ SizeType dim1Size() const
Number of elements in the first dimension.
Definition Span2.h:116
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:320
View of an array of elements of type T.
Definition Span.h:633
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
std::int32_t Int32
Signed integer type of 32 bits.