MUDA
Loading...
Searching...
No Matches
device_var.inl
1#include <muda/buffer/buffer_launch.h>
2#include <muda/launch/memory.h>
3
4namespace muda
5{
6template <typename T>
7DeviceVar<T>::DeviceVar()
8{
9 Memory().alloc(&m_data, sizeof(T)).wait();
10}
11template <typename T>
12DeviceVar<T>::DeviceVar(const T& value)
13{
14 Memory().alloc(&m_data, sizeof(T));
15 view().copy_from(&value);
16};
17
18template <typename T>
19DeviceVar<T>::DeviceVar(const DeviceVar& other)
20{
21 Memory().alloc(&m_data, sizeof(T)).wait();
22 view().copy_from(other.view());
23}
24
25template <typename T>
26DeviceVar<T>& DeviceVar<T>::operator=(const DeviceVar<T>& other)
27{
28 if(this == &other)
29 return *this;
30 view().copy_from(other.view());
31 return *this;
32}
33
34template <typename T>
35DeviceVar<T>& DeviceVar<T>::operator=(DeviceVar<T>&& other)
36{
37 if(this == &other)
38 return *this;
39
40 if(m_data)
41 Memory().free(m_data).wait();
42
43 m_data = other.m_data;
44
45 other.m_data = nullptr;
46
47 return *this;
48}
49
50template <typename T>
51DeviceVar<T>& DeviceVar<T>::operator=(CVarView<T> other)
52{
53 view().copy_from(other);
54 return *this;
55}
56
57template <typename T>
58void DeviceVar<T>::copy_from(CVarView<T> other)
59{
60 view().copy_from(other);
61}
62
63template <typename T>
64DeviceVar<T>& DeviceVar<T>::operator=(const T& val)
65{
66 view().copy_from(&val);
67 return *this;
68}
69
70template <typename T>
71DeviceVar<T>::DeviceVar(DeviceVar&& other) MUDA_NOEXCEPT : m_data(other.m_data)
72{
73 other.m_data = nullptr;
74}
75
76template <typename T>
77DeviceVar<T>::operator T() const
78{
79 T var;
80 view().copy_to(&var);
81 return var;
82}
83
84template <typename T>
85Dense<T> DeviceVar<T>::viewer() MUDA_NOEXCEPT
86{
87 return Dense<T>(m_data);
88}
89
90template <typename T>
91CDense<T> DeviceVar<T>::cviewer() const MUDA_NOEXCEPT
92{
93 return CDense<T>(m_data);
94}
95
96template <typename T>
97DeviceVar<T>::~DeviceVar()
98{
99 if(m_data)
100 Memory().free(m_data).wait();
101}
102} // namespace muda