3#include "CudaMemory.cuh"
5#include "util/cudify/cudify.hpp"
8#define CUDA_EVENT 0x1201
17 if (
hm != NULL &&
dm != NULL)
22 CUDA_SAFE_CALL(hipMemcpy(
dm,
hm,
sz+32,hipMemcpyHostToDevice));
24 CUDA_SAFE_CALL(cudaMemcpy(
dm,
hm,
sz+32,cudaMemcpyHostToDevice));
44 CUDA_SAFE_CALL(hipMalloc(&
dm,
sz+32));
45 #elif defined(CUDIFY_USE_CUDA)
46 CUDA_SAFE_CALL(cudaMalloc(&
dm,
sz+32));
50 dm =
new unsigned char[
sz+32];
51 #ifdef GARBAGE_INJECTOR
52 memset(
dm,0xFF,
sz+32);
61 std::cout << __FILE__ <<
":" << __LINE__ <<
" error FATAL: using allocate to resize the memory, please use resize." << std::endl;
68#if defined(GARBAGE_INJECTOR) && !defined(CUDA_ON_CPU)
69 CUDA_SAFE_CALL(cudaMemset(
dm,-1,
sz))
86 CUDA_SAFE_CALL(hipFree(
dm));
87 #elif defined(CUDIFY_USE_CUDA)
88 CUDA_SAFE_CALL(cudaFree(
dm));
90 delete [] (
unsigned char *)
dm;
99 CUDA_SAFE_CALL(hipHostFree(
hm));
100 #elif defined(CUDIFY_USE_CUDA)
101 CUDA_SAFE_CALL(cudaFreeHost(
hm));
103 delete [] (
unsigned char *)
hm;
122 CUDA_SAFE_CALL(hipMemcpy(((
unsigned char *)
dm)+offset,((
unsigned char *)ptr)+start,(stop-start),hipMemcpyDeviceToDevice));
124 CUDA_SAFE_CALL(cudaMemcpy(((
unsigned char *)
dm)+offset,((
unsigned char *)ptr)+start,(stop-start),cudaMemcpyDeviceToDevice));
138 CUDA_SAFE_CALL(hipHostMalloc(&
hm,
sz+32,hipHostMallocMapped))
139 #elif defined(CUDIFY_USE_CUDA)
140 CUDA_SAFE_CALL(cudaHostAlloc(&
hm,
sz+32,cudaHostAllocMapped))
142 hm =
new unsigned char[
sz+32];
143 #ifdef GARBAGE_INJECTOR
144 memset(
hm,0xFF,
sz+32);
167 CUDA_SAFE_CALL(hipHostGetDevicePointer(&dvp,
hm,0));
169 memcpy(dvp,ptr,
sz+32);
171 CUDA_SAFE_CALL(cudaHostGetDevicePointer(&dvp,
hm,0));
173 memcpy(dvp,ptr,
sz+32);
193 std::cerr <<
"Error " << __LINE__ << __FILE__ <<
": source buffer is too big to copy";
201 CUDA_SAFE_CALL(hipMemcpy(
dm,m.
dm,m.
sz+32,hipMemcpyDeviceToDevice));
203 CUDA_SAFE_CALL(cudaMemcpy(
dm,m.
dm,m.
sz+32,cudaMemcpyDeviceToDevice));
283 CUDA_SAFE_CALL(hipMalloc(&tdm,
sz+32));
284 #elif defined(CUDIFY_USE_CUDA)
285 CUDA_SAFE_CALL(cudaMalloc(&tdm,
sz+32));
287 tdm =
new unsigned char [
sz+32];
288 #ifdef GARBAGE_INJECTOR
289 memset(tdm,0xFF,
sz+32);
293#ifdef GARBAGE_INJECTOR
295 CUDA_SAFE_CALL(hipMemset(tdm,-1,
sz+32));
296 #elif defined(CUDIFY_USE_CUDA)
297 CUDA_SAFE_CALL(cudaMemset(tdm,-1,
sz+32));
315 CUDA_SAFE_CALL(hipHostMalloc(&thm,
sz+32,hipHostMallocMapped));
316 #elif defined(CUDIFY_USE_CUDA)
317 CUDA_SAFE_CALL(cudaHostAlloc(&thm,
sz+32,cudaHostAllocMapped));
319 thm =
new unsigned char [
sz+32];
320 #ifdef GARBAGE_INJECTOR
321 memset(thm,0xFF,
sz+32);
377 CUDA_SAFE_CALL(hipMemcpy(
hm,
dm,
sz+32,hipMemcpyDeviceToHost));
379 CUDA_SAFE_CALL(cudaMemcpy(
hm,
dm,
sz+32,cudaMemcpyDeviceToHost));
402 CUDA_SAFE_CALL(hipMemcpy(mem.
hm,
dm,mem.
sz+32,hipMemcpyDeviceToHost));
404 CUDA_SAFE_CALL(cudaMemcpy(mem.
hm,
dm,mem.
sz+32,cudaMemcpyDeviceToHost));
425 CUDA_SAFE_CALL(hipMemcpy(
dm,mem.
hm,mem.
sz+32,hipMemcpyHostToDevice));
427 CUDA_SAFE_CALL(cudaMemcpy(
dm,mem.
hm,mem.
sz+32,cudaMemcpyHostToDevice));
439 CUDA_SAFE_CALL(hipMemcpy(((
unsigned char *)
dm)+start,((
unsigned char *)
hm)+start,(stop-start),hipMemcpyHostToDevice));
441 CUDA_SAFE_CALL(cudaMemcpy(((
unsigned char *)
dm)+start,((
unsigned char *)
hm)+start,(stop-start),cudaMemcpyHostToDevice));
458 CUDA_SAFE_CALL(hipMemcpy(((
unsigned char *)
hm)+start,((
unsigned char *)
dm)+start,(stop-start),hipMemcpyDeviceToHost));
460 CUDA_SAFE_CALL(cudaMemcpy(((
unsigned char *)
hm)+start,((
unsigned char *)
dm)+start,(stop-start),cudaMemcpyDeviceToHost));
488 CUDA_SAFE_CALL(hipMemset(
dm,c,
size()));
489 #elif defined(CUDIFY_USE_CUDA)
490 CUDA_SAFE_CALL(cudaMemset(
dm,c,
size()));
522 CUDA_SAFE_CALL(hipMemcpy(
dm,
hm,
sz+32,hipMemcpyHostToDevice));
524 CUDA_SAFE_CALL(cudaMemcpy(
dm,
hm,
sz+32,cudaMemcpyHostToDevice));
void deviceToDevice(void *ptr, size_t start, size_t stop, size_t offset)
copy memory from device to device
virtual bool resize(size_t sz)
resize the momory allocated
size_t ref_cnt
Reference counter.
bool is_hm_sync
Is the host memory synchronized with the GPU memory.
virtual size_t size() const
the the size of the allocated memory
bool copyFromPointer(const void *ptr)
copy from Pointer to GPU
size_t sz
Size of the memory.
void allocate_host(size_t sz) const
Allocate an host buffer.
bool copyDeviceToDevice(const CudaMemory &m)
copy from GPU to GPU buffer directly
virtual void destroy()
destroy memory
virtual bool flush()
flush the memory
virtual bool allocate(size_t sz)
allocate memory
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
virtual bool resize(size_t sz)
resize the memory allocated
virtual void * getPointer()
get a readable pointer with the data
virtual size_t size() const
the the size of the allocated memory
virtual void deviceToHost()
Do nothing.
virtual bool copy(const memory &m)
copy memory
void swap(HeapMemory &mem)
Swap the memory.
virtual void hostToDevice()
Do nothing.
virtual void * getDevicePointer()
get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents
virtual void destroy()
destroy memory
bool copyDeviceToDevice(const HeapMemory &m)
copy from same Heap to Heap
virtual void * getPointer()=0
return a data pointer
this class is a functor for "for_each" algorithm