19#include "owl/helper/cuda.h"
25 inline bool alloced()
const {
return !empty(); }
26 inline bool empty()
const {
return sizeInBytes == 0; }
27 inline bool notEmpty()
const {
return !empty(); }
28 inline size_t size()
const {
return sizeInBytes; }
30 inline void alloc(
size_t size);
31 inline void allocManaged(
size_t size);
33 inline void upload(
const void *h_pointer,
const char *debugMessage =
nullptr);
34 inline void uploadAsync(
const void *h_pointer, cudaStream_t stream);
35 inline void uploadAsync(
const void *h_pointer,
size_t offset,
size_t size, cudaStream_t stream);
36 inline void download(
void *h_pointer);
39 inline void upload(
const std::vector<T> &vec);
41 size_t sizeInBytes { 0 };
42 CUdeviceptr d_pointer { 0 };
43 bool externallyManaged =
false;
46 inline void DeviceMemory::alloc(
size_t size)
48 assert(!externallyManaged);
49 if (alloced() || size > this->sizeInBytes) free();
52 this->sizeInBytes = size;
53 OWL_CUDA_CHECK(cudaMalloc( (
void**)&d_pointer, sizeInBytes));
54 assert(alloced() || size == 0);
57 inline void DeviceMemory::allocManaged(
size_t size)
59 assert(!externallyManaged);
61 this->sizeInBytes = size;
62 OWL_CUDA_CHECK(cudaMallocManaged( (
void**)&d_pointer, sizeInBytes));
63 assert(alloced() || size == 0);
66 inline void *DeviceMemory::get()
68 return (
void*)d_pointer;
71 inline void DeviceMemory::upload(
const void *h_pointer,
const char *debugMessage)
73 assert(alloced() || empty());
74 OWL_CUDA_CHECK2(debugMessage,
75 cudaMemcpy((
void*)d_pointer, h_pointer,
76 sizeInBytes, cudaMemcpyHostToDevice));
79 inline void DeviceMemory::uploadAsync(
const void *h_pointer, cudaStream_t stream)
81 assert(alloced() || empty());
82 OWL_CUDA_CHECK(cudaMemcpyAsync((
void*)d_pointer, h_pointer,
83 sizeInBytes, cudaMemcpyHostToDevice,
87 inline void DeviceMemory::uploadAsync(
const void *h_pointer,
size_t offset,
size_t size, cudaStream_t stream)
89 assert(alloced() || empty());
90 assert(offset + size <= sizeInBytes);
91 OWL_CUDA_CHECK(cudaMemcpyAsync((
void*)(d_pointer + offset), h_pointer,
92 size, cudaMemcpyHostToDevice,
96 inline void DeviceMemory::download(
void *h_pointer)
98 assert(alloced() || sizeInBytes == 0);
99 OWL_CUDA_CHECK(cudaMemcpy(h_pointer, (
void*)d_pointer,
100 sizeInBytes, cudaMemcpyDeviceToHost));
103 inline void DeviceMemory::free()
105 assert(!externallyManaged);
106 assert(alloced() || empty());
108 OWL_CUDA_CHECK(cudaFree((
void*)d_pointer));
116 inline void DeviceMemory::upload(
const std::vector<T> &vec)
119 alloc(vec.size()*
sizeof(T));
121 assert(size() == vec.size()*
sizeof(T));
123 assert(alloced() || vec.empty());
128 void resize(
size_t N) {
129 if (ptr) cudaFree(ptr);
131 cudaMallocHost(&ptr,N);
133 uint8_t *data() {
return ptr; }
Definition: DeviceMemory.h:23
Definition: DeviceMemory.h:127