forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_interface.cu
56 lines (54 loc) · 1.49 KB
/
array_interface.cu
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
/*!
* Copyright 2021 by Contributors
*/
#include "../common/common.h"
#include "array_interface.h"
namespace xgboost {
void ArrayInterfaceHandler::SyncCudaStream(int64_t stream) {
switch (stream) {
case 0:
/**
* disallowed by the `__cuda_array_interface__`. Quote:
*
* This is disallowed as it would be ambiguous between None and the default
* stream, and also between the legacy and per-thread default streams. Any use
* case where 0 might be given should either use None, 1, or 2 instead for
* clarity.
*/
LOG(FATAL) << "Invalid stream ID in array interface: " << stream;
case 1:
// default legacy stream
break;
case 2:
// default per-thread stream
default:
dh::safe_cuda(cudaStreamSynchronize(reinterpret_cast<cudaStream_t>(stream)));
}
}
bool ArrayInterfaceHandler::IsCudaPtr(void const* ptr) {
if (!ptr) {
return false;
}
cudaPointerAttributes attr;
auto err = cudaPointerGetAttributes(&attr, ptr);
// reset error
CHECK_EQ(err, cudaGetLastError());
if (err == cudaErrorInvalidValue) {
// CUDA < 11
return false;
} else if (err == cudaSuccess) {
// CUDA >= 11
switch (attr.type) {
case cudaMemoryTypeUnregistered:
case cudaMemoryTypeHost:
return false;
default:
return true;
}
return true;
} else {
// other errors, `cudaErrorNoDevice`, `cudaErrorInsufficientDriver` etc.
return false;
}
}
} // namespace xgboost