OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
memory_array.hpp
1 /*
2  * memory_stride.hpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef MEMORY_ARRAY_HPP_
9 #define MEMORY_ARRAY_HPP_
10 
11 #include "memory/memory.hpp"
12 
13 #include "util/cuda_util.hpp"
14 
25 template<typename T>
27 {
28 
29 #if defined(__GNUG__) || defined(__clang__)
30 
32  T __attribute__((aligned(16))) * ptr;
33 
34 #else
35 
37  T * ptr;
38 
39 #endif
40 
42  size_t sz;
43 
51  T get(mem_id i)
52  {
53  return ptr[i];
54  }
55 
56 
57  public:
58 
60  typedef T value_type;
61 
71  void initialize(void * ptr, size_t sz, bool init)
72  {
73  this->ptr = static_cast<T *>(ptr);
74 
75 
76  // Initialize the constructors
77 
78  if (init == false)
79  new (ptr)T[sz];
80 
81  this->sz = sz;
82  }
83 
85  size_t size()
86  {
87  return sz;
88  }
89 
95  void set_pointer(void * ptr_)
96  {
97  ptr = static_cast<T *>(ptr_);
98  }
99 
101  __device__ __host__ void * get_pointer() const
102  {
103  return ptr;
104  }
105 
111  template<typename Tbind> void bind_ref(const memory_array<Tbind> & ref)
112  {
113  ptr = static_cast<T *>(ref.get_pointer());
114  }
115 
123  __host__ __device__ T & operator[](mem_id i)
124  {
125  return ptr[i];
126  }
127 
135  __host__ __device__ const T & operator[](mem_id i) const
136  {
137  return ptr[i];
138  }
139 
145  void swap(memory_array<T> & obj)
146  {
147  size_t sz_tmp = sz;
148  sz = obj.sz;
149  obj.sz = sz_tmp;
150 
151  T * ptr_tmp = ptr;
152  ptr = obj.ptr;
153  obj.ptr = ptr_tmp;
154  }
155 
161  void deinit()
162  {
163  // Call the destructor of every objects
164  for (size_t i = 0 ; i < sz ; i++)
165  {
166  (&ptr[i])->~T();
167  }
168  }
169 
172  :ptr(NULL),sz(0)
173  {};
174 
182  memory_array(void * ptr, size_t sz, bool init)
183  {
185  }
186 
193  {
194  };
195 };
196 
197 
198 #endif
void bind_ref(const memory_array< Tbind > &ref)
Set from another memory array.
__host__ __device__ const T & operator[](mem_id i) const
Access element an element of the array.
size_t sz
number of elements
memory_array()
Default constructor.
void swap(memory_array< T > &obj)
swap the two objects memory
void set_pointer(void *ptr_)
Set the internal pointer to the indicated chunk of memory.
__device__ __host__ void * get_pointer() const
Return the pointer.
T get(mem_id i)
Get the i element.
memory_array(void *ptr, size_t sz, bool init)
Memory array constructor.
size_t size()
return the size of the array
void deinit()
Deinitialize the memory.
void initialize(void *ptr, size_t sz, bool init)
Initialize the memory array.
T value_type
type returned by this structure
This class give a representation to a chunk or memory.
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
~memory_array()
Destructor.
T * ptr
Internal pointer.
__host__ __device__ T & operator[](mem_id i)
Access element an element of the array.