OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
PtrMemory.hpp
1 /*
2  * PtrMemory.hpp
3  *
4  * Created on: Apr 15, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef PTRMEMORY_HPP_
9 #define PTRMEMORY_HPP_
10 
11 
32 #include "config.h"
33 #include "memory.hpp"
34 #include <cstddef>
35 #include <cstdint>
36 #include <iostream>
37 
38 
39 class PtrMemory : public memory
40 {
42  size_t spm;
43 
45  void * dm;
46 
48  long int ref_cnt;
49 
51  bool copyFromPointer(const void * ptr, size_t sz);
52 
54  void setAlignment(size_t align);
55 
56 public:
57 
59  bool copyDeviceToDevice(const PtrMemory & m);
60 
62  virtual bool flush() {return true;};
64  virtual bool allocate(size_t sz);
66  virtual void destroy();
68  virtual bool copy(const memory & m);
70  virtual size_t size() const;
72  virtual bool resize(size_t sz);
74  virtual void * getPointer();
75 
77  virtual const void * getPointer() const;
78 
80  virtual void * getDevicePointer();
81 
82 
84  virtual void deviceToHost(){};
85 
87  virtual void hostToDevice(){};
88 
90  virtual void deviceToHost(size_t start, size_t stop) {};
91 
93  virtual void hostToDevice(size_t start, size_t top) {};
94 
99  virtual void fill(unsigned char c);
100 
102  virtual void incRef()
103  {ref_cnt++;}
104 
106  virtual void decRef()
107  {ref_cnt--;}
108 
110  virtual long int ref()
111  {
112  return ref_cnt;
113  }
114 
120  constexpr static bool isDeviceHostSame()
121  {
122  return true;
123  }
124 
131  {
132  return true;
133  }
134 
135  // Default constructor
136  PtrMemory():spm(0),dm(NULL),ref_cnt(0)
137  {
138  };
139 
141  PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),ref_cnt(0)
142  {
143  };
144 
145  ~PtrMemory()
146  {
147  if(ref_cnt == 0)
148  destroy();
149  else
150  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
151  };
152 };
153 
154 
155 #endif /* PTRMEMORY_HPP_ */
long int ref_cnt
Reference counter.
Definition: PtrMemory.hpp:48
static constexpr bool isDeviceHostSame()
Return true if the device and the host pointer are the same.
Definition: PtrMemory.hpp:120
virtual void * getDevicePointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:167
bool isInitialized()
Allocated Memory is already initialized.
Definition: PtrMemory.hpp:130
virtual bool resize(size_t sz)
resize the memory allocated
Definition: PtrMemory.cpp:137
bool copyDeviceToDevice(const PtrMemory &m)
copy from same Heap to Heap
Definition: PtrMemory.cpp:72
virtual size_t size() const
the the size of the allocated memory
Definition: PtrMemory.cpp:122
virtual bool flush()
flush the memory
Definition: PtrMemory.hpp:62
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
void * dm
Pointed memory.
Definition: PtrMemory.hpp:45
virtual void deviceToHost()
Do nothing.
Definition: PtrMemory.hpp:84
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: PtrMemory.cpp:54
virtual void hostToDevice()
Do nothing.
Definition: PtrMemory.hpp:87
virtual void destroy()
destroy memory
Definition: PtrMemory.cpp:45
virtual void hostToDevice(size_t start, size_t top)
Do nothing.
Definition: PtrMemory.hpp:93
virtual void decRef()
Decrement the reference counter.
Definition: PtrMemory.hpp:106
virtual long int ref()
Return the reference counter.
Definition: PtrMemory.hpp:110
PtrMemory(void *ptr, size_t sz)
Constructor, we choose a default alignment of 32 for avx.
Definition: PtrMemory.hpp:141
virtual void deviceToHost(size_t start, size_t stop)
Do nothing.
Definition: PtrMemory.hpp:90
virtual void incRef()
Increment the reference counter.
Definition: PtrMemory.hpp:102
virtual void fill(unsigned char c)
fill memory with the selected byte
Definition: PtrMemory.cpp:22
virtual void * getPointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:156
virtual bool allocate(size_t sz)
allocate memory
Definition: PtrMemory.cpp:33
size_t spm
Size of the pointed memory.
Definition: PtrMemory.hpp:42
virtual bool copy(const memory &m)
copy memory
Definition: PtrMemory.cpp:92
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:39