muda
vector.h
1 #pragma once
2 #include <muda/tools/version.h>
3 #include <thrust/device_allocator.h>
4 
5 #include <thrust/device_vector.h>
6 #include <thrust/host_vector.h>
7 #include <vector>
8 
9 #include <muda/muda_def.h>
11 #include <muda/viewer/dense.h>
12 
13 namespace muda
14 {
15 namespace details
16 {
17  template <typename T, typename Alloc>
18  using vector_base = thrust::detail::vector_base<T, Alloc>;
19 }
20 
21 template <typename T>
22 class DeviceVector : public thrust::device_vector<T, thrust::device_allocator<T>>
23 {
24  public:
25  using Base = thrust::device_vector<T, thrust::device_allocator<T>>;
26  using Base::Base;
27  using Base::operator=;
28 
29  auto view() MUDA_NOEXCEPT { return BufferView<T>{raw_ptr(), Base::size()}; }
30 
31  auto view() const MUDA_NOEXCEPT
32  {
33  return CBufferView<T>{raw_ptr(), Base::size()};
34  }
35 
36  operator BufferView<T>() const MUDA_NOEXCEPT { return view(); }
37  operator CBufferView<T>() const MUDA_NOEXCEPT { return view(); }
38 
39  DeviceVector& operator=(CBufferView<T> v)
40  {
41  this->resize(v.size());
42  view().copy_from(v);
43  return *this;
44  }
45 
46  void copy_to(std::vector<T>& v) const
47  {
48  v.resize(this->size());
49  view().copy_to(v.data());
50  }
51 
52  auto viewer() MUDA_NOEXCEPT
53  {
54  return Dense1D<T>(raw_ptr(), static_cast<int>(this->size()));
55  }
56 
57  auto cviewer() const MUDA_NOEXCEPT
58  {
59  return CDense1D<T>(raw_ptr(), static_cast<int>(this->size()));
60  }
61 
62  private:
63  T* raw_ptr() { return thrust::raw_pointer_cast(Base::data()); }
64  const T* raw_ptr() const { return thrust::raw_pointer_cast(Base::data()); }
65 };
66 
67 template <typename T>
68 class HostVector : public thrust::host_vector<T, std::allocator<T>>
69 {
70  public:
71  using thrust::host_vector<T, std::allocator<T>>::host_vector;
72  using thrust::host_vector<T, std::allocator<T>>::operator=;
73 };
74 } // namespace muda
75 
76 
77 //namespace muda
78 //{
79 //template <typename T>
80 //MUDA_INLINE MUDA_HOST auto make_dense(DeviceVector<T>& v) MUDA_NOEXCEPT
81 //{
82 // return make_dense(v.view());
83 //}
84 //template <typename T>
85 //MUDA_INLINE MUDA_HOST auto make_cdense(const DeviceVector<T>& v) MUDA_NOEXCEPT
86 //{
87 // return make_cdense(v.view());
88 //}
89 //template <typename T>
90 //MUDA_INLINE MUDA_HOST auto make_viewer(DeviceVector<T>& v) MUDA_NOEXCEPT
91 //{
92 // return make_viewer(v.view());
93 //}
94 //template <typename T>
95 //MUDA_INLINE MUDA_HOST auto make_cviewer(const DeviceVector<T>& v) MUDA_NOEXCEPT
96 //{
97 // return make_cviewer(v.view());
98 //}
99 //template <typename T>
100 //MUDA_INLINE MUDA_HOST auto make_dense2D(DeviceVector<T>& v, int dimy) MUDA_NOEXCEPT
101 //{
102 // return make_dense2D(v.view(), dimy);
103 //}
104 //template <typename T>
105 //MUDA_INLINE MUDA_HOST auto make_cdense2D(const DeviceVector<T>& v, int dimy) MUDA_NOEXCEPT
106 //{
107 // return make_cdense2D(v.view(), dimy);
108 //}
109 //template <typename T>
110 //MUDA_INLINE MUDA_HOST auto make_dense2D(DeviceVector<T>& v, int dimx, int dimy) MUDA_NOEXCEPT
111 //{
112 // return make_dense2D(v.view(), dimx, dimy);
113 //}
114 //template <typename T>
115 //MUDA_INLINE MUDA_HOST auto make_cdense2D(const DeviceVector<T>& v, int dimx, int dimy) MUDA_NOEXCEPT
116 //{
117 // return make_cdense2D(v.view(), dimx, dimy);
118 //}
119 //template <typename T>
120 //MUDA_INLINE MUDA_HOST auto make_dense2D(DeviceVector<T>& v, const int2& dim) MUDA_NOEXCEPT
121 //{
122 // return make_dense2D(v.view(), dim.x, dim.y);
123 //}
124 //template <typename T>
125 //MUDA_INLINE MUDA_HOST auto make_cdense2D(const DeviceVector<T>& v, const int2& dim) MUDA_NOEXCEPT
126 //{
127 // return make_cdense2D(v.view(), dim.x, dim.y);
128 //}
129 //template <typename T>
130 //MUDA_INLINE MUDA_HOST auto make_dense3D(DeviceVector<T>& v, int dimy, int dimz) MUDA_NOEXCEPT
131 //{
132 // return make_dense3D(v.view(), dimy, dimz);
133 //}
134 //template <typename T>
135 //MUDA_INLINE MUDA_HOST auto make_cdense3D(const DeviceVector<T>& v, int dimy, int dimz) MUDA_NOEXCEPT
136 //{
137 // return make_cdense3D(v.view(), dimy, dimz);
138 //}
139 //template <typename T>
140 //MUDA_INLINE MUDA_HOST auto make_dense3D(DeviceVector<T>& v, const int2& dimyz) MUDA_NOEXCEPT
141 //{
142 // return make_dense3D(v.view(), dimyz.x, dimyz.y);
143 //}
144 //template <typename T>
145 //MUDA_INLINE MUDA_HOST auto make_cdense3D(const DeviceVector<T>& v, const int2& dimyz) MUDA_NOEXCEPT
146 //{
147 // return make_cdense3D(v.view(), dimyz.x, dimyz.y);
148 //}
149 //template <typename T>
150 //MUDA_INLINE MUDA_HOST auto make_dense3D(DeviceVector<T>& v, int dimx, int dimy, int dimz) MUDA_NOEXCEPT
151 //{
152 // return make_dense3D(v.view(), dimx, dimy, dimz);
153 //}
154 //template <typename T>
155 //MUDA_INLINE MUDA_HOST auto make_cdense3D(const DeviceVector<T>& v, int dimx, int dimy, int dimz) MUDA_NOEXCEPT
156 //{
157 // return make_cdense3D(v.view(), dimx, dimy, dimz);
158 //}
159 //template <typename T>
160 //MUDA_INLINE MUDA_HOST auto make_dense3D(DeviceVector<T>& v, const int3& dim) MUDA_NOEXCEPT
161 //{
162 // return make_dense3D(v.view(), dim.x, dim.y, dim.z);
163 //}
164 //template <typename T>
165 //MUDA_INLINE MUDA_HOST auto make_cdense3D(const DeviceVector<T>& v, const int3& dim) MUDA_NOEXCEPT
166 //{
167 // return make_cdense3D(v.view(), dim.x, dim.y, dim.z);
168 //}
169 //} // namespace muda
A view interface for any array-like liner memory, which can be constructed from DeviceBuffer/DeviceVe...
Definition: assert.h:13