OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
memory_c.hpp
1 /*
2  * memory_c.hpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #include <boost/multi_array.hpp>
9 #include <boost/fusion/mpl.hpp>
10 #include <boost/fusion/include/mpl.hpp>
11 #include <boost/mpl/vector.hpp>
12 #include <array>
13 #include "util/ct_array.hpp"
14 #include "memory_array.hpp"
15 #include "memory/memory.hpp"
16 
17 #ifndef MEMORY_C_HPP_
18 #define MEMORY_C_HPP_
19 
30 template<typename T, typename D = memory>
31 class memory_c
32 {
33  public:
34 
36  typedef memory_c<T> type;
37 
39  typedef T& reference;
40 
42  typedef T vtype;
43 
45  D * mem;
46 
49 
56  {
57  if (this->mem != NULL)
58  {
59  this->mem->decRef();
60 
61  if (this->mem->ref() == 0 && &mem != this->mem)
62  delete(this->mem);
63  }
64 // this->mem->decRef();
65  mem.incRef();
66  this->mem = &mem;
67  }
68 
76  {
77  return *this->mem;
78  }
79 
86  const memory& getMemory() const
87  {
88  return *this->mem;
89  }
90 
94  bool allocate(const size_t sz, bool skip_initialization = false)
95  {
96  memory * mem = this->mem;
97 
99  mem->resize( sz*sizeof(T) );
100 
102  mem_r = new memory_array<T>(mem->getPointer(),sz,mem->isInitialized() | skip_initialization);
103 
104  return true;
105  }
106 
112  void move_copy(memory_c & mem_c)
113  {
114  // if mem_r is allocated delete it
115  if (mem_r != NULL)
116  delete(mem_r);
117 
118  //if mem is already allocated, deallocate it
119  if (mem != NULL)
120  delete(mem);
121 
122  // move the pointer
123  mem = mem_c.mem;
124 
125  // Null the pointer to avoid double deallocation
126  mem_c.mem = NULL;
127 
128  // move the pointer
129  mem_r = mem_c.mem_r;
130 
131  // Null the pointer to avoid double deallocation
132  mem_c.mem_r = NULL;
133  }
134 
136  memory_c():mem(NULL),mem_r(NULL){}
137 
140  {
141  delete(mem_r);
142  if (mem != NULL)
143  {
144  mem->decRef();
145 
146  if (mem->ref() == 0)
147  delete(mem);
148  }
149  }
150 
156  void swap(memory_c & mem_obj)
157  {
158  // Save on temporal
159 
160  void * mem_tmp = static_cast<void*>(mem);
161  void * mem_r_tmp = static_cast<void*>(mem_r);
162 
163  // swap the memory between objects
164 
165  mem = mem_obj.mem;
166  mem_r = mem_obj.mem_r;
167 
168  mem_obj.mem = static_cast<D*>(mem_tmp);
169  mem_obj.mem_r = static_cast<memory_array<T>*>(mem_r_tmp);
170  }
171 
172 };
173 
183 template<typename T>
185 {
186  typedef T type;
187 };
188 
200 template<typename T>
201 class key
202 {
203  typedef T type;
204 };
205 
206 
212 template<typename T, unsigned int N>
213 struct mult
214 {
215  enum { value = mult<T,N-1>::value * boost::mpl::at<T,boost::mpl::int_<N>>::type::value };
216 };
217 
218 template <typename T>
219 struct mult<T,1>
220 {
221  enum { value = boost::mpl::at<T,boost::mpl::int_<1>>::type::value };
222 };
223 
239 template<typename T, typename D>
241 {
243 // typedef T type;
244 
246  template<int S> using int_ = boost::mpl::int_<S>;
247 
249  typedef typename boost::mpl::size<T> size_p;
250 
252  template< typename S, unsigned int n> using at = boost::mpl::at<S,boost::mpl::int_<n> >;
253 
254  typedef typename at<T,0>::type base;
255 
257  typedef typename boost::multi_array_ref<base,size_p::value>::size_type size_type;
258 
259 
267  template<size_t index,size_t N> struct ordering
268  {
269  enum { value = (N-index) % N };
270  };
271 
280  template<size_t index,size_t N> struct ascending
281  {
282  enum { value = true };
283  };
284 
285  public:
286 
294  {
295  if (this->mem != NULL)
296  {
297  this->mem->decRef();
298 
299  if (this->mem->ref() == 0 && &mem != this->mem)
300  delete(this->mem);
301  }
302  mem.incRef();
303  this->mem = &mem;
304  }
305 
313  {
314  return *this->mem;
315  }
316 
323  bool allocate(const size_t sz)
324  {
325  memory * mem = this->mem;
326 
328  mem->resize( sz*mult<T,size_p::value-1>::value*sizeof(base) );
329 
330  // We create an array dims from the boost::mpl::vector
331  typedef typename generate_array_vector<size_type,T>::result dims;
332 
334  std::array<size_type ,size_p::value> dimensions;
335 
336  // fill runtime, and the other dimensions
337  dimensions[0] = sz;
338  for (int i = 0 ; i < size_p::value-1 ; i++)
339  {dimensions[i+1] = dims::data[i];}
340 
342  typedef typename generate_array<typename boost::multi_array<T,size_p::value>::size_type,size_p::value, ordering>::result ord;
343 
344  // we generate the ascending buffer
345  typedef typename generate_array<bool,size_p::value, ascending>::result asc;
346 
348  mem_r = new boost::multi_array_ref<base,size_p::value>(static_cast<base *>(mem->getPointer()),dimensions,boost::general_storage_order<size_p::value>(ord::data,asc::data));
349 
350  return true;
351  }
352 
355  typedef boost::multi_array<base,size_p::value> type;
356 
358  D * mem;
359 
361  boost::multi_array_ref<base,boost::mpl::size<T>::value> * mem_r;
362 
364  memory_c():mem(NULL),mem_r(NULL){}
365 
368  {
369  delete(mem_r);
370  if (mem != NULL)
371  {
372  mem->decRef();
373  if (mem->ref() == 0)
374  delete(mem);
375  }
376  }
377 
380  {
381  this->mem = &mem;
382  }
383 
389  void swap(memory_c & mem_obj)
390  {
391  // Save on temporal
392 
393  void * mem_tmp = static_cast<void*>(mem);
394  void * mem_r_tmp = static_cast<void*>(mem_r);
395 
396  // swap the memory between objects
397 
398  mem = mem_obj.mem;
399  mem_r = mem_obj.mem_r;
400 
401  mem_obj.mem = static_cast<D*>(mem_tmp);
402  mem_obj.mem_r = static_cast<decltype(mem_obj.mem_r)>(mem_r_tmp);
403  }
404 };
405 
406 #endif
407 
memory & getMemory()
This function get the object that allocate memory.
Definition: memory_c.hpp:312
Derivative second order on h (spacing)
Definition: Derivative.hpp:28
bool allocate(const size_t sz, bool skip_initialization=false)
This function allocate memory.
Definition: memory_c.hpp:94
void swap(memory_c &mem_obj)
swap the memory
Definition: memory_c.hpp:156
bool allocate(const size_t sz)
This function allocate memory and associate the representation to mem_r.
Definition: memory_c.hpp:323
virtual bool resize(size_t sz)=0
resize on device a buffer
T & reference
define a reference to T
Definition: memory_c.hpp:39
void setMemory(memory &mem)
This function set the object that allocate memory.
Definition: memory_c.hpp:293
Main class to generate compile-time array.
Definition: ct_array.hpp:169
boost::mpl::at< S, boost::mpl::int_< n > > at
Define "at" meta function without boost::mpl.
Definition: memory_c.hpp:252
memory_c()
constructor
Definition: memory_c.hpp:136
void setMemory(memory &mem)
This function set the object that allocate memory.
Definition: memory_c.hpp:55
D * mem
Reference to an object to allocate memory.
Definition: memory_c.hpp:358
memory_c< T > type
define T
Definition: memory_c.hpp:36
virtual long int ref()=0
Return the actual reference counter.
T vtype
define T
Definition: memory_c.hpp:42
D * mem
object that allocate memory like HeapMemory or CudaMemory
Definition: memory_c.hpp:45
memory_array< T > * mem_r
object that represent the memory as an array of objects T
Definition: memory_c.hpp:48
boost::multi_array_ref< base, boost::mpl::size< T >::value > * mem_r
object that represent the memory as an multi-dimensional array of objects T
Definition: memory_c.hpp:361
boost::mpl::int_< S > int_
define T
Definition: memory_c.hpp:246
boost::mpl::size< T > size_p
define the template vector size it give a number at compile time
Definition: memory_c.hpp:249
void set_mem(memory &mem)
set the device memory interface, the object that allocate memory
Definition: memory_c.hpp:379
memory & getMemory()
This function get the object that allocate memory.
Definition: memory_c.hpp:75
void move_copy(memory_c &mem_c)
It absorb the allocated object from another memory_c.
Definition: memory_c.hpp:112
generate_array_vector_impl< T, boost::mpl::size< F >::value-1, F >::result result
generate compile-time array vector
Definition: ct_array.hpp:208
this class multiply all the elements in a boost::mpl::vector excluding the first element ...
Definition: memory_c.hpp:213
const memory & getMemory() const
This function get the object that allocate memory.
Definition: memory_c.hpp:86
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
void swap(memory_c &mem_obj)
swap the memory
Definition: memory_c.hpp:389
~memory_c()
destructor
Definition: memory_c.hpp:139
virtual void decRef()=0
Decrement the internal reference counter.
boost::multi_array< base, size_p::value > type
Definition: memory_c.hpp:355
virtual bool isInitialized()=0
Return if the actual memory that is going to be allocated is already initialized. ...
This class is a container for the memory interface like HeapMemory CudaMemory.
Definition: memory_c.hpp:31
This class give a representation to a chunk or memory.
virtual void * getPointer()=0
return a data pointer
virtual void incRef()=0
Increment the internal reference counter.
boost::multi_array_ref< base, size_p::value >::size_type size_type
define size_type
Definition: memory_c.hpp:257
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:184