MUDA
Loading...
Searching...
No Matches
device_buffer_2d.inl
1#include <muda/buffer/buffer_launch.h>
2
3namespace muda
4{
5template <typename T>
6DeviceBuffer2D<T>::DeviceBuffer2D(const Extent2D& n)
7{
8 BufferLaunch()
9 .resize(*this, n) //
10 .wait();
11}
12
13template <typename T>
14DeviceBuffer2D<T>::DeviceBuffer2D()
15 : m_data(nullptr)
16 , m_pitch_bytes(0)
17 , m_extent(Extent2D::Zero())
18 , m_capacity(Extent2D::Zero())
19{
20}
21
22template <typename T>
23DeviceBuffer2D<T>::DeviceBuffer2D(const DeviceBuffer2D<T>& other)
24{
25 BufferLaunch()
26 .resize(*this, other.extent()) //
27 .copy(view(), other.view()) //
28 .wait();
29}
30
31template <typename T>
32DeviceBuffer2D<T>::DeviceBuffer2D(DeviceBuffer2D<T>&& other) MUDA_NOEXCEPT
33 : m_data(other.m_data),
34 m_pitch_bytes(other.m_pitch_bytes),
35 m_extent(other.m_extent),
36 m_capacity(other.m_capacity)
37{
38 other.m_data = nullptr;
39 other.m_pitch_bytes = 0;
40 other.m_extent = Extent2D::Zero();
41 other.m_capacity = Extent2D::Zero();
42}
43
44template <typename T>
45DeviceBuffer2D<T>& DeviceBuffer2D<T>::operator=(const DeviceBuffer2D<T>& other)
46{
47 if(this == &other)
48 return *this;
49
50 BufferLaunch()
51 .resize(*this, other.extent()) //
52 .copy(view(), other.view()) //
53 .wait();
54
55 return *this;
56}
57
58template <typename T>
59DeviceBuffer2D<T>& DeviceBuffer2D<T>::operator=(DeviceBuffer2D<T>&& other)
60{
61 if(this == &other)
62 return *this;
63
64 if(m_data)
65 BufferLaunch().free(*this).wait();
66
67 m_data = other.m_data;
68 m_pitch_bytes = other.m_pitch_bytes;
69 m_extent = other.m_extent;
70 m_capacity = other.m_capacity;
71
72 other.m_data = nullptr;
73 other.m_pitch_bytes = 0;
74 other.m_extent = Extent2D::Zero();
75 other.m_capacity = Extent2D::Zero();
76
77 return *this;
78}
79
80template <typename T>
81DeviceBuffer2D<T>::DeviceBuffer2D(CBuffer2DView<T> other)
82{
83 BufferLaunch()
84 .alloc(*this, other.extent()) //
85 .copy(view(), other) //
86 .wait();
87}
88
89template <typename T>
90DeviceBuffer2D<T>& DeviceBuffer2D<T>::operator=(CBuffer2DView<T> other)
91{
92 BufferLaunch()
93 .resize(*this, other.extent()) //
94 .copy(view(), other) //
95 .wait();
96
97 return *this;
98}
99
100
101template <typename T>
102void DeviceBuffer2D<T>::copy_to(std::vector<T>& host) const
103{
104 host.resize(total_size());
105 view().copy_to(host.data());
106}
107
108
109template <typename T>
110void DeviceBuffer2D<T>::copy_from(const std::vector<T>& host)
111{
112 MUDA_ASSERT(host.size() == total_size(),
113 "Need eqaul total size, host_size=%d, total_size=%d",
114 host.size(),
115 total_size());
116
117 view().copy_from(host.data());
118}
119
120template <typename T>
121void DeviceBuffer2D<T>::resize(Extent2D new_extent)
122{
123 BufferLaunch()
124 .resize(*this, new_extent) //
125 .wait();
126}
127
128template <typename T>
129void DeviceBuffer2D<T>::resize(Extent2D new_extent, const T& value)
130{
131 BufferLaunch()
132 .resize(*this, new_extent, value) //
133 .wait();
134}
135
136template <typename T>
137void DeviceBuffer2D<T>::reserve(Extent2D new_capacity)
138{
139 BufferLaunch()
140 .reserve(*this, new_capacity) //
141 .wait();
142}
143
144template <typename T>
145void DeviceBuffer2D<T>::clear()
146{
147 BufferLaunch().clear(*this).wait();
148}
149
150template <typename T>
151void DeviceBuffer2D<T>::shrink_to_fit()
152{
153 BufferLaunch().shrink_to_fit(*this).wait();
154}
155
156template <typename T>
157void DeviceBuffer2D<T>::fill(const T& v)
158{
159 BufferLaunch().fill(view(), v).wait();
160}
161
162template <typename T>
163DeviceBuffer2D<T>::~DeviceBuffer2D()
164{
165 if(m_data)
166 BufferLaunch().free(*this).wait();
167}
168} // namespace muda