OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
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 
31 template<typename T, typename D = memory>
32 class memory_c
33 {
34  public:
35 
37  typedef memory_c<T> type;
38 
40  typedef T& reference;
41 
43  typedef T vtype;
44 
46  D * mem;
47 
50 
56  void setMemory(memory & mem)
57  {
58  if (this->mem != NULL)
59  {
60  this->mem->decRef();
61 
62  if (this->mem->ref() == 0 && &mem != this->mem)
63  delete(this->mem);
64  }
65 // this->mem->decRef();
66  mem.incRef();
67  this->mem = &mem;
68  }
69 
76  memory& getMemory()
77  {
78  return *this->mem;
79  }
80 
87  const memory& getMemory() const
88  {
89  return *this->mem;
90  }
91 
95  bool allocate(const size_t sz)
96  {
97  memory * mem = this->mem;
98 
100  mem->resize( sz*sizeof(T) );
101 
103  mem_r = new memory_array<T>(mem->getPointer(),sz,mem->isInitialized());
104 
105  return true;
106  }
107 
113  void move_copy(memory_c & mem_c)
114  {
115  //if mem is already allocated, deallocate it
116  if (mem != NULL)
117  delete(mem);
118 
119  // if mem_r is allocated delete it
120  if (mem_r != NULL)
121  delete(mem_r);
122 
123  // move the pointer
124  mem = mem_c.mem;
125 
126  // Null the pointer to avoid double deallocation
127  mem_c.mem = NULL;
128 
129  // move the pointer
130  mem_r = mem_c.mem_r;
131 
132  // Null the pointer to avoid double deallocation
133  mem_c.mem_r = NULL;
134  }
135 
137  memory_c():mem(NULL),mem_r(NULL){}
138 
141  {
142  if (mem != NULL)
143  {
144  mem->decRef();
145 
146  if (mem->ref() == 0)
147  delete(mem);
148  }
149  delete(mem_r);
150  }
151 
157  void swap(memory_c & mem_obj)
158  {
159  // Save on temporal
160 
161  void * mem_tmp = static_cast<void*>(mem);
162  void * mem_r_tmp = static_cast<void*>(mem_r);
163 
164  // swap the memory between objects
165 
166  mem = mem_obj.mem;
167  mem_r = mem_obj.mem_r;
168 
169  mem_obj.mem = static_cast<D*>(mem_tmp);
170  mem_obj.mem_r = static_cast<memory_array<T>*>(mem_r_tmp);
171  }
172 
173 };
174 
184 template<typename T>
186 {
187  typedef T type;
188 };
189 
201 template<typename T>
202 class key
203 {
204  typedef T type;
205 };
206 
207 
213 template<typename T, unsigned int N>
214 struct mult
215 {
216  enum { value = mult<T,N-1>::value * boost::mpl::at<T,boost::mpl::int_<N>>::type::value };
217 };
218 
219 template <typename T>
220 struct mult<T,1>
221 {
222  enum { value = boost::mpl::at<T,boost::mpl::int_<1>>::type::value };
223 };
224 
240 template<typename T, typename D>
241 class memory_c<multi_array<T>, D>
242 {
244 // typedef T type;
245 
247  template<int S> using int_ = boost::mpl::int_<S>;
248 
250  typedef typename boost::mpl::size<T> size_p;
251 
253  template< typename S, unsigned int n> using at = boost::mpl::at<S,boost::mpl::int_<n> >;
254 
255  typedef typename at<T,0>::type base;
256 
258  typedef typename boost::multi_array_ref<base,size_p::value>::size_type size_type;
259 
260 
268  template<size_t index,size_t N> struct ordering
269  {
270  enum { value = (N-index) % N };
271  };
272 
281  template<size_t index,size_t N> struct ascending
282  {
283  enum { value = true };
284  };
285 
286  public:
287 
294  void setMemory(memory & mem)
295  {
296  if (this->mem != NULL)
297  {
298  this->mem->decRef();
299 
300  if (this->mem->ref() == 0 && &mem != this->mem)
301  delete(this->mem);
302  }
303  mem.incRef();
304  this->mem = &mem;
305  }
306 
313  memory& getMemory()
314  {
315  return *this->mem;
316  }
317 
324  bool allocate(const size_t sz)
325  {
326  memory * mem = this->mem;
327 
329  mem->resize( sz*mult<T,size_p::value-1>::value*sizeof(base) );
330 
331  // We create an array dims from the boost::mpl::vector
332  typedef typename generate_array_vector<size_type,T>::result dims;
333 
335  std::array<size_type ,size_p::value> dimensions;
336 
337  // fill runtime, and the other dimensions
338  dimensions[0] = sz;
339  for (int i = 0 ; i < size_p::value-1 ; i++)
340  {dimensions[i+1] = dims::data[i];}
341 
343  typedef typename generate_array<typename boost::multi_array<T,size_p::value>::size_type,size_p::value, ordering>::result ord;
344 
345  // we generate the ascending buffer
346  typedef typename generate_array<bool,size_p::value, ascending>::result asc;
347 
349  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));
350 
351  return true;
352  }
353 
356  typedef boost::multi_array<base,size_p::value> type;
357 
359  D * mem;
360 
362  boost::multi_array_ref<base,boost::mpl::size<T>::value> * mem_r;
363 
365  memory_c():mem(NULL),mem_r(NULL){}
366 
369  {
370  if (mem != NULL)
371  {
372  mem->decRef();
373  if (mem->ref() == 0)
374  delete(mem);
375  }
376  delete(mem_r);
377  }
378 
380  void set_mem(memory & mem)
381  {
382  this->mem = &mem;
383  }
384 };
385 
386 #endif
387 
memory & getMemory()
This function get the object that allocate memory.
Definition: memory_c.hpp:313
void swap(memory_c &mem_obj)
swap the memory
Definition: memory_c.hpp:157
bool allocate(const size_t sz)
This function allocate memory and associate the representation to mem_r.
Definition: memory_c.hpp:324
T & reference
define a reference to T
Definition: memory_c.hpp:40
void setMemory(memory &mem)
This function set the object that allocate memory.
Definition: memory_c.hpp:294
Main class to generate compile-time array.
Definition: ct_array.hpp:113
boost::mpl::at< S, boost::mpl::int_< n > > at
Define "at" meta function without boost::mpl.
Definition: memory_c.hpp:253
memory_c()
constructor
Definition: memory_c.hpp:137
void setMemory(memory &mem)
This function set the object that allocate memory.
Definition: memory_c.hpp:56
D * mem
Reference to an object to allocate memory.
Definition: memory_c.hpp:359
memory_c< T > type
define T
Definition: memory_c.hpp:37
T vtype
define T
Definition: memory_c.hpp:43
D * mem
object that allocate memory like HeapMemory or CudaMemory
Definition: memory_c.hpp:46
memory_array< T > * mem_r
object that represent the memory as an array of objects T
Definition: memory_c.hpp:49
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:362
boost::mpl::int_< S > int_
define T
Definition: memory_c.hpp:247
boost::mpl::size< T > size_p
define the template vector size it give a number at compile time
Definition: memory_c.hpp:250
void set_mem(memory &mem)
set the device memory interface, the object that allocate memory
Definition: memory_c.hpp:380
memory & getMemory()
This function get the object that allocate memory.
Definition: memory_c.hpp:76
void move_copy(memory_c &mem_c)
It absorb the allocated object from another memory_c.
Definition: memory_c.hpp:113
this class multiply all the elements in a boost::mpl::vector excluding the first element ...
Definition: memory_c.hpp:214
const memory & getMemory() const
This function get the object that allocate memory.
Definition: memory_c.hpp:87
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:202
~memory_c()
destructor
Definition: memory_c.hpp:140
boost::multi_array< base, size_p::value > type
Definition: memory_c.hpp:356
This class is a container for the memory interface like HeapMemory CudaMemory.
Definition: memory_c.hpp:32
This class give a representation to a chunk or memory.
boost::multi_array_ref< base, size_p::value >::size_type size_type
define size_type
Definition: memory_c.hpp:258
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:185
bool allocate(const size_t sz)
This function allocate memory.
Definition: memory_c.hpp:95