Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
-Some CUDA errors were not being caught
-Replace 'unsigned long long' with 'uint64_t'
-Comment out self-test, uncomment if required for debugging
-Enforce limit on max 1024 threads per block (This is the limit in CUDA)
  • Loading branch information
brichard19 committed Sep 13, 2018
1 parent 8fec38c commit 2964776
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 50 deletions.
2 changes: 1 addition & 1 deletion KeyFinder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main(int argc, char **argv)

std::vector<std::string> targetList;
secp256k1::uint256 start(1);
unsigned long long range = 0;
uint64_t range = 0;
int deviceCount = 0;

try {
Expand Down
81 changes: 44 additions & 37 deletions KeyFinderLib/KeyFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ void KeyFinder::defaultStatusCallback(KeyFinderStatusInfo status)
// Do nothing
}

KeyFinder::KeyFinder(int device, const secp256k1::uint256 &start, unsigned long long range, int compression, int blocks, int threads, int pointsPerThread)
KeyFinder::KeyFinder(int device, const secp256k1::uint256 &start, uint64_t range, int compression, int blocks, int threads, int pointsPerThread)
{
_devCtx = NULL;
_total = 0;
_statusInterval = 1000;
_device = device;


if(threads > 1024) {
throw KeyFinderException("The maximum number of threads is 1024 per block");
}

if(threads <= 0 || threads % 32 != 0) {
throw KeyFinderException("The number of threads must be a multiple of 32");
}
Expand Down Expand Up @@ -173,6 +177,15 @@ void KeyFinder::setTargetsOnDevice()
}
}

void KeyFinder::cudaCall(cudaError_t err)
{
if(err) {
std::string errStr = cudaGetErrorString(err);

throw KeyFinderException(errStr);
}
}

void KeyFinder::init()
{
DeviceParameters params;
Expand All @@ -186,37 +199,30 @@ void KeyFinder::init()

Logger::log(LogLevel::Info, "Initializing " + _devCtx->getDeviceName());

_devCtx->init();
try {
_devCtx->init();
} catch(DeviceContextException &ex) {
throw KeyFinderException(ex.msg);
}

cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync);
cudaDeviceSetCacheConfig(cudaFuncCachePreferL1);
cudaCall(cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync));

cudaCall(cudaDeviceSetCacheConfig(cudaFuncCachePreferL1));

// Copy points to device
generateStartingPoints();

setTargetsOnDevice();

allocateChainBuf(_numThreads * _numBlocks * _pointsPerThread);
cudaCall(allocateChainBuf(_numThreads * _numBlocks * _pointsPerThread));

// Set the incrementor
secp256k1::ecpoint g = secp256k1::G();
secp256k1::ecpoint p = secp256k1::multiplyPoint(secp256k1::uint256(_numThreads * _numBlocks * _pointsPerThread), g);

cudaError_t err = _resultList.init(sizeof(KeyFinderDeviceResult), 16);

if(err) {
std::string cudaErrorString(cudaGetErrorString(err));

throw KeyFinderException("Error initializing device: " + cudaErrorString);
}
cudaCall(_resultList.init(sizeof(KeyFinderDeviceResult), 16));

err = setIncrementorPoint(p.x, p.y);

if(err) {
std::string cudaErrorString(cudaGetErrorString(err));

throw KeyFinderException("Error initializing device: " + cudaErrorString);
}
cudaCall(setIncrementorPoint(p.x, p.y));
}


Expand All @@ -226,39 +232,40 @@ void KeyFinder::generateStartingPoints()
_startingPoints.clear();
_iterCount = 0;

unsigned long long totalPoints = _pointsPerThread * _numThreads * _numBlocks;
unsigned long long totalMemory = totalPoints * 40;
uint64_t totalPoints = _pointsPerThread * _numThreads * _numBlocks;
uint64_t totalMemory = totalPoints * 40;

Logger::log(LogLevel::Info, "Generating " + util::formatThousands(totalPoints) + " starting points (" + util::format("%.1f", (double)totalMemory / (double)(1024 * 1024)) + "MB)");

// Generate key pairs for k, k+1, k+2 ... k + <total points in parallel - 1>
secp256k1::uint256 privKey = _startExponent;

