forked from clarenc3/CUDAProb3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuda_unique.cuh
151 lines (99 loc) · 3.24 KB
/
cuda_unique.cuh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
This file is part of CUDAProb3++.
CUDAProb3++ is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CUDAProb3++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with CUDAProb3++. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CUDAPROB3_CUDA_UNIQUE_CUH
#define CUDAPROB3_CUDA_UNIQUE_CUH
#include "hpc_helpers.cuh"
#include <cuda.h>
#include <cuda_runtime.h>
#include <memory>
struct CudaDeleter
{
int deviceId;
CudaDeleter(){}
CudaDeleter(const CudaDeleter& other){
*this = other;
}
CudaDeleter(const CudaDeleter&& other){
*this = std::move(other);
}
CudaDeleter(int id):deviceId(id){}
CudaDeleter& operator=(const CudaDeleter& other){
deviceId = other.deviceId;
return *this;
}
CudaDeleter& operator=(const CudaDeleter&& other){
deviceId = other.deviceId;
return *this;
}
void operator()(void *p){
cudaSetDevice(deviceId); CUERR;
cudaFree(p);// CUERR;
cudaError_t err;
if ((err = cudaGetLastError()) != cudaSuccess) {
std::cout << "CUDA error: " << cudaGetErrorString(err) << " : "
<< __FILE__ << ", line " << __LINE__ << std::endl;
}
}
};
struct PinnedCudaDeleter
{
void operator()(void *p){
cudaFreeHost(p); CUERR;
}
};
template <class T>
using unique_dev_ptr = std::unique_ptr<T, CudaDeleter>;
template <class T>
using shared_dev_ptr = std::shared_ptr<T>;
template <class T>
using unique_pinned_ptr = std::unique_ptr<T, PinnedCudaDeleter>;
template <class T>
using shared_pinned_ptr = std::shared_ptr<T>;
template <class T>
unique_dev_ptr<T> make_unique_dev(int deviceId, std::uint64_t elements){
cudaSetDevice(deviceId); CUERR;
T* mem = nullptr;
cudaMalloc(&mem, sizeof(T) * elements); CUERR;
return unique_dev_ptr<T>(mem, CudaDeleter{deviceId});
}
template <class T>
shared_dev_ptr<T> make_shared_dev(int deviceId, std::uint64_t elements){
cudaSetDevice(deviceId); CUERR;
T* mem = nullptr;
cudaMalloc(&mem, sizeof(T) * elements); CUERR;
return shared_dev_ptr<T>(mem, CudaDeleter{deviceId});
}
template <class T>
unique_pinned_ptr<T> make_unique_pinned(std::uint64_t elements){
T* mem = nullptr;
cudaMallocHost(&mem, sizeof(T) * elements); CUERR;
return unique_pinned_ptr<T>(mem, PinnedCudaDeleter{});
}
template <class T>
shared_pinned_ptr<T> make_shared_pinned(std::uint64_t elements){
T* mem = nullptr;
cudaMallocHost(&mem, sizeof(T) * elements); CUERR;
return shared_pinned_ptr<T>(mem, PinnedCudaDeleter{});
}
// wrap existing device pointer into unique pointer
template <class T>
unique_dev_ptr<T> make_unique_dev(int deviceId, T* ptr){
return std::unique_ptr<T, CudaDeleter>(ptr, CudaDeleter{deviceId});
}
// wrap existing device pointer into shared pointer
template <class T>
shared_dev_ptr<T> make_shared_dev(int deviceId, T* ptr){
return std::shared_ptr<T>(ptr, CudaDeleter{deviceId});
}
#endif