-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda.cpp
More file actions
68 lines (51 loc) · 3.71 KB
/
Copy pathcuda.cpp
File metadata and controls
68 lines (51 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module;
#include <cuda_runtime_api.h>
module xayah.cuda;
import std;
namespace xayah::cuda {
Resource::Resource() : native_stream(nullptr), memory_pool_(nullptr) {
int device;
if (const cudaError_t result = cudaGetDevice(&device); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
cudaMemPoolProps properties{};
properties.allocType = cudaMemAllocationTypePinned;
properties.handleTypes = cudaMemHandleTypeNone;
properties.location.type = cudaMemLocationTypeDevice;
properties.location.id = device;
cudaMemPool_t memory_pool;
if (const cudaError_t result = cudaMemPoolCreate(&memory_pool, &properties); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
memory_pool_ = memory_pool;
std::uint64_t release_threshold = std::numeric_limits<std::uint64_t>::max();
if (const cudaError_t result = cudaMemPoolSetAttribute(memory_pool, cudaMemPoolAttrReleaseThreshold, &release_threshold); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
cudaStream_t stream;
if (const cudaError_t result = cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
native_stream = stream;
}
Resource::~Resource() noexcept {
if (cudaStreamSynchronize(static_cast<cudaStream_t>(native_stream)) != cudaSuccess) std::terminate();
if (cudaStreamDestroy(static_cast<cudaStream_t>(native_stream)) != cudaSuccess) std::terminate();
if (cudaMemPoolDestroy(static_cast<cudaMemPool_t>(memory_pool_)) != cudaSuccess) std::terminate();
}
void* Resource::allocate(const std::size_t bytes) {
void* allocation;
if (const cudaError_t result = cudaMallocFromPoolAsync(&allocation, bytes, static_cast<cudaMemPool_t>(memory_pool_), static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
return allocation;
}
void Resource::release(void* allocation) noexcept {
if (cudaFreeAsync(allocation, static_cast<cudaStream_t>(native_stream)) != cudaSuccess) std::terminate();
}
void Resource::copy_from_host(void* destination, const void* source, const std::size_t bytes) {
if (const cudaError_t result = cudaMemcpyAsync(destination, source, bytes, cudaMemcpyHostToDevice, static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
}
void Resource::copy_to_host(void* destination, const void* source, const std::size_t bytes) {
if (const cudaError_t result = cudaMemcpyAsync(destination, source, bytes, cudaMemcpyDeviceToHost, static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
}
void Resource::copy_device(void* destination, const void* source, const std::size_t bytes) {
if (const cudaError_t result = cudaMemcpyAsync(destination, source, bytes, cudaMemcpyDeviceToDevice, static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
}
void Resource::zero(void* destination, const std::size_t bytes) {
if (const cudaError_t result = cudaMemsetAsync(destination, 0, bytes, static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
}
void Resource::synchronize() {
if (const cudaError_t result = cudaStreamSynchronize(static_cast<cudaStream_t>(native_stream)); result != cudaSuccess) throw std::runtime_error(cudaGetErrorString(result));
}
} // namespace xayah::cuda