for(unsigned long long i = 0; i < totalPoints; i++) {
for(uint64_t i = 0; i < totalPoints; i++) {
_exponents.push_back(privKey.add(i));
}

_deviceKeys.init(_numBlocks, _numThreads, _pointsPerThread, _exponents);
cudaCall(_deviceKeys.init(_numBlocks, _numThreads, _pointsPerThread, _exponents));

// Show progress in 10% increments
double pct = 10.0;
for(int i = 1; i <= 256; i++) {
_deviceKeys.doStep();
cudaCall(_deviceKeys.doStep());

if(((double)i / 256.0) * 100.0 >= pct) {
Logger::log(LogLevel::Info, util::format("%.1f%%", pct));
pct += 10.0;
}
}

#ifdef _DEBUG
try {
Logger::log(LogLevel::Debug, "Verifying points on device. This will take a while...");
_deviceKeys.selfTest(_exponents);
} catch(std::string &e) {
Logger::log(LogLevel::Debug, e);
exit(1);
}
#endif
//#ifdef _DEBUG
// try {
// Logger::log(LogLevel::Debug, "Verifying points on device. This will take a while...");
// _deviceKeys.selfTest(_exponents);
// } catch(std::string &e) {
// Logger::log(LogLevel::Debug, e);
// exit(1);
// }
//#endif

Logger::log(LogLevel::Info, "Done");

Expand Down Expand Up @@ -360,15 +367,15 @@ void KeyFinder::getResults(std::vector<KeyFinderResult> &r)

void KeyFinder::run()
{
unsigned int pointsPerIteration = _numBlocks * _numThreads * _pointsPerThread;
uint64_t pointsPerIteration = _numBlocks * _numThreads * _pointsPerThread;

_running = true;

util::Timer timer;

timer.start();

unsigned long long prevIterCount = 0;
uint64_t prevIterCount = 0;

_totalTime = 0;

Expand All @@ -395,7 +402,7 @@ void KeyFinder::run()

KeyFinderStatusInfo info;

unsigned long long count = (_iterCount - prevIterCount) * pointsPerIteration;
uint64_t count = (_iterCount - prevIterCount) * pointsPerIteration;

_total += count;

Expand Down Expand Up @@ -444,7 +451,7 @@ void KeyFinder::run()
secp256k1::uint256 exp = _exponents[index];
secp256k1::ecpoint publicKey = results[i].p;

unsigned long long offset = (unsigned long long)_numBlocks * _numThreads * _pointsPerThread * _iterCount;
uint64_t offset = (uint64_t)_numBlocks * _numThreads * _pointsPerThread * _iterCount;
exp = secp256k1::addModN(exp, secp256k1::uint256(offset));

if(!verifyKey(exp, publicKey, results[i].hash, results[i].compressed)) {
Expand Down
9 changes: 6 additions & 3 deletions KeyFinderLib/KeyFinder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _KEY_FINDER_H
#define _KEY_FINDER_H

#include <stdint.h>
#include <vector>
#include <set>
#include "secp256k1.h"
Expand Down Expand Up @@ -134,8 +135,8 @@ class KeyFinder {

CudaDeviceContext *_devCtx;

unsigned long long _iterCount;
unsigned long long _total;
uint64_t _iterCount;
uint64_t _total;
unsigned int _totalTime;


Expand Down Expand Up @@ -176,6 +177,8 @@ class KeyFinder {
bool isTargetInList(const unsigned int value[5]);
void setTargetsOnDevice();

void cudaCall(cudaError_t err);

public:
class Compression {
public:
Expand All @@ -186,7 +189,7 @@ class KeyFinder {
};
};

KeyFinder(int device, const secp256k1::uint256 &start, unsigned long long range, int compression, int blocks = 0, int threads = 0, int pointsPerThread = 0);
KeyFinder(int device, const secp256k1::uint256 &start, uint64_t range, int compression, int blocks = 0, int threads = 0, int pointsPerThread = 0);

~KeyFinder();

Expand Down
2 changes: 1 addition & 1 deletion cudaUtil/cudaUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace cuda {
int minor;
int mpCount;
int cores;
unsigned long long mem;
uint64_t mem;
std::string name;

}CudaDeviceInfo;
Expand Down
10 changes: 5 additions & 5 deletions util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace util {
#endif
}

std::string formatThousands(unsigned long long x)
std::string formatThousands(uint64_t x)
{
char buf[32] = "";

Expand Down Expand Up @@ -83,14 +83,14 @@ namespace util {
return result;
}

unsigned int parseUInt32(std::string s)
uint32_t parseUInt32(std::string s)
{
return (unsigned int)parseUInt64(s);
return (uint32_t)parseUInt64(s);
}

unsigned long long parseUInt64(std::string s)
uint64_t parseUInt64(std::string s)
{
unsigned long long val = 0;
uint64_t val = 0;
bool isHex = false;

if(s[0] == '0' && s[1] == 'x') {
Expand Down
6 changes: 3 additions & 3 deletions util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class Timer {
unsigned int getSystemTime();
void sleep(int seconds);

std::string formatThousands(unsigned long long x);
std::string formatThousands(uint64_t x);
std::string formatSeconds(unsigned int seconds);

unsigned int parseUInt32(std::string s);
unsigned long long parseUInt64(std::string s);
uint32_t parseUInt32(std::string s);
uint64_t parseUInt64(std::string s);
bool isHex(const std::string &s);
bool appendToFile(const std::string &fileName, const std::string &s);
bool readLinesFromStream(std::istream &in, std::vector<std::string> &lines);
Expand Down

0 comments on commit 2964776

Please sign in to comment.