OWL
Loading...
Searching...
No Matches
DeviceMemory.h
1// ======================================================================== //
2// Copyright 2019-2021 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include "owl/helper/cuda.h"
20
21namespace owl {
22
23 struct DeviceMemory {
24 inline ~DeviceMemory() { if (!externallyManaged) free(); }
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; }
29
30 inline void alloc(size_t size);
31 inline void allocManaged(size_t size);
32 inline void *get();
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);
37 inline void free();
38 template<typename T>
39 inline void upload(const std::vector<T> &vec);
40
41 size_t sizeInBytes { 0 };
42 CUdeviceptr d_pointer { 0 };
43 bool externallyManaged = false;
44 };
45
46 inline void DeviceMemory::alloc(size_t size)
47 {
48 assert(!externallyManaged);
49 if (alloced() || size > this->sizeInBytes) free();
50
51 assert(empty());
52 this->sizeInBytes = size;
53 OWL_CUDA_CHECK(cudaMalloc( (void**)&d_pointer, sizeInBytes));
54 assert(alloced() || size == 0);
55 }
56
57 inline void DeviceMemory::allocManaged(size_t size)
58 {
59 assert(!externallyManaged);
60 assert(empty());
61 this->sizeInBytes = size;
62 OWL_CUDA_CHECK(cudaMallocManaged( (void**)&d_pointer, sizeInBytes));
63 assert(alloced() || size == 0);
64 }
65
66 inline void *DeviceMemory::get()
67 {
68 return (void*)d_pointer;
69 }
70
71 inline void DeviceMemory::upload(const void *h_pointer, const char *debugMessage)
72 {
73 assert(alloced() || empty());
74 OWL_CUDA_CHECK2(debugMessage,
75 cudaMemcpy((void*)d_pointer, h_pointer,
76 sizeInBytes, cudaMemcpyHostToDevice));
77 }
78
79 inline void DeviceMemory::uploadAsync(const void *h_pointer, cudaStream_t stream)
80 {
81 assert(alloced() || empty());
82 OWL_CUDA_CHECK(cudaMemcpyAsync((void*)d_pointer, h_pointer,
83 sizeInBytes, cudaMemcpyHostToDevice,
84 stream));
85 }
86
87 inline void DeviceMemory::uploadAsync(const void *h_pointer, size_t offset, size_t size, cudaStream_t stream)
88 {
89 assert(alloced() || empty());
90 assert(offset + size <= sizeInBytes);
91 OWL_CUDA_CHECK(cudaMemcpyAsync((void*)(d_pointer + offset), h_pointer,
92 size, cudaMemcpyHostToDevice,
93 stream));
94 }
95
96 inline void DeviceMemory::download(void *h_pointer)
97 {
98 assert(alloced() || sizeInBytes == 0);
99 OWL_CUDA_CHECK(cudaMemcpy(h_pointer, (void*)d_pointer,
100 sizeInBytes, cudaMemcpyDeviceToHost));
101 }
102
103 inline void DeviceMemory::free()
104 {
105 assert(!externallyManaged);
106 assert(alloced() || empty());
107 if (!empty()) {
108 OWL_CUDA_CHECK(cudaFree((void*)d_pointer));
109 }
110 sizeInBytes = 0;
111 d_pointer = 0;
112 assert(empty());
113 }
114
115 template<typename T>
116 inline void DeviceMemory::upload(const std::vector<T> &vec)
117 {
118 if (!alloced()) {
119 alloc(vec.size()*sizeof(T));
120 } else {
121 assert(size() == vec.size()*sizeof(T));
122 }
123 assert(alloced() || vec.empty());
124 upload(vec.data());
125 }
126
128 void resize(size_t N) {
129 if (ptr) cudaFree(ptr);
130 ptr = 0;
131 cudaMallocHost(&ptr,N);
132 }
133 uint8_t *data() { return ptr; }
134
135 uint8_t *ptr = 0;
136 };
137
138} //::owl
Definition: DeviceMemory.h:23
Definition: DeviceMemory.h:127