forked from KlausT/ccminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda.cpp
284 lines (259 loc) · 6.78 KB
/
cuda.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
#include <cstdio>
#include <memory.h>
#include <cstring>
#include <map>
#ifndef _WIN32
#include <unistd.h>
#endif
// include thrust
#ifndef __cplusplus
#include <thrust/version.h>
#include <thrust/remove.h>
#include <thrust/device_vector.h>
#include <thrust/iterator/constant_iterator.h>
#else
#include <ctype.h>
#endif
#include "nvml.h"
#include "cuda_runtime.h"
#include "miner.h"
cudaDeviceProp device_props[MAX_GPUS];
cudaStream_t gpustream[MAX_GPUS] = { 0 };
extern uint16_t opt_api_listen;
// CUDA Devices on the System
int cuda_num_devices()
{
int version;
cudaError_t err = cudaDriverGetVersion(&version);
if (err != cudaSuccess)
{
applog(LOG_ERR, "Unable to query CUDA driver version! Is an Nvidia driver installed?");
exit(1);
}
if (version < CUDART_VERSION)
{
applog(LOG_ERR, "Driver does not support CUDA %d.%d API! Update your Nvidia driver!", CUDART_VERSION / 1000, (CUDART_VERSION % 1000) / 10);
exit(1);
}
int GPU_N;
err = cudaGetDeviceCount(&GPU_N);
if (err != cudaSuccess)
{
applog(LOG_ERR, "Unable to query number of CUDA devices! Error: %s", cudaGetErrorString(err));
exit(1);
}
if (GPU_N == 0)
{
applog(LOG_ERR, "No CUDA capable device found!", err);
exit(1);
}
return GPU_N;
}
int cuda_version()
{
return (int)CUDART_VERSION;
}
void cuda_devicenames()
{
cudaError_t err;
for(int i = 0; i < opt_n_threads; i++)
{
char vendorname[32] = {0};
int dev_id = device_map[i];
cudaDeviceProp props;
err = cudaGetDeviceProperties(&props, dev_id);
if(err != cudaSuccess)
{
applog(LOG_ERR, "%s", cudaGetErrorString(err));
exit(1);
}
#ifdef USE_WRAPNVML
if(gpu_vendor((uint8_t)props.pciBusID, vendorname) > 0 && strlen(vendorname))
{
device_name[dev_id] = (char*)calloc(1, strlen(vendorname) + strlen(props.name) + 2);
if(device_name[dev_id] == NULL)
{
applog(LOG_ERR, "Out of memory!");
proper_exit(EXIT_FAILURE);
}
if(!strncmp(props.name, "GeForce ", 8))
sprintf(device_name[dev_id], "%s %s", vendorname, &props.name[8]);
else
sprintf(device_name[dev_id], "%s %s", vendorname, props.name);
}
#endif
}
}
void cuda_get_device_sm()
{
cudaDeviceProp props;
cudaError_t err;
int dev_id;
for(int i = 0; i < opt_n_threads; i++)
{
dev_id = device_map[i];
err = cudaSetDevice(device_map[i]);
if(err != cudaSuccess)
{
applog(LOG_ERR, "%s", cudaGetErrorString(err));
exit(1);
}
err = cudaGetDeviceProperties(&props, dev_id);
if(err != cudaSuccess)
{
applog(LOG_ERR, "%s", cudaGetErrorString(err));
exit(1);
}
device_sm[dev_id] = (props.major * 100 + props.minor * 10);
}
}
void cuda_print_devices()
{
cudaError_t err;
int ngpus = cuda_num_devices();
for(int n = 0; n < min(ngpus, MAX_GPUS); n++)
{
int m = device_map[n];
cudaDeviceProp props;
err = cudaGetDeviceProperties(&props, m);
if(err != cudaSuccess)
{
applog(LOG_ERR, "%s", cudaGetErrorString(err));
exit(1);
}
fprintf(stdout, "GPU #%d: SM %d.%d %s\n", m, props.major, props.minor, device_name[n]);
}
}
// Can't be called directly in cpu-miner.c
void cuda_devicereset()
{
for (int i = 0; i < active_gpus; i++)
{
cudaSetDevice(device_map[i]);
cudaDeviceSynchronize();
cudaDeviceReset();
}
}
static bool substringsearch(const char *haystack, const char *needle, int &match)
{
int hlen = (int) strlen(haystack);
int nlen = (int) strlen(needle);
for (int i=0; i < hlen; ++i)
{
if (haystack[i] == ' ') continue;
int j=0, x = 0;
while(j < nlen)
{
if (haystack[i+x] == ' ') {++x; continue;}
if (needle[j] == ' ') {++j; continue;}
if (needle[j] == '#') return ++match == needle[j+1]-'0';
if (tolower(haystack[i+x]) != tolower(needle[j])) break;
++j; ++x;
}
if (j == nlen) return true;
}
return false;
}
// CUDA Gerät nach Namen finden (gibt Geräte-Index zurück oder -1)
int cuda_finddevice(char *name)
{
int num = cuda_num_devices();
int match = 0;
for (int i=0; i < num; ++i)
{
cudaDeviceProp props;
if (cudaGetDeviceProperties(&props, i) == cudaSuccess)
if (substringsearch(props.name, name, match)) return i;
}
return -1;
}
uint32_t device_intensity(int thr_id, const char *func, uint32_t defcount)
{
uint32_t throughput = gpus_intensity[thr_id] ? gpus_intensity[thr_id] : defcount;
api_set_throughput(thr_id, throughput);
return throughput;
}
// Zeitsynchronisations-Routine von cudaminer mit CPU sleep
typedef struct { double value[8]; } tsumarray;
cudaError_t MyStreamSynchronize(cudaStream_t stream, int situation, int thr_id)
{
cudaError_t result = cudaSuccess;
if (situation >= 0)
{
static std::map<int, tsumarray> tsum;
double tsync = 0.0;
double tsleep = 0.95;
double a = 0.95, b = 0.05;
if (tsum.find(situation) == tsum.end()) { a = 0.5; b = 0.5; } // faster initial convergence
tsleep = 0.95*tsum[situation].value[thr_id];
if (cudaStreamQuery(stream) == cudaErrorNotReady)
{
usleep((useconds_t)(1e6*tsleep));
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
result = cudaStreamSynchronize(stream);
gettimeofday(&tv_end, NULL);
tsync = 1e-6 * (tv_end.tv_usec - tv_start.tv_usec) + (tv_end.tv_sec - tv_start.tv_sec);
}
if (tsync >= 0) tsum[situation].value[thr_id] = a * tsum[situation].value[thr_id] + b * (tsleep + tsync);
}
else
result = cudaStreamSynchronize(stream);
return result;
}
int cuda_gpu_clocks(struct cgpu_info *gpu)
{
cudaDeviceProp props;
if (cudaGetDeviceProperties(&props, gpu->gpu_id) == cudaSuccess) {
gpu->gpu_clock = props.clockRate;
gpu->gpu_memclock = props.memoryClockRate;
gpu->gpu_mem = props.totalGlobalMem;
return 0;
}
return -1;
}
void cudaReportHardwareFailure(int thr_id, cudaError_t err, const char* func)
{
struct cgpu_info *gpu = &thr_info[thr_id].gpu;
gpu->hw_errors++;
applog(LOG_ERR, "GPU #%d: %s %s", device_map[thr_id], func, cudaGetErrorString(err));
sleep(1);
}
int cuda_gpu_info(struct cgpu_info *gpu)
{
cudaDeviceProp props;
if(cudaGetDeviceProperties(&props, gpu->gpu_id) == cudaSuccess)
{
gpu->gpu_clock = (uint32_t)props.clockRate;
gpu->gpu_memclock = (uint32_t)props.memoryClockRate;
gpu->gpu_mem = (uint64_t)(props.totalGlobalMem / 1024); // kB
#if defined(_WIN32) && defined(USE_WRAPNVML)
// required to get mem size > 4GB (size_t too small for bytes on 32bit)
nvapiMemGetInfo(gpu->gpu_id, &gpu->gpu_memfree, &gpu->gpu_mem); // kB
#endif
gpu->gpu_mem = gpu->gpu_mem / 1024; // MB
return 0;
}
return -1;
}
double throughput2intensity(uint32_t throughput)
{
double intensity = 0.;
uint32_t ws = throughput;
uint8_t i = 0;
while(ws > 1 && i++ < 32)
ws = ws >> 1;
intensity = (double)i;
if(i && ((1U << i) < throughput))
{
intensity += ((double)(throughput - (1U << i)) / (1U << i));
}
return intensity;
}
void cuda_reset_device(int thr_id, bool *init)
{
int dev_id = device_map[thr_id];
cudaSetDevice(dev_id);
cudaDeviceReset();
cudaDeviceSynchronize();
}