Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit 109ed8e

Browse files
authoredAug 28, 2017
CUDA Class Cleanup (#255)
* Moved CUDA related stuff to libethash-cuda * Optimized logging of cuda miner * Renamed EthashCUDAMiner to CUDAMiner to be on-par with CLMiner
1 parent 1a81f3b commit 109ed8e

File tree

6 files changed

+58
-87
lines changed

6 files changed

+58
-87
lines changed
 

‎ethminer/MinerAux.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
#include <libethcore/Exceptions.h>
3737
#include <libdevcore/SHA3.h>
3838
#include <libethcore/EthashAux.h>
39-
#include <libethcore/EthashCUDAMiner.h>
4039
#include <libethcore/Farm.h>
4140
#if ETH_ETHASHCL
4241
#include <libethash-cl/CLMiner.h>
4342
#endif
4443
#if ETH_ETHASHCUDA
45-
#include <libethash-cuda/ethash_cuda_miner.h>
44+
#include <libethash-cuda/CUDAMiner.h>
4645
#endif
4746
#include <jsonrpccpp/client/connectors/httpclient.h>
4847
#include "FarmClient.h"
@@ -460,7 +459,7 @@ class MinerCLI
460459
#endif
461460
#if ETH_ETHASHCUDA
462461
if (m_minerType == MinerType::CUDA || m_minerType == MinerType::Mixed)
463-
EthashCUDAMiner::listDevices();
462+
CUDAMiner::listDevices();
464463
#endif
465464
exit(0);
466465
}
@@ -494,12 +493,12 @@ class MinerCLI
494493
#if ETH_ETHASHCUDA
495494
if (m_cudaDeviceCount > 0)
496495
{
497-
EthashCUDAMiner::setDevices(m_cudaDevices, m_cudaDeviceCount);
496+
CUDAMiner::setDevices(m_cudaDevices, m_cudaDeviceCount);
498497
m_miningThreads = m_cudaDeviceCount;
499498
}
500499

501-
EthashCUDAMiner::setNumInstances(m_miningThreads);
502-
if (!EthashCUDAMiner::configureGPU(
500+
CUDAMiner::setNumInstances(m_miningThreads);
501+
if (!CUDAMiner::configureGPU(
503502
m_localWorkSize,
504503
m_globalWorkSizeMultiplier,
505504
m_numStreams,
@@ -510,7 +509,7 @@ class MinerCLI
510509
))
511510
exit(1);
512511

513-
EthashCUDAMiner::setParallelHash(m_parallelHash);
512+
CUDAMiner::setParallelHash(m_parallelHash);
514513
#else
515514
cerr << "CUDA support disabled. Configure project build with -DETHASHCUDA=ON" << endl;
516515
exit(1);
@@ -605,7 +604,7 @@ class MinerCLI
605604
sealers["opencl"] = Farm::SealerDescriptor{&CLMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CLMiner(_farm, _index); }};
606605
#endif
607606
#if ETH_ETHASHCUDA
608-
sealers["cuda"] = Farm::SealerDescriptor{ &EthashCUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new EthashCUDAMiner(_farm, _index); } };
607+
sealers["cuda"] = Farm::SealerDescriptor{ &CUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CUDAMiner(_farm, _index); } };
609608
#endif
610609
f.setSealers(sealers);
611610
f.onSolutionFound([&](Solution) { return false; });
@@ -669,7 +668,7 @@ class MinerCLI
669668
sealers["opencl"] = Farm::SealerDescriptor{ &CLMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CLMiner(_farm, _index); } };
670669
#endif
671670
#if ETH_ETHASHCUDA
672-
sealers["cuda"] = Farm::SealerDescriptor{ &EthashCUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new EthashCUDAMiner(_farm, _index); } };
671+
sealers["cuda"] = Farm::SealerDescriptor{ &CUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CUDAMiner(_farm, _index); } };
673672
#endif
674673
f.setSealers(sealers);
675674

@@ -742,7 +741,7 @@ class MinerCLI
742741
sealers["opencl"] = Farm::SealerDescriptor{&CLMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CLMiner(_farm, _index); }};
743742
#endif
744743
#if ETH_ETHASHCUDA
745-
sealers["cuda"] = Farm::SealerDescriptor{ &EthashCUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new EthashCUDAMiner(_farm, _index); } };
744+
sealers["cuda"] = Farm::SealerDescriptor{ &CUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CUDAMiner(_farm, _index); } };
746745
#endif
747746
(void)_m;
748747
(void)_remote;
@@ -877,7 +876,7 @@ class MinerCLI
877876
sealers["opencl"] = Farm::SealerDescriptor{ &CLMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CLMiner(_farm, _index); } };
878877
#endif
879878
#if ETH_ETHASHCUDA
880-
sealers["cuda"] = Farm::SealerDescriptor{ &EthashCUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new EthashCUDAMiner(_farm, _index); } };
879+
sealers["cuda"] = Farm::SealerDescriptor{ &CUDAMiner::instances, [](FarmFace& _farm, unsigned _index){ return new CUDAMiner(_farm, _index); } };
881880
#endif
882881
if (!m_farmRecheckSet)
883882
m_farmRecheckPeriod = m_defaultStratumFarmRecheckPeriod;

