OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
HeapMemory.cpp
1 /*
2  * HeapMemory.cpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #include "HeapMemory.hpp"
9 #include <cstddef>
10 #include <cstring>
11 #include <iostream>
12 #include <cstdint>
13 
14 static const int extra_pad = 512;
15 
16 // If debugging mode include memory leak check
17 #ifdef SE_CLASS2
18 #include "Memleak_check.hpp"
19 #endif
20 
27 bool HeapMemory::allocate(size_t sz)
28 {
30  if (dm == NULL)
31  dmOrig = new byte[sz+alignement+extra_pad];
32  else
33  std::cerr << __FILE__ << ":" << __LINE__ << " error memory already allocated\n";
34 
35  dm = dmOrig;
36 
37 #ifdef SE_CLASS2
38  check_new(dmOrig,sz+alignement,HEAPMEMORY_EVENT,0);
39 #endif
40 
41  // align it, we do not know the size of the element we put 1
42  // and we ignore the align check
43  size_t sz_a = sz+alignement;
44  align(alignement,1,(void *&)dm,sz_a);
45 
46  this->sz = sz;
47 
48  return true;
49 }
50 
54 void HeapMemory::setAlignment(size_t align)
55 {
56  this->alignement = align;
57 }
58 
64 {
65 #ifdef SE_CLASS2
66  check_delete(dmOrig);
67 #endif
68 
69  if (dmOrig != NULL)
70  delete [] dmOrig;
71 
72  sz = 0;
73  dm = NULL;
74  dmOrig = NULL;
75 }
76 
77 
83 bool HeapMemory::copyFromPointer(const void * ptr,size_t sz)
84 {
85  // memory copy
86 
87 #ifdef SE_CLASS2
88  check_valid(dm,sz);
89  check_valid(ptr,sz);
90 #endif
91  memcpy(dm,ptr,sz);
92 
93  return true;
94 }
95 
104 {
106 
107  if (m.sz > sz)
108  {
109  std::cerr << "Error " << __LINE__ << __FILE__ << ": source buffer is too big to copy";
110  return false;
111  }
112 
113 #ifdef SE_CLASS2
114  check_valid(dm,sz);
115  check_valid(m.dm,sz);
116 #endif
117  // Copy the memory from m
118  memcpy(dm,m.dm,m.sz);
119  return true;
120 }
121 
127 bool HeapMemory::copy(const memory & m)
128 {
130  const HeapMemory * ofpm = dynamic_cast<const HeapMemory *>(&m);
131 
133 
134  if (ofpm == NULL)
135  {
136  // copy the memory from device to host and from host to device
137 
138  return copyFromPointer(m.getPointer(),m.size());
139  }
140  else
141  {
142  // they are the same memory type, use cuda/thrust buffer copy
143 
144  return copyDeviceToDevice(*ofpm);
145  }
146  return false;
147 }
148 
157 size_t HeapMemory::size() const
158 {
159  return sz;
160 }
161 
171 bool HeapMemory::resize(size_t sz)
172 {
173  // if the allocated memory is enough, do not resize
174  if (sz <= HeapMemory::size())
175  return true;
176 
178 
179  if (HeapMemory::size() == 0)
180  return HeapMemory::allocate(sz);
181 
183  byte * tdm;
184  byte * tdmOrig;
185  tdmOrig = new byte[sz+alignement+extra_pad];
186 #ifdef SE_CLASS2
187  check_new(tdmOrig,sz+alignement,HEAPMEMORY_EVENT,0);
188 #endif
189  tdm = tdmOrig;
190 
192  size_t sz_a = sz+alignement;
193 
195  align(alignement,1,(void *&)tdm,sz_a);
196 
198 
199 #ifdef SE_CLASS2
200  check_valid(tdm,HeapMemory::size());
201  check_valid(dm,HeapMemory::size());
202 #endif
203  memcpy(tdm,dm,HeapMemory::size());
204 
205  this->sz = sz;
206 
208 
210 
212 
213  dm = tdm;
214  dmOrig = tdmOrig;
215  this->sz = sz;
216 
217  return true;
218 }
219 
227 {
228  return dm;
229 }
230 
238 {
239  return dm;
240 }
241 
248 const void * HeapMemory::getPointer() const
249 {
250  return dm;
251 }
virtual size_t size() const =0
get the size of the buffer
virtual bool allocate(size_t sz)
allocate memory
Definition: HeapMemory.cpp:27
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
Definition: HeapMemory.cpp:54
byte * dm
device memory
Definition: HeapMemory.hpp:48
virtual size_t size() const
the the size of the allocated memory
Definition: HeapMemory.cpp:157
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:237
virtual void destroy()
destroy memory
Definition: HeapMemory.cpp:63
byte * dmOrig
original pointer (before alignment)
Definition: HeapMemory.hpp:50
virtual void * getDevicePointer()
get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents ...
Definition: HeapMemory.cpp:226
virtual bool resize(size_t sz)
resize the memory allocated
Definition: HeapMemory.cpp:171
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: HeapMemory.cpp:83
virtual bool copy(const memory &m)
copy memory
Definition: HeapMemory.cpp:127
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
virtual void * getPointer()=0
return a data pointer