This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CUDAMiner.cpp
443 lines (377 loc) · 15.4 KB
/
CUDAMiner.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
This file is part of ethminer.
ethminer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethminer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ethminer. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libethcore/Farm.h>
#include <ethash/ethash.hpp>
#include "CUDAMiner.h"
using namespace std;
using namespace dev;
using namespace eth;
struct CUDAChannel : public LogChannel
{
static const char* name() { return EthOrange "cu"; }
static const int verbosity = 2;
};
#define cudalog clog(CUDAChannel)
CUDAMiner::CUDAMiner(unsigned _index, CUSettings _settings, DeviceDescriptor& _device)
: Miner("cuda-", _index),
m_settings(_settings),
m_batch_size(_settings.gridSize * _settings.blockSize),
m_streams_batch_size(_settings.gridSize * _settings.blockSize * _settings.streams)
{
m_deviceDescriptor = _device;
}
CUDAMiner::~CUDAMiner()
{
DEV_BUILD_LOG_PROGRAMFLOW(cudalog, "cuda-" << m_index << " CUDAMiner::~CUDAMiner() begin");
stopWorking();
kick_miner();
DEV_BUILD_LOG_PROGRAMFLOW(cudalog, "cuda-" << m_index << " CUDAMiner::~CUDAMiner() end");
}
bool CUDAMiner::initDevice()
{
cudalog << "Using Pci Id : " << m_deviceDescriptor.uniqueId << " " << m_deviceDescriptor.cuName
<< " (Compute " + m_deviceDescriptor.cuCompute + ") Memory : "
<< dev::getFormattedMemory((double)m_deviceDescriptor.totalMemory);
// Set Hardware Monitor Info
m_hwmoninfo.deviceType = HwMonitorInfoType::NVIDIA;
m_hwmoninfo.devicePciId = m_deviceDescriptor.uniqueId;
m_hwmoninfo.deviceIndex = -1; // Will be later on mapped by nvml (see Farm() constructor)
try
{
CUDA_SAFE_CALL(cudaSetDevice(m_deviceDescriptor.cuDeviceIndex));
CUDA_SAFE_CALL(cudaDeviceReset());
}
catch (const cuda_runtime_error& ec)
{
cudalog << "Could not set CUDA device on Pci Id " << m_deviceDescriptor.uniqueId
<< " Error : " << ec.what();
cudalog << "Mining aborted on this device.";
return false;
}
return true;
}
bool CUDAMiner::initEpoch_internal()
{
// If we get here it means epoch has changed so it's not necessary
// to check again dag sizes. They're changed for sure
bool retVar = false;
m_current_target = 0;
auto startInit = std::chrono::steady_clock::now();
size_t RequiredTotalMemory = (m_epochContext.dagSize + m_epochContext.lightSize);
size_t RequiredDagMemory = m_epochContext.dagSize;
// Release the pause flag if any
resume(MinerPauseEnum::PauseDueToInsufficientMemory);
resume(MinerPauseEnum::PauseDueToInitEpochError);
bool lightOnHost = false;
try
{
hash128_t* dag;
hash64_t* light;
// If we have already enough memory allocated, we just have to
// copy light_cache and regenerate the DAG
if (m_allocated_memory_dag < m_epochContext.dagSize ||
m_allocated_memory_light_cache < m_epochContext.lightSize)
{
// We need to reset the device and (re)create the dag
// cudaDeviceReset() frees all previous allocated memory
CUDA_SAFE_CALL(cudaDeviceReset());
CUDA_SAFE_CALL(cudaSetDeviceFlags(m_settings.schedule));
CUDA_SAFE_CALL(cudaDeviceSetCacheConfig(cudaFuncCachePreferL1));
// Check whether the current device has sufficient memory every time we recreate the dag
if (m_deviceDescriptor.totalMemory < RequiredTotalMemory)
{
if (m_deviceDescriptor.totalMemory < RequiredDagMemory)
{
cudalog << "Epoch " << m_epochContext.epochNumber << " requires "
<< dev::getFormattedMemory((double)RequiredDagMemory) << " memory.";
cudalog << "This device hasn't enough memory available. Mining suspended ...";
pause(MinerPauseEnum::PauseDueToInsufficientMemory);
return true; // This will prevent to exit the thread and
// Eventually resume mining when changing coin or epoch (NiceHash)
}
else
lightOnHost = true;
}
cudalog << "Generating DAG + Light(on " << (lightOnHost ? "host" : "GPU")
<< ") : " << dev::getFormattedMemory((double)RequiredTotalMemory);
// create buffer for cache
if (lightOnHost)
{
CUDA_SAFE_CALL(cudaHostAlloc(reinterpret_cast<void**>(&light),
m_epochContext.lightSize, cudaHostAllocDefault));
cudalog << "WARNING: Generating DAG will take minutes, not seconds";
}
else
CUDA_SAFE_CALL(
cudaMalloc(reinterpret_cast<void**>(&light), m_epochContext.lightSize));
m_allocated_memory_light_cache = m_epochContext.lightSize;
CUDA_SAFE_CALL(cudaMalloc(reinterpret_cast<void**>(&dag), m_epochContext.dagSize));
m_allocated_memory_dag = m_epochContext.dagSize;
// create mining buffers
for (unsigned i = 0; i != m_settings.streams; ++i)
{
CUDA_SAFE_CALL(cudaMallocHost(&m_search_buf[i], sizeof(Search_results)));
CUDA_SAFE_CALL(cudaStreamCreateWithFlags(&m_streams[i], cudaStreamNonBlocking));
}
}
else
{
cudalog << "Generating DAG + Light (reusing buffers): "
<< dev::getFormattedMemory((double)RequiredTotalMemory);
get_constants(&dag, NULL, &light, NULL);
}
CUDA_SAFE_CALL(cudaMemcpy(reinterpret_cast<void*>(light), m_epochContext.lightCache,
m_epochContext.lightSize, cudaMemcpyHostToDevice));
set_constants(dag, m_epochContext.dagNumItems, light,
m_epochContext.lightNumItems); // in ethash_cuda_miner_kernel.cu
ethash_generate_dag(
m_epochContext.dagSize, m_settings.gridSize, m_settings.blockSize, m_streams[0]);
cudalog << "Generated DAG + Light in "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - startInit)
.count()
<< " ms. "
<< dev::getFormattedMemory(
lightOnHost ? (double)(m_deviceDescriptor.totalMemory - RequiredDagMemory) :
(double)(m_deviceDescriptor.totalMemory - RequiredTotalMemory))
<< " left.";
retVar = true;
}
catch (const cuda_runtime_error& ec)
{
cudalog << "Unexpected error " << ec.what() << " on CUDA device "
<< m_deviceDescriptor.uniqueId;
cudalog << "Mining suspended ...";
pause(MinerPauseEnum::PauseDueToInitEpochError);
retVar = true;
}
return retVar;
}
void CUDAMiner::workLoop()
{
WorkPackage current;
current.header = h256();
m_search_buf.resize(m_settings.streams);
m_streams.resize(m_settings.streams);
if (!initDevice())
return;
try
{
while (!shouldStop())
{
// Wait for work or 3 seconds (whichever the first)
const WorkPackage w = work();
if (!w)
{
boost::system_time const timeout =
boost::get_system_time() + boost::posix_time::seconds(3);
boost::mutex::scoped_lock l(x_work);
m_new_work_signal.timed_wait(l, timeout);
continue;
}
// Epoch change ?
if (current.epoch != w.epoch)
{
if (!initEpoch())
break; // This will simply exit the thread
// As DAG generation takes a while we need to
// ensure we're on latest job, not on the one
// which triggered the epoch change
current = w;
continue;
}
// Persist most recent job.
// Job's differences should be handled at higher level
current = w;
uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)current.boundary >> 192);
// Eventually start searching
search(current.header.data(), upper64OfBoundary, current.startNonce, w);
}
// Reset miner and stop working
CUDA_SAFE_CALL(cudaDeviceReset());
}
catch (cuda_runtime_error const& _e)
{
string _what = "GPU error: ";
_what.append(_e.what());
throw std::runtime_error(_what);
}
}
void CUDAMiner::kick_miner()
{
m_new_work.store(true, std::memory_order_relaxed);
m_new_work_signal.notify_one();
}
int CUDAMiner::getNumDevices()
{
int deviceCount;
cudaError_t err = cudaGetDeviceCount(&deviceCount);
if (err == cudaSuccess)
return deviceCount;
if (err == cudaErrorInsufficientDriver)
{
int driverVersion = 0;
cudaDriverGetVersion(&driverVersion);
if (driverVersion == 0)
std::cerr << "CUDA Error : No CUDA driver found" << std::endl;
else
std::cerr << "CUDA Error : Insufficient CUDA driver " << std::to_string(driverVersion)
<< std::endl;
}
else
{
std::cerr << "CUDA Error : " << cudaGetErrorString(err) << std::endl;
}
return 0;
}
void CUDAMiner::enumDevices(std::map<string, DeviceDescriptor>& _DevicesCollection)
{
int numDevices = getNumDevices();
for (int i = 0; i < numDevices; i++)
{
string uniqueId;
ostringstream s;
DeviceDescriptor deviceDescriptor;
cudaDeviceProp props;
try
{
size_t freeMem, totalMem;
CUDA_SAFE_CALL(cudaGetDeviceProperties(&props, i));
CUDA_SAFE_CALL(cudaMemGetInfo(&freeMem, &totalMem));
s << setw(2) << setfill('0') << hex << props.pciBusID << ":" << setw(2)
<< props.pciDeviceID << ".0";
uniqueId = s.str();
if (_DevicesCollection.find(uniqueId) != _DevicesCollection.end())
deviceDescriptor = _DevicesCollection[uniqueId];
else
deviceDescriptor = DeviceDescriptor();
deviceDescriptor.name = string(props.name);
deviceDescriptor.cuDetected = true;
deviceDescriptor.uniqueId = uniqueId;
deviceDescriptor.type = DeviceTypeEnum::Gpu;
deviceDescriptor.cuDeviceIndex = i;
deviceDescriptor.cuDeviceOrdinal = i;
deviceDescriptor.cuName = string(props.name);
deviceDescriptor.totalMemory = freeMem;
deviceDescriptor.cuCompute =
(to_string(props.major) + "." + to_string(props.minor));
deviceDescriptor.cuComputeMajor = props.major;
deviceDescriptor.cuComputeMinor = props.minor;
_DevicesCollection[uniqueId] = deviceDescriptor;
}
catch (const cuda_runtime_error& _e)
{
std::cerr << _e.what() << std::endl;
}
}
}
void CUDAMiner::search(
uint8_t const* header, uint64_t target, uint64_t start_nonce, const dev::eth::WorkPackage& w)
{
set_header(*reinterpret_cast<hash32_t const*>(header));
if (m_current_target != target)
{
set_target(target);
m_current_target = target;
}
// prime each stream, clear search result buffers and start the search
uint32_t current_index;
for (current_index = 0; current_index < m_settings.streams;
current_index++, start_nonce += m_batch_size)
{
cudaStream_t stream = m_streams[current_index];
volatile Search_results& buffer(*m_search_buf[current_index]);
buffer.count = 0;
// Run the batch for this stream
run_ethash_search(m_settings.gridSize, m_settings.blockSize, stream, &buffer, start_nonce);
}
// process stream batches until we get new work.
bool done = false;
while (!done)
{
// Exit next time around if there's new work awaiting
bool t = true;
done = m_new_work.compare_exchange_strong(t, false);
// Check on every batch if we need to suspend mining
if (!done)
done = paused();
// This inner loop will process each cuda stream individually
for (current_index = 0; current_index < m_settings.streams;
current_index++, start_nonce += m_batch_size)
{
// Each pass of this loop will wait for a stream to exit,
// save any found solutions, then restart the stream
// on the next group of nonces.
cudaStream_t stream = m_streams[current_index];
// Wait for the stream complete
CUDA_SAFE_CALL(cudaStreamSynchronize(stream));
if (shouldStop())
{
m_new_work.store(false, std::memory_order_relaxed);
done = true;
}
// Detect solutions in current stream's solution buffer
volatile Search_results& buffer(*m_search_buf[current_index]);
uint32_t found_count = std::min((unsigned)buffer.count, MAX_SEARCH_RESULTS);
uint32_t gids[MAX_SEARCH_RESULTS];
h256 mixes[MAX_SEARCH_RESULTS];
if (found_count)
{
buffer.count = 0;
// Extract solution and pass to higer level
// using io_service as dispatcher
for (uint32_t i = 0; i < found_count; i++)
{
gids[i] = buffer.result[i].gid;
memcpy(mixes[i].data(), (void*)&buffer.result[i].mix,
sizeof(buffer.result[i].mix));
}
}
// restart the stream on the next batch of nonces
// unless we are done for this round.
if (!done)
run_ethash_search(
m_settings.gridSize, m_settings.blockSize, stream, &buffer, start_nonce);
if (found_count)
{
uint64_t nonce_base = start_nonce - m_streams_batch_size;
for (uint32_t i = 0; i < found_count; i++)
{
uint64_t nonce = nonce_base + gids[i];
Farm::f().submitProof(
Solution{nonce, mixes[i], w, std::chrono::steady_clock::now(), m_index});
cudalog << EthWhite << "Job: " << w.header.abridged() << " Sol: 0x"
<< toHex(nonce) << EthReset;
}
}
}
// Update the hash rate
updateHashRate(m_batch_size, m_settings.streams);
// Bail out if it's shutdown time
if (shouldStop())
{
m_new_work.store(false, std::memory_order_relaxed);
break;
}
}
#ifdef DEV_BUILD
// Optionally log job switch time
if (!shouldStop() && (g_logOptions & LOG_SWITCH))
cudalog << "Switch time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - m_workSwitchStart)
.count()
<< " ms.";
#endif
}