Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
BasicSerializer.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/* BasicSerializer.cc (C) 2000-2025 */
9/* */
10/* Simple implementation of 'ISerializer'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/serialize/BasicSerializer.h"
15
16#include "arccore/base/ArgumentException.h"
17#include "arccore/base/FatalErrorException.h"
18
19#include "arccore/serialize/internal/BasicSerializerInternal.h"
20
21#include <iostream>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
50{
52 static constexpr int IDX_TAG = 0;
54 static constexpr Int64 SERIALIZE_TAG = 0x7a9b3cd0;
56 static constexpr int IDX_VERSION = 1;
58 static constexpr int IDX_RESERVED1 = 2;
59
61 static constexpr int IDX_TOTAL_SIZE = 3;
62
63 static constexpr int IDX_NB_BYTE = 4;
64 static constexpr int IDX_NB_FLOAT16 = 5;
65 static constexpr int IDX_NB_FLOAT32 = 6;
66 static constexpr int IDX_NB_FLOAT64 = 7;
67 static constexpr int IDX_NB_FLOAT128 = 8;
68 static constexpr int IDX_NB_INT16 = 9;
69 static constexpr int IDX_NB_INT32 = 10;
70 static constexpr int IDX_NB_INT64 = 11;
71 static constexpr int IDX_NB_INT128 = 12;
72 static constexpr int IDX_NB_INT8 = 13;
73 static constexpr int IDX_NB_BFLOAT16 = 14;
74
75 // Leaves room for new types.
76 static constexpr int IDX_POS_BYTE = 32;
77 static constexpr int IDX_POS_FLOAT16 = 33;
78 static constexpr int IDX_POS_FLOAT32 = 34;
79 static constexpr int IDX_POS_FLOAT64 = 35;
80 static constexpr int IDX_POS_FLOAT128 = 36;
81 static constexpr int IDX_POS_INT16 = 37;
82 static constexpr int IDX_POS_INT32 = 38;
83 static constexpr int IDX_POS_INT64 = 39;
84 static constexpr int IDX_POS_INT128 = 40;
85 static constexpr int IDX_POS_INT8 = 41;
86 static constexpr int IDX_POS_BFLOAT16 = 42;
87
88 static constexpr Integer NB_SIZE_ELEM = 128;
89 // The alignment size is also a divisor of the memory
90 // allocated for the message. It must not be modified without modifying
91 // the MPI serialization handling.
92 static constexpr Integer ALIGN_SIZE = BasicSerializer::paddingSize();
93
94 public:
95
97 struct SizeInfo
98 {
99 public:
100
101 Int64 m_original_size = 0;
102 Int64 m_padded_size = 0;
103 };
104
105 public:
106
109
112
124
126
134
135 public:
136
137 Span<Real> getRealBuffer() override { return m_real_view; }
138 Span<Int16> getInt16Buffer() override { return m_int16_view; }
139 Span<Int32> getInt32Buffer() override { return m_int32_view; }
140 Span<Int64> getInt64Buffer() override { return m_int64_view; }
141 Span<Byte> getByteBuffer() override { return m_byte_view; }
142 Span<Int8> getInt8Buffer() override { return m_int8_view; }
143 Span<Float16> getFloat16Buffer() override { return m_float16_view; }
144 Span<BFloat16> getBFloat16Buffer() override { return m_bfloat16_view; }
145 Span<Float32> getFloat32Buffer() override { return m_float32_view; }
146 Span<Float128> getFloat128Buffer() override { return m_float128_view; }
147 Span<Int128> getInt128Buffer() override { return m_int128_view; }
148
149 void allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
150 Int64 nb_int64, Int64 nb_byte) override
151 {
152 Int64 nb_int8 = 0;
153 Int64 nb_float16 = 0;
154 Int64 nb_bfloat16 = 0;
155 Int64 nb_float32 = 0;
156 Int64 nb_float128 = 0;
157 Int64 nb_int128 = 0;
158 allocateBuffer(nb_real, nb_int16, nb_int32, nb_int64, nb_byte, nb_int8,
159 nb_float16, nb_bfloat16, nb_float32, nb_float128, nb_int128);
160 }
161
162 void allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
163 Int64 nb_int64, Int64 nb_byte, Int64 nb_int8, Int64 nb_float16,
164 Int64 nb_bfloat16, Int64 nb_float32, Int64 nb_float128, Int64 nb_int128) override
165 {
166 SizeInfo size_info = getPaddingSize(NB_SIZE_ELEM, sizeof(Int64));
167 Int64 total = size_info.m_padded_size;
168
169 Int64 real_position = total;
170 SizeInfo padded_real_size = getPaddingSize(nb_real, sizeof(Real));
171 total += padded_real_size.m_padded_size;
172
173 Int64 int16_position = total;
174 SizeInfo padded_int16_size = getPaddingSize(nb_int16, sizeof(Int16));
175 total += padded_int16_size.m_padded_size;
176
177 Int64 int32_position = total;
178 SizeInfo padded_int32_size = getPaddingSize(nb_int32, sizeof(Int32));
179 total += padded_int32_size.m_padded_size;
180
181 Int64 int64_position = total;
182 SizeInfo padded_int64_size = getPaddingSize(nb_int64, sizeof(Int64));
183 total += padded_int64_size.m_padded_size;
184
185 Int64 byte_position = total;
186 SizeInfo padded_byte_size = getPaddingSize(nb_byte, sizeof(Byte));
187 total += padded_byte_size.m_padded_size;
188
189 Int64 int8_position = total;
190 SizeInfo padded_int8_size = getPaddingSize(nb_int8, sizeof(Int8));
191 total += padded_int8_size.m_padded_size;
192
193 Int64 float16_position = total;
194 SizeInfo padded_float16_size = getPaddingSize(nb_float16, sizeof(Float16));
195 total += padded_float16_size.m_padded_size;
196
197 Int64 bfloat16_position = total;
198 SizeInfo padded_bfloat16_size = getPaddingSize(nb_bfloat16, sizeof(BFloat16));
199 total += padded_bfloat16_size.m_padded_size;
200
201 Int64 float32_position = total;
202 SizeInfo padded_float32_size = getPaddingSize(nb_float32, sizeof(Float32));
203 total += padded_float32_size.m_padded_size;
204
205 Int64 float128_position = total;
206 SizeInfo padded_float128_size = getPaddingSize(nb_float128, sizeof(Float128));
207 total += padded_float128_size.m_padded_size;
208
209 Int64 int128_position = total;
210 SizeInfo padded_int128_size = getPaddingSize(nb_int128, sizeof(Int128));
211 total += padded_int128_size.m_padded_size;
212
213 _allocBuffer(total);
214
215 _fillPadding(0, size_info);
216 _fillPadding(real_position, padded_real_size);
217 _fillPadding(int16_position, padded_int16_size);
218 _fillPadding(int32_position, padded_int32_size);
219 _fillPadding(int64_position, padded_int64_size);
220 _fillPadding(byte_position, padded_byte_size);
221 _fillPadding(int8_position, padded_int8_size);
222 _fillPadding(float16_position, padded_float16_size);
223 _fillPadding(bfloat16_position, padded_bfloat16_size);
224 _fillPadding(float32_position, padded_float32_size);
225 _fillPadding(float128_position, padded_float128_size);
226 _fillPadding(int128_position, padded_int128_size);
227
228 m_sizes_view = ArrayView<Int64>(NB_SIZE_ELEM, (Int64*)&m_buffer_view[0]);
229 m_sizes_view.fill(0);
230
234
236 m_sizes_view[IDX_NB_FLOAT64] = nb_real;
237 m_sizes_view[IDX_NB_INT64] = nb_int64;
238 m_sizes_view[IDX_NB_INT32] = nb_int32;
239 m_sizes_view[IDX_NB_INT16] = nb_int16;
240 m_sizes_view[IDX_NB_BYTE] = nb_byte;
241 m_sizes_view[IDX_NB_INT8] = nb_int8;
242 m_sizes_view[IDX_NB_FLOAT16] = nb_float16;
243 m_sizes_view[IDX_NB_BFLOAT16] = nb_bfloat16;
244 m_sizes_view[IDX_NB_FLOAT32] = nb_float32;
245 m_sizes_view[IDX_NB_FLOAT128] = nb_float128;
246 m_sizes_view[IDX_NB_INT128] = nb_int128;
247
248 m_sizes_view[IDX_POS_FLOAT64] = real_position;
249 m_sizes_view[IDX_POS_INT64] = int64_position;
250 m_sizes_view[IDX_POS_INT32] = int32_position;
251 m_sizes_view[IDX_POS_INT16] = int16_position;
252 m_sizes_view[IDX_POS_BYTE] = byte_position;
253 m_sizes_view[IDX_POS_INT8] = int8_position;
254 m_sizes_view[IDX_POS_FLOAT16] = float16_position;
255 m_sizes_view[IDX_POS_BFLOAT16] = bfloat16_position;
256 m_sizes_view[IDX_POS_FLOAT32] = float32_position;
257 m_sizes_view[IDX_POS_FLOAT128] = float32_position;
258 m_sizes_view[IDX_POS_INT128] = int128_position;
259
260 m_real_view = Span<Real>((Real*)&m_buffer_view[real_position], nb_real);
261 m_int16_view = Span<Int16>((Int16*)&m_buffer_view[int16_position], nb_int16);
262 m_int32_view = Span<Int32>((Int32*)&m_buffer_view[int32_position], nb_int32);
263 m_int64_view = Span<Int64>((Int64*)&m_buffer_view[int64_position], nb_int64);
264 m_byte_view = Span<Byte>((Byte*)&m_buffer_view[byte_position], nb_byte);
265 m_int8_view = Span<Int8>((Int8*)&m_buffer_view[int8_position], nb_int8);
266 m_float16_view = Span<Float16>((Float16*)&m_buffer_view[float16_position], nb_float16);
267 m_bfloat16_view = Span<BFloat16>((BFloat16*)&m_buffer_view[bfloat16_position], nb_bfloat16);
268 m_float32_view = Span<Float32>((Float32*)&m_buffer_view[float32_position], nb_float32);
269 m_float128_view = Span<Float128>((Float128*)&m_buffer_view[float128_position], nb_float128);
270 m_int128_view = Span<Int128>((Int128*)&m_buffer_view[int128_position], nb_int128);
271
272 _checkAlignment();
273 }
274
275 void copy(Impl* rhs) override
276 {
277 m_real_view.copy(rhs->getRealBuffer());
278 m_int64_view.copy(rhs->getInt64Buffer());
279 m_int32_view.copy(rhs->getInt32Buffer());
280 m_int16_view.copy(rhs->getInt16Buffer());
281 m_byte_view.copy(rhs->getByteBuffer());
282 m_int8_view.copy(rhs->getInt8Buffer());
283 m_float16_view.copy(rhs->getFloat16Buffer());
284 m_bfloat16_view.copy(rhs->getBFloat16Buffer());
285 m_float32_view.copy(rhs->getFloat32Buffer());
286 m_float128_view.copy(rhs->getFloat128Buffer());
287 m_int128_view.copy(rhs->getInt128Buffer());
288
289 _checkAlignment();
290 }
291
292 Span<Byte> globalBuffer() override
293 {
294 return m_buffer_view;
295 }
296
297 Span<const Byte> globalBuffer() const override
298 {
299 return m_buffer_view;
300 }
301
302 ConstArrayView<Int64> sizesBuffer() const override
303 {
304 return m_sizes_view;
305 }
306
307 void preallocate(Int64 size) override
308 {
309 _allocBuffer(size);
310 m_sizes_view = ArrayView<Int64>(NB_SIZE_ELEM, (Int64*)&m_buffer_view[0]);
311 }
312
313 void releaseBuffer() override
314 {
315 m_buffer.dispose();
316 }
317
318 void setFromSizes() override
319 {
320 Int64 tag_id = m_sizes_view[IDX_TAG];
321 if (tag_id != SERIALIZE_TAG)
322 ARCCORE_FATAL("Bad tag id '{0}' for serializer (expected={1})."
323 "The data are not from a BasicSerializer. SizeView={2}",
324 tag_id, SERIALIZE_TAG, m_sizes_view);
325 Int64 version_id = m_sizes_view[IDX_VERSION];
326 if (version_id != 1)
327 ARCCORE_FATAL("Bad version '{0}' for serializer. Only version 1 is allowed", version_id);
328
329 Int64 nb_real = m_sizes_view[IDX_NB_FLOAT64];
330 Int64 nb_int64 = m_sizes_view[IDX_NB_INT64];
331 Int64 nb_int32 = m_sizes_view[IDX_NB_INT32];
332 Int64 nb_int16 = m_sizes_view[IDX_NB_INT16];
333 Int64 nb_byte = m_sizes_view[IDX_NB_BYTE];
334 Int64 nb_int8 = m_sizes_view[IDX_NB_INT8];
335 Int64 nb_float16 = m_sizes_view[IDX_NB_FLOAT16];
336 Int64 nb_bfloat16 = m_sizes_view[IDX_NB_BFLOAT16];
337 Int64 nb_float32 = m_sizes_view[IDX_NB_FLOAT32];
338 Int64 nb_float128 = m_sizes_view[IDX_NB_FLOAT128];
339 Int64 nb_int128 = m_sizes_view[IDX_NB_INT128];
340
341 Int64 real_position = m_sizes_view[IDX_POS_FLOAT64];
342 Int64 int64_position = m_sizes_view[IDX_POS_INT64];
343 Int64 int32_position = m_sizes_view[IDX_POS_INT32];
344 Int64 int16_position = m_sizes_view[IDX_POS_INT16];
345 Int64 byte_position = m_sizes_view[IDX_POS_BYTE];
346 Int64 int8_position = m_sizes_view[IDX_POS_INT8];
347 Int64 float16_position = m_sizes_view[IDX_POS_FLOAT16];
348 Int64 bfloat16_position = m_sizes_view[IDX_POS_BFLOAT16];
349 Int64 float32_position = m_sizes_view[IDX_POS_FLOAT32];
350 Int64 float128_position = m_sizes_view[IDX_POS_FLOAT128];
351 Int64 int128_position = m_sizes_view[IDX_POS_INT128];
352
353 m_real_view = Span<Real>((Real*)&m_buffer_view[real_position], nb_real);
354 m_int16_view = Span<Int16>((Int16*)&m_buffer_view[int16_position], nb_int16);
355 m_int32_view = Span<Int32>((Int32*)&m_buffer_view[int32_position], nb_int32);
356 m_int64_view = Span<Int64>((Int64*)&m_buffer_view[int64_position], nb_int64);
357 m_byte_view = Span<Byte>((Byte*)&m_buffer_view[byte_position], nb_byte);
358 m_int8_view = Span<Int8>((Int8*)&m_buffer_view[int8_position], nb_int8);
359 m_float16_view = Span<Float16>((Float16*)&m_buffer_view[float16_position], nb_float16);
360 m_bfloat16_view = Span<BFloat16>((BFloat16*)&m_buffer_view[bfloat16_position], nb_bfloat16);
361 m_float32_view = Span<Float32>((Float32*)&m_buffer_view[float32_position], nb_float32);
362 m_float128_view = Span<Float128>((Float128*)&m_buffer_view[float128_position], nb_float128);
363 m_int128_view = Span<Int128>((Int128*)&m_buffer_view[float128_position], nb_int128);
364
365 _checkAlignment();
366 }
367
368 ConstArrayView<Byte> copyAndGetSizesBuffer() override
369 {
370 // Copies the values from \a m_size_view into \a m_size_copy_buffer
371 // and returns a pointer to \a m_size_copy_buffer.
372 ArrayView<Int64> copy_buf(NB_SIZE_ELEM, m_size_copy_buffer);
373 copy_buf.copy(m_sizes_view);
374 ConstArrayView<Byte> bytes(sizeof(m_size_copy_buffer), (const Byte*)m_size_copy_buffer);
375 return bytes;
376 }
377
378 Int64 totalSize() const override
379 {
381 }
382
383 void printSizes(std::ostream& o) const override
384 {
385 ConstArrayView<Int64> sbuf_sizes = this->sizesBuffer();
386 Int64 total_size = totalSize();
387 Span<Byte> bytes = m_buffer_view;
388 o << " bytes " << bytes.size()
389 << " total_size " << total_size
390 << " float64 " << sbuf_sizes[IDX_NB_FLOAT64]
391 << " int64 " << sbuf_sizes[IDX_NB_INT64]
392 << " int32 " << sbuf_sizes[IDX_NB_INT32]
393 << " int16 " << sbuf_sizes[IDX_NB_INT16]
394 << " byte " << sbuf_sizes[IDX_NB_BYTE]
395 << " int8 " << sbuf_sizes[IDX_NB_INT8]
396 << " float16 " << sbuf_sizes[IDX_NB_FLOAT16]
397 << " bfloat16 " << sbuf_sizes[IDX_NB_BFLOAT16]
398 << " float32 " << sbuf_sizes[IDX_NB_FLOAT32]
399 << " float128 " << sbuf_sizes[IDX_NB_FLOAT128]
400 << " int128 " << sbuf_sizes[IDX_NB_INT128]
401 << " ptr=" << (void*)bytes.data();
402 }
403
404 protected:
405
406 SizeInfo getPaddingSize(Int64 nb_elem, Int64 elem_size)
407 {
408 if (nb_elem < 0)
409 ARCCORE_FATAL("Bad number of element '{0}' (should be >=0)", nb_elem);
410 if (elem_size <= 0)
411 ARCCORE_FATAL("Bad elem_size '{0}'", elem_size);
412 Int64 s = nb_elem * elem_size;
413 Int64 pad = s % ALIGN_SIZE;
414 if (pad == 0)
415 pad = ALIGN_SIZE;
416 Int64 new_size = s + (ALIGN_SIZE - pad);
417 if ((new_size % ALIGN_SIZE) != 0)
418 ARCCORE_FATAL("Bad padding {0}", new_size);
419 //std::cout << " nb_elem=" << nb_elem << " elem_size=" << elem_size << " s=" << s << " new_size=" << new_size << '\n';
420 return { s, new_size };
421 }
422
429 void _fillPadding(Int64 position, SizeInfo size_info)
430 {
431 Int64 begin = position + size_info.m_original_size;
432 Int64 end = position + size_info.m_padded_size;
433 _fillPadding(m_buffer_view.subspan(begin, end - begin));
434 }
435
436 void _fillPadding(Span<Byte> buf)
437 {
438 // Uses a non-zero value to easily locate
439 // padding zones if needed.
440 constexpr Byte v = (Byte)(250);
441 for (Int64 i = 0, s = buf.size(); i < s; ++i)
442 buf[i] = v;
443 }
444
445 void _checkAlignment()
446 {
447 _checkAddr(m_real_view.data());
448 _checkAddr(m_int16_view.data());
449 _checkAddr(m_int32_view.data());
450 _checkAddr(m_int64_view.data());
451 _checkAddr(m_byte_view.data());
452 _checkAddr(m_int8_view.data());
453 _checkAddr(m_float16_view.data());
454 _checkAddr(m_bfloat16_view.data());
455 _checkAddr(m_float32_view.data());
456 _checkAddr(m_float128_view.data());
457 _checkAddr(m_int128_view.data());
458 }
459
460 void _checkAddr(void* ptr)
461 {
462 Int64 addr = (Int64)ptr;
463 if ((addr % ALIGN_SIZE) != 0) {
464 _printAlignment();
465 ARCCORE_FATAL("Bad alignment addr={0} - {1}", addr, (addr % ALIGN_SIZE));
466 }
467 }
468
469 void _printAlignment()
470 {
471 for (Integer i = 0, n = m_sizes_view.size(); i < n; ++i)
472 std::cout << " Size i=" << i << " v=" << m_sizes_view[i] << " pad=" << (m_sizes_view[i] % ALIGN_SIZE) << '\n';
473 _printAddr(m_buffer_view.data(), "Buffer");
474 _printAddr(m_real_view.data(), "Real");
475 _printAddr(m_int16_view.data(), "Int16");
476 _printAddr(m_int32_view.data(), "Int32");
477 _printAddr(m_int64_view.data(), "Int64");
478 _printAddr(m_byte_view.data(), "Byte");
479 _printAddr(m_int8_view.data(), "Int8");
480 _printAddr(m_float16_view.data(), "Float16");
481 _printAddr(m_bfloat16_view.data(), "BFloat16");
482 _printAddr(m_float32_view.data(), "Float32");
483 _printAddr(m_float128_view.data(), "Float128");
484 _printAddr(m_int128_view.data(), "Int128");
485 }
486
487 void _printAddr(void* ptr, const String& name)
488 {
489 Int64 addr = (Int64)ptr;
490 std::cout << "Align type=" << name << " addr=" << addr << " offset=" << (addr % ALIGN_SIZE) << '\n';
491 }
492
493 void _allocBuffer(Int64 size)
494 {
495 if (size < 1024)
496 size = 1024;
497 m_buffer.resize(size + ALIGN_SIZE * 4);
498 Int64 addr = (Int64)(&m_buffer[0]);
499 Int64 padding = addr % ALIGN_SIZE;
500 Int64 position = 0;
501 if (padding != 0) {
502 position = ALIGN_SIZE - padding;
503 }
504 // The size must be a multiple of ALIGN_SIZE;
505 Int64 new_size = (size + ALIGN_SIZE) - (size % ALIGN_SIZE);
506 m_buffer_view = m_buffer.span().subspan(position, new_size);
507
508 // Initialize the buffer zone values
509 auto padding_view = m_buffer.span().subspan(position + size, new_size - size);
510 _fillPadding(padding_view);
511 }
512};
513
514/*---------------------------------------------------------------------------*/
515/*---------------------------------------------------------------------------*/
516
517/*---------------------------------------------------------------------------*/
518/*---------------------------------------------------------------------------*/
519
520BasicSerializer::Impl2::
521Impl2()
522: m_mode(ModeReserve)
523, m_read_mode(ReadReplace)
524, m_p(new BasicSerializerNewImpl())
525{
526}
527
528/*---------------------------------------------------------------------------*/
529/*---------------------------------------------------------------------------*/
530
531BasicSerializer::Impl2::
532~Impl2()
533{
534 delete m_p;
535}
536
537/*---------------------------------------------------------------------------*/
538/*---------------------------------------------------------------------------*/
539
540void BasicSerializer::Impl2::
541reserve(eDataType dt, Int64 n, Int64 nb_put)
542{
543 ARCCORE_ASSERT((m_mode == ModeReserve), ("Bad mode"));
544 switch (dt) {
545 case DT_Real:
546 m_real.m_reserved_size += n;
547 break;
548 case DT_Int64:
549 m_int64.m_reserved_size += n;
550 break;
551 case DT_Int32:
552 m_int32.m_reserved_size += n;
553 break;
554 case DT_Int16:
555 m_int16.m_reserved_size += n;
556 break;
557 case DT_Byte:
558 m_byte.m_reserved_size += n;
559 break;
560 case DT_Int8:
561 m_int8.m_reserved_size += n;
562 break;
563 case DT_Float16:
564 m_float16.m_reserved_size += n;
565 break;
566 case DT_BFloat16:
567 m_bfloat16.m_reserved_size += n;
568 break;
569 case DT_Float32:
570 m_float32.m_reserved_size += n;
571 break;
572 case DT_Float128:
573 m_float128.m_reserved_size += n;
574 break;
575 case DT_Int128:
576 m_int128.m_reserved_size += n;
577 break;
578 default:
579 ARCCORE_THROW(ArgumentException, "bad datatype v={0}", (int)dt);
580 }
581 if (m_is_serialize_typeinfo)
582 // For the data type.
583 m_byte.m_reserved_size += nb_put;
584}
585
586/*---------------------------------------------------------------------------*/
587/*---------------------------------------------------------------------------*/
588
589void BasicSerializer::Impl2::
590reserve(eBasicDataType bdt, Int64 n, Int64 nb_put)
591{
592 ARCCORE_ASSERT((m_mode == ModeReserve), ("Bad mode"));
593 switch (bdt) {
595 m_real.m_reserved_size += n;
596 break;
598 m_int64.m_reserved_size += n;
599 break;
601 m_int32.m_reserved_size += n;
602 break;
604 m_int16.m_reserved_size += n;
605 break;
607 m_byte.m_reserved_size += n;
608 break;
610 m_int8.m_reserved_size += n;
611 break;
613 m_float16.m_reserved_size += n;
614 break;
615 case eBasicDataType::BFloat16:
616 m_bfloat16.m_reserved_size += n;
617 break;
619 m_float32.m_reserved_size += n;
620 break;
622 m_float128.m_reserved_size += n;
623 break;
625 m_int128.m_reserved_size += n;
626 break;
627 default:
628 ARCCORE_THROW(ArgumentException, "Bad basic datatype v={0}", (int)bdt);
629 }
630 if (m_is_serialize_typeinfo)
631 // For the data type.
632 m_byte.m_reserved_size += nb_put;
633}
634
635/*---------------------------------------------------------------------------*/
636/*---------------------------------------------------------------------------*/
637
638void BasicSerializer::Impl2::
639putType(eBasicDataType t)
640{
641 if (m_is_serialize_typeinfo) {
642 Byte b = static_cast<Byte>(t);
643 m_byte.put(Span<const Byte>(&b, 1));
644 }
645}
646
647/*---------------------------------------------------------------------------*/
648/*---------------------------------------------------------------------------*/
649
650void BasicSerializer::Impl2::
651getAndCheckType(eBasicDataType expected_type)
652{
653 if (!m_is_serialize_typeinfo)
654 return;
655 Byte b = 0;
656 m_byte.get(Span<Byte>(&b, 1));
657 eBasicDataType t = static_cast<eBasicDataType>(b);
658 if (t != expected_type)
659 ARCCORE_FATAL("Bad serialized type t='{0}' int={1}' expected='{2}'", t, (int)t, expected_type);
660}
661
662/*---------------------------------------------------------------------------*/
663/*---------------------------------------------------------------------------*/
664
665void BasicSerializer::Impl2::
666allocateBuffer()
667{
668 m_p->allocateBuffer(m_real.m_reserved_size, m_int16.m_reserved_size, m_int32.m_reserved_size,
669 m_int64.m_reserved_size, m_byte.m_reserved_size, m_int8.m_reserved_size,
670 m_float16.m_reserved_size, m_bfloat16.m_reserved_size, m_float32.m_reserved_size,
671 m_float128.m_reserved_size, m_int128.m_reserved_size);
672 _setViews();
673}
674
675/*---------------------------------------------------------------------------*/
676/*---------------------------------------------------------------------------*/
677
678void BasicSerializer::Impl2::
679setFromSizes()
680{
681 m_p->setFromSizes();
682 _setViews();
683}
684
685/*---------------------------------------------------------------------------*/
686/*---------------------------------------------------------------------------*/
687
688void BasicSerializer::Impl2::
689_setViews()
690{
691 m_real.m_buffer = m_p->getRealBuffer();
692 m_real.m_current_position = 0;
693
694 m_int64.m_buffer = m_p->getInt64Buffer();
695 m_int64.m_current_position = 0;
696
697 m_int32.m_buffer = m_p->getInt32Buffer();
698 m_int32.m_current_position = 0;
699
700 m_int16.m_buffer = m_p->getInt16Buffer();
701 m_int16.m_current_position = 0;
702
703 m_byte.m_buffer = m_p->getByteBuffer();
704 m_byte.m_current_position = 0;
705
706 m_int8.m_buffer = m_p->getInt8Buffer();
707 m_int8.m_current_position = 0;
708
709 m_float16.m_buffer = m_p->getFloat16Buffer();
710 m_float16.m_current_position = 0;
711
712 m_bfloat16.m_buffer = m_p->getBFloat16Buffer();
713 m_bfloat16.m_current_position = 0;
714
715 m_float32.m_buffer = m_p->getFloat32Buffer();
716 m_float32.m_current_position = 0;
717
718 m_float128.m_buffer = m_p->getFloat128Buffer();
719 m_float128.m_current_position = 0;
720
721 m_int128.m_buffer = m_p->getInt128Buffer();
722 m_int128.m_current_position = 0;
723}
724
725/*---------------------------------------------------------------------------*/
726/*---------------------------------------------------------------------------*/
727
728void BasicSerializer::Impl2::
729allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
730 Int64 nb_int64, Int64 nb_byte)
731{
732 Int64 nb_int8 = 0;
733 Int64 nb_float16 = 0;
734 Int64 nb_bfloat16 = 0;
735 Int64 nb_float32 = 0;
736 Int64 nb_float128 = 0;
737 Int64 nb_int128 = 0;
738 allocateBuffer(nb_real, nb_int16, nb_int32, nb_int64, nb_byte, nb_int8,
739 nb_float16, nb_bfloat16, nb_float32, nb_float128, nb_int128);
740}
741
742/*---------------------------------------------------------------------------*/
743/*---------------------------------------------------------------------------*/
744
745void BasicSerializer::Impl2::
746allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
747 Int64 nb_int64, Int64 nb_byte, Int64 nb_int8, Int64 nb_float16,
748 Int64 nb_bfloat16, Int64 nb_float32, Int64 nb_float128, Int64 nb_int128)
749{
750 m_real.m_reserved_size = nb_real;
751 m_int64.m_reserved_size = nb_int64;
752 m_int32.m_reserved_size = nb_int32;
753 m_int16.m_reserved_size = nb_int16;
754 m_byte.m_reserved_size = nb_byte;
755 m_int8.m_reserved_size = nb_int8;
756 m_float16.m_reserved_size = nb_float16;
757 m_bfloat16.m_reserved_size = nb_bfloat16;
758 m_float32.m_reserved_size = nb_float32;
759 m_float128.m_reserved_size = nb_float128;
760 m_int128.m_reserved_size = nb_int128;
762}
763
764/*---------------------------------------------------------------------------*/
765/*---------------------------------------------------------------------------*/
766
767void BasicSerializer::Impl2::
768copy(const BasicSerializer& rhs)
769{
770 auto rhs_p = rhs._p();
771 Span<Real> real_b = rhs_p->getRealBuffer();
772 Span<Int64> int64_b = rhs_p->getInt64Buffer();
773 Span<Int32> int32_b = rhs_p->getInt32Buffer();
774 Span<Int16> int16_b = rhs_p->getInt16Buffer();
775 Span<Byte> byte_b = rhs_p->getByteBuffer();
776 Span<Int8> int8_b = rhs_p->getInt8Buffer();
777 Span<Float16> float16_b = rhs_p->getFloat16Buffer();
778 Span<BFloat16> bfloat16_b = rhs_p->getBFloat16Buffer();
779 Span<Float32> float32_b = rhs_p->getFloat32Buffer();
780 Span<Float128> float128_b = rhs_p->getFloat128Buffer();
781 Span<Int128> int128_b = rhs_p->getInt128Buffer();
782 allocateBuffer(real_b.size(), int16_b.size(), int32_b.size(), int64_b.size(), byte_b.size(),
783 int8_b.size(), float16_b.size(), bfloat16_b.size(),
784 float32_b.size(), float128_b.size(), int128_b.size());
785 m_p->copy(rhs_p);
786}
787
788/*---------------------------------------------------------------------------*/
789/*---------------------------------------------------------------------------*/
790
791void BasicSerializer::Impl2::
792setMode(eMode new_mode)
793{
794 if (new_mode == BasicSerializer::ModeGet && new_mode != m_mode) {
795 m_real.m_current_position = 0;
796 m_int64.m_current_position = 0;
797 m_int32.m_current_position = 0;
798 m_int16.m_current_position = 0;
799 m_byte.m_current_position = 0;
800 m_int8.m_current_position = 0;
801 m_float16.m_current_position = 0;
802 m_bfloat16.m_current_position = 0;
803 m_float32.m_current_position = 0;
804 m_float128.m_current_position = 0;
805 m_int128.m_current_position = 0;
806 }
807
808 m_mode = new_mode;
809}
810
811/*---------------------------------------------------------------------------*/
812/*---------------------------------------------------------------------------*/
813
814/*---------------------------------------------------------------------------*/
815/*---------------------------------------------------------------------------*/
816
817BasicSerializer::
818BasicSerializer()
819: m_p2(new Impl2())
820{
821}
822
823/*---------------------------------------------------------------------------*/
824/*---------------------------------------------------------------------------*/
825
826BasicSerializer::
827BasicSerializer(const BasicSerializer& sb)
828: m_p2(new Impl2())
829{
830 copy(sb);
831}
832
833/*---------------------------------------------------------------------------*/
834/*---------------------------------------------------------------------------*/
835
836BasicSerializer::
837~BasicSerializer()
838{
839 delete m_p2;
840}
841
842/*---------------------------------------------------------------------------*/
843/*---------------------------------------------------------------------------*/
844
845BasicSerializer::Impl* BasicSerializer::
846_p() const
847{
848 return m_p2->m_p;
849}
850
851/*---------------------------------------------------------------------------*/
852/*---------------------------------------------------------------------------*/
853
854// TODO: make these methods private.
855Span<Real> BasicSerializer::realBuffer()
856{
857 return m_p2->m_real.m_buffer;
858}
859Span<Int64> BasicSerializer::int64Buffer()
860{
861 return m_p2->m_int64.m_buffer;
862}
863Span<Int32> BasicSerializer::int32Buffer()
864{
865 return m_p2->m_int32.m_buffer;
866}
867Span<Int16> BasicSerializer::int16Buffer()
868{
869 return m_p2->m_int16.m_buffer;
870}
871Span<Byte> BasicSerializer::byteBuffer()
872{
873 return m_p2->m_byte.m_buffer;
874}
875
876/*---------------------------------------------------------------------------*/
877/*---------------------------------------------------------------------------*/
878
881{
882 m_p2->reserve(dt, n, 1);
883}
884
885/*---------------------------------------------------------------------------*/
886/*---------------------------------------------------------------------------*/
887
890{
891 m_p2->reserve(dt, n, 1);
892}
893
894/*---------------------------------------------------------------------------*/
895/*---------------------------------------------------------------------------*/
896
899{
900 m_p2->reserve(dt, n, n);
901}
902
903/*---------------------------------------------------------------------------*/
904/*---------------------------------------------------------------------------*/
905
908{
909 m_p2->reserve(dt, n, n);
910}
911
912/*---------------------------------------------------------------------------*/
913/*---------------------------------------------------------------------------*/
914
916reserve(const String& str)
917{
918 reserveInt64(1);
919 reserveSpan(str.bytes());
920}
921
922/*---------------------------------------------------------------------------*/
923/*---------------------------------------------------------------------------*/
924
927{
928 reserveInt64(1);
929 reserveSpan(values);
930}
931
934{
935 reserveInt64(1);
936 reserveSpan(values);
937}
938
941{
942 reserveInt64(1);
943 reserveSpan(values);
944}
945
948{
949 reserveInt64(1);
950 reserveSpan(values);
951}
952
955{
956 reserveInt64(1);
957 reserveSpan(values);
958}
959
962{
963 reserveInt64(1);
964 reserveSpan(values);
965}
966
969{
970 reserveInt64(1);
971 reserveSpan(values);
972}
973
976{
977 reserveInt64(1);
978 reserveSpan(values);
979}
980
983{
984 reserveInt64(1);
985 reserveSpan(values);
986}
987
990{
991 reserveInt64(1);
992 reserveSpan(values);
993}
994
997{
998 reserveInt64(1);
999 reserveSpan(values);
1000}
1001
1002/*---------------------------------------------------------------------------*/
1003/*---------------------------------------------------------------------------*/
1004
1006put(Span<const Real> values)
1007{
1008 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1009 m_p2->putType(eBasicDataType::Float64);
1010 m_p2->m_real.put(values);
1011}
1012
1013/*---------------------------------------------------------------------------*/
1014/*---------------------------------------------------------------------------*/
1015
1017put(Span<const Int64> values)
1018{
1019 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1020 m_p2->putType(eBasicDataType::Int64);
1021 m_p2->m_int64.put(values);
1022}
1023
1024/*---------------------------------------------------------------------------*/
1025/*---------------------------------------------------------------------------*/
1026
1028put(Span<const Int32> values)
1029{
1030 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1031 m_p2->putType(eBasicDataType::Int32);
1032 m_p2->m_int32.put(values);
1033}
1034
1035/*---------------------------------------------------------------------------*/
1036/*---------------------------------------------------------------------------*/
1037
1039put(Span<const Int16> values)
1040{
1041 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1042 m_p2->putType(eBasicDataType::Int16);
1043 m_p2->m_int16.put(values);
1044}
1045
1046/*---------------------------------------------------------------------------*/
1047/*---------------------------------------------------------------------------*/
1048
1050put(Span<const Byte> values)
1051{
1052 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1053 m_p2->putType(eBasicDataType::Byte);
1054 m_p2->m_byte.put(values);
1055}
1056
1057/*---------------------------------------------------------------------------*/
1058/*---------------------------------------------------------------------------*/
1059
1062{
1063 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1064 m_p2->putType(eBasicDataType::Int8);
1065 m_p2->m_int8.put(values);
1066}
1067
1068/*---------------------------------------------------------------------------*/
1069/*---------------------------------------------------------------------------*/
1070
1073{
1074 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1075 m_p2->putType(eBasicDataType::Float16);
1076 m_p2->m_float16.put(values);
1077}
1078
1079/*---------------------------------------------------------------------------*/
1080/*---------------------------------------------------------------------------*/
1081
1084{
1085 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1086 m_p2->putType(eBasicDataType::BFloat16);
1087 m_p2->m_bfloat16.put(values);
1088}
1089
1090/*---------------------------------------------------------------------------*/
1091/*---------------------------------------------------------------------------*/
1092
1095{
1096 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1097 m_p2->putType(eBasicDataType::Float32);
1098 m_p2->m_float32.put(values);
1099}
1100
1101/*---------------------------------------------------------------------------*/
1102/*---------------------------------------------------------------------------*/
1103
1106{
1107 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1108 m_p2->putType(eBasicDataType::Float128);
1109 m_p2->m_float128.put(values);
1110}
1111
1112/*---------------------------------------------------------------------------*/
1113/*---------------------------------------------------------------------------*/
1114
1117{
1118 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1119 m_p2->putType(eBasicDataType::Int128);
1120 m_p2->m_int128.put(values);
1121}
1122
1123/*---------------------------------------------------------------------------*/
1124/*---------------------------------------------------------------------------*/
1125
1127put(const String& str)
1128{
1129 ARCCORE_ASSERT((m_p2->m_mode == ModePut), ("Bad mode"));
1130 Int64 len = str.length();
1131 putInt64(len);
1132 putSpan(str.bytes());
1133}
1134
1135/*---------------------------------------------------------------------------*/
1136/*---------------------------------------------------------------------------*/
1137
1140{
1141 putInt64(values.size());
1142 putSpan(values);
1143}
1144
1147{
1148 putInt64(values.size());
1149 putSpan(values);
1150}
1151
1154{
1155 putInt64(values.size());
1156 putSpan(values);
1157}
1158
1161{
1162 putInt64(values.size());
1163 putSpan(values);
1164}
1165
1168{
1169 putInt64(values.size());
1170 putSpan(values);
1171}
1172
1175{
1176 putInt64(values.size());
1177 putSpan(values);
1178}
1179
1182{
1183 putInt64(values.size());
1184 putSpan(values);
1185}
1186
1189{
1190 putInt64(values.size());
1191 putSpan(values);
1192}
1193
1196{
1197 putInt64(values.size());
1198 putSpan(values);
1199}
1200
1203{
1204 putInt64(values.size());
1205 putSpan(values);
1206}
1207
1210{
1211 putInt64(values.size());
1212 putSpan(values);
1213}
1214
1215/*---------------------------------------------------------------------------*/
1216/*---------------------------------------------------------------------------*/
1217
1219getSpan(Span<Real> values)
1220{
1221 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1222 m_p2->getAndCheckType(eBasicDataType::Float64);
1223 m_p2->m_real.get(values);
1224}
1225
1226/*---------------------------------------------------------------------------*/
1227/*---------------------------------------------------------------------------*/
1228
1230getSpan(Span<Int64> values)
1231{
1232 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1233 m_p2->getAndCheckType(eBasicDataType::Int64);
1234 m_p2->m_int64.get(values);
1235}
1236
1237/*---------------------------------------------------------------------------*/
1238/*---------------------------------------------------------------------------*/
1239
1241getSpan(Span<Int32> values)
1242{
1243 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1244 m_p2->getAndCheckType(eBasicDataType::Int32);
1245 m_p2->m_int32.get(values);
1246}
1247
1248/*---------------------------------------------------------------------------*/
1249/*---------------------------------------------------------------------------*/
1250
1252getSpan(Span<Int16> values)
1253{
1254 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1255 m_p2->getAndCheckType(eBasicDataType::Int16);
1256 m_p2->m_int16.get(values);
1257}
1258
1259/*---------------------------------------------------------------------------*/
1260/*---------------------------------------------------------------------------*/
1261
1263getSpan(Span<Byte> values)
1264{
1265 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1266 m_p2->getAndCheckType(eBasicDataType::Byte);
1267 m_p2->m_byte.get(values);
1268}
1269
1270/*---------------------------------------------------------------------------*/
1271/*---------------------------------------------------------------------------*/
1272
1274getSpan(Span<Int8> values)
1275{
1276 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1277 m_p2->getAndCheckType(eBasicDataType::Int8);
1278 m_p2->m_int8.get(values);
1279}
1280
1281/*---------------------------------------------------------------------------*/
1282/*---------------------------------------------------------------------------*/
1283
1285getSpan(Span<Float16> values)
1286{
1287 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1288 m_p2->getAndCheckType(eBasicDataType::Float16);
1289 m_p2->m_float16.get(values);
1290}
1291
1292/*---------------------------------------------------------------------------*/
1293/*---------------------------------------------------------------------------*/
1294
1296getSpan(Span<BFloat16> values)
1297{
1298 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1299 m_p2->getAndCheckType(eBasicDataType::BFloat16);
1300 m_p2->m_bfloat16.get(values);
1301}
1302
1303/*---------------------------------------------------------------------------*/
1304/*---------------------------------------------------------------------------*/
1305
1307getSpan(Span<Float32> values)
1308{
1309 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1310 m_p2->getAndCheckType(eBasicDataType::Float32);
1311 m_p2->m_float32.get(values);
1312}
1313
1314/*---------------------------------------------------------------------------*/
1315/*---------------------------------------------------------------------------*/
1316
1318getSpan(Span<Float128> values)
1319{
1320 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1321 m_p2->getAndCheckType(eBasicDataType::Float128);
1322 m_p2->m_float128.get(values);
1323}
1324
1325/*---------------------------------------------------------------------------*/
1326/*---------------------------------------------------------------------------*/
1327
1329getSpan(Span<Int128> values)
1330{
1331 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1332 m_p2->getAndCheckType(eBasicDataType::Int128);
1333 m_p2->m_int128.get(values);
1334}
1335
1336/*---------------------------------------------------------------------------*/
1337/*---------------------------------------------------------------------------*/
1338
1340getArray(Array<Real>& values)
1341{
1342 values.resize(getInt64());
1343 getSpan(values);
1344}
1345
1347getArray(Array<Int16>& values)
1348{
1349 values.resize(getInt64());
1350 getSpan(values);
1351}
1352
1354getArray(Array<Int32>& values)
1355{
1356 values.resize(getInt64());
1357 getSpan(values);
1358}
1359
1361getArray(Array<Int64>& values)
1362{
1363 values.resize(getInt64());
1364 getSpan(values);
1365}
1366
1368getArray(Array<Byte>& values)
1369{
1370 values.resize(getInt64());
1371 getSpan(values);
1372}
1373
1375getArray(Array<Int8>& values)
1376{
1377 values.resize(getInt64());
1378 getSpan(values);
1379}
1380
1382getArray(Array<Float16>& values)
1383{
1384 values.resize(getInt64());
1385 getSpan(values);
1386}
1387
1390{
1391 values.resize(getInt64());
1392 getSpan(values);
1393}
1394
1396getArray(Array<Float32>& values)
1397{
1398 values.resize(getInt64());
1399 getSpan(values);
1400}
1401
1404{
1405 values.resize(getInt64());
1406 getSpan(values);
1407}
1408
1410getArray(Array<Int128>& values)
1411{
1412 values.resize(getInt64());
1413 getSpan(values);
1414}
1415
1416/*---------------------------------------------------------------------------*/
1417/*---------------------------------------------------------------------------*/
1418
1420get(String& str)
1421{
1422 // TODO: it would be necessary to use Int64 but this breaks compatibility.
1423 // To be studied.
1424 ARCCORE_ASSERT((m_p2->m_mode == ModeGet), ("Bad mode"));
1425 Int64 len = getInt64();
1426 UniqueArray<Byte> bytes(len);
1427 getSpan(bytes);
1428 str = String(bytes.span());
1429}
1430
1431/*---------------------------------------------------------------------------*/
1432/*---------------------------------------------------------------------------*/
1433
1436{
1437 m_p2->allocateBuffer();
1438}
1439
1440/*---------------------------------------------------------------------------*/
1441/*---------------------------------------------------------------------------*/
1442
1444mode() const
1445{
1446 return m_p2->m_mode;
1447}
1448
1449/*---------------------------------------------------------------------------*/
1450/*---------------------------------------------------------------------------*/
1451
1453setMode(eMode new_mode)
1454{
1455 m_p2->setMode(new_mode);
1456}
1457
1458/*---------------------------------------------------------------------------*/
1459/*---------------------------------------------------------------------------*/
1460
1462readMode() const
1463{
1464 return m_p2->m_read_mode;
1465}
1466
1467/*---------------------------------------------------------------------------*/
1468/*---------------------------------------------------------------------------*/
1469
1471setReadMode(eReadMode new_read_mode)
1472{
1473 m_p2->m_read_mode = new_read_mode;
1474}
1475
1476/*---------------------------------------------------------------------------*/
1477/*---------------------------------------------------------------------------*/
1478
1480allocateBuffer(Int64 nb_real, Int64 nb_int16, Int64 nb_int32,
1481 Int64 nb_int64, Int64 nb_byte)
1482{
1483 m_p2->allocateBuffer(nb_real, nb_int16, nb_int32, nb_int64, nb_byte, 0, 0, 0, 0, 0, 0);
1484}
1485
1486/*---------------------------------------------------------------------------*/
1487/*---------------------------------------------------------------------------*/
1488
1490copy(const BasicSerializer& rhs)
1491{
1492 m_p2->copy(rhs);
1493}
1494
1495/*---------------------------------------------------------------------------*/
1496/*---------------------------------------------------------------------------*/
1497
1499copy(const ISerializer* from)
1500{
1501 auto sbuf = dynamic_cast<const BasicSerializer*>(from);
1502 if (!sbuf)
1503 ARCCORE_FATAL("Can only copy from 'BasicSerializer'");
1504 copy(*sbuf);
1505}
1506
1507/*---------------------------------------------------------------------------*/
1508/*---------------------------------------------------------------------------*/
1509
1510Span<Byte> BasicSerializer::
1511globalBuffer()
1512{
1513 return _p()->globalBuffer();
1514}
1515
1516/*---------------------------------------------------------------------------*/
1517/*---------------------------------------------------------------------------*/
1518
1519Span<const Byte> BasicSerializer::
1520globalBuffer() const
1521{
1522 return _p()->globalBuffer();
1523}
1524
1525/*---------------------------------------------------------------------------*/
1526/*---------------------------------------------------------------------------*/
1527
1528ConstArrayView<Byte> BasicSerializer::
1529copyAndGetSizesBuffer()
1530{
1531 return _p()->copyAndGetSizesBuffer();
1532}
1533
1534/*---------------------------------------------------------------------------*/
1535/*---------------------------------------------------------------------------*/
1536
1537ConstArrayView<Int64> BasicSerializer::
1538sizesBuffer()
1539{
1540 return _p()->sizesBuffer();
1541}
1542
1543/*---------------------------------------------------------------------------*/
1544/*---------------------------------------------------------------------------*/
1545
1546void BasicSerializer::
1547preallocate(Int64 size)
1548{
1549 return _p()->preallocate(size);
1550}
1551
1552/*---------------------------------------------------------------------------*/
1553/*---------------------------------------------------------------------------*/
1554
1555void BasicSerializer::
1556releaseBuffer()
1557{
1558 _p()->releaseBuffer();
1559}
1560
1561/*---------------------------------------------------------------------------*/
1562/*---------------------------------------------------------------------------*/
1563
1564void BasicSerializer::
1565setFromSizes()
1566{
1567 m_p2->setFromSizes();
1568}
1569
1570/*---------------------------------------------------------------------------*/
1571/*---------------------------------------------------------------------------*/
1572
1573Int64 BasicSerializer::
1574totalSize() const
1575{
1576 return _p()->totalSize();
1577}
1578
1579/*---------------------------------------------------------------------------*/
1580/*---------------------------------------------------------------------------*/
1581
1582void BasicSerializer::
1583printSizes(std::ostream& o) const
1584{
1585 _p()->printSizes(o);
1586}
1587
1588/*---------------------------------------------------------------------------*/
1589/*---------------------------------------------------------------------------*/
1590
1593{
1594 Int64 nb_byte = buf.size();
1596 preallocate(nb_byte);
1597 globalBuffer().copy(buf);
1598 setFromSizes();
1599}
1600
1601/*---------------------------------------------------------------------------*/
1602/*---------------------------------------------------------------------------*/
1603
1606{
1607 m_p2->setSerializeTypeInfo(v);
1608}
1609
1610/*---------------------------------------------------------------------------*/
1611/*---------------------------------------------------------------------------*/
1612
1613bool BasicSerializer::
1614isSerializeTypeInfo() const
1615{
1616 return m_p2->isSerializeTypeInfo();
1617}
1618
1619/*---------------------------------------------------------------------------*/
1620/*---------------------------------------------------------------------------*/
1621
1622} // namespace Arcane
1623
1624/*---------------------------------------------------------------------------*/
1625/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
#define ARCCORE_THROW(exception_class,...)
Macro to throw an exception with formatting.
Modifiable view of an array of type T.
Base class for 1D data vectors.
void resize(Int64 s)
Changes the number of elements in the array to s.
Implementation of a contiguous in-memory serialization buffer.
UniqueArray< Byte > m_buffer
Array containing the serialized data.
ArrayView< Int64 > m_sizes_view
View for sizes (must be a multiple of ALIGN_SIZE);.
Span< Int16 > m_int16_view
View on 16-bit integers.
Span< Float16 > m_float16_view
View on Float16.
static constexpr int IDX_TAG
Index of the tag to identify that it is a serialization.
Span< BFloat16 > m_bfloat16_view
View on BFloat16.
Span< Int32 > m_int32_view
View on 32-bit integers.
Int64 m_size_copy_buffer[NB_SIZE_ELEM]
Copy of sizes used for multi-part sending.
Span< Real > m_real_view
View on reals;.
Span< Int128 > m_int128_view
View on Int128.
static constexpr int IDX_VERSION
Serialization version.
static constexpr Int64 SERIALIZE_TAG
Tag identifying the serialization.
Span< Int8 > m_int8_view
View on Int8.
Span< Byte > m_byte_view
View on bytes.
Span< Float128 > m_float128_view
View on Float128.
void _fillPadding(Int64 position, SizeInfo size_info)
Fills the padding zones corresponding to the padding with a fixed value. This prevents having uniniti...
Span< Int64 > m_int64_view
View on 64-bit integers.
static constexpr int IDX_TOTAL_SIZE
Position of the field indicating the total size of the serialization.
Span< Byte > m_buffer_view
View aligned to ALIGN_SIZE of m_buffer.
static constexpr int IDX_RESERVED1
Field reserved for additional information (e.g., compression).
Span< Float32 > m_float32_view
View on Float32.
Basic implementation of 'ISerializer'.
void getArray(Array< Real > &values) override
Resize and fill values.
void initFromBuffer(Span< const Byte > buf)
Initializes the serializer for reading from the data buf.
void putSpan(Span< const Int8 > values) override
Add the array values.
void setReadMode(eReadMode new_read_mode) override
Sets the read mode.
void setSerializeTypeInfo(bool v)
Indicates whether to serialize the data type to ensure consistency.
void putInt64(Int64 value) override
Add the integer value.
void getSpan(Span< Real > values) override
Retrieve the array values.
static ARCCORE_CONSTEXPR Integer paddingSize()
Padding and alignment size.
eReadMode readMode() const override
Read mode.
void copy(const ISerializer *from) override
Copies the data from from into this instance.
void put(Span< const Real > values) override
Add the array values.
void reserve(eBasicDataType dt, Int64 n) override
Reserves memory for n objects of type dt.
void allocateBuffer() override
Allocates the serializer memory.
void reserveSpan(eDataType dt, Int64 n) override
Reserves memory for n values of dt.
void reserveArray(Span< const Real > values) override
Reserve to save the number of elements and the values elements.
Int64 getInt64() override
Retrieve a size.
void putArray(Span< const Real > values) override
Save the number of elements and the values elements.
void get(ArrayView< Real > values) override
Retrieve the array values.
void setMode(eMode new_mode) override
Sets the current mode.
eMode mode() const override
Current operating mode.
Constant view of an array of type T.
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
Int64 length() const
Returns the length of the string.
Definition String.cc:340
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
1D data vector with value semantics (STL style).
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
-- 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.
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.
Information about the allocated size with and without padding.