OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
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 
24 template<typename T>
25 class 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 
36 public:
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 
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 
269  :err_code(0)
270  {
271  base = v.base;
272  }
273 
279  vector(const std::initializer_list<T> & v)
280  :base(v)
281  {
282  }
283 
286  :err_code(0)
287  {
288  base.swap(v.base);
289  }
290 
292  ~vector() noexcept
293  {
294  }
295 
302  {
303  base.swap(v.base);
304  }
305 
315  {
316  base = v.base;
317 
318  return *this;
319  }
320 
326  vector_key_iterator getIterator() const
327  {
328  return vector_key_iterator(base.size());
329  }
330 
338  vector_key_iterator getIteratorTo(size_t k) const
339  {
340  return vector_key_iterator(k);
341  }
342 
348  void * getPointer()
349  {
350  return &base.template get<0>(0);
351  }
352 
358  const void * getPointer() const
359  {
360  return &base.template get<0>(0);
361  }
362 
366  template<unsigned int ... prp> void hostToDevice()
367  {
368  base.template hostToDevice<0>();
369  }
370 
374  template<unsigned int ... prp> void deviceToHost()
375  {
376  base.template deviceToHost<0>();
377  }
378 
383  template<unsigned int ... prp> void deviceToHost(size_t start, size_t stop)
384  {
385  base.template deviceToHost<0>(start,stop);
386  }
387 
392  template<unsigned int ... prp> void hostToDevice(size_t start, size_t stop)
393  {
394  base.template hostToDevice<0>(start,stop);
395  }
396 
405  {
407 
408  return v;
409  }
410 
416  static bool noPointers()
417  {
418  return false;
419  }
420 
426  size_t getLastError()
427  {
428  return err_code;
429  }
430 
436  inline void vector_overflow(size_t v1) const
437  {
438  if (v1 >= base.size())
439  {
440  std::cerr << "Error vector: " << __FILE__ << ":" << __LINE__ << " overflow id: " << v1 << "\n";\
441  size_t * err_code_pointer = (size_t *)&this->err_code;\
442  *err_code_pointer = 2001;\
443 
444  ACTION_ON_ERROR(VECTOR_ERROR_OBJECT);\
445  }
446  }
447 };
448 
449 
450 #endif /* MAP_VECTOR_STD_CUDA_HPP_ */
vector(const std::initializer_list< T > &v)
Initializer from constructor.
const void * getPointer() const
Return the pointer to the chunk of memory.
void swap(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &&v)
swap the memory between the two vector
void reserve(size_t ns)
reserve a memory space in advance to avoid reallocation
vector(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &&v) noexcept
Constructor from another vector.
void add(const T &v)
It insert a new object on the vector, eventually it reallocate the grid.
grid interface available when on gpu
vector_key_iterator getIterator() const
Get an iterator over all the elements of the vector.
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:83
vector< aggregate< T >, CudaMemory, memory_traits_inte, grow_policy_double > base
1-D static grid
void add(T &&v)
It insert a new object on the vector, eventually it reallocate the grid.
vector_key_iterator getIteratorTo(size_t k) const
Get iterator until a specified element.
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 vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &v) noexcept
Constructor from another vector.
void swap(vector< T, CudaMemory, memory_traits_inte, grow_policy_double, STD_VECTOR > &v)
void vector_overflow(size_t v1) const
check that the id does not overflow the buffer
void add()
Add an empty object (it call the default constructor () ) at the end of the vector.
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.