OpenFPM_pdata  4.1.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 
68 
75  {
76  return mem->copyDeviceToDevice(*m.mem);
77  }
78 
87  void deviceToDevice(void * ptr, size_t start, size_t stop, size_t offset)
88  {
89  mem->deviceToDevice(ptr,start,stop,offset);
90  }
91 
92  constexpr static bool isDeviceHostSame()
93  {
94  return Mem::isDeviceHostSame();
95  }
96 
98  virtual void incRef()
99  {ref_cnt++;}
100 
102  virtual void decRef()
103  {ref_cnt--;}
104 
106  virtual long int ref()
107  {
108  return ref_cnt;
109  }
110 
112  virtual bool flush() {return mem->flush();};
113 
118  virtual void fill(unsigned char c)
119  {
120  mem->fill(c);
121  }
122 
130  virtual bool allocate(size_t sz)
131  {
132  // Zero sized allocation are ignored
133  if (sz == 0)
134  return true;
135 
136  a_seq = l_size;
137  l_size += sz;
138 
139  // Check we do not overflow the allocated memory
140 #ifdef SE_CLASS1
141 
142  if (l_size > mem->size())
143  std::cerr << __FILE__ << ":" << __LINE__ << " Error requesting more memory than the allocated one" << std::endl;
144 
145 #endif
146 
147  return true;
148  }
149 
157  bool allocate_nocheck(size_t sz)
158  {
159  // Zero sized allocation are ignored
160  if (sz == 0)
161  return true;
162 
163  a_seq = l_size;
164  l_size += sz;
165 
166  return true;
167  }
168 
174  void * getPointerEnd()
175  {
176  return (char *)mem->getPointer() + l_size;
177  }
178 
185  {
186  return (char *)mem->getDevicePointer() + l_size;
187  }
188 
194  void * getPointerBase()
195  {
196  return mem->getPointer();
197  }
198 
206  virtual void * getDevicePointer()
207  {
208  if (mem != NULL)
209  {return (((unsigned char *)mem->getDevicePointer()) + a_seq );}
210  else {return NULL;}
211  }
212 
218  virtual void hostToDevice()
219  {
220  mem->hostToDevice();
221  }
222 
228  virtual void hostToDevice(size_t start, size_t stop)
229  {
230  mem->hostToDevice(start,stop);
231  }
232 
234  virtual void deviceToHost()
235  {
236  mem->deviceToHost();
237  };
238 
240  virtual void deviceToHost(size_t start, size_t stop)
241  {
242  mem->deviceToHost(start,stop);
243  };
244 
250  virtual void * getPointer()
251  {
252  return (((unsigned char *)mem->getPointer()) + a_seq );
253  }
254 
260  virtual const void * getPointer() const
261  {
262  return (((unsigned char *)mem->getPointer()) + a_seq);
263  }
264 
270  void * getPointerOffset(size_t offset)
271  {
272  return (((unsigned char *)mem->getPointer()) + offset);
273  }
274 
285  virtual bool resize(size_t sz)
286  {
287  return allocate(sz);
288  }
289 
298  virtual size_t size() const
299  {
300  return l_size;
301  }
302 
307  void destroy()
308  {
309  mem->destroy();
310  }
311 
316  virtual bool copy(const memory & m)
317  {
318  return mem->copy(m);
319  }
320 
327  {
328  return false;
329  }
330 
336  static size_t calculateMem(std::vector<size_t> & mm)
337  {
338  size_t s = 0;
339 
340  for (size_t i = 0 ; i < mm.size() ; i++)
341  s += mm[i];
342 
343  return s;
344  }
345 
380  void shift_backward(size_t sz)
381  {
382  a_seq -= sz;
383  l_size = a_seq;
384  }
385 
397  void shift_forward(size_t sz)
398  {
399  a_seq += sz;
400  l_size = a_seq;
401  }
402 
408  size_t getOffset()
409  {
410  return a_seq;
411  }
412 
418  size_t getOffsetEnd()
419  {
420  return l_size;
421  }
422 
427  void reset()
428  {
429  a_seq = 0;
430  l_size = 0;
431  }
432 };
433 
434 #endif /* PREALLOCHEAPMEMORY_HPP_ */
bool isInitialized()
Allocated Memory is never initialized.
virtual size_t size() const
Get the size of the LAST allocated memory.
virtual void deviceToHost()
Do nothing.
virtual const void * getPointer() const
Return the pointer of the last allocation.
long int ref_cnt
Reference counter.
Definition: ExtPreAlloc.hpp:39
virtual void * getPointer()
Return the pointer of the last allocation.
size_t getOffsetEnd()
Get offset.
void shift_forward(size_t sz)
shift the pointer forward
virtual void hostToDevice(size_t start, size_t stop)
Return the pointer of the last allocation.
void shift_backward(size_t sz)
shift the pointer backward
size_t a_seq
Actual allocation pointer.
Definition: ExtPreAlloc.hpp:30
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
void * getDevicePointerEnd()
Return the device end pointer of the previous allocated memory.
ExtPreAlloc()
Default constructor.
Definition: ExtPreAlloc.hpp:50
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
virtual long int ref()
Return the reference counter.
virtual void hostToDevice()
Return the pointer of the last allocation.
size_t l_size
Last allocation size.
Definition: ExtPreAlloc.hpp:33
ExtPreAlloc(size_t size, Mem &mem)
Preallocated memory sequence.
Definition: ExtPreAlloc.hpp:61
size_t getOffset()
Get offset.
virtual bool flush()
flush the memory
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
bool copyDeviceToDevice(const ExtPreAlloc< Mem > &m)
Copy the memory from device to device.
Definition: ExtPreAlloc.hpp:74
bool allocate_nocheck(size_t sz)
Allocate a chunk of memory.
virtual void incRef()
Increment the reference counter.
Definition: ExtPreAlloc.hpp:98
virtual bool copy(const memory &m)
Copy memory.
void deviceToDevice(void *ptr, size_t start, size_t stop, size_t offset)
special function to move memory from a raw device pointer
Definition: ExtPreAlloc.hpp:87
virtual bool resize(size_t sz)
Allocate or resize the allocated memory.
virtual void deviceToHost(size_t start, size_t stop)
Do nothing.
void * getPointerBase()
The the base pointer of the preallocate memory.
void destroy()
Destroy memory.
virtual void decRef()
Decrement the reference counter.
void reset()
Reset the internal counters.
Mem * mem
Main class for memory allocation.
Definition: ExtPreAlloc.hpp:36
virtual void * getDevicePointer()
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.
void * getPointerEnd()
Return the end pointer of the previous allocated memory.