12#ifndef ARCANE_UTILS_MEMORYVIEW_H
13#define ARCANE_UTILS_MEMORYVIEW_H
17#include "arcane/utils/ArrayView.h"
37class ARCANE_UTILS_EXPORT ConstMemoryView
39 friend ARCANE_UTILS_EXPORT ConstMemoryView
40 makeConstMemoryView(
const void* ptr, Int32 datatype_size, Int64 nb_element);
44 using SpanType = Span<const std::byte>;
45 friend MutableMemoryView;
49 ConstMemoryView() =
default;
50 explicit constexpr ConstMemoryView(Span<const std::byte> bytes)
52 , m_nb_element(bytes.size())
55 template <
typename DataType>
explicit constexpr ConstMemoryView(Span<DataType> v)
56 : ConstMemoryView(Span<const DataType>(v), 1)
58 template <
typename DataType>
explicit constexpr ConstMemoryView(Span<const DataType> v)
59 : ConstMemoryView(v, 1)
61 template <
typename DataType>
explicit constexpr ConstMemoryView(ConstArrayView<DataType> v)
62 : ConstMemoryView(Span<const DataType>(v), 1)
64 template <
typename DataType>
explicit constexpr ConstMemoryView(ArrayView<DataType> v)
65 : ConstMemoryView(Span<const DataType>(v), 1)
67 template <
typename DataType>
constexpr ConstMemoryView(ConstArrayView<DataType> v, Int32 nb_component)
68 : ConstMemoryView(Span<const DataType>(v), nb_component)
70 template <
typename DataType>
constexpr ConstMemoryView(ArrayView<DataType> v, Int32 nb_component)
71 : ConstMemoryView(Span<const DataType>(v), nb_component)
73 template <
typename DataType>
constexpr ConstMemoryView(Span<DataType> v, Int32 nb_component)
74 : ConstMemoryView(Span<const DataType>(v), nb_component)
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)
82 m_bytes = SpanType(x.data(), x.size() * nb_component);
87 template <
typename DataType>
constexpr ConstMemoryView&
88 operator=(Span<DataType> v)
91 m_nb_element = v.size();
92 m_datatype_size =
static_cast<Int32
>(
sizeof(DataType));
98 constexpr ConstMemoryView(Span<const std::byte> bytes, Int32 datatype_size, Int64 nb_element)
100 , m_nb_element(nb_element)
101 , m_datatype_size(datatype_size)
107 constexpr SpanType bytes()
const {
return m_bytes; }
110 constexpr const std::byte* data()
const {
return m_bytes.data(); }
113 constexpr Int64 nbElement()
const {
return m_nb_element; }
116 constexpr Int32 datatypeSize()
const {
return m_datatype_size; }
119 constexpr ConstMemoryView subView(Int64 begin_index, Int64 nb_element)
const
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 };
142 void copyToIndexesHost(MutableMemoryView v, Span<const Int32> indexes)
const;
160 void copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
161 RunQueue* run_queue =
nullptr)
const;
166 ARCANE_DEPRECATED_REASON(
"Use bytes() instead")
167 SpanType span()
const {
return m_bytes; }
169 ARCANE_DEPRECATED_REASON(
"Use bytes().size() instead")
170 constexpr Int64 size()
const {
return m_bytes.size(); }
175 Int64 m_nb_element = 0;
176 Int32 m_datatype_size = 0;
192class ARCANE_UTILS_EXPORT MutableMemoryView
194 friend ARCANE_UTILS_EXPORT MutableMemoryView
195 makeMutableMemoryView(
void* ptr, Int32 datatype_size, Int64 nb_element);
199 using SpanType = Span<std::byte>;
203 MutableMemoryView() =
default;
204 explicit constexpr MutableMemoryView(SpanType bytes)
206 , m_nb_element(bytes.size())
209 template <
typename DataType>
explicit constexpr MutableMemoryView(Span<DataType> v)
210 : MutableMemoryView(v, 1)
212 template <
typename DataType>
explicit constexpr MutableMemoryView(ArrayView<DataType> v)
213 : MutableMemoryView(Span<DataType>(v), 1)
215 template <
typename DataType>
explicit constexpr MutableMemoryView(ArrayView<DataType> v, Int32 nb_component)
216 : MutableMemoryView(Span<DataType>(v), nb_component)
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)
223 m_bytes = SpanType(x.data(), x.size() * nb_component);
228 template <
typename DataType>
constexpr MutableMemoryView&
229 operator=(Span<DataType> v)
232 m_nb_element = v.size();
233 m_datatype_size =
static_cast<Int32
>(
sizeof(DataType));
239 constexpr MutableMemoryView(Span<std::byte> bytes, Int32 datatype_size, Int64 nb_element)
241 , m_nb_element(nb_element)
242 , m_datatype_size(datatype_size)
247 constexpr operator ConstMemoryView()
const {
return { m_bytes, m_datatype_size, m_nb_element }; }
252 constexpr SpanType bytes()
const {
return m_bytes; }
255 constexpr std::byte* data()
const {
return m_bytes.data(); }
258 constexpr Int64 nbElement()
const {
return m_nb_element; }
261 constexpr Int32 datatypeSize()
const {
return m_datatype_size; }
264 constexpr MutableMemoryView subView(Int64 begin_index, Int64 nb_element)
const
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 };
280 void copyHost(ConstMemoryView v)
const;
296 void copyFromIndexesHost(ConstMemoryView v, Span<const Int32> indexes)
const;
314 void copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
315 RunQueue* run_queue =
nullptr)
const;
337 void fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
338 RunQueue* run_queue =
nullptr)
const;
357 void fill(ConstMemoryView v, RunQueue* run_queue =
nullptr)
const;
361 ARCANE_DEPRECATED_REASON(
"Use bytes() instead")
362 constexpr SpanType span()
const {
return m_bytes; }
364 ARCANE_DEPRECATED_REASON(
"Use bytes().size() instead")
365 constexpr Int64 size()
const {
return m_bytes.size(); }
370 Int64 m_nb_element = 0;
371 Int32 m_datatype_size = 0;
383class ARCANE_UTILS_EXPORT ConstMultiMemoryView
387 ConstMultiMemoryView(SmallSpan<
const Span<const std::byte>> views, Int32 datatype_size)
389 , m_datatype_size(datatype_size)
391 ConstMultiMemoryView(SmallSpan<
const Span<std::byte>> views, Int32 datatype_size)
392 : m_datatype_size(datatype_size)
394 auto* ptr =
reinterpret_cast<const Span<const std::byte>*
>(views.data());
395 m_views = { ptr, views.size() };
422 void copyToIndexes(MutableMemoryView v, SmallSpan<const Int32> indexes,
423 RunQueue* run_queue =
nullptr);
427 SmallSpan<const Span<const std::byte>> m_views;
428 Int32 m_datatype_size;
440class ARCANE_UTILS_EXPORT MutableMultiMemoryView
444 MutableMultiMemoryView(SmallSpan<Span<std::byte>> views, Int32 datatype_size)
446 , m_datatype_size(datatype_size)
473 void copyFromIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
474 RunQueue* run_queue =
nullptr);
498 void fillIndexes(ConstMemoryView v, SmallSpan<const Int32> indexes,
499 RunQueue* run_queue =
nullptr);
520 void fill(ConstMemoryView v, RunQueue* run_queue =
nullptr);
524 SmallSpan<Span<std::byte>> m_views;
525 Int32 m_datatype_size;
535template <
typename DataType> ConstMemoryView
536makeMemoryView(Span<DataType> v)
538 return ConstMemoryView(v);
542template <
typename DataType> ConstMemoryView
543makeMemoryView(
const DataType* v)
545 return ConstMemoryView(Span<const DataType>(v, 1));
552template <
typename DataType> MutableMemoryView
553makeMutableMemoryView(Span<DataType> v)
555 return MutableMemoryView(v);
562template <
typename DataType> MutableMemoryView
563makeMutableMemoryView(DataType* v)
565 return MutableMemoryView(Span<DataType>(v, 1));
579extern "C++" ARCANE_UTILS_EXPORT MutableMemoryView
580makeMutableMemoryView(
void* ptr, Int32 datatype_size, Int64 nb_element);
593extern "C++" ARCANE_UTILS_EXPORT ConstMemoryView
594makeConstMemoryView(
const void* ptr, Int32 datatype_size, Int64 nb_element);
-*- 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.
detail::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.