Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
BasicSerializer.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/* BasicSerializer.h (C) 2000-2025 */
9/* */
10/* Simple implementation of 'ISerializer'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_SERIALIZE_BASICSERIALIZER_H
13#define ARCCORE_SERIALIZE_BASICSERIALIZER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/serialize/ISerializer.h"
18
19#include "arccore/base/Float16.h"
20#include "arccore/base/BFloat16.h"
21#include "arccore/base/Float128.h"
22#include "arccore/base/Int128.h"
23#include "arccore/collections/Array.h"
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
39template <class DataType>
40class BasicSerializerDataT
41{
42 public:
43
44 BasicSerializerDataT()
45 : m_reserved_size(0)
46 , m_current_position(0)
47 {}
48
49 public:
50
51 void put(Span<const DataType> values)
52 {
53 Int64 n = values.size();
54 Int64 cp = m_current_position;
55 Int64 max_size = 1 + m_buffer.size();
56 arccoreCheckAt(n + cp, max_size);
57
58 DataType* ptr = m_buffer.data() + cp;
59 const DataType* vptr = values.data();
60 for (Int64 i = 0; i < n; ++i)
61 ptr[i] = vptr[i];
62 m_current_position += n;
63 }
64
65 void get(Span<DataType> values)
66 {
67 Int64 n = values.size();
68 Int64 cp = m_current_position;
69 Int64 max_size = 1 + m_buffer.size();
70 arccoreCheckAt(n + cp, max_size);
71
72 const DataType* ptr = m_buffer.data() + cp;
73 DataType* vptr = values.data();
74 for (Int64 i = 0; i < n; ++i)
75 vptr[i] = ptr[i];
76 m_current_position += n;
77 }
78
79 public:
80
81 Int64 m_reserved_size;
82 Int64 m_current_position;
83 Span<DataType> m_buffer;
84};
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
93class ARCCORE_SERIALIZE_EXPORT BasicSerializer
94: public ISerializer
95{
97 typedef BasicSerializer ThatClass;
100
101 public:
102
103 // Temporary class to display buffer sizes.
104 class SizesPrinter
105 {
106 public:
107
108 explicit SizesPrinter(const BasicSerializer& sbuf)
109 : m_sbuf(sbuf)
110 {}
111 const BasicSerializer& buf() const { return m_sbuf; }
112
113 private:
114
115 const BasicSerializer& m_sbuf;
116 };
117
118 public:
119
120 class Impl;
121 class Impl2;
122
123 public:
124
125 BasicSerializer();
126 BasicSerializer(const BasicSerializer& sb);
127 ~BasicSerializer() override;
128
129 private:
130
131 void operator=(const BasicSerializer& sb);
132
133 public:
134
136
137 public:
138
139 void reserveSpan(eDataType dt, Int64 n) override;
140 void reserveSpan(eBasicDataType dt, Int64 n) override;
141 void reserve(eBasicDataType dt, Int64 n) override;
142 void reserve(eDataType dt, Int64 n) override;
143 void reserveInteger(Int64 n) override
144 {
145#ifdef ARCANE_64BIT
146 reserve(DT_Int64, n);
147#else
148 reserve(DT_Int32, n);
149#endif
150 }
151 void reserve(const String& str) override;
152
153 void reserveArray(Span<const Real> values) override;
154 void reserveArray(Span<const Int16> values) override;
155 void reserveArray(Span<const Int32> values) override;
156 void reserveArray(Span<const Int64> values) override;
157 void reserveArray(Span<const Byte> values) override;
158 void reserveArray(Span<const Int8> values) override;
159 void reserveArray(Span<const Float16> values) override;
160 void reserveArray(Span<const BFloat16> values) override;
161 void reserveArray(Span<const Float32> values) override;
162 void reserveArray(Span<const Float128> values) override;
163 void reserveArray(Span<const Int128> values) override;
164
165 void put(Span<const Real> values) override;
166 void put(Span<const Int16> values) override;
167 void put(Span<const Int32> values) override;
168 void put(Span<const Int64> values) override;
169 void put(Span<const Byte> values) override;
170 void putSpan(Span<const Int8> values) override;
171 void putSpan(Span<const Float16> values) override;
172 void putSpan(Span<const BFloat16> values) override;
173 void putSpan(Span<const Float32> values) override;
174 void putSpan(Span<const Float128> values) override;
175 void putSpan(Span<const Int128> values) override;
176 void put(const String& value) override;
177
178 void put(Real value) override
179 {
180 putReal(value);
181 }
182 void put(Int64 value) override
183 {
184 putInt64(value);
185 }
186 void put(Int32 value) override
187 {
188 putInt32(value);
189 }
190 void put(Int16 value) override
191 {
192 putInt16(value);
193 }
194 void put(Byte value) override
195 {
196 putByte(value);
197 }
198 void put(Int8 value) override
199 {
200 putInt8(value);
201 }
202 void put(Float16 value) override
203 {
204 putFloat16(value);
205 }
206 void put(BFloat16 value) override
207 {
208 putBFloat16(value);
209 }
210 void put(Float32 value) override
211 {
212 putFloat32(value);
213 }
214 void put(Float128 value) override
215 {
216 putFloat128(value);
217 }
218 void put(Int128 value) override
219 {
220 putInt128(value);
221 }
222
223 void putReal(Real value) override
224 {
225 put(ConstArrayView<Real>(1, &value));
226 }
227 void putInt64(Int64 value) override
228 {
229 put(ConstArrayView<Int64>(1, &value));
230 }
231 void putInt32(Int32 value) override
232 {
233 put(ConstArrayView<Int32>(1, &value));
234 }
235 void putInt16(Int16 value) override
236 {
237 put(ConstArrayView<Int16>(1, &value));
238 }
239 void putInteger(Integer value) override
240 {
241#ifdef ARCANE_64BIT
242 put(ConstArrayView<Int64>(1, &value));
243#else
244 put(ConstArrayView<Int32>(1, &value));
245#endif
246 }
247 void putByte(Byte value) override
248 {
249 put(ConstArrayView<Byte>(1, &value));
250 }
251 void putInt8(Int8 value) override
252 {
253 putSpan(ConstArrayView<Int8>(1, &value));
254 }
255 void putFloat16(Float16 value) override
256 {
258 }
259 void putBFloat16(BFloat16 value) override
260 {
262 }
263 void putFloat32(Float32 value) override
264 {
266 }
267 void putFloat128(Float128 value) override
268 {
270 }
271 void putInt128(Int128 value) override
272 {
274 }
275
276 void putArray(Span<const Real> values) override;
277 void putArray(Span<const Int16> values) override;
278 void putArray(Span<const Int32> values) override;
279 void putArray(Span<const Int64> values) override;
280 void putArray(Span<const Byte> values) override;
281 void putArray(Span<const Int8> values) override;
282 void putArray(Span<const Float16> values) override;
283 void putArray(Span<const BFloat16> values) override;
284 void putArray(Span<const Float32> values) override;
285 void putArray(Span<const Float128> values) override;
286 void putArray(Span<const Int128> values) override;
287
288 void get(ArrayView<Real> values) override { ThatClass::getSpan(values); }
289 void get(ArrayView<Int64> values) override { ThatClass::getSpan(values); }
290 void get(ArrayView<Int32> values) override { ThatClass::getSpan(values); }
291 void get(ArrayView<Int16> values) override { ThatClass::getSpan(values); }
292 void get(ArrayView<Byte> values) override { ThatClass::getSpan(values); }
293
294 void getSpan(Span<Real> values) override;
295 void getSpan(Span<Int16> values) override;
296 void getSpan(Span<Int32> values) override;
297 void getSpan(Span<Int64> values) override;
298 void getSpan(Span<Byte> values) override;
299 void getSpan(Span<Int8> values) override;
300 void getSpan(Span<Float16> values) override;
301 void getSpan(Span<BFloat16> values) override;
302 void getSpan(Span<Float32> values) override;
303 void getSpan(Span<Float128> values) override;
304 void getSpan(Span<Int128> values) override;
305
306 void getArray(Array<Real>& values) override;
307 void getArray(Array<Int16>& values) override;
308 void getArray(Array<Int32>& values) override;
309 void getArray(Array<Int64>& values) override;
310 void getArray(Array<Byte>& values) override;
311 void getArray(Array<Int8>& values) override;
312 void getArray(Array<Float16>& values) override;
313 void getArray(Array<BFloat16>& values) override;
314 void getArray(Array<Float32>& values) override;
315 void getArray(Array<Float128>& values) override;
316 void getArray(Array<Int128>& values) override;
317
318 void get(String& values) override;
319
320 Real getReal() override
321 {
322 Real r = 0.;
323 get(ArrayView<Real>(1, &r));
324 return r;
325 }
326 Int64 getInt64() override
327 {
328 Int64 r = 0;
329 get(ArrayView<Int64>(1, &r));
330 return r;
331 }
332 Int32 getInt32() override
333 {
334 Int32 r = 0;
335 get(ArrayView<Int32>(1, &r));
336 return r;
337 }
338 Int16 getInt16() override
339 {
340 Int16 r = 0;
341 get(ArrayView<Int16>(1, &r));
342 return r;
343 }
345 {
346#ifdef ARCANE_64BIT
347 return getInt64();
348#else
349 return getInt32();
350#endif
351 }
352 Byte getByte() override
353 {
354 Byte r = 0;
355 get(ArrayView<Byte>(1, &r));
356 return r;
357 }
358 Int8 getInt8() override
359 {
360 Int8 r = 0;
361 getSpan(ArrayView<Int8>(1, &r));
362 return r;
363 }
365 {
366 Float16 r = {};
368 return r;
369 }
371 {
372 BFloat16 r = {};
374 return r;
375 }
377 {
378 Float32 r = {};
380 return r;
381 }
383 {
384 Float128 r = {};
386 return r;
387 }
388 Int128 getInt128() override
389 {
390 Int128 r = {};
392 return r;
393 }
394
395 void allocateBuffer() override;
396
397 eMode mode() const override;
398 void setMode(eMode new_mode) override;
399 eReadMode readMode() const override;
400 void setReadMode(eReadMode new_read_mode) override;
401
402 public:
403
416 void setSerializeTypeInfo(bool v);
417 bool isSerializeTypeInfo() const;
418
419 private:
420
421 // Obsolete method in the interface. To be removed as soon as possible
422 void allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
423 Int64 nb_int64, Int64 nb_byte) override;
424
425 public:
426
427 ARCCORE_DEPRECATED_2020("internal method")
428 Span<Real> realBuffer();
429 ARCCORE_DEPRECATED_2020("internal method")
430 Span<Int64> int64Buffer();
431 ARCCORE_DEPRECATED_2020("internal method")
432 Span<Int32> int32Buffer();
433 ARCCORE_DEPRECATED_2020("internal method")
434 Span<Int16> int16Buffer();
435 ARCCORE_DEPRECATED_2020("internal method")
436 Span<Byte> byteBuffer();
437
438 public:
439
440 ConstArrayView<Byte> copyAndGetSizesBuffer();
441 Span<Byte> globalBuffer();
442 Span<const Byte> globalBuffer() const;
443 ARCCORE_DEPRECATED_2020("Do not use. get total size with totalSize()")
444 ConstArrayView<Int64> sizesBuffer();
445 Int64 totalSize() const;
446 void preallocate(Int64 size);
447 void releaseBuffer();
448 void setFromSizes();
449 void printSizes(std::ostream& o) const;
450
451 friend inline std::ostream&
452 operator<<(std::ostream& o, const BasicSerializer::SizesPrinter& x)
453 {
454 x.buf().printSizes(o);
455 return o;
456 }
457
458 public:
459
466 void initFromBuffer(Span<const Byte> buf);
467 void copy(const ISerializer* from) override;
468 void copy(const BasicSerializer& rhs);
476 static ARCCORE_CONSTEXPR Integer paddingSize() { return 128; }
477
478 // TEMPORARY until we use Arcane AllGather. Then to be made private
479 protected:
480
481 Impl2* m_p2;
482
483 Impl* _p() const;
484};
485
486/*---------------------------------------------------------------------------*/
487/*---------------------------------------------------------------------------*/
488
489} // namespace Arcane
490
491/*---------------------------------------------------------------------------*/
492/*---------------------------------------------------------------------------*/
493
494#endif
#define ARCCORE_CONSTEXPR
Macro allowing specification of the C++11 'constexpr' keyword.
Modifiable view of an array of type T.
Base class for 1D data vectors.
Basic implementation of 'ISerializer'.
void putInt128(Int128 value) override
Add value.
void putInt16(Int16 value) override
Add the integer value.
void put(Float16 value) override
Add value.
void get(ArrayView< Int32 > values) override
Retrieve the array values.
void putSpan(Span< const Int8 > values) override
Add the array values.
Float16 getFloat16() override
Retrieve a Float16.
void get(ArrayView< Int64 > values) override
Retrieve the array values.
void put(Int8 value) override
Add value.
void put(Int64 value) override
Add value.
void putBFloat16(BFloat16 value) override
Add value.
void putInt64(Int64 value) override
Add the integer value.
void put(Int16 value) override
Add value.
void getSpan(Span< Real > values) override
Retrieve the array values.
static ARCCORE_CONSTEXPR Integer paddingSize()
Padding and alignment size.
void put(Int128 value) override
Add value.
void put(Span< const Real > values) override
Add the array values.
void put(Float32 value) override
Add value.
Real getReal() override
Retrieve a real number.
void reserve(eBasicDataType dt, Int64 n) override
Reserves memory for n objects of type dt.
void putInt32(Int32 value) override
Add the integer value.
void putFloat16(Float16 value) override
Add value.
void reserveSpan(eDataType dt, Int64 n) override
Reserves memory for n values of dt.
void get(ArrayView< Byte > values) override
Retrieve the array values.
BFloat16 getBFloat16() override
Retrieve a BFloat16.
void putFloat128(Float128 value) override
Add value.
Int64 getInt64() override
Retrieve a size.
void put(Float128 value) override
Add value.
void putFloat32(Float32 value) override
Add value.
Int16 getInt16() override
Retrieve a 16-bit integer.
Integer getInteger() override
Retrieve a size.
void get(ArrayView< Real > values) override
Retrieve the array values.
Int32 getInt32() override
Retrieve an integer.
void put(Int32 value) override
Add value.
void putReal(Real value) override
Add the real value.
Int8 getInt8() override
Retrieve an Int8.
void putInteger(Integer value) override
Add the integer value.
Int128 getInt128() override
Retrieve an Int128.
void putInt8(Int8 value) override
Add value.
Byte getByte() override
Retrieve a byte.
void get(ArrayView< Int16 > values) override
Retrieve the array values.
Float32 getFloat32() override
Retrieve a Float32.
Float128 getFloat128() override
Retrieve a Float128.
void put(Real value) override
Add value.
void putByte(Byte value) override
Add the byte value.
void put(BFloat16 value) override
Add value.
void put(Byte value) override
Add value.
Constant view of an array of type T.
Half-precision floating-point type.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual void putSpan(Span< const Real > values)
Add the array values.
virtual void reserveSpan(eBasicDataType dt, Int64 n)=0
Reserves memory for n values of dt.
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
View of an array of elements of type T.
Definition Span.h:635
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int8_t Int8
Signed integer type of 8 bits.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
eBasicDataType
Type of a basic data item.
__host__ __device__ void arccoreCheckAt(Int64 i, Int64 max_size)
Checks for potential array overflow.
std::int16_t Int16
Signed integer type of 16 bits.
double Real
Type representing a real number.
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
float Float32
IEEE-753 single-precision floating-point type.
std::int32_t Int32
Signed integer type of 32 bits.