OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
HeapMemory.hpp
1 /*
2  * HeapMempory.hpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef HEAP_MEMORY_HPP
9 #define HEAP_MEMORY_HPP
10 
11 #include "config.h"
12 #include "memory.hpp"
13 #include <cstddef>
14 #include <cstdint>
15 #include <iostream>
16 
17 typedef unsigned char byte;
18 
19 #define MEM_ALIGNMENT 32
20 
21 
39 class HeapMemory : public memory
40 {
42  size_t alignement;
43 
45  size_t sz;
46 
48  byte * dm;
50  byte * dmOrig;
52  long int ref_cnt;
53 
55  bool copyFromPointer(const void * ptr, size_t sz);
56 
58  void setAlignment(size_t align);
59 
60 public:
61 
63  bool copyDeviceToDevice(const HeapMemory & m);
64 
66  virtual bool flush() {return true;};
68  virtual bool allocate(size_t sz);
70  virtual void destroy();
72  virtual bool copy(const memory & m);
74  virtual size_t size() const;
76  virtual bool resize(size_t sz);
78  virtual void * getPointer();
79 
81  virtual const void * getPointer() const;
82 
84  virtual void * getDevicePointer();
85 
90  virtual void fill(unsigned char c);
91 
93  virtual void hostToDevice(){};
94 
96  virtual void deviceToHost(){};
97 
99  virtual void deviceToHost(size_t start, size_t stop) {};
100 
102  virtual void hostToDevice(size_t start, size_t stop) {};
103 
105  virtual void incRef()
106  {ref_cnt++;}
107 
109  virtual void decRef()
110  {ref_cnt--;}
111 
113  virtual long int ref()
114  {
115  return ref_cnt;
116  }
117 
124  {
125  return false;
126  }
127 
128  // Copy the Heap memory
129  HeapMemory & operator=(const HeapMemory & mem)
130  {
131  copy(mem);
132  return *this;
133  }
134 
135  // Copy the Heap memory
136  HeapMemory(const HeapMemory & mem)
137  :HeapMemory()
138  {
139  allocate(mem.size());
140  copy(mem);
141  }
142 
143  HeapMemory(HeapMemory && mem) noexcept
144  {
146  alignement = mem.alignement;
147  sz = mem.sz;
148  dm = mem.dm;
149  dmOrig = mem.dmOrig;
150  ref_cnt = mem.ref_cnt;
151 
152  mem.alignement = MEM_ALIGNMENT;
153  mem.sz = 0;
154  mem.dm = NULL;
155  mem.dmOrig = NULL;
156  mem.ref_cnt = 0;
157  }
158 
160  HeapMemory():alignement(MEM_ALIGNMENT),sz(0),dm(NULL),dmOrig(NULL),ref_cnt(0) {};
161 
162  virtual ~HeapMemory() noexcept
163  {
164  if(ref_cnt == 0)
166  else
167  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
168  };
169 
175  void swap(HeapMemory & mem)
176  {
177  size_t alignement_tmp;
178  size_t sz_tmp;
179  byte * dm_tmp;
180  byte * dmOrig_tmp;
181  long int ref_cnt_tmp;
182 
183  alignement_tmp = alignement;
184  sz_tmp = sz;
185  dm_tmp = dm;
186  dmOrig_tmp = dmOrig;
187  ref_cnt_tmp = ref_cnt;
188 
189  alignement = mem.alignement;
190  sz = mem.sz;
191  dm = mem.dm;
192  dmOrig = mem.dmOrig;
193  ref_cnt = mem.ref_cnt;
194 
195  mem.alignement = alignement_tmp;
196  mem.sz = sz_tmp;
197  mem.dm = dm_tmp;
198  mem.dmOrig = dmOrig_tmp;
199  mem.ref_cnt = ref_cnt_tmp;
200  }
201 
207  constexpr static bool isDeviceHostSame()
208  {
209  return true;
210  }
211 };
212 
223 inline size_t align_number(size_t al, size_t number)
224 {
225  return number + ((number % al) != 0)*(al - number % al);
226 }
227 
234 inline void *align( std::size_t alignment, std::size_t size,
235  void *&ptr, std::size_t &space ) {
236  std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr );
237  std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment;
238  std::size_t padding = aligned - pn;
239  if ( space < size + padding ) return nullptr;
240  space -= padding;
241  return ptr = reinterpret_cast< void * >( aligned );
242 }
243 
244 #endif
HeapMemory(HeapMemory &&mem) noexcept
Definition: HeapMemory.hpp:143
virtual void hostToDevice(size_t start, size_t stop)
Do nothing.
Definition: HeapMemory.hpp:102
void swap(HeapMemory &mem)
Swap the memory.
Definition: HeapMemory.hpp:175
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
Definition: HeapMemory.cpp:22
virtual void deviceToHost()
Do nothing.
Definition: HeapMemory.hpp:96
virtual bool flush()
flush the memory
Definition: HeapMemory.hpp:66
virtual bool allocate(size_t sz)
allocate memory
Definition: HeapMemory.cpp:33
virtual void decRef()
Decrement the reference counter.
Definition: HeapMemory.hpp:109
virtual void incRef()
Increment the reference counter.
Definition: HeapMemory.hpp:105
virtual void deviceToHost(size_t start, size_t stop)
Do nothing.
Definition: HeapMemory.hpp:99
virtual void hostToDevice()
Do nothing.
Definition: HeapMemory.hpp:93
long int ref_cnt
Reference counter.
Definition: HeapMemory.hpp:52
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
Definition: HeapMemory.cpp:64
byte * dm
device memory
Definition: HeapMemory.hpp:48
HeapMemory()
Constructor, we choose a default alignment of 32 for avx.
Definition: HeapMemory.hpp:160
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
size_t alignement
memory alignment
Definition: HeapMemory.hpp:42
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:228
virtual void destroy()
destroy memory
Definition: HeapMemory.cpp:73
byte * dmOrig
original pointer (before alignment)
Definition: HeapMemory.hpp:50
virtual size_t size() const
the the size of the allocated memory
Definition: HeapMemory.cpp:153
virtual void * getDevicePointer()
get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents
Definition: HeapMemory.cpp:218
static constexpr bool isDeviceHostSame()
Return true if the device and the host pointer are the same.
Definition: HeapMemory.hpp:207
virtual long int ref()
Return the reference counter.
Definition: HeapMemory.hpp:113
virtual bool resize(size_t sz)
resize the memory allocated
Definition: HeapMemory.cpp:167
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: HeapMemory.cpp:89
bool isInitialized()
Allocated Memory is never initialized.
Definition: HeapMemory.hpp:123
virtual bool copy(const memory &m)
copy memory
Definition: HeapMemory.cpp:123
size_t sz
Size of the memory.
Definition: HeapMemory.hpp:45
bool copyDeviceToDevice(const HeapMemory &m)
copy from same Heap to Heap
Definition: HeapMemory.cpp:103