MUDA
Loading...
Searching...
No Matches
buffer_view.inl
1#include <muda/buffer/buffer_launch.h>
2#include <muda/compute_graph/compute_graph_builder.h>
3
4namespace muda
5{
6template <bool IsConst, typename T>
7MUDA_GENERIC BufferViewT<IsConst, T>::BufferViewT(auto_const_t<T>* data,
8 size_t offset,
9 size_t size) MUDA_NOEXCEPT
10 : m_data(data),
11 m_offset(offset),
12 m_size(size)
13{
14}
15
16template <bool IsConst, typename T>
17MUDA_GENERIC BufferViewT<IsConst, T>::BufferViewT(auto_const_t<T>* data, size_t size) MUDA_NOEXCEPT
18 : m_data(data),
19 m_offset(0),
20 m_size(size)
21{
22}
23
24template <bool IsConst, typename T>
25template <bool OtherIsConst>
26MUDA_GENERIC BufferViewT<IsConst, T>::BufferViewT(const BufferViewT<OtherIsConst, T>& other) MUDA_NOEXCEPT
27 MUDA_REQUIRES(!OtherIsConst)
28 : m_data(other.m_data)
29 , m_offset(other.m_offset)
30 , m_size(other.m_size)
31{
32 static_assert(!OtherIsConst, "This must be non-const");
33}
34template <bool IsConst, typename T>
35MUDA_GENERIC auto BufferViewT<IsConst, T>::as_const() const MUDA_NOEXCEPT->ConstView
36{
37 return ConstView{*this};
38}
39
40template <bool IsConst, typename T>
41MUDA_GENERIC auto BufferViewT<IsConst, T>::data() const MUDA_NOEXCEPT->auto_const_t<T>*
42{
43 return m_data + m_offset;
44}
45
46template <bool IsConst, typename T>
47MUDA_GENERIC auto BufferViewT<IsConst, T>::data(size_t i) const MUDA_NOEXCEPT->auto_const_t<T>*
48{
49 i += m_offset;
50 return m_data + i;
51}
52
53template <bool IsConst, typename T>
54MUDA_GENERIC auto BufferViewT<IsConst, T>::origin_data() const MUDA_NOEXCEPT->auto_const_t<T>*
55{
56 return m_data;
57}
58
59template <bool IsConst, typename T>
60MUDA_GENERIC auto BufferViewT<IsConst, T>::operator[](size_t i) const
61 MUDA_NOEXCEPT->auto_const_t<T>&
62{
63 return *data(i);
64}
65
66template <bool IsConst, typename T>
67MUDA_GENERIC auto BufferViewT<IsConst, T>::subview(size_t offset, size_t size) const
68 MUDA_NOEXCEPT->ThisView
69{
70#ifndef __CUDA_ARCH__
71 if(ComputeGraphBuilder::is_topo_building())
72 return ThisView{}; // dummy
73#endif
74 if(size == ~0)
75 size = m_size - offset;
76 MUDA_KERNEL_ASSERT(offset + size <= m_size,
77 "BufferView out of range, size = %d, yours = %d",
78 m_size,
79 offset + size);
80 return ThisView{m_data, m_offset + offset, size};
81}
82
83template <bool IsConst, typename T>
84MUDA_GENERIC auto BufferViewT<IsConst, T>::viewer() const MUDA_NOEXCEPT->ThisViewer
85{
86 return ThisViewer{data(), static_cast<int>(m_size)};
87}
88
89template <bool IsConst, typename T>
90MUDA_GENERIC auto BufferViewT<IsConst, T>::cviewer() const MUDA_NOEXCEPT->CViewer
91{
92 return CViewer{data(), static_cast<int>(m_size)};
93}
94
95template <bool IsConst, typename T>
96MUDA_HOST void BufferViewT<IsConst, T>::fill(const T& v) const MUDA_REQUIRES(!IsConst)
97{
98 static_assert(!IsConst, "This must be non-const");
99
100 BufferLaunch()
101 .template fill<T>(*this, v) //
102 .wait();
103}
104
105template <bool IsConst, typename T>
106MUDA_HOST void BufferViewT<IsConst, T>::copy_from(const BufferViewT<true, T>& other) const
107 MUDA_REQUIRES(!IsConst)
108{
109 static_assert(!IsConst, "This must be non-const");
110
111 BufferLaunch()
112 .template copy<T>(*this, other) //
113 .wait();
114}
115
116template <bool IsConst, typename T>
117MUDA_HOST void BufferViewT<IsConst, T>::copy_from(const T* host) const
118 MUDA_REQUIRES(!IsConst)
119{
120 static_assert(!IsConst, "This must be non-const");
121
122 BufferLaunch()
123 .template copy<T>(*this, host) //
124 .wait();
125}
126
127template <bool IsConst, typename T>
128MUDA_HOST void BufferViewT<IsConst, T>::copy_to(T* host) const
129{
130 BufferLaunch()
131 .template copy<T>(host, *this) //
132 .wait();
133}
134
135template <bool IsConst, typename T>
136MUDA_GENERIC auto BufferViewT<IsConst, T>::operator+(int i) const MUDA_NOEXCEPT->ThisView
137{
138 return ThisView{m_data, m_offset + i, m_size - i};
139}
140
141template <bool IsConst, typename T>
142MUDA_GENERIC auto BufferViewT<IsConst, T>::operator*() const MUDA_NOEXCEPT->reference
143{
144 return *data(0);
145}
146
147template <bool IsConst, typename T>
148MUDA_GENERIC auto BufferViewT<IsConst, T>::operator[](int i) const MUDA_NOEXCEPT->auto_const_t<T>&
149{
150 return *data(i);
151}
152} // namespace muda