OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
map_vector_std_cuda.hpp
1/*
2 * map_vector_std_cuda.hpp
3 *
4 * Created on: Mar 7, 2019
5 * Author: i-bird
6 */
7
8#ifndef MAP_VECTOR_STD_CUDA_HPP_
9#define MAP_VECTOR_STD_CUDA_HPP_
10
11#include "map_vector_std_cuda_ker.cuh"
12
24template<typename T>
25class vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR>
26{
27 // Memory layout
28 typedef typename memory_traits_inte<aggregate<T>>::type layout;
29
31 vector<aggregate<T>,CudaMemory,memory_traits_inte,grow_policy_double> base;
32
34 size_t err_code = 0;
35
36public:
37
39 typedef int yes_i_am_vector;
40
42
44 typedef vector_key_iterator iterator_key;
46 typedef T value_type;
47
48 typedef void base_to_copy;
49
51 typedef grow_policy_double grow_policy;
52
53 template<typename Tobj>
54 struct layout_base__
55 {
57 };
58
60 inline size_t size() const
61 {
62 return base.size();
63 }
64
65
71 inline void resize(size_t slot)
72 {
73 base.resize_no_device(slot);
74 }
75
79 inline void clear()
80 {
81 base.clear();
82 }
83
93 inline void add(const T & v)
94 {
95 base.add_no_device();
96 base.template get<0>(size()-1) = v;
97 }
98
108 inline void add(T && v)
109 {
110 base.add_no_device();
111 base.template get<0>(size()-1).swap(v);
112 }
113
117 inline void add()
118 {
119 base.add_no_device();
120 }
121
127 inline T & last()
128 {
129#ifdef SE_CLASS1
130 if (base.size() == 0)
131 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
132#endif
133 return base.template get<0>(size()-1);
134 }
135
141 inline const T & last() const
142 {
143#ifdef SE_CLASS1
144 if (base.size() == 0)
145 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " vector of size 0\n";
146#endif
147 return base.template get<0>(size()-1);
148 }
149
155 void swap(vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> && v)
156 {
157 base.swap(v.base);
158 }
159
160 inline T& at(int i)
161 {
162 return get(i);
163 }
164
172 inline T& operator[](int id)
173 {
174#ifdef SE_CLASS1
175 vector_overflow(id);
176#endif
177
178 return base.template get<0>(id);
179 }
180
188 inline T& get(int id)
189 {
190#ifdef SE_CLASS1
191 vector_overflow(id);
192#endif
193
194 return base.template get<0>(id);
195 }
196
206 inline const T& get(int id) const
207 {
208#ifdef SE_CLASS1
209 vector_overflow(id);
210#endif
211
212 return base.template get<0>(id);
213 }
214
222 inline T & get(size_t id)
223 {
224#ifdef SE_CLASS1
225 vector_overflow(id);
226#endif
227 return base.template get<0>(id);
228 }
229
237 inline const T & get(size_t id) const
238 {
239#ifdef SE_CLASS1
240 vector_overflow(id);
241#endif
242 return base.template get<0>(id);
243 }
244
250 inline void reserve(size_t ns)
251 {
252 base.reserve(ns);
253 }
254
256 vector() noexcept
257 :err_code(0)
258 {
259 }
260
262 vector(size_t sz) noexcept
263 :base(sz),err_code(0)
264 {
265 }
266
268 vector(const vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> & v) noexcept
269 :err_code(0)
270 {
271 base = v.base;
272 }
273
279 vector(const std::initializer_list<T> & v)
280 // :base(v)
281 {
282 // causes error if simply passed to base
283 for (T t: v)
284 add(t);
285 }
286
288 vector(vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> && v) noexcept
289 :err_code(0)
290 {
291 base.swap(v.base);
292 }
293
295 template< class InputIt >
296 vector(InputIt first, InputIt last)
297 {
298 while(first != last) {
299 add(*first);
300 ++first;
301 }
302 }
303
305 ~vector() noexcept
306 {
307 }
308
314 void swap(vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> & v)
315 {
316 base.swap(v.base);
317 }
318
326 vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> &
327 operator=(const vector<T,CudaMemory,memory_traits_inte,grow_policy_double,STD_VECTOR> & v)
328 {
329 base = v.base;
330
331 return *this;
332 }
333
339 vector_key_iterator getIterator() const
340 {
341 return vector_key_iterator(base.size());
342 }
343
351 vector_key_iterator getIteratorTo(size_t k) const
352 {
353 return vector_key_iterator(k);
354 }
355
361 void * getPointer()
362 {
363 return &base.template get<0>(0);
364 }
365
371 const void * getPointer() const
372 {
373 return &base.template get<0>(0);
374 }
375
379 template<unsigned int ... prp> void hostToDevice()
380 {
381 base.template hostToDevice<0>();
382 }
383
387 template<unsigned int ... prp> void deviceToHost()
388 {
389 base.template deviceToHost<0>();
390 }
391
396 template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
397 {
398 base.template deviceToHost<0>(start,stop);
399 }
400
405 template<unsigned int ... prp> void hostToDevice(size_t start, size_t stop)
406 {
407 base.template hostToDevice<0>(start,stop);
408 }
409
418 {
420
421 return v;
422 }
423
429 static bool noPointers()
430 {
431 return false;
432 }
433
440 {
441 return err_code;
442 }
443
449 inline void vector_overflow(size_t v1) const
450 {
451 if (v1 >= base.size())
452 {
453 std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
454 size_t * err_code_pointer = (size_t *)&this->err_code;\
455 *err_code_pointer = 2001;\
456
457 ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
458 }
459 }
460};
461
462
463#endif /* MAP_VECTOR_STD_CUDA_HPP_ */
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
vector(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &&v) noexcept
Constructor from another vector.
void add(T &&v)
It insert a new object on the vector, eventually it reallocate the grid.
vector< aggregate< T >, CudaMemory, memory_traits_inte, grow_policy_double > base
1-D static grid
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > & operator=(const vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &v)
Operator= copy the vector into another.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
vector_key_iterator getIterator() const
Get an iterator over all the elements of the vector.
const void * getPointer() const
Return the pointer to the chunk of memory.
vector_key_iterator getIteratorTo(size_t k) const
Get iterator until a specified element.
void swap(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &v)
vector_custd_ker< typename apply_transform< memory_traits_inte, aggregate< T > >::type, memory_traits_inte > toKernel()
Convert the grid into a data-structure compatible for computing into GPU.
vector(const std::initializer_list< T > &v)
Initializer from constructor.
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
void swap(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &&v)
swap the memory between the two vector
vector(const vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
Transform the boost::fusion::vector into memory specification (memory_traits)
grid interface available when on gpu