forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
THCGeneral.cpp
367 lines (301 loc) · 10.3 KB
/
THCGeneral.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <THC/THCGeneral.h>
#include <TH/TH.h>
#include <THC/THCAllocator.h>
#include <THC/THCCachingHostAllocator.h>
#include <THC/THCGeneral.hpp>
#include <c10/cuda/CUDAStream.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDACachingAllocator.h>
#include <stdlib.h>
#include <stdint.h>
/* Size of scratch space available in global memory per each SM + stream */
#define MIN_GLOBAL_SCRATCH_SPACE_PER_SM_STREAM 4 * sizeof(float)
/* Minimum amount of scratch space per device. Total scratch memory per
* device is either this amount, or the # of SMs * the space per SM defined
* above, whichever is greater.*/
#define MIN_GLOBAL_SCRATCH_SPACE_PER_DEVICE 32768 * sizeof(float)
/* Maximum number of P2P connections (if there are more than 9 then P2P is
* enabled in groups of 8). */
#define THC_CUDA_MAX_PEER_SIZE 8
void THCState_free(THCState* state)
{
free(state);
}
THCCudaResourcesPerDevice* THCState_getDeviceResourcePtr(
THCState *state, int device);
THCState* THCState_alloc(void)
{
THCState* state = (THCState*) calloc(1, sizeof(THCState));
return state;
}
void THCudaInit(THCState* state)
{
if (!state->cudaDeviceAllocator) {
state->cudaDeviceAllocator = c10::cuda::CUDACachingAllocator::get();
}
if (!state->cudaHostAllocator) {
state->cudaHostAllocator = getTHCCachingHostAllocator();
}
int numDevices = 0;
THCudaCheck(cudaGetDeviceCount(&numDevices));
state->numDevices = numDevices;
int device = 0;
THCudaCheck(cudaGetDevice(&device));
state->resourcesPerDevice = (THCCudaResourcesPerDevice*)
calloc(numDevices, sizeof(THCCudaResourcesPerDevice));
// p2pAccessEnabled records if p2p copies are allowed between pairs of
// devices. Values include "1" (copy allowed), "0" (copy not allowed), and
// "-1" (unknown).
// Currently the max number of gpus in P2P group is 8, so if there are more
// we enable P2P in groups of 8
state->p2pAccessEnabled = (int**) calloc(numDevices, sizeof(int*));
for (int i = 0; i < numDevices; ++i) {
state->p2pAccessEnabled[i] = (int*) calloc(numDevices, sizeof(int));
for (int j = 0; j < numDevices; ++j)
if (i == j)
state->p2pAccessEnabled[i][j] = 1;
else
state->p2pAccessEnabled[i][j] = -1;
}
for (int i = 0; i < numDevices; ++i) {
THCCudaResourcesPerDevice* res = THCState_getDeviceResourcePtr(state, i);
THCudaCheck(cudaSetDevice(i));
/* The scratch space that we want to have available per each device is
based on the number of SMs available per device. We guarantee a
minimum of 128kb of space per device, but to future-proof against
future architectures that may have huge #s of SMs, we guarantee that
we have at least 16 bytes for each SM. */
int numSM = at::cuda::getDeviceProperties(i)->multiProcessorCount;
size_t sizePerStream =
MIN_GLOBAL_SCRATCH_SPACE_PER_DEVICE >= numSM * MIN_GLOBAL_SCRATCH_SPACE_PER_SM_STREAM ?
MIN_GLOBAL_SCRATCH_SPACE_PER_DEVICE :
numSM * MIN_GLOBAL_SCRATCH_SPACE_PER_SM_STREAM;
res->scratchSpacePerStream = sizePerStream;
}
/* Restore to previous device */
THCudaCheck(cudaSetDevice(device));
}
void THCudaShutdown(THCState* state)
{
int deviceCount = 0;
int prevDev = -1;
THCudaCheck(cudaGetDevice(&prevDev));
THCudaCheck(cudaGetDeviceCount(&deviceCount));
/* cleanup p2p access state */
for (int dev = 0; dev < deviceCount; ++dev) {
free(state->p2pAccessEnabled[dev]);
}
free(state->p2pAccessEnabled);
free(state->resourcesPerDevice);
if (state->cudaDeviceAllocator == c10::cuda::CUDACachingAllocator::get()) {
c10::cuda::CUDACachingAllocator::emptyCache();
}
if (state->cudaHostAllocator == getTHCCachingHostAllocator()) {
THCCachingHostAllocator_emptyCache();
}
THCudaCheck(cudaSetDevice(prevDev));
}
int THCState_getPeerToPeerAccess(THCState* state, int dev, int devToAccess)
{
if (dev < 0 || dev >= state->numDevices) {
THError("%d is not a device", dev);
}
if (devToAccess < 0 || devToAccess >= state->numDevices) {
THError("%d is not a device", devToAccess);
}
if (state->p2pAccessEnabled[dev][devToAccess] == -1) {
int prevDev = 0;
THCudaCheck(cudaGetDevice(&prevDev));
THCudaCheck(cudaSetDevice(dev));
int access = 0;
THCudaCheck(cudaDeviceCanAccessPeer(&access, dev, devToAccess));
if (access) {
cudaError_t err = cudaDeviceEnablePeerAccess(devToAccess, 0);
if (err == cudaErrorPeerAccessAlreadyEnabled) {
// ignore and clear the error if access was already enabled
cudaGetLastError();
} else {
THCudaCheck(err);
}
state->p2pAccessEnabled[dev][devToAccess] = 1;
} else {
state->p2pAccessEnabled[dev][devToAccess] = 0;
}
THCudaCheck(cudaSetDevice(prevDev));
}
return state->p2pAccessEnabled[dev][devToAccess];
}
c10::Allocator* THCState_getCudaHostAllocator(THCState* state)
{
return state->cudaHostAllocator;
}
int THCState_getNumDevices(THCState *state)
{
return state->numDevices;
}
THCCudaResourcesPerDevice* THCState_getDeviceResourcePtr(
THCState *state, int device)
{
/* `device` is a CUDA index */
if (device >= state->numDevices || device < 0)
{
THError("%d is not a device", device + 1 /* back to Torch index */);
}
return &(state->resourcesPerDevice[device]);
}
// TODO: delete me
cudaStream_t THCState_getCurrentStreamOnDevice(THCState *state, int device) {
return at::cuda::getCurrentCUDAStream(device).stream();
}
// TODO: delete me
cudaStream_t THCState_getCurrentStream(THCState *state) {
return at::cuda::getCurrentCUDAStream().stream();
}
size_t THCState_getCurrentDeviceScratchSpaceSize(THCState* state)
{
int device = -1;
THCudaCheck(cudaGetDevice(&device));
THCCudaResourcesPerDevice* res = THCState_getDeviceResourcePtr(state, device);
return res->scratchSpacePerStream;
}
void __THCudaCheck(cudaError_t err, const char *file, const int line)
{
if(err != cudaSuccess)
{
static int alreadyFailed = 0;
if(!alreadyFailed) {
fprintf(stderr, "THCudaCheck FAIL file=%s line=%i error=%i : %s\n", file, line, err, cudaGetErrorString(err));
alreadyFailed = 1;
}
_THError(file, line, "cuda runtime error (%d) : %s", err,
cudaGetErrorString(err));
}
}
void __THCudaCheckWarn(cudaError_t err, const char *file, const int line)
{
if(err != cudaSuccess)
{
fprintf(stderr, "THCudaCheckWarn FAIL file=%s line=%i error=%i : %s\n", file, line, err, cudaGetErrorString(err));
}
}
void __THCublasCheck(cublasStatus_t status, const char *file, const int line)
{
if(status != CUBLAS_STATUS_SUCCESS)
{
const char* errmsg = NULL;
switch(status)
{
case CUBLAS_STATUS_NOT_INITIALIZED:
errmsg = "library not initialized";
break;
case CUBLAS_STATUS_ALLOC_FAILED:
errmsg = "resource allocation failed";
break;
case CUBLAS_STATUS_INVALID_VALUE:
errmsg = "an invalid numeric value was used as an argument";
break;
case CUBLAS_STATUS_ARCH_MISMATCH:
errmsg = "an absent device architectural feature is required";
break;
#ifndef __HIP_PLATFORM_HCC__
case CUBLAS_STATUS_MAPPING_ERROR:
errmsg = "an access to GPU memory space failed";
break;
case CUBLAS_STATUS_EXECUTION_FAILED:
errmsg = "the GPU program failed to execute";
break;
#endif
case CUBLAS_STATUS_INTERNAL_ERROR:
errmsg = "an internal operation failed";
break;
default:
errmsg = "unknown error";
break;
}
_THError(file, line, "cublas runtime error : %s", errmsg);
}
}
void __THCusparseCheck(cusparseStatus_t status, const char *file, const int line)
{
if(status != CUSPARSE_STATUS_SUCCESS)
{
const char* errmsg = NULL;
switch(status)
{
case CUSPARSE_STATUS_NOT_INITIALIZED:
errmsg = "library not initialized";
break;
case CUSPARSE_STATUS_ALLOC_FAILED:
errmsg = "resource allocation failed";
break;
case CUSPARSE_STATUS_INVALID_VALUE:
errmsg = "an invalid numeric value was used as an argument";
break;
case CUSPARSE_STATUS_ARCH_MISMATCH:
errmsg = "an absent device architectural feature is required";
break;
case CUSPARSE_STATUS_MAPPING_ERROR:
errmsg = "an access to GPU memory space failed";
break;
case CUSPARSE_STATUS_EXECUTION_FAILED:
errmsg = "the GPU program failed to execute";
break;
case CUSPARSE_STATUS_INTERNAL_ERROR:
errmsg = "an internal operation failed";
break;
case CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
errmsg = "the matrix type is not supported by this function";
break;
default:
errmsg = "unknown error";
break;
}
_THError(file, line, "cusparse runtime error : %s", errmsg);
}
}
void* THCudaMalloc(THCState *state, size_t size)
{
THCudaCheck(cudaGetLastError());
c10::Allocator* allocator = state->cudaDeviceAllocator;
return allocator->raw_allocate(size);
}
void THCudaFree(THCState *state, void* ptr) {
state->cudaDeviceAllocator->raw_deallocate(ptr);
}
at::DataPtr THCudaHostAlloc(THCState *state, size_t size)
{
THCudaCheck(cudaGetLastError());
c10::Allocator* allocator = state->cudaHostAllocator;
return allocator->allocate(size);
}
void THCudaHostRecord(THCState *state, void *ptr) {
if (state->cudaHostAllocator == getTHCCachingHostAllocator()) {
THCCachingHostAllocator_recordEvent(ptr, at::cuda::getCurrentCUDAStream());
}
}
cudaError_t THCudaMemGetInfo(THCState *state, size_t* freeBytes, size_t* totalBytes, size_t* largestBlock)
{
size_t cachedBytes = 0;
c10::Allocator* allocator = state->cudaDeviceAllocator;
*largestBlock = 0;
/* get info from CUDA first */
cudaError_t ret = cudaMemGetInfo(freeBytes, totalBytes);
if (ret!= cudaSuccess)
return ret;
int device;
ret = cudaGetDevice(&device);
if (ret!= cudaSuccess)
return ret;
/* not always true - our optimistic guess here */
*largestBlock = *freeBytes;
if (allocator == c10::cuda::CUDACachingAllocator::get()) {
c10::cuda::CUDACachingAllocator::cacheInfo(device, &cachedBytes, largestBlock);
}
/* Adjust resulting free bytes number. largesBlock unused for now */
*freeBytes += cachedBytes;
return cudaSuccess;
}
#undef MIN_GLOBAL_SCRATCH_SPACE_PER_SM_STREAM
#undef MIN_GLOBAL_SCRATCH_SPACE_PER_DEVICE
#include <THC/THCStorage.cpp>
#include <THC/THCAllocator.cpp>