forked from girving/mandelbrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutil.cc
55 lines (46 loc) · 1.28 KB
/
cutil.cc
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
// Cuda utilities
#ifdef __CUDACC__
#include "cutil.h"
namespace mandelbrot {
void cuda_check_fail(cudaError_t code, const char* function, const char* file, unsigned int line,
const char* expression, const string& message) {
die("%s:%d:%s: %s = %d (%s)%s",
file, line, function, expression, int(code), cudaGetErrorString(code),
message.size() ? ", " + message : "");
}
// Move to header if we want to go to multiple streams later
struct Stream {
private:
shared_ptr<CUstream_st> s;
public:
Stream() {
CUstream p;
cuda_check(cudaStreamCreate(&p));
s.reset(p, [](CUstream p) { cuda_check(cudaStreamDestroy(p)); });
}
CUstream stream() const { return s.get(); }
operator CUstream() const { return stream(); }
};
CUstream stream() {
const bool synchronous = false;
if (synchronous)
return 0;
else {
static Stream s;
return s;
}
}
void cuda_sync() {
cuda_check(cudaStreamSynchronize(stream()));
}
int num_sms() {
static const int num_sms = []() {
const int device = 0; // This will need to change if we go multi-device
int num_sms;
cuda_check(cudaDeviceGetAttribute(&num_sms, cudaDevAttrMultiProcessorCount, device));
return num_sms;
}();
return num_sms;
}
} // namespace mandelbrot
#endif // __CUDACC_