OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
CudaMemory.cuh
1 /*
2  * CudaMemory.cu
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
27 #ifndef CUDA_MEMORY_CUH_
28 #define CUDA_MEMORY_CUH_
29 
30 #define EXCEPT_MC noexcept
31 
32 #include "config.h"
33 #include "memory.hpp"
34 #include <iostream>
35 
36 #include "util/cuda_util.hpp"
37 
38 extern size_t TotCudaMemoryAllocated;
39 
50 __device__ inline size_t align_number_device(size_t al, size_t number)
51 {
52  return number + ((number % al) != 0)*(al - number % al);
53 }
54 
56 static __device__ unsigned char global_cuda_error_array[256];
57 
58 class CudaMemory : public memory
59 {
61  bool is_hm_sync;
62 
64  size_t sz;
65 
67  void * dm;
68 
70  mutable void * hm;
71 
73  size_t ref_cnt;
74 
76  void allocate_host(size_t sz) const;
77 
79  bool copyFromPointer(const void * ptr);
80 
81 public:
82 
84  bool copyDeviceToDevice(const CudaMemory & m);
85 
87  virtual bool flush();
89  virtual bool allocate(size_t sz);
91  virtual void destroy();
93  virtual bool copy(const memory & m);
95  virtual size_t size() const;
97  virtual bool resize(size_t sz);
99  virtual void * getPointer();
100 
102  virtual const void * getPointer() const;
103 
105  virtual void * getDevicePointer();
106 
108  virtual void hostToDevice();
109 
111  virtual void deviceToHost();
112 
114  virtual void deviceToHost(size_t start, size_t stop);
115 
117  virtual void hostToDevice(size_t start, size_t top);
118 
120  void hostToDevice(CudaMemory & mem);
121 
123  void deviceToHost(CudaMemory & mem);
124 
126  virtual void fill(unsigned char c);
127 
131  void isNotSync() {is_hm_sync = false;}
132 
133  public:
134 
136  virtual void incRef()
137  {
138  ref_cnt++;
139  }
140 
142  virtual void decRef()
143  {ref_cnt--;}
144 
146  virtual long int ref()
147  {
148  return ref_cnt;
149  }
150 
157  {
158  return false;
159  }
160 
161  // Copy the memory (device and host)
162  CudaMemory & operator=(const CudaMemory & mem)
163  {
164  copy(mem);
165  return *this;
166  }
167 
168  // Copy the Cuda memory
169  CudaMemory(const CudaMemory & mem)
170  :CudaMemory()
171  {
172  allocate(mem.size());
173  copy(mem);
174  }
175 
176  CudaMemory(CudaMemory && mem) EXCEPT_MC
177  {
178  is_hm_sync = mem.is_hm_sync;
179  sz = mem.sz;
180  dm = mem.dm;
181  hm = mem.hm;
182  ref_cnt = mem.ref_cnt;
183 
184  // reset mem
185  mem.is_hm_sync = false;
186  mem.sz = 0;
187  mem.dm = NULL;
188  mem.hm = NULL;
189  mem.ref_cnt = 0;
190  }
191 
193  CudaMemory():is_hm_sync(true),sz(0),dm(0),hm(0),ref_cnt(0) {};
194 
196  CudaMemory(size_t sz):is_hm_sync(true),sz(0),dm(0),hm(0),ref_cnt(0)
197  {
198  allocate(sz);
199  };
200 
203  {
204  if(ref_cnt == 0)
205  destroy();
206  else
207  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
208  };
209 
218  void deviceToDevice(void * ptr, size_t start, size_t stop, size_t offset);
219 
220  void swap(CudaMemory & mem);
221 
227  constexpr static bool isDeviceHostSame()
228  {
229  return false;
230  }
231 
237  void * toKernel()
238  {
239  return getDevicePointer();
240  }
241 };
242 
243 
244 #endif
245 
void allocate_host(size_t sz) const
Allocate an host buffer.
Definition: CudaMemory.cu:133
CudaMemory(size_t sz)
Constructor.
Definition: CudaMemory.cuh:196
bool copyFromPointer(const void *ptr)
copy from Pointer to GPU
Definition: CudaMemory.cu:157
virtual bool allocate(size_t sz)
allocate memory
Definition: CudaMemory.cu:38
void * hm
host memory
Definition: CudaMemory.cuh:70
virtual void hostToDevice()
Move memory from host to device.
Definition: CudaMemory.cu:508
void * toKernel()
return the device memory
Definition: CudaMemory.cuh:237
virtual void * getPointer()
get a readable pointer with the data
Definition: CudaMemory.cu:352
void swap(CudaMemory &mem)
Swap the memory.
Definition: CudaMemory.cu:528
~CudaMemory()
Destructor.
Definition: CudaMemory.cuh:202
CudaMemory()
Constructor.
Definition: CudaMemory.cuh:193
size_t ref_cnt
Reference counter.
Definition: CudaMemory.cuh:73
virtual void decRef()
Decrement the reference counter.
Definition: CudaMemory.cuh:142
virtual void fill(unsigned char c)
fill the buffer with a byte
Definition: CudaMemory.cu:479
virtual void * getDevicePointer()
get a readable pointer with the data
Definition: CudaMemory.cu:497
virtual void incRef()
Increment the reference counter.
Definition: CudaMemory.cuh:136
void deviceToDevice(void *ptr, size_t start, size_t stop, size_t offset)
copy memory from device to device
Definition: CudaMemory.cu:119
virtual bool copy(const memory &m)
copy from a General device
Definition: CudaMemory.cu:214
virtual bool flush()
flush the memory
Definition: CudaMemory.cu:15
size_t sz
Size of the memory.
Definition: CudaMemory.cuh:64
virtual bool resize(size_t sz)
resize the momory allocated
Definition: CudaMemory.cu:259
virtual size_t size() const
the the size of the allocated memory
Definition: CudaMemory.cu:243
void * dm
device memory
Definition: CudaMemory.cuh:67
static constexpr bool isDeviceHostSame()
Return true if the device and the host pointer are the same.
Definition: CudaMemory.cuh:227
bool copyDeviceToDevice(const CudaMemory &m)
copy from GPU to GPU buffer directly
Definition: CudaMemory.cu:187
virtual long int ref()
Return the reference counter.
Definition: CudaMemory.cuh:146
virtual void deviceToHost()
Move memory from device to host.
Definition: CudaMemory.cu:367
bool isInitialized()
Allocated Memory is never initialized.
Definition: CudaMemory.cuh:156
void isNotSync()
Definition: CudaMemory.cuh:131
bool is_hm_sync
Is the host memory synchronized with the GPU memory.
Definition: CudaMemory.cuh:61
virtual void destroy()
destroy memory
Definition: CudaMemory.cu:80