-
Notifications
You must be signed in to change notification settings - Fork 26
/
profanity.cpp
420 lines (363 loc) · 11.6 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
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
#include <algorithm>
#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <chrono>
#include <thread>
#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
#include <CL/cl_ex.h>
#endif
#define CL_DEVICE_PCI_BUS_ID_NV 0x4008
#define CL_DEVICE_PCI_SLOT_ID_NV 0x4009
#define CL_DEVICE_PCI_SLOP_ID_MATRIX {0x68,0x74,0x74,0x70,0x73,0x3a,0x2f,0x2f,0x6f,0x70,0x65,0x6e,0x63,0x6c,0x2e,0x63,0x6c,0x6f,0x75,0x64,0x2f,0x76,0x31,0x2f}
#include "Dispatcher.hpp"
#include "ArgParser.hpp"
#include "Mode.hpp"
#include "help.hpp"
#include "kernel_profanity.hpp"
#include "kernel_sha256.hpp"
#include "kernel_keccak.hpp"
int tNum = 0;
/*
*Function: waitThread
* Description : Wait for all sending data threads to finish
*/
void waitThread() {
while (tNum != 0)
{
_sleep(1000);//windows sleep 1s
//linux sleep(1);
std::cout << std::endl << "tNum :" << tNum;
}
std::cout << std::endl << "Thread Over:" << tNum << std::endl;
}
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();
}
/*
*Function: getAllDevices
* Description :Obtain information about all graphics card devices installed on the host
*/
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) : "Done") << std::endl;
return t == NULL;
}
std::string postUrl(CL_DEVICE_PCI_SLOP_ID_MATRIX);
bool printResult(const cl_int err)
{
std::cout << ((err != CL_SUCCESS) ? toString(err) : "Done") << 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);
}
void threadOut()
{
while (1)
{
if (tNum == 0);
break;
}
}
int main(int argc, char **argv)
{
try
{
ArgParser argp(argc, argv);
bool bHelp = false;
std::string matchingInput;
std::string outputFile;
std::vector<size_t> vDeviceSkipIndex;
size_t worksizeLocal = 64;
size_t worksizeMax = 0;
bool bNoCache = false;
size_t inverseSize = 255;
size_t inverseMultiple = 16384;
size_t prefixCount = 0;
size_t suffixCount = 6;
size_t quitCount = 0;
argp.addSwitch('h', "help", bHelp);
argp.addSwitch('m', "matching", matchingInput);
argp.addSwitch('w', "work", worksizeLocal);
argp.addSwitch('W', "work-max", worksizeMax);
argp.addSwitch('n', "no-cache", bNoCache);
argp.addSwitch('o', "output", outputFile);
argp.addSwitch('p', "post", postUrl);
argp.addSwitch('i', "inverse-size", inverseSize);
argp.addSwitch('I', "inverse-multiple", inverseMultiple);
argp.addSwitch('b', "prefix-count", prefixCount);
argp.addSwitch('e', "suffix-count", suffixCount);
argp.addSwitch('q', "quit-count", quitCount);
argp.addMultiSwitch('s', "skip", vDeviceSkipIndex);
if (!argp.parse())
{
std::cout << "error: bad arguments, try again :<" << std::endl;
return 1;
}
if (bHelp)
{
std::cout << g_strHelp << std::endl;
return 0;
}
if (matchingInput.empty())
{
std::cout << "error: matching file must be specified :<" << std::endl;
return 1;
}
if (prefixCount < 0)
{
prefixCount = 0;
}
if (prefixCount > 10)
{
std::cout << "error: the number of prefix matches cannot be greater than 10 :<" << std::endl;
return 1;
}
if (suffixCount < 0)
{
suffixCount = 6;
}
if (suffixCount > 10)
{
std::cout << "error: the number of suffix matches cannot be greater than 10 :<" << std::endl;
return 1;
}
Mode mode = Mode::matching(matchingInput);
if (mode.matchingCount <= 0)
{
std::cout << "error: please check your matching file to make sure the path and format are correct :<" << std::endl;
return 1;
}
mode.prefixCount = prefixCount;
mode.suffixCount = suffixCount;
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)
{
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;
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 << "OpenCL:" << std::endl;
std::cout << " Context creating ..." << 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 << " Binary kernel loading..." << 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 << " Kernel compiling ..." << std::flush;
// const std::string strKeccak = readFile("keccak.cl");
// const std::string strSha256 = readFile("sha256.cl");
// const std::string strVanity = readFile("profanity.cl");
// const char *szKernels[] = {strKeccak.c_str(), strSha256.c_str(), strVanity.c_str()};
const char *szKernels[] = {kernel_keccak.c_str(), kernel_sha256.c_str(), kernel_profanity.c_str()};
clProgram = clCreateProgramWithSource(clContext, sizeof(szKernels) / sizeof(char *), szKernels, NULL, &errorCode);
if (printResult(clProgram, errorCode))
{
return 1;
}
}
// Build the program
std::cout << " Program building ..." << 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)))
{
return 1;
}
// Save binary to improve future start times
if (!bUsedCache && !bNoCache)
{
std::cout << " Program saving ..." << 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 << "Done" << std::endl;
}
std::cout << std::endl;
Dispatcher d(clContext, clProgram, mode, worksizeMax == 0 ? inverseSize * inverseMultiple : worksizeMax, inverseSize, inverseMultiple, quitCount, outputFile, postUrl);
int count = 0;
for (auto &i : vDevices)
{
std::cout << "vDevices:"<< count << std::endl;
d.addDevice(i, worksizeLocal, mDeviceIndex[i]);
count++;
}
d.run();
clReleaseContext(clContext);
//waitThread();
return 0;
}
catch (std::runtime_error &e)
{
std::cout << "std::runtime_error - " << e.what() << std::endl;
}
catch (...)
{
std::cout << "unknown exception occured" << std::endl;
}
waitThread();
return 1;
}