-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.h
176 lines (155 loc) · 4.99 KB
/
utils.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
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yichao Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Utils.
*
* Author: Yichao Cheng ([email protected])
* Created on: 2014-10-20
* Last Modified: 2014-10-23
*/
#ifndef UTILS_H
#define UTILS_H
#include <utility>
#include "cuda_runtime.h"
#include "common.h"
#include "logging.h"
namespace util {
/**
* Calculates the block number to launch a kernel by specifying
* the thread number to fulfill the job and per-block thread number.
*
* The block number is redundant and can not exceed the limits
* which is defined by the architecture.
*
* @param threads How many threads is requires
* @param threadsPerBlock How many threads in each block (256 by default)
* @return A pair (block number, thread number per block)
*/
std::pair<int, int> kernelConfig(int threads,
int threadsPerBlock = DEFAULT_THREADS_PER_BLOCK) {
if (threads == 0) {
return std::make_pair(0, 0);
}
assert(threadsPerBlock <= MAX_THREADS_PER_BLOCK);
if (threads < threadsPerBlock) threadsPerBlock = threads;
int blocks = threads % threadsPerBlock == 0 ?
threads / threadsPerBlock :
threads / threadsPerBlock + 1;
if (blocks > MAX_BLOCKS) blocks = MAX_BLOCKS;
// LOG(INFO) << "The kernel is configured to (" << blocks
// << ", " << threadsPerBlock << ")";
return std::make_pair(blocks, threadsPerBlock);
}
/**
* Enable peer access from `self` to `other`
*/
void enablePeerAccess(int self, int other) {
CUDA_CHECK(cudaSetDevice(self));
int canAccess = 0;
CUDA_CHECK(cudaDeviceCanAccessPeer(&canAccess, self, other));
if (canAccess == 1) {
CUDA_CHECK(cudaDeviceEnablePeerAccess(other, 0));
LOG(INFO) << self << " enable peer access " << other;
} else {
LOG(WARNING) << self << " cannot access peer " << other;
}
}
/**
* disable peer access from `self` to `other`
*/
void disablePeerAccess(int self, int other) {
CUDA_CHECK(cudaSetDevice(self));
int canAccess = 0;
CUDA_CHECK(cudaDeviceCanAccessPeer(&canAccess, self, other));
if (canAccess == 1) {
CUDA_CHECK(cudaDeviceDisablePeerAccess(other));
LOG(INFO) << self << " disable peer access " << other;
} else {
LOG(WARNING) << self << " cannot access peer " << other;
}
}
/**
* Enable all-to-all peer access
* TODO(onesuper): Temporarily making the following functions a part of utilities.
*/
void enableAllPeerAccess() {
int numGpus = 0;
CUDA_CHECK(cudaGetDeviceCount(&numGpus));
LOG(INFO) << numGpus << " GPU detected";
for (int i = 0; i < numGpus; i++) {
for (int j = i + 1; j < numGpus; j++) {
enablePeerAccess(i, j);
enablePeerAccess(j, i);
}
}
}
void disableAllPeerAccess() {
int numGpus = 0;
CUDA_CHECK(cudaGetDeviceCount(&numGpus));
LOG(INFO) << numGpus << " GPU detected";
for (int i = 0; i < numGpus; i++) {
for (int j = i + 1; j < numGpus; j++) {
disablePeerAccess(i, j);
disablePeerAccess(j, i);
}
}
}
void expectOverlapOnAllDevices() {
int dev_count;
cudaDeviceProp prop;
cudaGetDeviceCount(&dev_count);
for (int i = 0; i < dev_count; i++) {
cudaGetDeviceProperties(&prop, i);
assert(prop.deviceOverlap) ;
}
}
/**
* Checks if the string is a numeric number
* @param str String to check
* @return True If the string represents a numeric number
*/
bool isNumeric(const char *str) {
assert(str);
while ((* str) != '\0') {
if (!isdigit(* str)) {
return false;
} else {
str++;
}
}
return true;
}
/**
* Get the hash code of any given number very quickly
* @param a The number to hash
* @return The hash code
*/
size_t hashCode(size_t a) {
a ^= (a << 13);
a ^= (a >> 17);
a ^= (a << 5);
return a;
}
} // namespace util
#endif // UTILS_H