OpenFPM  5.2.0
Project that contain the implementation of distributed structures
ExtPreAlloc.hpp
1 /* ExtPreAlloc.hpp
2  *
3  * Created on: Apr 3, 2015
4  * Author: Pietro Incardona
5  */
6 
7 #ifndef EXTPREALLOC_HPP_
8 #define EXTPREALLOC_HPP_
9 
10 #include <stddef.h>
11 #include "memory.hpp"
12 #include <iostream>
13 
26 template<typename Mem>
27 class ExtPreAlloc : public memory
28 {
30  size_t a_seq ;
31 
33  size_t l_size;
34 
36  Mem * mem;
37 
39  long int ref_cnt;
40 
41 public:
42 
43  virtual ~ExtPreAlloc()
44  {
45  if (ref_cnt != 0)
46  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
47  }
48 
51  :a_seq(0),l_size(0),mem(NULL),ref_cnt(0)
52  {
53  }
54 
61  ExtPreAlloc(size_t size, Mem & mem)
62  :a_seq(0),l_size(0),mem(&mem),ref_cnt(0)
63  {
64  // Allocate the total size of memory
65  mem.resize(size);
66  }
67 
74  void setMemory(size_t size, Mem & mem)
75  {
76  this->mem = &mem;
77  mem.resize(size);
78  }
79 
85  Mem * getMemory()
86  {
87  return this->mem;
88  }
89 
96  {
97  return mem->copyDeviceToDevice(*m.mem);
98  }
99 
108  void deviceToDevice(void * ptr, size_t start, size_t stop, size_t offset)
109  {
110  mem->deviceToDevice(ptr,start,stop,offset);
111  }
112 
113  constexpr static bool isDeviceHostSame()
114  {
115  return Mem::isDeviceHostSame();
116  }
117 
119  virtual void incRef()
120  {ref_cnt++;}
121 
123  virtual void decRef()
124  {ref_cnt--;}
125 
127  virtual long int ref()
128  {
129  return ref_cnt;
130  }
131 
133  virtual bool flush() {return mem->flush();};
134 
139  virtual void fill(unsigned char c)
140  {
141  mem->fill(c);
142  }
143 
151  virtual bool allocate(size_t sz)
152  {
153  // Zero sized allocation are ignored
154  if (sz == 0)
155  return true;
156 
157  a_seq = l_size;
158  l_size += sz;
159 
160  // Check we do not overflow the allocated memory
161 #ifdef SE_CLASS1
162 
163  if (l_size > mem->size())
164  std::cerr << __FILE__ << ":" << __LINE__ << " Error requesting more memory than the allocated one" << std::endl;
165 
166 #endif
167 
168  return true;
169  }
170 
178  bool allocate_nocheck(size_t sz)
179  {
180  // Zero sized allocation are ignored
181  if (sz == 0)
182  return true;
183 
184  a_seq = l_size;
185  l_size += sz;
186 
187  return true;
188  }
189 
195  void * getPointerEnd()
196  {
197  return (char *)mem->getPointer() + l_size;
198  }
199 
206  {
207  return (char *)mem->getDevicePointer() + l_size;
208  }
209 
215  void * getPointerBase()
216  {
217  return mem->getPointer();
218  }
219 
227  virtual void * getDevicePointer()
228  {
229  if (mem != NULL)
230  {return (((unsigned char *)mem->getDevicePointer()) + a_seq );}
231  else {return NULL;}
232  }
233 
239  virtual void hostToDevice()
240  {
241  mem->hostToDevice();
242  }
243 
249  virtual void hostToDevice(size_t start, size_t stop)
250  {
251  mem->hostToDevice(start,stop);
252  }
253 
255  virtual void deviceToHost()
256  {
257  mem->deviceToHost();
258  };
259 
261  virtual void deviceToHost(size_t start, size_t stop)
262  {
263  mem->deviceToHost(start,stop);
264  };
265 
271  virtual void * getPointer()
272  {
273  return (((unsigned char *)mem->getPointer()) + a_seq );
274  }
275 
281  virtual const void * getPointer() const
282  {
283  return (((unsigned char *)mem->getPointer()) + a_seq);
284  }
285 
291  void * getPointerOffset(size_t offset)
292  {
293  return (((unsigned char *)mem->getPointer()) + offset);
294  }
295 
306  virtual bool resize(size_t sz)
307  {
308  return allocate(sz);
309  }
310 
319  virtual size_t size() const
320  {
321  return l_size;
322  }
323 
328  void destroy()
329  {
330  mem->destroy();
331  }
332 
337  virtual bool copy(const memory & m)
338  {
339  return mem->copy(m);
340  }
341 
348  {
349  return false;
350  }
351 
357  static size_t calculateMem(std::vector<size_t> & mm)
358  {
359  size_t s = 0;
360 
361  for (size_t i = 0 ; i < mm.size() ; i++)
362  s += mm[i];
363 
364  return s;
365  }
366 
401  void shift_backward(size_t sz)
402  {
403  a_seq -= sz;
404  l_size = a_seq;
405  }
406 
418  void shift_forward(size_t sz)
419  {
420  a_seq += sz;
421  l_size = a_seq;
422  }
423 
429  size_t getOffset()
430  {
431  return a_seq;
432  }
433 
439  size_t getOffsetEnd()
440  {
441  return l_size;
442  }
443 
448  void reset()
449  {
450  a_seq = 0;
451  l_size = 0;
452  }
453 };
454 
455 #endif /* PREALLOCHEAPMEMORY_HPP_ */
virtual void decRef()
Decrement the reference counter.
void * getDevicePointerEnd()
Return the device end pointer of the previous allocated memory.
void destroy()
Destroy memory.
size_t getOffsetEnd()
Get offset.
ExtPreAlloc(size_t size, Mem &mem)
Preallocated memory sequence.
Definition: ExtPreAlloc.hpp:61
void shift_backward(size_t sz)
shift the pointer backward
virtual const void * getPointer() const
Return the pointer of the last allocation.
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
size_t a_seq
Actual allocation pointer.
Definition: ExtPreAlloc.hpp:30
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
virtual bool resize(size_t sz)
Allocate or resize the allocated memory.
size_t getOffset()
Get offset.
virtual void * getDevicePointer()
Return the pointer of the last allocation.
long int ref_cnt
Reference counter.
Definition: ExtPreAlloc.hpp:39
Mem * mem
Main class for memory allocation.
Definition: ExtPreAlloc.hpp:36
virtual void deviceToHost(size_t start, size_t stop)
Do nothing.
void shift_forward(size_t sz)
shift the pointer forward
Mem * getMemory()
Get the internal memory if you did not do it in the constructor.
Definition: ExtPreAlloc.hpp:85
virtual void incRef()
Increment the reference counter.
virtual void deviceToHost()
Do nothing.
virtual void * getPointer()
Return the pointer of the last allocation.
static size_t calculateMem(std::vector< size_t > &mm)
Calculate the total memory required to pack the message.
ExtPreAlloc()
Default constructor.
Definition: ExtPreAlloc.hpp:50
bool isInitialized()
Allocated Memory is never initialized.
size_t l_size
Last allocation size.
Definition: ExtPreAlloc.hpp:33
void reset()
Reset the internal counters.
virtual long int ref()
Return the reference counter.
virtual void hostToDevice()
Return the pointer of the last allocation.
void deviceToDevice(void *ptr, size_t start, size_t stop, size_t offset)
special function to move memory from a raw device pointer
virtual bool flush()
flush the memory
void setMemory(size_t size, Mem &mem)
Set the internal memory if you did not do it in the constructor.
Definition: ExtPreAlloc.hpp:74
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
bool allocate_nocheck(size_t sz)
Allocate a chunk of memory.
virtual void hostToDevice(size_t start, size_t stop)
Return the pointer of the last allocation.
virtual size_t size() const
Get the size of the LAST allocated memory.
void * getPointerEnd()
Return the end pointer of the previous allocated memory.
void * getPointerBase()
The the base pointer of the preallocate memory.
virtual bool copy(const memory &m)
Copy memory.
bool copyDeviceToDevice(const ExtPreAlloc< Mem > &m)
Copy the memory from device to device.
Definition: ExtPreAlloc.hpp:95