Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Data.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/* Data.cc (C) 2000-2025 */
9/* */
10/* Types related to 'IData'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/IData.h"
15
16#include "arcane/utils/NotSupportedException.h"
17#include "arcane/utils/FatalErrorException.h"
18#include "arcane/utils/ArrayShape.h"
20
21#include "arcane/core/IDataFactory.h"
22#include "arcane/core/IDataVisitor.h"
23#include "arcane/core/ISerializedData.h"
24#include "arcane/core/internal/IDataInternal.h"
25
27
28#include "arcane/accelerator/core/RunQueue.h"
29#include "arcane/accelerator/core/Memory.h"
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34namespace Arccore
35{
37ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(Arcane::ISerializedData);
38} // namespace Arccore
39
40namespace Arcane
41{
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46void IDataVisitor::
47applyDataVisitor(IMultiArray2Data*)
48{
49 ARCANE_THROW(NotSupportedException, "using applyDataVisitor with IMultiArray2Data is no longer supported");
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
57{
58 ARCANE_THROW(NotSupportedException, "Visiting IMultiArray2Data is no longer supported");
59}
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
64void impl::
65copyContiguousData(INumericDataInternal* num_destination, ConstMemoryView source_buf,
66 RunQueue& queue)
67{
68 ARCANE_CHECK_POINTER(num_destination);
69
70 MutableMemoryView destination_buf = num_destination->memoryView();
71 if (source_buf.datatypeSize() != destination_buf.datatypeSize())
72 ARCANE_FATAL("Source and destination do not have the same datatype s={0} d={1}",
73 source_buf.datatypeSize(), destination_buf.datatypeSize());
74 if (queue.isNull())
75 MemoryUtils::copy(destination_buf, source_buf);
76 else
77 queue.copyMemory(Accelerator::MemoryCopyArgs(destination_buf, source_buf));
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83void impl::
84copyContiguousData(IData* destination, IData* source, RunQueue& queue)
85{
87 ARCANE_CHECK_POINTER(destination);
88
89 INumericDataInternal* num_destination = destination->_commonInternal()->numericData();
90 if (!num_destination)
91 ARCANE_FATAL("Destination is not a numerical data");
92 INumericDataInternal* num_source = source->_commonInternal()->numericData();
93 if (!num_source)
94 ARCANE_FATAL("Source is not a numerical data");
95 copyContiguousData(num_destination, num_source->memoryView(), queue);
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101void impl::
102fillContiguousDataGeneric(IData* data, const void* fill_address,
103 Int32 datatype_size, RunQueue& queue)
104{
105 INumericDataInternal* num_data = data->_commonInternal()->numericData();
106 if (!num_data)
107 ARCANE_FATAL("Destination is not a numerical data");
108
109 ConstMemoryView fill_value_view(makeConstMemoryView(fill_address, datatype_size, 1));
110 MutableMemoryView destination_buf = num_data->memoryView();
111
112 // If the data is a 2D array or more, it must be transformed into a 1D array
113 // of the total number of elements, otherwise 'destination_buf.datatypeSize()' is not
114 // consistent with 'datatype_size'
115 if (data->dimension() > 1) {
116 Int64 total_dim = data->shape().totalNbElement();
117 Int64 nb_element = destination_buf.nbElement();
118 destination_buf = makeMutableMemoryView(destination_buf.data(), datatype_size, nb_element * total_dim);
119 }
120
121 MemoryUtils::fill(destination_buf, fill_value_view, &queue);
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127} // namespace Arcane
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(class_name)
Macro to define methods and types for a class that uses a reference counter.
Memory and allocator management functions.
void copyMemory(const MemoryCopyArgs &args) const
Copies information between two memory regions.
Definition RunQueue.cc:237
Constant view on a contiguous memory region containing fixed-size elements.
constexpr Int32 datatypeSize() const
Size of the associated data type (1 by default).
Interface of a data item.
Definition IData.h:34
virtual void visitMultiArray2(IMultiArray2DataVisitor *visitor)
Apply the visitor to the data.
Definition Data.cc:56
Interface of the visitor pattern for a variable-sized 2D array data item.
Interface of a multi 2D array data item.
Definition IData.h:361
Mutable view on a contiguous memory region containing fixed-size elements.
constexpr Int32 datatypeSize() const
Size of the associated data type (1 by default).
void fill(MutableMemoryView destination, ConstMemoryView source, const RunQueue *run_queue=nullptr)
Fills a memory region with a value.
void copy(MutableMemoryView destination, eMemoryResource destination_mem, ConstMemoryView source, eMemoryResource source_mem, const RunQueue *queue=nullptr)
Copies source to destination using the queue queue.
-- 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
std::int64_t Int64
Signed integer type of 64 bits.
ConstMemoryView makeConstMemoryView(const void *ptr, Int32 datatype_size, Int64 nb_element)
Creates a read-only memory view.
Definition MemoryView.cc:36
std::int32_t Int32
Signed integer type of 32 bits.
Namespace of Arccore.