Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ArrayData.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/* ArrayData.h (C) 2000-2025 */
9/* */
10/* Data of type 'Array'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_IMPL_INTERNAL_ARRAYDATA_H
13#define ARCANE_IMPL_INTERNAL_ARRAYDATA_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include "arcane/utils/ArrayShape.h"
20#include "arcane/utils/String.h"
21#include "arcane/utils/IDataCompressor.h"
22#include "arcane/utils/Array.h"
23#include "arcane/utils/MemoryView.h"
24#include "arcane/utils/Ref.h"
25#include "arcane/utils/NotSupportedException.h"
26
27#include "arcane/core/IData.h"
28#include "arcane/core/IDataVisitor.h"
29#include "arcane/core/internal/IDataInternal.h"
30#include "arcane/core/datatype/DataAllocationInfo.h"
31#include "arcane/core/datatype/DataTypeTraits.h"
32#include "arcane/core/datatype/DataStorageTypeInfo.h"
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
46template <class DataType>
47class ArrayDataT
49, public IArrayDataT<DataType>
50{
52 class Impl;
53 friend class Impl;
54
55 public:
56
57 typedef ArrayDataT<DataType> ThatClass;
58 typedef IArrayDataT<DataType> DataInterfaceType;
59
60 public:
61
62 explicit ArrayDataT(ITraceMng* trace);
63 explicit ArrayDataT(const DataStorageBuildInfo& dsbi);
64 ArrayDataT(const ArrayDataT<DataType>& rhs);
65 ~ArrayDataT() override;
66
67 public:
68
69 Integer dimension() const override { return 1; }
70 Integer multiTag() const override { return 0; }
71 eDataType dataType() const override { return DataTypeTraitsT<DataType>::type(); }
72 void serialize(ISerializer* sbuf, IDataOperation* operation) override;
73 void serialize(ISerializer* sbuf, Int32ConstArrayView ids, IDataOperation* operation) override;
74 Array<DataType>& value() override { return m_value; }
75 const Array<DataType>& value() const override { return m_value; }
76 ConstArrayView<DataType> view() const override { return m_value; }
77 ArrayView<DataType> view() override { return m_value; }
78 void resize(Integer new_size) override { m_value.resize(new_size); }
79 IData* clone() override { return _cloneTrue(); }
80 IData* cloneEmpty() override { return _cloneTrueEmpty(); };
81 Ref<IData> cloneRef() override { return makeRef(cloneTrue()); }
83 DataStorageTypeInfo storageTypeInfo() const override;
84 DataInterfaceType* cloneTrue() override { return _cloneTrue(); }
85 DataInterfaceType* cloneTrueEmpty() override { return _cloneTrueEmpty(); }
87 {
88 auto* d = _cloneTrue();
89 return makeRef(d);
90 }
92 {
93 auto* d = _cloneTrueEmpty();
94 return makeRef(d);
95 }
96 void fillDefault() override;
97 void setName(const String& name) override;
98 Ref<ISerializedData> createSerializedDataRef(bool use_basic_type) const override;
100 void assignSerializedData(const ISerializedData* sdata) override;
101 void copy(const IData* data) override;
102 void swapValues(IData* data) override;
103 void computeHash(IHashAlgorithm* algo, ByteArray& output) const override;
104 void computeHash(DataHashInfo& hash_info) const;
105 ArrayShape shape() const override { return m_shape; }
106 void setShape(const ArrayShape& new_shape) override { m_shape = new_shape; }
107 void setAllocationInfo(const DataAllocationInfo& v) override;
108 DataAllocationInfo allocationInfo() const override { return m_allocation_info; }
109 void visit(IArrayDataVisitor* visitor) override
110 {
111 visitor->applyVisitor(this);
112 }
113 void visit(IDataVisitor* visitor) override
114 {
115 visitor->applyDataVisitor(this);
116 }
118 {
119 ARCANE_THROW(NotSupportedException, "Can not visit scalar data with array data");
120 }
121 void visitArray(IArrayDataVisitor* visitor) override
122 {
123 visitor->applyVisitor(this);
124 }
126 {
127 ARCANE_THROW(NotSupportedException, "Can not visit array2 data with array data");
128 }
129
130 public:
131
132 void swapValuesDirect(ThatClass* true_data);
133 void changeAllocator(const MemoryAllocationOptions& alloc_info);
134
135 public:
136
137 IArrayDataInternalT<DataType>* _internal() override { return m_internal; }
138 IDataInternal* _commonInternal() override { return m_internal; }
139
140 public:
141
142 static DataStorageTypeInfo staticStorageTypeInfo();
143
144 public:
145 private:
146
148 ITraceMng* m_trace;
150 ArrayShape m_shape;
151 DataAllocationInfo m_allocation_info;
152
153 private:
154
155 void _serialize(ISerializer* sbuf, Span<const Int32> ids, IDataOperation* operation);
156 IArrayDataT<DataType>* _cloneTrue() const { return new ThatClass(*this); }
157 IArrayDataT<DataType>* _cloneTrueEmpty() const { return new ThatClass(m_trace); }
158 void _setShape();
159};
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164template <typename DataType>
165class ArrayDataT<DataType>::Impl
166: public IArrayDataInternalT<DataType>
168{
169 public:
170
171 explicit Impl(ArrayDataT<DataType>* p)
172 : m_p(p)
173 {}
174
175 public:
176
177 void reserve(Integer new_capacity) override { m_p->m_value.reserve(new_capacity); }
178 Array<DataType>& _internalDeprecatedValue() override { return m_p->m_value; }
179 Integer capacity() const override { return m_p->m_value.capacity(); }
180 void shrink() const override { m_p->m_value.shrink(); }
181 void resize(Integer new_size) override { m_p->m_value.resize(new_size); }
182 void dispose() override { m_p->m_value.dispose(); }
184 {
185 IDataCompressor* compressor = buf.m_compressor;
186 if (!compressor)
187 return false;
188 Span<const DataType> values = m_p->m_value;
189 Span<const std::byte> bytes = asBytes(values);
190 compressor->compress(bytes, buf.m_buffer);
191 buf.m_original_dim1_size = values.size();
192 m_p->m_value.clear();
193 m_p->m_value.shrink();
194 return true;
195 }
197 {
198 IDataCompressor* compressor = buf.m_compressor;
199 if (!compressor)
200 return false;
201 m_p->m_value.resize(buf.m_original_dim1_size);
202 Span<DataType> values = m_p->m_value;
203 compressor->decompress(buf.m_buffer, asWritableBytes(values));
204 return true;
205 }
207 {
208 return makeMutableMemoryView<DataType>(m_p->view());
209 }
210 Int32 extent0() const override
211 {
212 return m_p->view().size();
213 }
214 INumericDataInternal* numericData() override { return this; }
215 void changeAllocator(const MemoryAllocationOptions& v) override { m_p->changeAllocator(v); }
216 void computeHash(DataHashInfo& hash_info) override
217 {
218 m_p->computeHash(hash_info);
219 }
220 IMemoryAllocator* memoryAllocator() const override { return m_p->m_value.allocator(); }
221
222 private:
223
224 ArrayDataT<DataType>* m_p = nullptr;
225};
226
227/*---------------------------------------------------------------------------*/
228/*---------------------------------------------------------------------------*/
229
230} // namespace Arcane
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
235#endif
#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.
void computeHash(DataHashInfo &hash_info) override
Calculates the hash of the data.
Definition ArrayData.h:216
void reserve(Integer new_capacity) override
Reserves memory for new_capacity elements.
Definition ArrayData.h:177
void resize(Integer new_size) override
Resizes the container.
Definition ArrayData.h:181
INumericDataInternal * numericData() override
Generic interface for numeric data (nullptr if the data is not numeric).
Definition ArrayData.h:214
bool decompressAndFill(DataCompressionBuffer &buf) override
Decompresses the data and fills the data values.
Definition ArrayData.h:196
Integer capacity() const override
Capacity allocated by the container.
Definition ArrayData.h:179
Array< DataType > & _internalDeprecatedValue() override
Container associated with the data.
Definition ArrayData.h:178
void changeAllocator(const MemoryAllocationOptions &v) override
Changes the variable's allocator.
Definition ArrayData.h:215
void dispose() override
Clears the container and frees allocated memory.
Definition ArrayData.h:182
void shrink() const override
Frees additional allocated memory.
Definition ArrayData.h:180
IMemoryAllocator * memoryAllocator() const override
Allocator used for the data.
Definition ArrayData.h:220
Int32 extent0() const override
Number of elements in the first dimension.
Definition ArrayData.h:210
MutableMemoryView memoryView() override
Memory view of the data.
Definition ArrayData.h:206
bool compressAndClear(DataCompressionBuffer &buf) override
Compresses the data and frees the associated memory.
Definition ArrayData.h:183
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
Definition ArrayData.h:80
ArrayView< DataType > view() override
View on the data.
Definition ArrayData.h:77
eDataType dataType() const override
Data type.
Definition ArrayData.h:71
ConstArrayView< DataType > view() const override
Constant view on the data.
Definition ArrayData.h:76
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
void visitScalar(IScalarDataVisitor *) override
Apply the visitor to the data.
Definition ArrayData.h:117
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
Definition ArrayData.h:105
Ref< IData > cloneRef() override
Clone the data.
Definition ArrayData.h:81
const Array< DataType > & value() const override
Constant data value.
Definition ArrayData.h:75
void setName(const String &name) override
Sets the name of the data (internal).
void visitArray2(IArray2DataVisitor *) override
Apply the visitor to the data.
Definition ArrayData.h:125
void visitArray(IArrayDataVisitor *visitor) override
Apply the visitor to the data.
Definition ArrayData.h:121
DataAllocationInfo allocationInfo() const override
Allocation information.
Definition ArrayData.h:108
DataInterfaceType * cloneTrue() override
Clone the data.
Definition ArrayData.h:84
void visit(IArrayDataVisitor *visitor) override
Applies the visitor to the data.
Definition ArrayData.h:109
void swapValues(IData *data) override
Swap the values of data with those of the instance.
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
DataInterfaceType * cloneTrueEmpty() override
Clone the data but without elements.
Definition ArrayData.h:85
UniqueArray< DataType > m_value
Data.
Definition ArrayData.h:147
Array< DataType > & value() override
Data value.
Definition ArrayData.h:74
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
Definition ArrayData.h:70
void fillDefault() override
Fills the data with its default value.
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
Definition ArrayData.h:113
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
Definition ArrayData.h:69
void copy(const IData *data) override
Copy the data data into the current instance.
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
Definition ArrayData.h:91
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
Definition ArrayData.h:82
void resize(Integer new_size) override
Resize the data.
Definition ArrayData.h:78
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
Definition ArrayData.h:79
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
void setShape(const ArrayShape &new_shape) override
Sets the array shape.
Definition ArrayData.h:106
IDataInternal * _commonInternal() override
Definition ArrayData.h:138
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
Definition ArrayData.h:86
IArrayDataInternalT< DataType > * _internal() override
Definition ArrayData.h:137
Array shape.
Definition ArrayShape.h:42
Modifiable view of an array of type T.
Base class for 1D data vectors.
Constant view of an array of type T.
Information on data allocation.
Class to manage data compression/decompression.
Information for calculating data hash.
Information to construct an instance of 'IData'.
Type information for a data container.
Interface of the visitor pattern for a 2D array data item.
Interface for an array data of type T.
Interface of a 1D array data item of type T.
Definition IData.h:300
Interface of the visitor pattern for an array data item.
Interface of a service for compressing/decompressing data.
virtual void decompress(Span< const std::byte > compressed_values, Span< std::byte > values)=0
Decompresses the data compressed_values and stores it in values.
virtual void compress(Span< const std::byte > values, Array< std::byte > &compressed_values)=0
Compresses the data values and stores it in compressed_values.
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
Interface of a hashing algorithm.
Interface for an 'IData' of a numeric type.
Interface of the visitor pattern for a scalar data item.
Interface of a serialized data.
Mutable view on a contiguous memory region containing fixed-size elements.
Reference to an instance.
Thread-safe implementation of a reference counter.
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
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
MutableMemoryView makeMutableMemoryView(void *ptr, Int32 datatype_size, Int64 nb_element)
Creates a mutable memory view.
Definition MemoryView.cc:26
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
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
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of modifiable bytes.
Definition Span.h:1068
eDataType
Data type.
Definition DataTypes.h:41
std::int32_t Int32
Signed integer type of 32 bits.