forked from johguse/profanity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
profanity.cpp
364 lines (313 loc) · 11.8 KB
/
profanity.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
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.h>
#include <OpenCL/cl_ext.h> // Included to get topology to get an actual unique identifier per device
#else
#include <CL/cl.h>
#include <CL/cl_ext.h> // Included to get topology to get an actual unique identifier per device
#endif
#define CL_DEVICE_PCI_BUS_ID_NV 0x4008
#define CL_DEVICE_PCI_SLOT_ID_NV 0x4009
#include "Dispatcher.hpp"
#include "ArgParser.hpp"
#include "Mode.hpp"
#include "help.hpp"
std::string readFile(const char * const szFilename)
{
std::ifstream in(szFilename, std::ios::in | std::ios::binary);
std::ostringstream contents;
contents << in.rdbuf();
return contents.str();
}
std::vector<cl_device_id> getAllDevices(cl_device_type deviceType = CL_DEVICE_TYPE_GPU)
{
std::vector<cl_device_id> vDevices;
cl_uint platformIdCount = 0;
clGetPlatformIDs (0, NULL, &platformIdCount);
std::vector<cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs (platformIdCount, platformIds.data (), NULL);
for( auto it = platformIds.cbegin(); it != platformIds.cend(); ++it ) {
cl_uint countDevice;
clGetDeviceIDs(*it, deviceType, 0, NULL, &countDevice);
std::vector<cl_device_id> deviceIds(countDevice);
clGetDeviceIDs(*it, deviceType, countDevice, deviceIds.data(), &countDevice);
std::copy( deviceIds.begin(), deviceIds.end(), std::back_inserter(vDevices) );
}
return vDevices;
}
template <typename T, typename U, typename V, typename W>
T clGetWrapper(U function, V param, W param2) {
T t;
function(param, param2, sizeof(t), &t, NULL);
return t;
}
template <typename U, typename V, typename W>
std::string clGetWrapperString(U function, V param, W param2) {
size_t len;
function(param, param2, 0, NULL, &len);
char * const szString = new char[len];
function(param, param2, len, szString, NULL);
std::string r(szString);
delete[] szString;
return r;
}
template <typename T, typename U, typename V, typename W>
std::vector<T> clGetWrapperVector(U function, V param, W param2) {
size_t len;
function(param, param2, 0, NULL, &len);
len /= sizeof(T);
std::vector<T> v;
if (len > 0) {
T * pArray = new T[len];
function(param, param2, len * sizeof(T), pArray, NULL);
for (size_t i = 0; i < len; ++i) {
v.push_back(pArray[i]);
}
delete[] pArray;
}
return v;
}
std::vector<std::string> getBinaries(cl_program & clProgram) {
std::vector<std::string> vReturn;
auto vSizes = clGetWrapperVector<size_t>(clGetProgramInfo, clProgram, CL_PROGRAM_BINARY_SIZES);
if (!vSizes.empty()) {
unsigned char * * pBuffers = new unsigned char *[vSizes.size()];
for (size_t i = 0; i < vSizes.size(); ++i) {
pBuffers[i] = new unsigned char[vSizes[i]];
}
clGetProgramInfo(clProgram, CL_PROGRAM_BINARIES, vSizes.size() * sizeof(unsigned char *), pBuffers, NULL);
for (size_t i = 0; i < vSizes.size(); ++i) {
std::string strData(reinterpret_cast<char *>(pBuffers[i]), vSizes[i]);
vReturn.push_back(strData);
delete[] pBuffers[i];
}
delete[] pBuffers;
}
return vReturn;
}
unsigned int getUniqueDeviceIdentifier(const cl_device_id & deviceId) {
#if defined(CL_DEVICE_TOPOLOGY_AMD)
auto topology = clGetWrapper<cl_device_topology_amd>(clGetDeviceInfo, deviceId, CL_DEVICE_TOPOLOGY_AMD);
if (topology.raw.type == CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD) {
return (topology.pcie.bus << 16) + (topology.pcie.device << 8) + topology.pcie.function;
}
#endif
cl_int bus_id = clGetWrapper<cl_int>(clGetDeviceInfo, deviceId, CL_DEVICE_PCI_BUS_ID_NV);
cl_int slot_id = clGetWrapper<cl_int>(clGetDeviceInfo, deviceId, CL_DEVICE_PCI_SLOT_ID_NV);
return (bus_id << 16) + slot_id;
}
template <typename T> bool printResult(const T & t, const cl_int & err) {
std::cout << ((t == NULL) ? toString(err) : "OK") << std::endl;
return t == NULL;
}
bool printResult(const cl_int err) {
std::cout << ((err != CL_SUCCESS) ? toString(err) : "OK") << std::endl;
return err != CL_SUCCESS;
}
std::string getDeviceCacheFilename(cl_device_id & d, const size_t & inverseSize) {
const auto uniqueId = getUniqueDeviceIdentifier(d);
return "cache-opencl." + toString(inverseSize) + "." + toString(uniqueId);
}
int main(int argc, char * * argv) {
try {
ArgParser argp(argc, argv);
bool bHelp = false;
bool bModeBenchmark = false;
bool bModeZeros = false;
bool bModeGas = false;
bool bModeLetters = false;
bool bModeNumbers = false;
std::string strModeLeading;
std::string strModeMatching;
bool bModeLeadingRange = false;
bool bModeRange = false;
bool bModeMirror = false;
bool bModeDoubles = false;
int rangeMin = 0;
int rangeMax = 0;
std::vector<size_t> vDeviceSkipIndex;
size_t worksizeLocal = 64;
size_t worksizeMax = 0; // Will be automatically determined later if not overriden by user
bool bNoCache = false;
size_t inverseSize = 255;
size_t inverseMultiple = 16384;
bool bMineContract = false;
argp.addSwitch('h', "help", bHelp);
argp.addSwitch('0', "benchmark", bModeBenchmark);
argp.addSwitch('1', "zeros", bModeZeros);
argp.addSwitch('2', "letters", bModeLetters);
argp.addSwitch('3', "numbers", bModeNumbers);
argp.addSwitch('4', "leading", strModeLeading);
argp.addSwitch('5', "matching", strModeMatching);
argp.addSwitch('6', "leading-range", bModeLeadingRange);
argp.addSwitch('7', "range", bModeRange);
argp.addSwitch('8', "mirror", bModeMirror);
argp.addSwitch('9', "leading-doubles", bModeDoubles);
argp.addSwitch('m', "min", rangeMin);
argp.addSwitch('M', "max", rangeMax);
argp.addMultiSwitch('s', "skip", vDeviceSkipIndex);
argp.addSwitch('w', "work", worksizeLocal);
argp.addSwitch('W', "work-max", worksizeMax);
argp.addSwitch('n', "no-cache", bNoCache);
argp.addSwitch('i', "inverse-size", inverseSize);
argp.addSwitch('I', "inverse-multiple", inverseMultiple);
argp.addSwitch('c', "contract", bMineContract);
argp.addSwitch('g', "gas", bModeGas);
if (!argp.parse()) {
std::cout << "error: bad arguments, try again :<" << std::endl;
return 1;
}
if (bHelp) {
std::cout << g_strHelp << std::endl;
return 0;
}
Mode mode = Mode::benchmark();
if (bModeBenchmark) {
mode = Mode::benchmark();
} else if (bModeZeros) {
mode = Mode::zeros();
} else if (bModeGas) {
mode = Mode::gas();
} else if (bModeLetters) {
mode = Mode::letters();
} else if (bModeNumbers) {
mode = Mode::numbers();
} else if (!strModeLeading.empty()) {
mode = Mode::leading(strModeLeading.front());
} else if (!strModeMatching.empty()) {
mode = Mode::matching(strModeMatching);
} else if (bModeLeadingRange) {
mode = Mode::leadingRange(rangeMin, rangeMax);
} else if (bModeRange) {
mode = Mode::range(rangeMin, rangeMax);
} else if(bModeMirror) {
mode = Mode::mirror();
} else if (bModeDoubles) {
mode = Mode::doubles();
} else {
std::cout << g_strHelp << std::endl;
return 0;
}
std::cout << "Mode: " << mode.name << std::endl;
if (bMineContract) {
mode.target = CONTRACT;
} else {
mode.target = ADDRESS;
}
std::cout << "Target: " << mode.transformName() << std:: endl;
std::vector<cl_device_id> vFoundDevices = getAllDevices();
std::vector<cl_device_id> vDevices;
std::map<cl_device_id, size_t> mDeviceIndex;
std::vector<std::string> vDeviceBinary;
std::vector<size_t> vDeviceBinarySize;
cl_int errorCode;
bool bUsedCache = false;
std::cout << "Devices:" << std::endl;
for (size_t i = 0; i < vFoundDevices.size(); ++i) {
// Ignore devices in skip index
if (std::find(vDeviceSkipIndex.begin(), vDeviceSkipIndex.end(), i) != vDeviceSkipIndex.end()) {
continue;
}
cl_device_id & deviceId = vFoundDevices[i];
const auto strName = clGetWrapperString(clGetDeviceInfo, deviceId, CL_DEVICE_NAME);
const auto computeUnits = clGetWrapper<cl_uint>(clGetDeviceInfo, deviceId, CL_DEVICE_MAX_COMPUTE_UNITS);
const auto globalMemSize = clGetWrapper<cl_ulong>(clGetDeviceInfo, deviceId, CL_DEVICE_GLOBAL_MEM_SIZE);
bool precompiled = false;
// Check if there's a prebuilt binary for this device and load it
if(!bNoCache) {
std::ifstream fileIn(getDeviceCacheFilename(deviceId, inverseSize), std::ios::binary);
if (fileIn.is_open()) {
vDeviceBinary.push_back(std::string((std::istreambuf_iterator<char>(fileIn)), std::istreambuf_iterator<char>()));
vDeviceBinarySize.push_back(vDeviceBinary.back().size());
precompiled = true;
}
}
std::cout << " GPU" << i << ": " << strName << ", " << globalMemSize << " bytes available, " << computeUnits << " compute units (precompiled = " << (precompiled ? "yes" : "no") << ")" << std::endl;
vDevices.push_back(vFoundDevices[i]);
mDeviceIndex[vFoundDevices[i]] = i;
}
if (vDevices.empty()) {
return 1;
}
std::cout << std::endl;
std::cout << "Initializing OpenCL..." << std::endl;
std::cout << " Creating context..." << std::flush;
auto clContext = clCreateContext( NULL, vDevices.size(), vDevices.data(), NULL, NULL, &errorCode);
if (printResult(clContext, errorCode)) {
return 1;
}
cl_program clProgram;
if (vDeviceBinary.size() == vDevices.size()) {
// Create program from binaries
bUsedCache = true;
std::cout << " Loading kernel from binary..." << std::flush;
const unsigned char * * pKernels = new const unsigned char *[vDevices.size()];
for (size_t i = 0; i < vDeviceBinary.size(); ++i) {
pKernels[i] = reinterpret_cast<const unsigned char *>(vDeviceBinary[i].data());
}
cl_int * pStatus = new cl_int[vDevices.size()];
clProgram = clCreateProgramWithBinary(clContext, vDevices.size(), vDevices.data(), vDeviceBinarySize.data(), pKernels, pStatus, &errorCode);
if(printResult(clProgram, errorCode)) {
return 1;
}
} else {
// Create a program from the kernel source
std::cout << " Compiling kernel..." << std::flush;
const std::string strKeccak = readFile("keccak.cl");
const std::string strVanity = readFile("profanity.cl");
const char * szKernels[] = { strKeccak.c_str(), strVanity.c_str() };
clProgram = clCreateProgramWithSource(clContext, sizeof(szKernels) / sizeof(char *), szKernels, NULL, &errorCode);
if (printResult(clProgram, errorCode)) {
return 1;
}
}
// Build the program
std::cout << " Building program..." << std::flush;
const std::string strBuildOptions = "-D PROFANITY_INVERSE_SIZE=" + toString(inverseSize) + " -D PROFANITY_MAX_SCORE=" + toString(PROFANITY_MAX_SCORE);
if (printResult(clBuildProgram(clProgram, vDevices.size(), vDevices.data(), strBuildOptions.c_str(), NULL, NULL))) {
#ifdef PROFANITY_DEBUG
std::cout << std::endl;
std::cout << "build log:" << std::endl;
size_t sizeLog;
clGetProgramBuildInfo(clProgram, vDevices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &sizeLog);
char * const szLog = new char[sizeLog];
clGetProgramBuildInfo(clProgram, vDevices[0], CL_PROGRAM_BUILD_LOG, sizeLog, szLog, NULL);
std::cout << szLog << std::endl;
delete[] szLog;
#endif
return 1;
}
// Save binary to improve future start times
if( !bUsedCache && !bNoCache ) {
std::cout << " Saving program..." << std::flush;
auto binaries = getBinaries(clProgram);
for (size_t i = 0; i < binaries.size(); ++i) {
std::ofstream fileOut(getDeviceCacheFilename(vDevices[i], inverseSize), std::ios::binary);
fileOut.write(binaries[i].data(), binaries[i].size());
}
std::cout << "OK" << std::endl;
}
std::cout << std::endl;
Dispatcher d(clContext, clProgram, mode, worksizeMax == 0 ? inverseSize * inverseMultiple : worksizeMax, inverseSize, inverseMultiple, 0);
for (auto & i : vDevices) {
d.addDevice(i, worksizeLocal, mDeviceIndex[i]);
}
d.run();
clReleaseContext(clContext);
return 0;
} catch (std::runtime_error & e) {
std::cout << "std::runtime_error - " << e.what() << std::endl;
} catch (...) {
std::cout << "unknown exception occured" << std::endl;
}
return 1;
}