‎libethash-cuda/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ file(GLOB sources "*.cpp" "*.cu")
2525
file(GLOB headers "*.h" "*.cuh")
2626

2727
cuda_add_library(ethash-cuda STATIC ${sources} ${headers})
28-
target_link_libraries(ethash-cuda ethash)
28+
target_link_libraries(ethash-cuda ethash devcore)
2929
target_include_directories(ethash-cuda PUBLIC ${CUDA_INCLUDE_DIRS})
3030
target_include_directories(ethash-cuda PRIVATE .. ${CMAKE_CURRENT_BINARY_DIR})
3131

‎libethcore/EthashCUDAMiner.cpp ‎libethash-cuda/CUDAMiner.cpp

+19-25
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ GNU General Public License for more details.
1414
You should have received a copy of the GNU General Public License
1515
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
/** @file EthashCUDAMiner.cpp
17+
/** @file CUDAMiner.cpp
1818
* @author Gav Wood <i@gavwood.com>
1919
* @date 2014
2020
*
2121
* Determines the PoW algorithm.
2222
*/
2323

24-
#if ETH_ETHASHCUDA
25-
26-
#include "EthashCUDAMiner.h"
27-
#include <libethash-cuda/ethash_cuda_miner.h>
24+
#include "CUDAMiner.h"
2825

