OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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
38extern 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
56static __device__ unsigned char global_cuda_error_array[256];
57
58class CudaMemory : public memory
59{
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
81public:
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
virtual void * getDevicePointer()
get a readable pointer with the data
void * toKernel()
return the device memory
void deviceToDevice(void *ptr, size_t start, size_t stop, size_t offset)
copy memory from device to device
virtual void incRef()
Increment the reference counter.
virtual void deviceToHost()
Move memory from device to host.
virtual bool resize(size_t sz)
resize the momory allocated
virtual bool copy(const memory &m)
copy from a General device
virtual bool flush()
flush the memory
Definition CudaMemory.cu:15
virtual long int ref()
Return the reference counter.
virtual void hostToDevice()
Move memory from host to device.
size_t ref_cnt
Reference counter.
bool is_hm_sync
Is the host memory synchronized with the GPU memory.
virtual void decRef()
Decrement the reference counter.
void * dm
device memory
void * hm
host memory
virtual size_t size() const
the the size of the allocated memory
CudaMemory(size_t sz)
Constructor.
bool copyFromPointer(const void *ptr)
copy from Pointer to GPU
virtual void fill(unsigned char c)
fill the buffer with a byte
void swap(CudaMemory &mem)
Swap the memory.
static constexpr bool isDeviceHostSame()
Return true if the device and the host pointer are the same.
~CudaMemory()
Destructor.
size_t sz
Size of the memory.
virtual void * getPointer()
get a readable pointer with the data
void allocate_host(size_t sz) const
Allocate an host buffer.
bool copyDeviceToDevice(const CudaMemory &m)
copy from GPU to GPU buffer directly
void isNotSync()
virtual void destroy()
destroy memory
Definition CudaMemory.cu:80
bool isInitialized()
Allocated Memory is never initialized.
CudaMemory()
Constructor.
this class is a functor for "for_each" algorithm