17#include "arcane/utils/NumArray.h"
18#include "arcane/utils/NotSupportedException.h"
19#include "arcane/utils/Real2.h"
20#include "arcane/utils/Real2x2.h"
21#include "arcane/utils/Real3.h"
22#include "arcane/utils/Real3x3.h"
23#include "arcane/utils/IHashAlgorithm.h"
24#include "arcane/utils/NotImplementedException.h"
25#include "arcane/utils/ArgumentException.h"
26#include "arcane/utils/FatalErrorException.h"
27#include "arcane/utils/ITraceMng.h"
28#include "arcane/utils/CheckedConvert.h"
29#include "arcane/utils/ArrayShape.h"
33#include "arcane/core/datatype/DataAllocationInfo.h"
34#include "arcane/core/datatype/IDataOperation.h"
35#include "arcane/core/datatype/DataStorageTypeInfo.h"
36#include "arcane/core/datatype/DataStorageBuildInfo.h"
37#include "arcane/core/datatype/DataTypeTraits.h"
39#include "arcane/core/ISerializer.h"
40#include "arcane/core/IData.h"
41#include "arcane/core/IDataVisitor.h"
43#include "arcane/core/internal/IDataInternal.h"
45#include "arcane/impl/SerializedData.h"
46#include "arcane/impl/DataStorageFactory.h"
59 const Int64 SERIALIZE2_MAGIC_NUMBER = 0x923abd20;
68template <
class DataType,
int RankValue>
98template <
class DataType,
int RankValue>
109 typedef NumArrayDataT<DataType, RankValue> ThatClass;
150 auto* d = _cloneTrue();
155 auto* d = _cloneTrueEmpty();
176 ARCANE_UNUSED(visitor);
195 void swapValuesDirect(ThatClass* true_data);
213 void _resizeDim1(Int32 dim1_size);
214 Int64 _getDim2Size()
const;
215 Span2<DataType> _valueAsSpan2();
216 Span2<const DataType> _valueAsConstSpan2();
233: m_value(rhs.m_value)
234, m_trace(rhs.m_trace)
235, m_allocation_info(rhs.m_allocation_info)
244: m_trace(dsbi.traceMng())
267 Int32 nb_basic_type = TraitsType::nbBasicType();
268 Int32 dimension = RankValue;
270 String impl_name =
"NumArray";
280 return staticStorageTypeInfo();
290 auto extents = m_value.extents();
291 extents.setExtent0(dim1_size);
292 m_value.
resize(extents.dynamicExtents());
298template <
typename DataType,
int RankValue> Int64
299NumArrayDataT<DataType, RankValue>::
304 auto extents = m_value.extents();
305 auto std_extents = extents.asStdArray();
307 for (
Integer i = 0; i < RankValue; ++i)
308 dim2_size *= std_extents[i];
320 Int64 dim1_size = m_value.dim1Size();
321 Int64 dim2_size = _getDim2Size();
322 Span2<DataType> value_as_span2(m_value.to1DSpan().data(), dim1_size, dim2_size);
323 return value_as_span2;
336 Int64 dim1_size = m_value.dim1Size();
337 Int64 dim2_size = _getDim2Size();
339 return value_as_span2;
348 _resizeDim1(new_size);
362 Int64 type_size =
sizeof(DataType);
364 if (use_basic_type) {
367 type_size =
sizeof(BasicType);
371 Int64 nb_base_element = nb_element * nb_count;
372 Int64 full_size = nb_base_element * type_size;
373 const Byte* bt =
reinterpret_cast<const Byte*
>(
m_value.to1DSpan().data());
375 auto extents =
m_value.extents();
376 auto std_extents = extents.asStdArray();
377 const Int32 nb_extent = CheckedConvert::toInt32(std_extents.size());
379 for (
Int32 i = 0; i < nb_extent; ++i)
380 dimensions[i] = std_extents[i];
382 nb_base_element,
false, dimensions,
shape());
383 sd->setConstBytes(base_values);
398 if (data_type !=
dataType() && data_type == base_data_type)
407 std::array<Int32, RankValue> numarray_extents;
408 for (
Int32 i = 0; i < RankValue; ++i)
409 numarray_extents[i] = CheckedConvert::toInt32(sdata_extents[i]);
411 m_value.resize(extents.dynamicExtents());
424 ARCANE_UNUSED(sdata);
441 if (mode == ISerializer::ModeReserve) {
454 n[0] = SERIALIZE2_MAGIC_NUMBER;
458 auto ext =
m_value.extents().asStdArray();
464 Int64 n[2] = { 0, 0 };
467 if (n[0] != SERIALIZE2_MAGIC_NUMBER)
469 Int32 extents_buf[RankValue];
472 Int32 count = extents_span[0];
479 BasicType* bt =
reinterpret_cast<BasicType*
>(
m_value.to1DSpan().data());
491 _resizeDim1(current_size + count);
494 BasicType* bt =
reinterpret_cast<BasicType*
>(
m_value.to1DSpan().data() + current_total);
509 ARCANE_UNUSED(operation);
512 [[maybe_unused]]
Integer nb_count = 1;
516 if (mode == ISerializer::ModeReserve) {
525 auto sub_extent =
m_value.extents().removeFirstExtent();
526 sbuf->
reserveSpan(data_type, sub_extent.totalNbElement() * ids.
size());
530 Int64 dim2_size = _getDim2Size();
531 Int64 total_nb_value = count * dim2_size;
532 Int64 total = total_nb_value;
535 n[0] = SERIALIZE2_MAGIC_NUMBER;
547 auto ext =
m_value.extents().asStdArray();
555 for (
Int32 i = 0, n = count; i < n; ++i) {
556 const BasicType* sub_a =
reinterpret_cast<const BasicType*
>(value_as_span2[ids[i]].
data());
557 for (
Int64 z = 0, iz = dim2_size * nb_count; z < iz; ++z) {
569 Int64 n[3] = { 0, 0, 0 };
571 Int32 count = CheckedConvert::toInt32(n[1]);
572 Int64 dim2_size = n[2];
573 Int64 total = count * dim2_size;
583 if (n[1] != SERIALIZE2_MAGIC_NUMBER)
586 Int32 extents_buf[RankValue];
592 Int64 current_dim2_size = _getDim2Size();
594 if (dim2_size != current_dim2_size) {
595 if (current_dim2_size != 0 && dim2_size != 0)
596 ARCANE_FATAL(
"serialized data should have the same dim2Size current={0} found={1}",
597 current_dim2_size, dim2_size);
599 _resizeDim1(
m_value.dim1Size());
601 Int64 nb_value = count;
607 Span<DataType> data_value(
reinterpret_cast<DataType*
>(base_value.
data()), nb_value * dim2_size);
614 if (operation && nb_value != 0) {
618 for (
Int32 i = 0, n = count; i < n; ++i) {
620 for (
Int64 z = 0, iz = dim2_size; z < iz; ++z) {
621 current_value[index] = a[z];
626 transformed_value = current_value.
view();
627 operation->applySpan(transformed_value, data_value);
630 transformed_value = data_value;
635 for (
Int32 i = 0, n = count; i < n; ++i) {
637 for (
Int64 z = 0, iz = dim2_size; z < iz; ++z) {
638 a[z] = transformed_value[index];
678 Int64 type_size =
sizeof(DataType);
680 const Byte* ptr =
reinterpret_cast<const Byte*
>(values.
data());
687 auto ext =
m_value.extents().asStdArray();
699 auto* true_data =
dynamic_cast<const DataInterfaceType*
>(data);
702 m_value.copy(true_data->view());
711 auto* true_data =
dynamic_cast<ThatClass*
>(data);
714 swapValuesDirect(true_data);
723 m_value.swap(true_data->m_value);
730registerNumArrayDataFactory(IDataFactoryMng* dfm)
732 DataStorageFactory<NumArrayDataT<Real, 1>>::registerDataFactory(dfm);
735 DataStorageFactory<NumArrayDataT<Int64, 1>>::registerDataFactory(dfm);
737 DataStorageFactory<NumArrayDataT<Real, 2>>::registerDataFactory(dfm);
740 DataStorageFactory<NumArrayDataT<Int64, 2>>::registerDataFactory(dfm);
742 DataStorageFactory<NumArrayDataT<Real, 3>>::registerDataFactory(dfm);
745 DataStorageFactory<NumArrayDataT<Int64, 3>>::registerDataFactory(dfm);
747 DataStorageFactory<NumArrayDataT<Real, 4>>::registerDataFactory(dfm);
750 DataStorageFactory<NumArrayDataT<Int64, 4>>::registerDataFactory(dfm);
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro to define methods managing counters of references.
Types and functions associated with the classes Span2Impl, Small2Span and Span2.
Management of references to a C++ class.
Exception when an argument is invalid.
void resize(Int64 s)
Changes the number of elements in the array to s.
const T * data() const
Access to the root of the array without any protection.
ArrayView< T > view() const
Mutable view of this 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'.
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 an operation on a data.
Interface of the visitor pattern for a data item.
Interface of a data item.
Interface of a hashing algorithm.
virtual void computeHash64(Span< const Byte > input, ByteArray &output)
Calculates the hash value for the array input.
Interface of an 'IData' whose container is based on a 'NumArray'.
virtual Ref< ThatClass > cloneTrueEmptyRef()=0
Clone the data but without elements.
virtual MDSpan< const DataType, ExtentType > view() const =0
Constant view of the data.
virtual MDSpan< DataType, ExtentType > view()=0
View of the data.
virtual Ref< ThatClass > cloneTrueRef()=0
Clone the data.
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 Int64ConstArrayView extents() const =0
Array containing the number of elements for each dimension.
virtual bool isMultiSize() const =0
Indicates if it is a multi-size array. (only relevant if nbDimension()>1).
eMode
Serializer operating mode.
@ ModePut
The serializer expects reserve().
@ ModeGet
The serializer expects get().
virtual eReadMode readMode() const =0
Read mode.
virtual void putSpan(Span< const Real > values)
Add the array values.
virtual eMode mode() const =0
Current operating mode.
@ ReadReplace
Replace current elements with those read.
@ ReadAdd
Add those read to the current elements.
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.
Base class for multi-dimensional views.
Exception when a function is not implemented.
Exception when an operation is not supported.
void computeHash(DataHashInfo &) override
Calculates the hash of the data.
Implementation of an 'IData' whose container is based on a 'NumArray'.
void setShape(const ArrayShape &new_shape) override
Sets the array shape.
void copy(const IData *data) override
Copy the data data into the current instance.
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
void visitArray(IArrayDataVisitor *) override
Apply the visitor to the data.
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
void swapValues(IData *data) override
Swap the values of data with those of the instance.
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
IDataInternal * _commonInternal() override
Ref< IData > cloneRef() override
Clone the data.
NumArray< DataType, ExtentType > m_value
Data.
void fillDefault() override
Fills the data with its default value.
MDSpan< DataType, ExtentType > view() override
View of the data.
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
eDataType dataType() const override
Data type.
MDSpan< const DataType, ExtentType > view() const override
Constant view of the data.
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
void resize(Integer new_size) override
Resize the data.
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
void setName(const String &name) override
Sets the name of the data (internal).
void visitArray2(IArray2DataVisitor *) override
Apply the visitor to the data.
void visitScalar(IScalarDataVisitor *) override
Apply the visitor to the data.
DataAllocationInfo allocationInfo() const override
Allocation information.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
Multi-dimensional arrays for numerical types accessible on accelerators.
Reference to an instance.
Thread-safe implementation of a reference counter.
View of an array of elements of type T.
constexpr __host__ __device__ ElementType * data()
Pointer to the allocated memory.
View for a 2D array whose size is an 'Int64'.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
View of an array of elements of type T.
Unicode character string.
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.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
eBasicDataType
Type of a basic data item.
@ Int32
32-bit integer data type
@ Int64
64-bit integer data type
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
unsigned char Byte
Type of a byte.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.