Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MemoryView.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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/* MemoryView.cc (C) 2000-2023 */
9/* */
10/* Vues constantes ou modifiables sur une zone mémoire. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/MemoryView.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/internal/SpecificMemoryCopyList.h"
18
19#include <cstring>
20
21// TODO: ajouter statistiques sur les tailles de 'datatype' utilisées.
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane::impl
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
33{
34 public:
35
36 using InterfaceType = ISpecificMemoryCopy;
37 template <typename DataType, typename Extent> using SpecificType = SpecificMemoryCopy<DataType, Extent>;
38 using RefType = SpecificMemoryCopyRef<IndexedCopyTraits>;
39};
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44} // namespace Arcane::impl
45
46namespace Arcane
47{
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
53namespace
54{
55 impl::SpecificMemoryCopyList<impl::IndexedCopyTraits> global_copy_list;
56 impl::ISpecificMemoryCopyList* default_global_copy_list = nullptr;
57
58 impl::ISpecificMemoryCopyList* _getDefaultCopyList(RunQueue* queue)
59 {
60 if (queue && !default_global_copy_list)
61 ARCANE_FATAL("No instance of copier is available for RunQueue");
62 if (default_global_copy_list && queue)
63 return default_global_copy_list;
64 return &global_copy_list;
65 }
66 Int32 _checkDataTypeSize(const TraceInfo& trace, Int32 data_size1, Int32 data_size2)
67 {
68 if (data_size1 != data_size2)
69 throw FatalErrorException(trace, String::format("Datatype size are not equal this={0} v={1}", data_size1, data_size2));
70 return data_size1;
71 }
72} // namespace
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77void impl::ISpecificMemoryCopyList::
78setDefaultCopyListIfNotSet(ISpecificMemoryCopyList* ptr)
79{
80 if (!default_global_copy_list) {
81 default_global_copy_list = ptr;
82 }
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88void MutableMemoryView::
89copyHost(ConstMemoryView v) const
90{
91 auto source = v.bytes();
92 auto destination = bytes();
93 Int64 source_size = source.size();
94 if (source_size == 0)
95 return;
96 Int64 destination_size = destination.size();
97 if (source_size > destination_size)
98 ARCANE_FATAL("Destination is too small source_size={0} destination_size={1}",
99 source_size, destination_size);
100 auto* dest_data = destination.data();
101 auto* source_data = source.data();
102 ARCANE_CHECK_POINTER(dest_data);
103 ARCANE_CHECK_POINTER(source_data);
104 std::memmove(destination.data(), source.data(), source_size);
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110void MutableMemoryView::
111copyFromIndexesHost(ConstMemoryView v, Span<const Int32> indexes) const
112{
113 copyFromIndexes(v, indexes.smallView(), nullptr);
114}
115
116void MutableMemoryView::
117copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
118 RunQueue* queue) const
119{
120
121 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
122
123 Int64 nb_index = indexes.size();
124 if (nb_index == 0)
125 return;
126
127 auto source = v.bytes();
128 auto destination = bytes();
129
130 _getDefaultCopyList(queue)->copyFrom(one_data_size, { indexes, source, destination, queue });
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136void MutableMemoryView::
137fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
138 RunQueue* queue) const
139{
140 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
141
142 Int64 nb_index = indexes.size();
143 if (nb_index == 0)
144 return;
145
146 auto source = v.bytes();
147 auto destination = bytes();
148
149 _getDefaultCopyList(queue)->fill(one_data_size, { indexes, source, destination, queue });
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155void MutableMemoryView::
156fill(ConstMemoryView v, RunQueue* queue) const
157{
158 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
159
160 auto source = v.bytes();
161 auto destination = bytes();
162
163 _getDefaultCopyList(queue)->fill(one_data_size, { {}, source, destination, queue });
164}
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169void ConstMemoryView::
170copyToIndexesHost(MutableMemoryView v, Span<const Int32> indexes) const
171{
172 copyToIndexes(v, indexes.smallView(), nullptr);
173}
174
175void ConstMemoryView::
176copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
177 RunQueue* queue) const
178{
179 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
180
181 Int64 nb_index = indexes.size();
182 if (nb_index == 0)
183 return;
184
185 auto source = bytes();
186 auto destination = v.bytes();
187
188 _getDefaultCopyList(queue)->copyTo(one_data_size, { indexes, source, destination, queue });
189}
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
193
194MutableMemoryView
195makeMutableMemoryView(void* ptr, Int32 datatype_size, Int64 nb_element)
196{
197 Span<std::byte> bytes(reinterpret_cast<std::byte*>(ptr), datatype_size * nb_element);
198 return { bytes, datatype_size, nb_element };
199}
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204ConstMemoryView
205makeConstMemoryView(const void* ptr, Int32 datatype_size, Int64 nb_element)
206{
207 Span<const std::byte> bytes(reinterpret_cast<const std::byte*>(ptr), datatype_size * nb_element);
208 return { bytes, datatype_size, nb_element };
209}
210
211/*---------------------------------------------------------------------------*/
212/*---------------------------------------------------------------------------*/
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217void MutableMultiMemoryView::
218copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
219 RunQueue* queue)
220{
221 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
222
223 Int64 nb_index = indexes.size();
224 if (nb_index == 0)
225 return;
226
227 _getDefaultCopyList(queue)->copyFrom(one_data_size, { indexes, m_views, v.bytes(), queue });
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233void MutableMultiMemoryView::
234fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes, RunQueue* queue)
235{
236 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
237
238 Int64 nb_index = indexes.size();
239 if (nb_index == 0)
240 return;
241
242 _getDefaultCopyList(queue)->fill(one_data_size, { indexes, m_views, v.bytes(), queue });
243}
244
245/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248void MutableMultiMemoryView::
249fill(ConstMemoryView v, RunQueue* queue)
250{
251 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
252
253 _getDefaultCopyList(queue)->fill(one_data_size, { {}, m_views, v.bytes(), queue });
254}
255
256/*---------------------------------------------------------------------------*/
257/*---------------------------------------------------------------------------*/
258
259void ConstMultiMemoryView::
260copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
261 RunQueue* queue)
262{
263 Int32 one_data_size = _checkDataTypeSize(A_FUNCINFO, m_datatype_size, v.datatypeSize());
264
265 Int64 nb_index = indexes.size();
266 if (nb_index == 0)
267 return;
268
269 _getDefaultCopyList(queue)->copyTo(one_data_size, { indexes, m_views, v.bytes(), queue });
270}
271
272/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
274
275extern "C++" ARCANE_UTILS_EXPORT void
276arcanePrintSpecificMemoryStats()
277{
278 if (arcaneIsCheck()) {
279 // N'affiche que pour les tests
280 //global_copy_list.printStats();
281 }
282}
283
284/*---------------------------------------------------------------------------*/
285/*---------------------------------------------------------------------------*/
286
287} // End namespace Arcane
288
289/*---------------------------------------------------------------------------*/
290/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
File d'exécution pour un accélérateur.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
bool arcaneIsCheck()
Vrai si on est en mode vérification.
Definition Misc.cc:151