Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
TestSerialize.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#include <gtest/gtest.h>
8
9#include "arccore/serialize/BasicSerializer.h"
10
11#include "arccore/base/Ref.h"
12#include "arccore/base/FatalErrorException.h"
13#include "arccore/base/ValueFiller.h"
15#include "arccore/base/Float128.h"
16#include "arccore/base/Int128.h"
17#include "arccore/common/NumArray.h"
18#include "arccore/collections/Array2.h"
19#include "arccore/serialize/SerializerExtension.h"
20
21using namespace Arccore;
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
27{
28 public:
29
30 virtual ~ISerializeValue() = default;
31
32 virtual void serialize(ISerializer* s) = 0;
33 virtual void checkValid() = 0;
34};
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39namespace
40{
41using Float64 = Real;
42template <typename T> class ValueTraits;
43#define VALUE_TRAITS(type_name, basic_type_name) \
44 template <> class ValueTraits<type_name> \
45 { \
46 public: \
47\
48 static eBasicDataType dataType() { return eBasicDataType::basic_type_name; } \
49 static void getValue(ISerializer* s, type_name& v) { v = s->get##type_name(); } \
50 };
51
52#define VALUE_TRAITS2(type_name) VALUE_TRAITS(type_name, type_name)
53
54VALUE_TRAITS2(Byte);
55VALUE_TRAITS2(Int8);
56VALUE_TRAITS2(Int16);
57VALUE_TRAITS2(Int32)
58VALUE_TRAITS2(Int64);
59VALUE_TRAITS2(Float16);
60VALUE_TRAITS2(Float32);
61VALUE_TRAITS2(BFloat16);
62VALUE_TRAITS(Real, Float64);
63VALUE_TRAITS2(Float128);
64VALUE_TRAITS2(Int128);
65
66} // namespace
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71template <typename DataType>
72class SerializeValue
73: public ISerializeValue
74{
75 using ValueTraitsType = ValueTraits<DataType>;
76
77 public:
78
79 SerializeValue()
80 : m_data_type(ValueTraitsType::dataType())
81 {}
82
83 public:
84
85 void serialize(ISerializer* s) override
86 {
87 Int64 size = m_array_values.size();
88 switch (s->mode()) {
89 case ISerializer::ModeReserve:
90 std::cout << "ReserveArray type=" << m_data_type << " size=" << m_array_values.size() << "\n";
91 s->reserveArray(m_array_values);
92 if (size > 0)
93 s->reserve(m_data_type, 1);
94 break;
96 std::cout << "PutArray type=" << m_data_type << " size=" << m_array_values.size() << "\n";
97 s->putArray(m_array_values);
98 if (size > 0)
99 s->put(m_array_values[0]);
100 break;
102 s->getArray(m_result_array_values);
103 if (size > 0)
104 ValueTraitsType::getValue(s, m_unique_value);
105 }
106 }
107
108 void checkValid() override
109 {
110 std::cout << "ref_size=" << m_array_values.size()
111 << " result_size=" << m_result_array_values.size() << "\n";
112 ASSERT_EQ(m_array_values, m_result_array_values);
113 ASSERT_EQ(m_array_values[0], m_unique_value);
114 }
115
116 void resizeAndFill(Int32 size)
117 {
118 m_array_values.resize(size);
119 ValueFiller::fillRandom(542, m_array_values.span());
120 }
121
122 public:
123
124 UniqueArray<DataType> m_array_values;
125 UniqueArray<DataType> m_result_array_values;
126 DataType m_unique_value = {};
128};
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133class StringSerializeValue
134: public ISerializeValue
135{
136 public:
137
138 explicit StringSerializeValue(const String& v)
139 {
140 m_ref_string.add(v);
141 }
142 explicit StringSerializeValue(Span<String> v)
143 {
144 m_ref_string.addRange(v);
145 }
146
147 public:
148
149 void serialize(ISerializer* s) override
150 {
151 const Arcane::SerializerExtension se(s);
152 Int64 size = m_ref_string.size();
153 switch (s->mode()) {
154 case ISerializer::ModeReserve:
155 std::cout << "ReserveArray type=String size=" << m_ref_string.size() << "\n";
156 se.reserveArray(m_ref_string);
157 if (size > 0)
158 s->reserve(m_ref_string[0]);
159 break;
161 std::cout << "PutArray type=String size=" << m_ref_string.size() << "\n";
162 se.putArray(m_ref_string);
163 if (size > 0)
164 s->put(m_ref_string[0]);
165 break;
167 se.getArray(m_result_string);
168 if (size > 0)
169 s->get(m_unique_value);
170 }
171 }
172
173 void checkValid() override
174 {
175 std::cout << "ref_size=" << m_ref_string.size()
176 << " result_size=" << m_result_string.size() << "\n";
177 for (Integer i = 0; i < m_ref_string.size(); ++i) {
178 // std::cout << m_ref_string[i] << " == " << m_result_string[i] << std::endl;
179 ASSERT_EQ(m_ref_string[i], m_result_string[i]);
180 }
181 if (!m_ref_string.empty()) {
182 // std::cout << m_ref_string[0] << " == " << m_unique_value << std::endl;
183 ASSERT_EQ(m_ref_string[0], m_unique_value);
184 }
185 }
186
187 public:
188
189 UniqueArray<String> m_ref_string;
190 UniqueArray<String> m_result_string;
191 String m_unique_value;
192};
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197template <typename DataType>
198class Serialize2DValue
199: public ISerializeValue
200{
201 using ValueTraitsType = ValueTraits<DataType>;
202
203public:
204
205 Serialize2DValue()
206 : m_data_type(ValueTraitsType::dataType())
207 {}
208
209public:
210
211 void serialize(ISerializer* s) override
212 {
213 const Arcane::SerializerExtension se(s);
214 switch (s->mode()) {
215 case ISerializer::ModeReserve:
216 std::cout << "ReserveArray type=" << m_data_type << " size1=" << m_array_values.dim1Size() << " size2=" << m_array_values.dim2Size() << "\n";
217 se.reserveArray(m_array_values.constSpan());
218 break;
220 std::cout << "PutArray type=" << m_data_type << " size1=" << m_array_values.dim1Size() << " size2=" << m_array_values.dim2Size() << "\n";
221 se.putArray(m_array_values.constSpan());
222 break;
224 se.getArray(m_result_array_values);
225 }
226 }
227
228 void checkValid() override
229 {
230 std::cout << "ref_size1=" << m_array_values.dim1Size()
231 << " ref_size2=" << m_array_values.dim2Size()
232 << " result_size1=" << m_result_array_values.dim1Size()
233 << " result_size2=" << m_result_array_values.dim2Size() << "\n";
234 for (Int32 i = 0; i < m_array_values.dim1Size(); ++i) {
235 ASSERT_EQ(m_array_values[i], m_result_array_values[i]);
236 }
237 }
238
239 void resizeAndFill(Int32 size1, Int32 size2)
240 {
241 m_array_values.resize(size1, size2);
242 ValueFiller::fillRandom(542, m_array_values.to1DSpan());
243 }
244
245public:
246
247 UniqueArray2<DataType> m_array_values;
248 UniqueArray2<DataType> m_result_array_values;
250};
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
254
255template <typename DataType, typename Extents>
256class MDSerializeValue
257: public ISerializeValue
258{
259 using ValueTraitsType = ValueTraits<DataType>;
260
261 public:
262
263 MDSerializeValue()
264 : m_data_type(ValueTraitsType::dataType())
265 {}
266
267 public:
268
269 void serialize(ISerializer* s) override
270 {
271 const Arcane::SerializerExtension se(s);
272 switch (s->mode()) {
273 case ISerializer::ModeReserve:
274 std::cout << "ReserveArray type=" << m_data_type << " dim=" << Extents::rank() << " total_size=" << m_array_values.totalNbElement() << "\n";
275 se.reserveArray(m_array_values.constSpan());
276 break;
278 std::cout << "PutArray type=" << m_data_type << " dim=" << Extents::rank() << " total_size=" << m_array_values.totalNbElement() << "\n";
279 se.putArray(m_array_values.constSpan());
280 break;
282 se.getArray(m_result_array_values);
283 }
284 }
285
286 void checkValid() override
287 {
288 std::cout << "ref_size=" << m_array_values.totalNbElement()
289 << " result_size=" << m_result_array_values.totalNbElement() << "\n";
290 //TODO : Vérifier les dimensions.
291 ASSERT_EQ(m_array_values.to1DSpan(), m_result_array_values.to1DSpan());
292 }
293
294 void resizeAndFill(Arcane::MDIndex<Extents::rank(), typename Extents::ExtentIndexType> size)
295 {
296 m_array_values.resize(size);
297 ValueFiller::fillRandom(542, m_array_values.to1DSpan());
298 }
299
300 public:
301
303 Arcane::NumArray<DataType, Extents> m_result_array_values;
305};
306
307/*---------------------------------------------------------------------------*/
308/*---------------------------------------------------------------------------*/
309
311{
312 public:
313
315 {
316 for (ISerializeValue* v : m_values)
317 delete v;
318 }
319 void doSerialize(ISerializer* s)
320 {
321 for (ISerializeValue* v : m_values)
322 v->serialize(s);
323 }
324 void checkValid()
325 {
326 for (ISerializeValue* v : m_values)
327 v->checkValid();
328 }
329
330 template <typename DataType>
331 void add(Int32 size)
332 {
333 auto* sval = new SerializeValue<DataType>();
334 sval->resizeAndFill(size);
335 m_values.add(sval);
336 }
337 void addString(const String& v)
338 {
339 m_values.add(new StringSerializeValue(v));
340 }
341
342 void addString(Span<String> v)
343 {
344 m_values.add(new StringSerializeValue(v));
345 }
346
347 template <typename DataType>
348 void add(Int32 size1, Int32 size2)
349 {
350 auto* sval = new Serialize2DValue<DataType>();
351 sval->resizeAndFill(size1, size2);
352 m_values.add(sval);
353 }
354
355 template <typename DataType, typename Extents>
356 void add(Arcane::MDIndex<Extents::rank(), typename Extents::ExtentIndexType> size)
357 {
358 auto* sval = new MDSerializeValue<DataType, Extents>();
359 sval->resizeAndFill(size);
360 m_values.add(sval);
361 }
362
363 public:
364
366};
367
368/*---------------------------------------------------------------------------*/
369/*---------------------------------------------------------------------------*/
370
371void _doMisc()
372{
373 Ref<ISerializer> serializer_ref = createSerializer();
374 ISerializer* serializer = serializer_ref.get();
375
376 SerializeValueList values;
377
378 values.add<Float16>(12679);
379 values.add<BFloat16>(3212);
380 values.add<Int8>(6357);
381 values.add<Float32>(983);
382 values.add<Int16>(16353);
383 values.add<Real>(29123);
384 values.add<Int32>(16);
385 values.add<Byte>(3289);
386 values.add<Int64>(12932);
387 values.add<Float128>(19328);
388 values.add<Int128>(32422);
389
390 values.addString("Ceci est un test de chaîne de caractères");
391 values.addString(UniqueArray<String>({ "Ceci", "est", "un", "test", "de", "chaîne", "de", "caractères" }));
392
393 values.add<Real>(29, 123);
394 values.add<Int32>(16, 23);
395
396 values.add<Float16, Arcane::MDDim1>(125);
397 values.add<BFloat16, Arcane::MDDim2>({ 51, 21 });
398 values.add<Int8, Arcane::MDDim3>({ 12, 9, 45 });
399 values.add<Float32, Arcane::MDDim4>({ 14, 45, 1, 4 });
400 values.add<Int16, Arcane::MDDim1>(15);
401 values.add<Real, Arcane::MDDim2>({ 84, 31 });
402 values.add<Int32, Arcane::MDDim3>({ 25, 19, 41 });
403 values.add<Byte, Arcane::MDDim4>({ 12, 17, 26, 4 });
404 values.add<Int64, Arcane::MDDim1>(18);
405 values.add<Float128, Arcane::MDDim2>({ 41, 48 });
406
407 serializer->setMode(ISerializer::ModeReserve);
408 values.doSerialize(serializer);
409 serializer->allocateBuffer();
410 serializer->setMode(ISerializer::ModePut);
411 values.doSerialize(serializer);
412 serializer->setMode(ISerializer::ModeGet);
413 values.doSerialize(serializer);
414
415 values.checkValid();
416}
417
418TEST(Serialize, Misc)
419{
420 try {
421 _doMisc();
422 }
423 catch (const Exception& e) {
424 e.write(std::cerr);
425 throw;
426 }
427}
File containing definitions of basic data types managed by Arccore.
Management of references to a C++ class.
virtual void allocateBuffer()=0
Allocates the serializer memory.
virtual void setMode(eMode new_mode)=0
Sets the current mode.
Multi-dimensional arrays for numerical types accessible on accelerators.
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
Class that extends the functionalities of the ISerializer.
void reserveArray(Span2< const Type > values) const
Reserve to save the number of elements and the values elements.
void putArray(Span2< const Type > values) const
Save the number of elements and the values elements.
void getArray(Array2< Type > &values) const
Resize and fill values.
View of an array of elements of type T.
Definition Span.h:633
2D data vector with value semantics (STL style).
1D data vector with value semantics (STL style).
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual void putArray(Span< const Real > values)=0
Save the number of elements and the values elements.
virtual void reserveArray(Span< const Real > values)=0
Reserve to save the number of elements and the values elements.
virtual eMode mode() const =0
Current operating mode.
virtual void getArray(Array< Real > &values)=0
Resize and fill values.
virtual void put(Span< const Real > values)=0
Add the array values.
virtual void get(ArrayView< Real > values)=0
Retrieve the array values.
Int32 Integer
Type representing an integer.
eBasicDataType
Type of a basic data item.
@ Unknown
Unknown or uninitialized data type.
ExtentsV< Int32, DynExtent, DynExtent, DynExtent, DynExtent > MDDim4
Constant for a rank 4 dynamic array.
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:42
ExtentsV< Int32, DynExtent, DynExtent > MDDim2
Constant for a rank 2 dynamic array.
ExtentsV< Int32, DynExtent > MDDim1
Constant for a rank 1 dynamic array.
ExtentsV< Int32, DynExtent, DynExtent, DynExtent > MDDim3
Constant for a rank 3 dynamic array.
Namespace of Arccore.
Arcane::Float128 Float128
Type representing a 128-bit float.
Arcane::BFloat16 BFloat16
Type 'Brain Float16'.
float Float32
IEEE-753 single precision floating point type (binary32).
Arcane::Float16 Float16
Type 'Float16' (binary16).
ARCCORE_SERIALIZE_EXPORT Ref< ISerializer > createSerializer()
Creates an instance of ISerializer.
Arcane::Int8 Int8
Type representing an 8-bit integer.
Arcane::Int128 Int128
Type representing a 128-bit integer.