Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
MemoryView.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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.h (C) 2000-2024 */
9/* */
10/* Vues constantes ou modifiables sur une zone mémoire. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_MEMORYVIEW_H
13#define ARCANE_UTILS_MEMORYVIEW_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/ArrayView.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
37class ARCANE_UTILS_EXPORT ConstMemoryView
38{
39 friend ARCANE_UTILS_EXPORT ConstMemoryView
40 makeConstMemoryView(const void* ptr, Int32 datatype_size, Int64 nb_element);
41
42 public:
43
44 using SpanType = Span<const std::byte>;
45 friend MutableMemoryView;
46
47 public:
48
49 ConstMemoryView() = default;
50 explicit constexpr ConstMemoryView(Span<const std::byte> bytes)
51 : m_bytes(bytes)
52 , m_nb_element(bytes.size())
53 , m_datatype_size(1)
54 {}
55 template <typename DataType> explicit constexpr ConstMemoryView(Span<DataType> v)
56 : ConstMemoryView(Span<const DataType>(v), 1)
57 {}
58 template <typename DataType> explicit constexpr ConstMemoryView(Span<const DataType> v)
59 : ConstMemoryView(v, 1)
60 {}
61 template <typename DataType> explicit constexpr ConstMemoryView(ConstArrayView<DataType> v)
62 : ConstMemoryView(Span<const DataType>(v), 1)
63 {}
64 template <typename DataType> explicit constexpr ConstMemoryView(ArrayView<DataType> v)
65 : ConstMemoryView(Span<const DataType>(v), 1)
66 {}
67 template <typename DataType> constexpr ConstMemoryView(ConstArrayView<DataType> v, Int32 nb_component)
68 : ConstMemoryView(Span<const DataType>(v), nb_component)
69 {}
70 template <typename DataType> constexpr ConstMemoryView(ArrayView<DataType> v, Int32 nb_component)
71 : ConstMemoryView(Span<const DataType>(v), nb_component)
72 {}
73 template <typename DataType> constexpr ConstMemoryView(Span<DataType> v, Int32 nb_component)
74 : ConstMemoryView(Span<const DataType>(v), nb_component)
75 {
76 }
77 template <typename DataType> constexpr ConstMemoryView(Span<const DataType> v, Int32 nb_component)
78 : m_nb_element(v.size())
79 , m_datatype_size(static_cast<Int32>(sizeof(DataType)) * nb_component)
80 {
81 auto x = asBytes(v);
82 m_bytes = SpanType(x.data(), x.size() * nb_component);
83 }
84
85 public:
86
87 template <typename DataType> constexpr ConstMemoryView&
88 operator=(Span<DataType> v)
89 {
90 m_bytes = asBytes(v);
91 m_nb_element = v.size();
92 m_datatype_size = static_cast<Int32>(sizeof(DataType));
93 return (*this);
94 }
95
96 private:
97
98 constexpr ConstMemoryView(Span<const std::byte> bytes, Int32 datatype_size, Int64 nb_element)
99 : m_bytes(bytes)
100 , m_nb_element(nb_element)
101 , m_datatype_size(datatype_size)
102 {}
103
104 public:
105
107 constexpr SpanType bytes() const { return m_bytes; }
108
110 constexpr const std::byte* data() const { return m_bytes.data(); }
111
113 constexpr Int64 nbElement() const { return m_nb_element; }
114
116 constexpr Int32 datatypeSize() const { return m_datatype_size; }
117
119 constexpr ConstMemoryView subView(Int64 begin_index, Int64 nb_element) const
120 {
121 Int64 byte_offset = begin_index * m_datatype_size;
122 auto sub_bytes = m_bytes.subspan(byte_offset, nb_element * m_datatype_size);
123 return { sub_bytes, m_datatype_size, nb_element };
124 }
125
126 public:
127
142 void copyToIndexesHost(MutableMemoryView v, Span<const Int32> indexes) const;
143
160 void copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
161 RunQueue* run_queue = nullptr) const;
162
163 public:
164
166 ARCANE_DEPRECATED_REASON("Use bytes() instead")
167 SpanType span() const { return m_bytes; }
168
169 ARCANE_DEPRECATED_REASON("Use bytes().size() instead")
170 constexpr Int64 size() const { return m_bytes.size(); }
171
172 private:
173
174 SpanType m_bytes;
175 Int64 m_nb_element = 0;
176 Int32 m_datatype_size = 0;
177};
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
192class ARCANE_UTILS_EXPORT MutableMemoryView
193{
194 friend ARCANE_UTILS_EXPORT MutableMemoryView
195 makeMutableMemoryView(void* ptr, Int32 datatype_size, Int64 nb_element);
196
197 public:
198
199 using SpanType = Span<std::byte>;
200
201 public:
202
203 MutableMemoryView() = default;
204 explicit constexpr MutableMemoryView(SpanType bytes)
205 : m_bytes(bytes)
206 , m_nb_element(bytes.size())
207 , m_datatype_size(1)
208 {}
209 template <typename DataType> explicit constexpr MutableMemoryView(Span<DataType> v)
210 : MutableMemoryView(v, 1)
211 {}
212 template <typename DataType> explicit constexpr MutableMemoryView(ArrayView<DataType> v)
213 : MutableMemoryView(Span<DataType>(v), 1)
214 {}
215 template <typename DataType> explicit constexpr MutableMemoryView(ArrayView<DataType> v, Int32 nb_component)
216 : MutableMemoryView(Span<DataType>(v), nb_component)
217 {}
218 template <typename DataType> constexpr MutableMemoryView(Span<DataType> v, Int32 nb_component)
219 : m_nb_element(v.size())
220 , m_datatype_size(static_cast<Int32>(sizeof(DataType)) * nb_component)
221 {
222 auto x = asWritableBytes(v);
223 m_bytes = SpanType(x.data(), x.size() * nb_component);
224 }
225
226 public:
227
228 template <typename DataType> constexpr MutableMemoryView&
229 operator=(Span<DataType> v)
230 {
231 m_bytes = asWritableBytes(v);
232 m_nb_element = v.size();
233 m_datatype_size = static_cast<Int32>(sizeof(DataType));
234 return (*this);
235 }
236
237 private:
238
239 constexpr MutableMemoryView(Span<std::byte> bytes, Int32 datatype_size, Int64 nb_element)
240 : m_bytes(bytes)
241 , m_nb_element(nb_element)
242 , m_datatype_size(datatype_size)
243 {}
244
245 public:
246
247 constexpr operator ConstMemoryView() const { return { m_bytes, m_datatype_size, m_nb_element }; }
248
249 public:
250
252 constexpr SpanType bytes() const { return m_bytes; }
253
255 constexpr std::byte* data() const { return m_bytes.data(); }
256
258 constexpr Int64 nbElement() const { return m_nb_element; }
259
261 constexpr Int32 datatypeSize() const { return m_datatype_size; }
262
264 constexpr MutableMemoryView subView(Int64 begin_index, Int64 nb_element) const
265 {
266 Int64 byte_offset = begin_index * m_datatype_size;
267 auto sub_bytes = m_bytes.subspan(byte_offset, nb_element * m_datatype_size);
268 return { sub_bytes, m_datatype_size, nb_element };
269 }
270
271 public:
272
280 void copyHost(ConstMemoryView v) const;
281
296 void copyFromIndexesHost(ConstMemoryView v, Span<const Int32> indexes) const;
297
314 void copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
315 RunQueue* run_queue = nullptr) const;
316
337 void fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
338 RunQueue* run_queue = nullptr) const;
339
357 void fill(ConstMemoryView v, RunQueue* run_queue = nullptr) const;
358
359 public:
360
361 ARCANE_DEPRECATED_REASON("Use bytes() instead")
362 constexpr SpanType span() const { return m_bytes; }
363
364 ARCANE_DEPRECATED_REASON("Use bytes().size() instead")
365 constexpr Int64 size() const { return m_bytes.size(); }
366
367 private:
368
369 SpanType m_bytes;
370 Int64 m_nb_element = 0;
371 Int32 m_datatype_size = 0;
372};
373
374/*---------------------------------------------------------------------------*/
375/*---------------------------------------------------------------------------*/
383class ARCANE_UTILS_EXPORT ConstMultiMemoryView
384{
385 public:
386
387 ConstMultiMemoryView(SmallSpan<const Span<const std::byte>> views, Int32 datatype_size)
388 : m_views(views)
389 , m_datatype_size(datatype_size)
390 {}
391 ConstMultiMemoryView(SmallSpan<const Span<std::byte>> views, Int32 datatype_size)
392 : m_datatype_size(datatype_size)
393 {
394 auto* ptr = reinterpret_cast<const Span<const std::byte>*>(views.data());
395 m_views = { ptr, views.size() };
396 }
397
398 public:
399
422 void copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
423 RunQueue* run_queue = nullptr);
424
425 private:
426
427 SmallSpan<const Span<const std::byte>> m_views;
428 Int32 m_datatype_size;
429};
430
431/*---------------------------------------------------------------------------*/
432/*---------------------------------------------------------------------------*/
440class ARCANE_UTILS_EXPORT MutableMultiMemoryView
441{
442 public:
443
444 MutableMultiMemoryView(SmallSpan<Span<std::byte>> views, Int32 datatype_size)
445 : m_views(views)
446 , m_datatype_size(datatype_size)
447 {}
448
449 public:
450
473 void copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
474 RunQueue* run_queue = nullptr);
475
498 void fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
499 RunQueue* run_queue = nullptr);
500
520 void fill(ConstMemoryView v, RunQueue* run_queue = nullptr);
521
522 private:
523
524 SmallSpan<Span<std::byte>> m_views;
525 Int32 m_datatype_size;
526};
527
528/*---------------------------------------------------------------------------*/
529/*---------------------------------------------------------------------------*/
530
531/*---------------------------------------------------------------------------*/
532/*---------------------------------------------------------------------------*/
533
535template <typename DataType> ConstMemoryView
536makeMemoryView(Span<DataType> v)
537{
538 return ConstMemoryView(v);
539}
540
542template <typename DataType> ConstMemoryView
543makeMemoryView(const DataType* v)
544{
545 return ConstMemoryView(Span<const DataType>(v, 1));
546}
547
548/*---------------------------------------------------------------------------*/
549/*---------------------------------------------------------------------------*/
550
552template <typename DataType> MutableMemoryView
553makeMutableMemoryView(Span<DataType> v)
554{
555 return MutableMemoryView(v);
556}
557
558/*---------------------------------------------------------------------------*/
559/*---------------------------------------------------------------------------*/
560
562template <typename DataType> MutableMemoryView
563makeMutableMemoryView(DataType* v)
564{
565 return MutableMemoryView(Span<DataType>(v, 1));
566}
567
568/*---------------------------------------------------------------------------*/
569/*---------------------------------------------------------------------------*/
579extern "C++" ARCANE_UTILS_EXPORT MutableMemoryView
580makeMutableMemoryView(void* ptr, Int32 datatype_size, Int64 nb_element);
581
582/*---------------------------------------------------------------------------*/
583/*---------------------------------------------------------------------------*/
593extern "C++" ARCANE_UTILS_EXPORT ConstMemoryView
594makeConstMemoryView(const void* ptr, Int32 datatype_size, Int64 nb_element);
595
596/*---------------------------------------------------------------------------*/
597/*---------------------------------------------------------------------------*/
598
599} // End namespace Arcane
600
601/*---------------------------------------------------------------------------*/
602/*---------------------------------------------------------------------------*/
603
604#endif
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
detail::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets non modifiables.
Definition Span.h:881
detail::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.
Definition Span.h:916