-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.h
120 lines (105 loc) · 3.29 KB
/
util.h
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
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_UTIL_HEADER
#define CACC_UTIL_HEADER
#include <chrono>
#include <thread>
#include <iostream>
#include "defines.h"
#define TERM_WIDTH 76
CACC_NAMESPACE_BEGIN
void
print_cuda_devices(void) {
int count;
CHECK(cudaGetDeviceCount(&count));
std::cout << "Found " << count << " CUDA Devices:" << std::endl;
std::cout << std::string(TERM_WIDTH, '=') << std::endl;
for (int i = 0; i < count; ++i) {
cudaDeviceProp prop;
CHECK(cudaGetDeviceProperties(&prop, i));
std::cout << std::string(TERM_WIDTH, '-') << std::endl;
std::cout << "Device:\t" << prop.name << std::endl;
std::cout << std::string(TERM_WIDTH, '-') << std::endl;
std::cout << "\tCompute Capability:\t" << prop.major <<
"." << prop.minor << std::endl
<< "\tMultiprocessor Count:\t"
<< prop.multiProcessorCount << std::endl
<< "\tGPU Clock Rate:\t\t"
<< prop.clockRate / 1000 << " Mhz" << std::endl
<< "\tTotal Global Memory:\t"
<< prop.totalGlobalMem / (2 << 20) << " MB" << std::endl
<< "\tL2 Cache Size:\t\t"
<< prop.l2CacheSize / (2 << 10) << " KB" << std::endl
<< "\tMax Block Size:\t\t"
<< prop.maxThreadsDim[0] << "x"
<< prop.maxThreadsDim[1] << "x"
<< prop.maxThreadsDim[2] << std::endl
<< "\tMax Threads Per Block:\t"
<< prop.maxThreadsPerBlock << std::endl
<< "\tShared Memory Size:\t"
<< prop.sharedMemPerBlock / (2 << 10) << " KB" << std::endl;
}
std::cout << std::string(TERM_WIDTH, '=') << std::endl;
}
int
get_cuda_device(int major, int minor) {
int device;
cudaDeviceProp prop;
prop.major = major;
prop.minor = minor;
CHECK(cudaChooseDevice(&device, &prop));
return device;
}
int
get_cuda_devices(int major, int minor, std::vector<int> *devices) {
int count;
int num_devices = 0;
CHECK(cudaGetDeviceCount(&count));
for (int i = 0; i < count; ++i) {
cudaDeviceProp prop;
CHECK(cudaGetDeviceProperties(&prop, i));
if (prop.major > major || prop.major == major && prop.minor >= minor) {
devices->push_back(i);
num_devices += 1;
}
}
return num_devices;
}
int
select_cuda_device(int major, int minor) {
int device = get_cuda_device(major, minor);
CHECK(cudaSetDevice(device));
return device;
}
std::string
device_name(int device) {
cudaDeviceProp prop;
CHECK(cudaGetDeviceProperties(&prop, device));
return prop.name;
}
void
set_cuda_device(int device) {
CHECK(cudaSetDevice(device));
}
void
set_cuda_gl_device(int device) {
CHECK(cudaSetDevice(device));
}
template <class Rep, class Period>
void
sync(cudaStream_t stream, cudaEvent_t event,
std::chrono::duration<Rep, Period> sleep = std::chrono::milliseconds(1))
{
CHECK(cudaEventRecord(event, stream));
while (cudaEventQuery(event) == cudaErrorNotReady) {
std::this_thread::sleep_for(sleep);
}
CHECK(cudaEventQuery(event));
}
CACC_NAMESPACE_END
#endif /* CACC_UTIL_HEADER */