2926
using namespace std;
3027
using namespace dev;
@@ -37,7 +34,7 @@ namespace eth
3734
class EthashCUDAHook : public ethash_cuda_miner::search_hook
3835
{
3936
public:
40-
EthashCUDAHook(EthashCUDAMiner& _owner): m_owner(_owner) {}
37+
EthashCUDAHook(CUDAMiner& _owner): m_owner(_owner) {}
4138

4239
EthashCUDAHook(EthashCUDAHook const&) = delete;
4340

@@ -83,29 +80,28 @@ namespace eth
8380
Mutex x_all;
8481
bool m_abort = false;
8582
Notified<bool> m_aborted = { true };
86-
EthashCUDAMiner& m_owner;
83+
CUDAMiner& m_owner;
8784
};
8885
}
8986
}
87+
unsigned CUDAMiner::s_platformId = 0;
88+
unsigned CUDAMiner::s_deviceId = 0;
89+
unsigned CUDAMiner::s_numInstances = 0;
90+
int CUDAMiner::s_devices[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
9091

91-
unsigned EthashCUDAMiner::s_platformId = 0;
92-
unsigned EthashCUDAMiner::s_deviceId = 0;
93-
unsigned EthashCUDAMiner::s_numInstances = 0;
94-
int EthashCUDAMiner::s_devices[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
95-
96-
EthashCUDAMiner::EthashCUDAMiner(FarmFace& _farm, unsigned _index) :
92+
CUDAMiner::CUDAMiner(FarmFace& _farm, unsigned _index) :
9793
Miner("CUDA", _farm, _index),
9894
m_hook(new EthashCUDAHook(*this)) // FIXME!
9995
{}
10096

101-
EthashCUDAMiner::~EthashCUDAMiner()
97+
CUDAMiner::~CUDAMiner()
10298
{
10399
pause();
104100
delete m_miner;
105101
delete m_hook;
106102
}
107103

108-
void EthashCUDAMiner::report(uint64_t _nonce)
104+
void CUDAMiner::report(uint64_t _nonce)
109105
{
110106
// FIXME: This code is exactly the same as in EthashGPUMiner.
111107
WorkPackage w = work(); // Copy work package to avoid repeated mutex lock.
@@ -114,13 +110,13 @@ void EthashCUDAMiner::report(uint64_t _nonce)
114110
farm.submitProof(Solution{_nonce, r.mixHash, w.header, w.seed, w.boundary});
115111
}
116112

117-
void EthashCUDAMiner::kickOff()
113+
void CUDAMiner::kickOff()
118114
{
119115
m_hook->reset();
120116
startWorking();
121117
}
122118

123-
void EthashCUDAMiner::workLoop()
119+
void CUDAMiner::workLoop()
124120
{
125121
// take local copy of work since it may end up being overwritten by kickOff/pause.
126122
try {
@@ -196,28 +192,28 @@ void EthashCUDAMiner::workLoop()
196192
}
197193
}
198194

199-
void EthashCUDAMiner::pause()
195+
void CUDAMiner::pause()
200196
{
201197
m_hook->abort();
202198
stopWorking();
203199
}
204200

205-
std::string EthashCUDAMiner::platformInfo()
201+
std::string CUDAMiner::platformInfo()
206202
{
207203
return ethash_cuda_miner::platform_info(s_deviceId);
208204
}
209205

210-
unsigned EthashCUDAMiner::getNumDevices()
206+
unsigned CUDAMiner::getNumDevices()
211207
{
212208
return ethash_cuda_miner::getNumDevices();
213209
}
214210

215-
void EthashCUDAMiner::listDevices()
211+
void CUDAMiner::listDevices()
216212
{
217213
return ethash_cuda_miner::listDevices();
218214
}
219215

220-
bool EthashCUDAMiner::configureGPU(
216+
bool CUDAMiner::configureGPU(
221217
unsigned _blockSize,
222218
unsigned _gridSize,
223219
unsigned _numStreams,
@@ -246,9 +242,7 @@ bool EthashCUDAMiner::configureGPU(
246242
return true;
247243
}
248244

249-
void EthashCUDAMiner::setParallelHash(unsigned _parallelHash)
245+
void CUDAMiner::setParallelHash(unsigned _parallelHash)
250246
{
251247
ethash_cuda_miner::setParallelHash(_parallelHash);
252248
}
253-
254-
#endif

‎libethcore/EthashCUDAMiner.h ‎libethash-cuda/CUDAMiner.h

+8-13
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,32 @@ GNU General Public License for more details.
1414
You should have received a copy of the GNU General Public License
1515
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
/** @file EthashCUDAMiner.h
17+
/** @file CUDAMiner.h
1818
* @author Gav Wood <i@gavwood.com>
1919
* @date 2014
2020
*
2121
* Determines the PoW algorithm.
2222
*/
23-
2423
#pragma once
25-
#if ETH_ETHASHCUDA
26-
27-
#include "libdevcore/Worker.h"
28-
#include "EthashAux.h"
29-
#include "Miner.h"
3024

31-
class ethash_cuda_miner;
25+
#include <libdevcore/Worker.h>
26+
#include <libethcore/EthashAux.h>
27+
#include <libethcore/Miner.h>
28+
#include "ethash_cuda_miner.h"
3229

3330
namespace dev
3431
{
3532
namespace eth
3633
{
3734
class EthashCUDAHook;
3835

39-
class EthashCUDAMiner: public Miner
36+
class CUDAMiner: public Miner
4037
{
4138
friend class dev::eth::EthashCUDAHook;
4239

4340
public:
44-
EthashCUDAMiner(FarmFace& _farm, unsigned _index);
45-
~EthashCUDAMiner();
41+
CUDAMiner(FarmFace& _farm, unsigned _index);
42+
~CUDAMiner();
4643

4744
static unsigned instances()
4845
{
@@ -92,5 +89,3 @@ class EthashCUDAHook;
9289
};
9390
}
9491
}
95-
96-
#endif

‎libethash-cuda/ethash_cuda_miner.cpp

+20-35
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,33 @@
3333
#include <thread>
3434
#include <libethash/ethash.h>
3535
#include <libethash/internal.h>
36+
#include <libdevcore/Log.h>
3637
#include <cuda_runtime.h>
3738
#include "ethash_cuda_miner.h"
3839
#include "ethash_cuda_miner_kernel_globals.h"
3940

40-
4141
// workaround lame platforms
4242

4343
#undef min
4444
#undef max
4545

4646
using namespace std;
47+
using namespace dev;
48+
49+
struct CUDAChannel: public LogChannel
50+
{
51+
static const char* name() { return EthOrange " cu"; }
52+
static const int verbosity = 2;
53+
static const bool debug = false;
54+
};
55+
#define cudalog clog(CUDAChannel)
56+
#define ETHCUDA_LOG(_contents) cudalog << _contents
57+
4758

4859
unsigned const ethash_cuda_miner::c_defaultBlockSize = 128;
4960
unsigned const ethash_cuda_miner::c_defaultGridSize = 8192; // * CL_DEFAULT_LOCAL_WORK_SIZE
5061
unsigned const ethash_cuda_miner::c_defaultNumStreams = 2;
5162

52-
#if defined(_WIN32)
53-
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* lpOutputString);
54-
static std::atomic_flag s_logSpin = ATOMIC_FLAG_INIT;
55-
#define ETHCUDA_LOG(_contents) \
56-
do \
57-
{ \
58-
std::stringstream ss; \
59-
ss << _contents; \
60-
while (s_logSpin.test_and_set(std::memory_order_acquire)) {} \
61-
OutputDebugStringA(ss.str().c_str()); \
62-
cout << ss.str() << endl << flush; \
63-
s_logSpin.clear(std::memory_order_release); \
64-
} while (false)
65-
#else
66-
#define ETHCUDA_LOG(_contents) cout << "[CUDA]:" << _contents << endl
67-
#endif
68-
6963
ethash_cuda_miner::search_hook::~search_hook() {}
7064

7165
ethash_cuda_miner::ethash_cuda_miner()
@@ -136,9 +130,7 @@ bool ethash_cuda_miner::configureGPU(
136130
s_numStreams = _numStreams;
137131
s_scheduleFlag = _scheduleFlag;
138132

139-
ETHCUDA_LOG(
140-
"Using grid size " << s_gridSize << ", block size " << s_blockSize << endl
141-
);
133+
cudalog << "Using grid size " << s_gridSize << ", block size " << s_blockSize;
142134

143135
// by default let's only consider the DAG of the first epoch
144136
uint64_t dagSize = ethash_get_datasize(_currentBlock);
@@ -153,18 +145,11 @@ bool ethash_cuda_miner::configureGPU(
153145
CUDA_SAFE_CALL(cudaGetDeviceProperties(&props, deviceId));
154146
if (props.totalGlobalMem >= dagSize)
155147
{
156-
ETHCUDA_LOG(
157-
"Found suitable CUDA device [" << string(props.name)
158-
<< "] with " << props.totalGlobalMem << " bytes of GPU memory"
159-
);
148+
cudalog << "Found suitable CUDA device [" << string(props.name) << "] with " << props.totalGlobalMem << " bytes of GPU memory";
160149
}
161150
else
162151
{
163-
ETHCUDA_LOG(
164-
"CUDA device " << string(props.name)
165-
<< " has insufficient GPU memory." << props.totalGlobalMem <<
166-
" bytes of memory found < " << dagSize << " bytes of memory required"
167-
);
152+
cudalog << "CUDA device " << string(props.name) << " has insufficient GPU memory." << props.totalGlobalMem << " bytes of memory found < " << dagSize << " bytes of memory required";
168153
return false;
169154
}
170155
}
@@ -207,7 +192,7 @@ void ethash_cuda_miner::listDevices()
207192
}
208193
catch(std::runtime_error const& err)
209194
{
210-
std::cerr << "CUDA error: " << err.what() << '\n';
195+
cwarn << "CUDA error: " << err.what();
211196
}
212197
}
213198

@@ -231,7 +216,7 @@ bool ethash_cuda_miner::init(ethash_light_t _light, uint8_t const* _lightData, u
231216
cudaDeviceProp device_props;
232217
CUDA_SAFE_CALL(cudaGetDeviceProperties(&device_props, device_num));
233218

234-
cout << "Using device: " << device_props.name << " (Compute " << device_props.major << "." << device_props.minor << ")" << endl;
219+
cudalog << "Using device: " << device_props.name << " (Compute " << device_props.major << "." << device_props.minor << ")";
235220

236221
CUDA_SAFE_CALL(cudaSetDevice(device_num));
237222
CUDA_SAFE_CALL(cudaDeviceReset());
@@ -275,21 +260,21 @@ bool ethash_cuda_miner::init(ethash_light_t _light, uint8_t const* _lightData, u
275260

276261
if (!*hostDAG)
277262
{
278-
cout << "Generating DAG for GPU #" << device_num << endl;
263+
cudalog << "Generating DAG for GPU #" << device_num;
279264
ethash_generate_dag(dagSize, s_gridSize, s_blockSize, m_streams[0], device_num);
280265

281266
if (_cpyToHost)
282267
{
283268
uint8_t* memoryDAG = new uint8_t[dagSize];
284-
cout << "Copying DAG from GPU #" << device_num << " to host" << endl;
269+
cudalog << "Copying DAG from GPU #" << device_num << " to host";
285270
CUDA_SAFE_CALL(cudaMemcpy(reinterpret_cast<void*>(memoryDAG), dag, dagSize, cudaMemcpyDeviceToHost));
286271

287272
*hostDAG = (void*)memoryDAG;
288273
}
289274
}
290275
else
291276
{
292-
cout << "Copying DAG from host to GPU #" << device_num << endl;
277+
cudalog << "Copying DAG from host to GPU #" << device_num;
293278
const void* hdag = (const void*)(*hostDAG);
294279
CUDA_SAFE_CALL(cudaMemcpy(reinterpret_cast<void*>(dag), hdag, dagSize, cudaMemcpyHostToDevice));
295280
}

1 commit comments

Comments
 (1)

Himan2001 commented on Aug 29, 2017

@Himan2001

Looks like, something is missing, please look here:

#268

This repository has been archived.