Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ScalarData.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/* ScalarData.cc (C) 2000-2024 */
9/* */
10/* Scalar type data. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/utils/NotSupportedException.h"
17#include "arcane/utils/ArgumentException.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/IHashAlgorithm.h"
20#include "arcane/utils/NotImplementedException.h"
21#include "arcane/utils/Real2.h"
22#include "arcane/utils/Real3.h"
23#include "arcane/utils/Real2x2.h"
24#include "arcane/utils/Real3x3.h"
25#include "arcane/utils/ArrayShape.h"
26#include "arcane/utils/Array.h"
27#include "arcane/utils/String.h"
28#include "arcane/utils/Ref.h"
29
30#include "arcane/core/IDataFactory.h"
31#include "arcane/core/IData.h"
32#include "arcane/core/IDataVisitor.h"
33
34#include "arcane/core/datatype/DataAllocationInfo.h"
35#include "arcane/core/datatype/DataStorageTypeInfo.h"
36#include "arcane/core/datatype/DataStorageBuildInfo.h"
37#include "arcane/core/datatype/DataTypeTraits.h"
38
39#include "arcane/impl/ScalarData.h"
40#include "arcane/impl/SerializedData.h"
41#include "arcane/impl/DataStorageFactory.h"
42
43#include "arcane/core/ISerializer.h"
44#include "arcane/core/internal/IDataInternal.h"
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49namespace Arcane
50{
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
58template <class DataType>
59class ScalarDataT
61, public IScalarDataT<DataType>
62{
64
65 public:
66
67 typedef ScalarDataT<DataType> ThatClass;
68 typedef IScalarDataT<DataType> DataInterfaceType;
69
70 public:
71
72 class Internal
73 : public IDataInternal
74 {
75 public:
76
77 explicit Internal(ScalarDataT<DataType>* p)
78 : m_p(p)
79 {}
80
81 public:
82
83 void computeHash(DataHashInfo& hash_info) override
84 {
85 m_p->computeHash(hash_info);
86 }
87
88 private:
89
90 ScalarDataT<DataType>* m_p = nullptr;
91 };
92
93 public:
94
95 explicit ScalarDataT(ITraceMng* trace)
96 : m_value(DataTypeTraitsT<DataType>::defaultValue())
97 , m_trace(trace)
98 , m_internal(this)
99 {}
100 explicit ScalarDataT(const DataStorageBuildInfo& dsbi);
101 ScalarDataT(const ScalarDataT<DataType>& rhs)
102 : m_value(rhs.m_value)
103 , m_trace(rhs.m_trace)
104 , m_internal(this)
105 , m_allocation_info(rhs.m_allocation_info)
106 {}
107
108 public:
109
110 Integer dimension() const override { return 0; }
111 Integer multiTag() const override { return 0; }
113 void serialize(ISerializer* sbuf, IDataOperation* operation) override;
114 void serialize(ISerializer* sbuf, Int32ConstArrayView ids, IDataOperation* operation) override;
115 DataType& value() override { return m_value; }
116 const DataType& value() const override { return m_value; }
117 void resize(Integer) override {}
118 IData* clone() override { return _cloneTrue(); }
119 IData* cloneEmpty() override { return _cloneTrueEmpty(); };
120 Ref<IData> cloneRef() override { return makeRef(cloneTrue()); }
122 DataStorageTypeInfo storageTypeInfo() const override;
123 DataInterfaceType* cloneTrue() override { return _cloneTrue(); }
124 DataInterfaceType* cloneTrueEmpty() override { return _cloneTrueEmpty(); }
126 {
127 auto* d = _cloneTrue();
128 return makeRef(d);
129 }
131 {
132 auto* d = _cloneTrueEmpty();
133 return makeRef(d);
134 }
135 void fillDefault() override;
136 void setName(const String& name) override;
137 Ref<ISerializedData> createSerializedDataRef(bool use_basic_type) const override;
139 void assignSerializedData(const ISerializedData* sdata) override;
140 void copy(const IData* data) override;
141 void swapValues(IData* data) override;
142 void computeHash(IHashAlgorithm* algo, ByteArray& output) const override;
143 void computeHash(DataHashInfo& hash_info) const;
144 ArrayShape shape() const override;
145 void setShape(const ArrayShape&) override;
146 void setAllocationInfo(const DataAllocationInfo& v) override { m_allocation_info = v; }
147 DataAllocationInfo allocationInfo() const override { return m_allocation_info; }
148 void visit(IScalarDataVisitor* visitor) override
149 {
150 visitor->applyVisitor(this);
151 }
152 void visit(IDataVisitor* visitor) override
153 {
154 visitor->applyDataVisitor(this);
155 }
156 void visitScalar(IScalarDataVisitor* visitor) override;
157 void visitArray(IArrayDataVisitor* visitor) override;
158 void visitArray2(IArray2DataVisitor* visitor) override;
159
160 IDataInternal* _commonInternal() override { return &m_internal; }
161
162 public:
163
164 static DataStorageTypeInfo staticStorageTypeInfo();
165
166 private:
167
168 DataType m_value;
169 ITraceMng* m_trace;
170 Internal m_internal;
171 DataAllocationInfo m_allocation_info;
172
173 private:
174
175 DataInterfaceType* _cloneTrue() const { return new ThatClass(*this); }
176 DataInterfaceType* _cloneTrueEmpty() const { return new ThatClass(m_trace); }
177};
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182template <typename DataType> ScalarDataT<DataType>::
184: m_value(DataTypeTraitsT<DataType>::defaultValue())
185, m_trace(dsbi.traceMng())
186, m_internal(this)
187{
188}
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
193template <typename DataType>
196{
197 typedef DataTypeTraitsT<DataType> TraitsType;
198 eBasicDataType bdt = TraitsType::basicDataType();
199 Int32 nb_basic_type = TraitsType::nbBasicType();
200 Int32 dimension = 0;
201 Int32 multi_tag = 0;
202 return DataStorageTypeInfo(bdt, nb_basic_type, dimension, multi_tag);
203}
204
205/*---------------------------------------------------------------------------*/
206/*---------------------------------------------------------------------------*/
207
208template <typename DataType>
210storageTypeInfo() const
211{
212 return staticStorageTypeInfo();
213}
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218template <typename DataType>
220createSerializedDataRef(bool use_basic_type) const
221{
222 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
223
224 Integer nb_count = 1;
225 eDataType data_type = dataType();
226 Integer type_size = sizeof(DataType);
227
228 if (use_basic_type) {
231 type_size = sizeof(BasicType);
232 }
233
234 Integer nb_element = 1;
235 Integer nb_base_element = nb_element * nb_count;
236 Integer full_size = nb_base_element * type_size;
237 ByteConstArrayView base_values(full_size, reinterpret_cast<const Byte*>(&m_value));
238 UniqueArray<Int64> dimensions;
239 dimensions.add(nb_element);
240 auto sd = arcaneCreateSerializedDataRef(data_type, base_values.size(), 0, nb_element,
241 nb_base_element, false, dimensions);
242 sd->setConstBytes(base_values);
243 return sd;
244}
245
246/*---------------------------------------------------------------------------*/
247/*---------------------------------------------------------------------------*/
248
249template <typename DataType>
252{
253 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
254
255 eDataType data_type = sdata->baseDataType();
257
258 if (data_type != dataType() && data_type != base_data_type)
259 throw ArgumentException(A_FUNCINFO, "Bad serialized type");
260
261 Span<Byte> byte_values(reinterpret_cast<Byte*>(&m_value), sdata->memorySize());
262 sdata->setWritableBytes(byte_values);
263}
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268template <typename DataType>
271{
272 // Nothing to do because \a sdata points directly to m_value
273}
274
275/*---------------------------------------------------------------------------*/
276/*---------------------------------------------------------------------------*/
277
278template <typename DataType>
281{
283 typedef typename DataTypeTraitsT<DataType>::BasicType BasicType;
285
286 DataType ttmp = m_value;
287 ArrayView<BasicType> vtmp(1 * nb_count, reinterpret_cast<BasicType*>(&ttmp));
288
289 ISerializer::eMode mode = sbuf->mode();
290 if (mode == ISerializer::ModeReserve)
291 sbuf->reserveSpan(data_type, vtmp.size());
292 else if (mode == ISerializer::ModePut)
293 sbuf->putSpan(vtmp);
294 else if (mode == ISerializer::ModeGet)
295 sbuf->getSpan(vtmp);
296}
297
298/*---------------------------------------------------------------------------*/
299/*---------------------------------------------------------------------------*/
300
301template <typename DataType>
307
308/*---------------------------------------------------------------------------*/
309/*---------------------------------------------------------------------------*/
310
311template <typename DataType>
314{
315 m_value = DataType();
316}
317
318/*---------------------------------------------------------------------------*/
319/*---------------------------------------------------------------------------*/
320
321template <typename DataType>
323setName(const String&)
324{
325}
326
327/*---------------------------------------------------------------------------*/
328/*---------------------------------------------------------------------------*/
329
330template <typename DataType>
332computeHash(IHashAlgorithm* algo, ByteArray& output) const
333{
334 Integer type_size = sizeof(DataType);
335 const Byte* ptr = reinterpret_cast<const Byte*>(&m_value);
336 ByteConstArrayView input(type_size, ptr);
337 algo->computeHash64(input, output);
338}
339
340/*---------------------------------------------------------------------------*/
341/*---------------------------------------------------------------------------*/
342
343template <typename DataType>
345computeHash(DataHashInfo& hash_info) const
346{
347 hash_info.setVersion(2);
348 Span<const DataType> value_as_span(&m_value, 1);
349 hash_info.context()->updateHash(asBytes(value_as_span));
350}
351
352/*---------------------------------------------------------------------------*/
353/*---------------------------------------------------------------------------*/
354
355template <typename DataType>
357copy(const IData* data)
358{
359 const DataInterfaceType* true_data = dynamic_cast<const DataInterfaceType*>(data);
360 if (!true_data)
361 ARCANE_THROW(ArgumentException, "Can not cast 'IData' to 'IScalarDataT'");
362 m_value = true_data->value();
363}
364
365/*---------------------------------------------------------------------------*/
366/*---------------------------------------------------------------------------*/
367
368template <typename DataType>
370swapValues(IData* data)
371{
372 DataInterfaceType* true_data = dynamic_cast<DataInterfaceType*>(data);
373 if (!true_data)
374 ARCANE_THROW(ArgumentException, "Can not cast 'IData' to 'IScalarDataT'");
375 std::swap(m_value, true_data->value());
376}
377
378/*---------------------------------------------------------------------------*/
379/*---------------------------------------------------------------------------*/
380
381template <typename DataType>
384{
385 visitor->applyVisitor(this);
386}
387
388/*---------------------------------------------------------------------------*/
389/*---------------------------------------------------------------------------*/
390
391template <typename DataType>
394{
395 ARCANE_THROW(NotSupportedException, "Can not visit array data with scalar data");
396}
397
398/*---------------------------------------------------------------------------*/
399/*---------------------------------------------------------------------------*/
400
401template <typename DataType>
404{
405 ARCANE_THROW(NotSupportedException, "Can not visit array2 data with scalar data");
406}
407
408/*---------------------------------------------------------------------------*/
409/*---------------------------------------------------------------------------*/
410
411template <typename DataType>
413shape() const
414{
415 return {};
416}
417
418/*---------------------------------------------------------------------------*/
419/*---------------------------------------------------------------------------*/
420
421template <typename DataType>
426
427/*---------------------------------------------------------------------------*/
428/*---------------------------------------------------------------------------*/
429
430extern "C++" void
431registerScalarDataFactory(IDataFactoryMng* dfm)
432{
433 DataStorageFactory<ScalarDataT<Byte>>::registerDataFactory(dfm);
434 DataStorageFactory<ScalarDataT<Real>>::registerDataFactory(dfm);
435 DataStorageFactory<ScalarDataT<BFloat16>>::registerDataFactory(dfm);
436 DataStorageFactory<ScalarDataT<Float16>>::registerDataFactory(dfm);
437 DataStorageFactory<ScalarDataT<Float32>>::registerDataFactory(dfm);
438 DataStorageFactory<ScalarDataT<Int8>>::registerDataFactory(dfm);
439 DataStorageFactory<ScalarDataT<Int16>>::registerDataFactory(dfm);
440 DataStorageFactory<ScalarDataT<Int32>>::registerDataFactory(dfm);
441 DataStorageFactory<ScalarDataT<Int64>>::registerDataFactory(dfm);
442 DataStorageFactory<ScalarDataT<Real2>>::registerDataFactory(dfm);
443 DataStorageFactory<ScalarDataT<Real3>>::registerDataFactory(dfm);
444 DataStorageFactory<ScalarDataT<Real2x2>>::registerDataFactory(dfm);
445 DataStorageFactory<ScalarDataT<Real3x3>>::registerDataFactory(dfm);
446}
447
448/*---------------------------------------------------------------------------*/
449/*---------------------------------------------------------------------------*/
450
451ARCANE_INTERNAL_INSTANTIATE_TEMPLATE_FOR_NUMERIC_DATATYPE(ScalarDataT);
452
453/*---------------------------------------------------------------------------*/
454/*---------------------------------------------------------------------------*/
455
456} // End namespace Arcane
457
458/*---------------------------------------------------------------------------*/
459/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro to define methods managing counters of references.
Array shape.
Definition ArrayShape.h:42
Modifiable view of an array of type T.
constexpr Integer size() const noexcept
Returns the size of the array.
void add(ConstReferenceType val)
Adds element val to the end of the array.
constexpr Integer size() const noexcept
Number of elements in the array.
Information on data allocation.
Information for calculating data hash.
Information to construct an instance of 'IData'.
Data container factory.
Type information for a data container.
Interface of the visitor pattern for a 2D array data item.
Interface of the visitor pattern for an array data item.
Interface of the data factory manager.
Internal part of IData.
Interface of an operation on a data.
Interface of the visitor pattern for a data item.
Interface of a data item.
Definition IData.h:34
virtual void updateHash(Span< const std::byte > input)=0
Adds the array input to the calculated hash.
Interface of a hashing algorithm.
virtual void computeHash64(Span< const Byte > input, ByteArray &output)
Calculates the hash value for the array input.
Interface of a scalar data item of type T.
Definition IData.h:246
virtual DataType & value()=0
Data value.
Interface of the visitor pattern for a scalar data item.
Interface of a serialized data.
virtual eDataType baseDataType() const =0
Data type.
virtual Int64 memorySize() const =0
Indicates the number of bytes that must be allocated to store or read the data.
virtual void setWritableBytes(Span< Byte > bytes)=0
Positions the serialized values.
virtual void putSpan(Span< const Real > values)
Add the array values.
virtual eMode mode() const =0
Current operating mode.
virtual void getSpan(Span< Real > values)
Retrieve the array values.
virtual void reserveSpan(eBasicDataType dt, Int64 n)=0
Reserves memory for n values of dt.
Reference to an instance.
Thread-safe implementation of a reference counter.
void computeHash(DataHashInfo &hash_info) override
Calculates the hash of the data.
Definition ScalarData.cc:83
Scalar data of type T.
Definition ScalarData.cc:62
DataType & value() override
Data value.
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
void setShape(const ArrayShape &) override
Sets the array shape.
DataInterfaceType * cloneTrueEmpty() override
Clone the data but without elements.
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
void copy(const IData *data) override
Copy the data data into the current instance.
void resize(Integer) override
Resize the data.
IDataInternal * _commonInternal() override
void setName(const String &name) override
Sets the name of the data (internal).
DataInterfaceType * cloneTrue() override
Clone the data.
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
void visitArray2(IArray2DataVisitor *visitor) override
Apply the visitor to the data.
void fillDefault() override
Fills the data with its default value.
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
const DataType & value() const override
Data value.
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
DataAllocationInfo allocationInfo() const override
Allocation information.
DataType m_value
Data.
void swapValues(IData *data) override
Swap the values of data with those of the instance.
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
void visitArray(IArrayDataVisitor *visitor) override
Apply the visitor to the data.
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
eDataType dataType() const override
Data type.
Ref< IData > cloneRef() override
Clone the data.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
void visit(IScalarDataVisitor *visitor) override
Applies the visitor to the data.
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
void visitScalar(IScalarDataVisitor *visitor) override
Apply the visitor to the data.
View of an array of elements of type T.
Definition Span.h:635
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Ref< ISerializedData > arcaneCreateSerializedDataRef(eDataType data_type, Int64 memory_size, Integer nb_dim, Int64 nb_element, Int64 nb_base_element, bool is_multi_size, Int64ConstArrayView dimensions)
Creates serialized data.
Int32 Integer
Type representing an integer.
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
Definition UtilsTypes.h:121
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
eBasicDataType
Type of a basic data item.
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1032
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:476
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
eDataType
Data type.
Definition DataTypes.h:41
std::int32_t Int32
Signed integer type of 32 bits.