34 #include <cugar/basic/types.h> 35 #include <cugar/basic/iterator.h> 36 #include <cugar/basic/thrust_view.h> 37 #include <cugar/basic/vector_view.h> 38 #include <cugar/basic/cuda/allocator.h> 39 #include <thrust/host_vector.h> 40 #include <thrust/device_vector.h> 71 template<
typename TTargetVector,
typename TSourceVector>
72 static CUGAR_FORCEINLINE
void thrust_copy_vector(TTargetVector& target, TSourceVector& source)
74 if (target.size() != source.size())
77 target.resize(source.size());
80 thrust::copy(source.begin(), source.end(), target.begin());
83 template<
typename TTargetVector,
typename TSourceVector>
84 static CUGAR_FORCEINLINE
void thrust_copy_vector(TTargetVector& target, TSourceVector& source, uint32 count)
86 if (target.size() != count)
92 thrust::copy(source.begin(), source.begin() + count, target.begin());
99 template <
typename system_tag,
typename T>
102 template <
typename T>
105 typedef std::allocator<T> type;
108 template <
typename T>
111 typedef thrust::device_malloc_allocator<T> type;
116 template <typename system_tag, typename T, typename Alloc = typename default_vector_allocator<system_tag,T>::type>
121 template <
typename T,
typename Alloc>
126 typedef thrust::host_vector<T,Alloc> base_type;
127 typedef typename base_type::const_iterator const_iterator;
128 typedef typename base_type::iterator iterator;
129 typedef typename base_type::value_type value_type;
136 vector(
const size_t size = 0,
const T val = T()) : base_type( size, val ) {}
138 template <
typename OtherAlloc>
139 vector(
const thrust::host_vector<T,OtherAlloc>& v) : base_type( v ) {}
141 template <
typename OtherAlloc>
142 vector(
const thrust::device_vector<T,OtherAlloc>& v) : base_type( v ) {}
144 template <
typename OtherAlloc>
145 vector<host_tag,T,Alloc>& operator= (
const thrust::host_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
147 template <
typename OtherAlloc>
148 vector<host_tag,T,Alloc>& operator= (
const thrust::device_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
152 operator plain_view_type() {
return plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
156 operator const_plain_view_type()
const {
return const_plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
166 T& last() {
return (*
this)[size() - 1]; }
167 const T& last()
const {
return (*
this)[size() - 1]; }
172 template <
typename T,
typename Alloc>
177 typedef thrust::device_vector<T,Alloc> base_type;
178 typedef typename base_type::const_iterator const_iterator;
179 typedef typename base_type::iterator iterator;
180 typedef typename base_type::value_type value_type;
187 vector(
const size_t size = 0,
const T val = T()) : base_type( size, val ) {}
189 template <
typename OtherAlloc>
190 vector(
const thrust::host_vector<T,OtherAlloc>& v) : base_type( v ) {}
192 template <
typename OtherAlloc>
193 vector(
const thrust::device_vector<T,OtherAlloc>& v) : base_type( v ) {}
195 template <
typename OtherAlloc>
196 vector<device_tag,T,Alloc>& operator= (
const thrust::host_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
198 template <
typename OtherAlloc>
199 vector<device_tag,T,Alloc>& operator= (
const thrust::device_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
203 operator plain_view_type() {
return plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
207 operator const_plain_view_type()
const {
return const_plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
217 T& last() {
return (*
this)[size() - 1]; }
218 const T& last()
const {
return (*
this)[size() - 1]; }
223 template <
typename T>
229 typedef typename base_type::const_iterator const_iterator;
230 typedef typename base_type::iterator iterator;
231 typedef typename base_type::value_type value_type;
233 typedef typename base_type::plain_view_type plain_view_type;
234 typedef typename base_type::const_plain_view_type const_plain_view_type;
238 host_vector(
const size_t size = 0,
const T val = T()) : base_type( size, val ) {}
240 template <
typename OtherAlloc>
241 host_vector(
const thrust::host_vector<T,OtherAlloc>& v) : base_type( v ) {}
243 template <
typename OtherAlloc>
244 host_vector(
const thrust::device_vector<T,OtherAlloc>& v) : base_type( v ) {}
246 template <
typename OtherAlloc>
247 host_vector<T>& operator= (
const thrust::host_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
249 template <
typename OtherAlloc>
250 host_vector<T>& operator= (
const thrust::device_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
254 operator plain_view_type() {
return plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
258 operator const_plain_view_type()
const {
return const_plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
263 template <
typename T>
269 typedef typename base_type::const_iterator const_iterator;
270 typedef typename base_type::iterator iterator;
271 typedef typename base_type::value_type value_type;
273 typedef typename base_type::plain_view_type plain_view_type;
274 typedef typename base_type::const_plain_view_type const_plain_view_type;
278 device_vector(
const size_t size = 0,
const T val = T()) : base_type( size, val ) {}
280 template <
typename OtherAlloc>
281 device_vector(
const thrust::host_vector<T,OtherAlloc>& v) : base_type( v ) {}
283 template <
typename OtherAlloc>
284 device_vector(
const thrust::device_vector<T,OtherAlloc>& v) : base_type( v ) {}
286 template <
typename OtherAlloc>
287 device_vector<T>& operator= (
const thrust::host_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
289 template <
typename OtherAlloc>
290 device_vector<T>& operator= (
const thrust::device_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
294 operator plain_view_type() {
return plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
298 operator const_plain_view_type()
const {
return const_plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
303 template <
typename T>
309 typedef typename base_type::const_iterator const_iterator;
310 typedef typename base_type::iterator iterator;
311 typedef typename base_type::value_type value_type;
313 typedef typename base_type::plain_view_type plain_view_type;
314 typedef typename base_type::const_plain_view_type const_plain_view_type;
320 template <
typename OtherAlloc>
323 template <
typename OtherAlloc>
326 template <
typename OtherAlloc>
327 caching_device_vector<T>& operator= (
const thrust::host_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
329 template <
typename OtherAlloc>
330 caching_device_vector<T>& operator= (
const thrust::device_vector<T,OtherAlloc>& v) { cuda::thrust_copy_vector( *
this, v );
return *
this; }
334 operator plain_view_type() {
return plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
338 operator const_plain_view_type()
const {
return const_plain_view_type( base_type::size(),
cugar::raw_pointer( *
this ) ); }
349 template <
typename T>
353 return typename device_iterator_type<T>::type( it );
356 template <
typename domain_tag,
typename T>
359 if (vec.size() < min_size)
360 vec.resize(min_size);
365 template <
typename T>
368 typedef typename thrust::device_vector<T>::const_reference const_reference;
369 typedef typename thrust::device_vector<T>::reference reference;
423 template <
typename T>
428 template <
typename T>
device_vector(const size_t size=0, const T val=T())
Definition: vector.h:278
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator+=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:931
void expand(const size_t sz)
Definition: vector.h:160
Definition: vector_view.h:87
vector(const size_t size=0, const T val=T())
Definition: vector.h:187
device_iterator_type< T >::type device_iterator(const T it)
Definition: vector.h:350
T * raw_pointer(thrust::device_vector< T, Alloc > &vec)
Definition: thrust_view.h:69
host_vector(const size_t size=0, const T val=T())
Definition: vector.h:238
void expand(const size_t sz)
Definition: vector.h:211
const T * ptr() const
Definition: vector.h:412
T * ptr()
Definition: vector.h:416
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
device_var(const T val=T())
Definition: vector.h:373
vector(const size_t size=0, const T val=T())
Definition: vector.h:136
CUGAR_HOST_DEVICE diff_var< VT, N, O > & operator-=(diff_var< VT, N, O > &a, const diff_var< VT, N, O > b)
Definition: diff.h:940
caching_device_vector(const size_t size=0, const T val=T())
Definition: vector.h:318