-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamr_cuda_device.cxx
210 lines (181 loc) · 6.62 KB
/
hamr_cuda_device.cxx
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "hamr_cuda_device.h"
#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>
namespace hamr
{
// **************************************************************************
int get_active_cuda_device(int &dev_id)
{
cudaError_t ierr = cudaSuccess;
if ((ierr = cudaGetDevice(&dev_id)) != cudaSuccess)
{
std::cerr << "Failed to get the active CUDA device. "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
return 0;
}
// **************************************************************************
int set_active_cuda_device(int dev_id)
{
cudaError_t ierr = cudaSuccess;
if ((ierr = cudaSetDevice(dev_id)) != cudaSuccess)
{
std::cerr << "Failed to set the active CUDA device. "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
return 0;
}
// **************************************************************************
int get_cuda_device(const void *ptr, int &device_id)
{
cudaError_t ierr = cudaSuccess;
cudaPointerAttributes ptrAtts;
ierr = cudaPointerGetAttributes(&ptrAtts, ptr);
// these types of pointers are NOT accessible on the GPU
// cudaErrorInValue occurs when the pointer is unknown to CUDA, as is
// the case with pointers allocated by malloc or new.
if ((ierr == cudaErrorInvalidValue) ||
((ierr == cudaSuccess) && ((ptrAtts.type == cudaMemoryTypeHost) ||
(ptrAtts.type == cudaMemoryTypeUnregistered))))
{
// this is host backed memory not associate with a GPU
device_id = -1;
}
else if (ierr != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to get pointer attributes for " << ptr << std::endl;
return -1;
}
else
{
device_id = ptrAtts.device;
}
return 0;
}
// --------------------------------------------------------------------------
activate_cuda_device::activate_cuda_device(int new_dev) : m_device(-1)
{
int cur_dev = -1;
if (!get_active_cuda_device(cur_dev) && (cur_dev != new_dev) &&
!set_active_cuda_device(new_dev))
{
m_device = cur_dev;
}
}
// --------------------------------------------------------------------------
activate_cuda_device::~activate_cuda_device()
{
if (m_device >= 0)
{
set_active_cuda_device(m_device);
}
}
// **************************************************************************
int access_cuda_peer::enable(int dest_device, int src_device, bool symetric)
{
int access = 0;
cudaError_t ierr = cudaSuccess;
// ensure that the current device is restored if we return due to an error
// hamr::cuda_device activate_dest(dest_device);
// enable dest to access the source memory
if ((ierr = cudaDeviceCanAccessPeer(&access, dest_device, src_device)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to determine peer accessibility between "
<< dest_device << " and " << src_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
if (!access)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Can't access device " << src_device
<< " from " << dest_device << std::endl;
return -1;
}
if ((ierr = cudaDeviceEnablePeerAccess(src_device, 0)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to enable peer accessibility between "
<< dest_device << " and " << src_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
// record that p2p was enabled
m_src_device = src_device;
m_dest_device = dest_device;
m_symetric = false;
// enable the source to access the dest memory
if (symetric)
{
if ((ierr = cudaDeviceCanAccessPeer(&access, src_device, dest_device)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to determine peer accessibility between "
<< dest_device << " and " << src_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
if (!access)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Can't access device " << dest_device
<< " from " << src_device << std::endl;
return -1;
}
// ensure that the current device is restored if we return due to an error
hamr::activate_cuda_device activate_src(src_device);
if ((ierr = cudaDeviceEnablePeerAccess(dest_device, 0)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to enable peer accessibility between "
<< src_device << " and " << dest_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
// record taht p2p was enabled
m_symetric = true;
}
return 0;
}
// **************************************************************************
int access_cuda_peer::disable()
{
if (m_src_device >= 0)
{
// ensure that the current device is restored if we return due to an error
hamr::activate_cuda_device activate_dest(m_dest_device);
// disable peer to peer memory map between dest and src
cudaError_t ierr = cudaSuccess;
if ((ierr = cudaDeviceDisablePeerAccess(m_src_device)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to disable peer accessibility between "
<< m_dest_device << " and " << m_src_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
if (m_symetric)
{
// ensure that the current device is restored if we return due to an error
hamr::activate_cuda_device activate_src(m_src_device);
// disable peer to peer memory map between dest and src
if ((ierr = cudaDeviceDisablePeerAccess(m_dest_device)) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to disable peer accessibility between "
<< m_src_device << " and " << m_dest_device << ". "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
}
// record that p2p was disabled
m_src_device = -1;
}
return 0;
}
}