forked from JeanLucPons/Kangaroo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.cpp
364 lines (286 loc) · 9.11 KB
/
Thread.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
/*
* This file is part of the BSGS distribution (https://github.com/JeanLucPons/Kangaroo).
* Copyright (c) 2020 Jean Luc PONS.
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Kangaroo.h"
#include "Timer.h"
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#ifndef WIN64
#include <pthread.h>
#endif
using namespace std;
// ----------------------------------------------------------------------------
#ifdef WIN64
THREAD_HANDLE Kangaroo::LaunchThread(LPTHREAD_START_ROUTINE func, TH_PARAM *p) {
p->obj = this;
return CreateThread(NULL, 0, func, (void*)(p), 0, NULL);
}
void Kangaroo::JoinThreads(THREAD_HANDLE *handles,int nbThread) {
WaitForMultipleObjects(nbThread, handles, TRUE, INFINITE);
}
void Kangaroo::FreeHandles(THREAD_HANDLE *handles, int nbThread) {
for (int i = 0; i < nbThread; i++)
CloseHandle(handles[i]);
}
#else
THREAD_HANDLE Kangaroo::LaunchThread(void *(*func) (void *), TH_PARAM *p) {
THREAD_HANDLE h;
p->obj = this;
pthread_create(&h, NULL, func, (void*)(p));
return h;
}
void Kangaroo::JoinThreads(THREAD_HANDLE *handles, int nbThread) {
for (int i = 0; i < nbThread; i++)
pthread_join(handles[i], NULL);
}
void Kangaroo::FreeHandles(THREAD_HANDLE *handles, int nbThread) {
}
#endif
// ----------------------------------------------------------------------------
bool Kangaroo::isAlive(TH_PARAM *p) {
bool isAlive = false;
int total = nbCPUThread + nbGPUThread;
for(int i=0;i<total;i++)
isAlive = isAlive || p[i].isRunning;
return isAlive;
}
// ----------------------------------------------------------------------------
bool Kangaroo::hasStarted(TH_PARAM *p) {
bool hasStarted = true;
int total = nbCPUThread + nbGPUThread;
for (int i = 0; i < total; i++)
hasStarted = hasStarted && p[i].hasStarted;
return hasStarted;
}
// ----------------------------------------------------------------------------
bool Kangaroo::isWaiting(TH_PARAM *p) {
bool isWaiting = true;
int total = nbCPUThread + nbGPUThread;
for (int i = 0; i < total; i++)
isWaiting = isWaiting && p[i].isWaiting;
return isWaiting;
}
// ----------------------------------------------------------------------------
uint64_t Kangaroo::getGPUCount() {
uint64_t count = 0;
for(int i = 0; i<nbGPUThread; i++)
count += counters[0x80L + i];
return count;
}
// ----------------------------------------------------------------------------
uint64_t Kangaroo::getCPUCount() {
uint64_t count = 0;
for(int i=0;i<nbCPUThread;i++)
count += counters[i];
return count;
}
// ----------------------------------------------------------------------------
string Kangaroo::GetTimeStr(double dTime) {
char tmp[256];
double nbDay = dTime / 86400.0;
if (nbDay >= 1) {
double nbYear = nbDay / 365.0;
if (nbYear > 1) {
if (nbYear < 5)
sprintf(tmp, "%.1fy", nbYear);
else
sprintf(tmp, "%gy", nbYear);
} else {
sprintf(tmp, "%.1fd", nbDay);
}
} else {
int iTime = (int)dTime;
int nbHour = (int)((iTime % 86400) / 3600);
int nbMin = (int)(((iTime % 86400) % 3600) / 60);
int nbSec = (int)(iTime % 60);
if (nbHour == 0) {
if (nbMin == 0) {
sprintf(tmp, "%02ds", nbSec);
} else {
sprintf(tmp, "%02d:%02d", nbMin, nbSec);
}
} else {
sprintf(tmp, "%02d:%02d:%02d", nbHour, nbMin, nbSec);
}
}
return string(tmp);
}
// Wait for end of server and dispay stats
void Kangaroo::ProcessServer() {
double t0;
double t1;
t0 = Timer::get_tick();
startTime = t0;
double lastSave = 0;
// Acquire mutex ownership
#ifndef WIN64
pthread_mutex_init(&ghMutex, NULL);
setvbuf(stdout, NULL, _IONBF, 0);
#else
ghMutex = CreateMutex(NULL,FALSE,NULL);
#endif
while(!endOfSearch) {
t0 = Timer::get_tick();
LOCK(ghMutex);
// Get back all dps
localCache.clear();
for(int i=0;i<(int)recvDP.size();i++)
localCache.push_back(recvDP[i]);
recvDP.clear();
UNLOCK(ghMutex);
// Add to hashTable
for(int i = 0; i<(int)localCache.size() && !endOfSearch; i++) {
DP_CACHE dp = localCache[i];
for(int j = 0; j<(int)dp.nbDP && !endOfSearch; j++) {
uint64_t h = dp.dp[j].h;
if(!AddToTable(h,&dp.dp[j].x,&dp.dp[j].d)) {
// Collision inside the same herd
collisionInSameHerd++;
}
}
free(dp.dp);
}
t1 = Timer::get_tick();
double toSleep = SEND_PERIOD - (t1-t0);
if(toSleep<0) toSleep = 0.0;
Timer::SleepMillis((uint32_t)(toSleep*1000.0));
t1 = Timer::get_tick();
if(!endOfSearch)
printf("\r[Client %d][Kang 2^%.2f][DP Count 2^%.2f/2^%.2f][Dead %.0f][%s][%s] ",
connectedClient,
log2((double)totalRW),
log2((double)hashTable.GetNbItem()),
log2(expectedNbOp / pow(2.0,dpSize)),
(double)collisionInSameHerd,
GetTimeStr(t1 - startTime).c_str(),
hashTable.GetSizeInfo().c_str()
);
if(workFile.length() > 0 && !endOfSearch) {
if((t1 - lastSave) > saveWorkPeriod) {
SaveServerWork();
lastSave = t1;
}
}
}
}
// Wait for end of threads and display stats
void Kangaroo::Process(TH_PARAM *params,std::string unit) {
double t0;
double t1;
uint64_t count;
uint64_t lastCount = 0;
uint64_t gpuCount = 0;
uint64_t lastGPUCount = 0;
double avgKeyRate = 0.0;
double avgGpuKeyRate = 0.0;
double lastSave = 0;
#ifndef WIN64
setvbuf(stdout, NULL, _IONBF, 0);
#endif
// Key rate smoothing filter
#define FILTER_SIZE 8
double lastkeyRate[FILTER_SIZE];
double lastGpukeyRate[FILTER_SIZE];
uint32_t filterPos = 0;
double keyRate = 0.0;
double gpuKeyRate = 0.0;
memset(lastkeyRate,0,sizeof(lastkeyRate));
memset(lastGpukeyRate,0,sizeof(lastkeyRate));
// Wait that all threads have started
while(!hasStarted(params))
Timer::SleepMillis(5);
t0 = Timer::get_tick();
startTime = t0;
lastGPUCount = getGPUCount();
lastCount = getCPUCount() + gpuCount;
while(isAlive(params)) {
int delay = 2000;
while(isAlive(params) && delay > 0) {
Timer::SleepMillis(50);
delay -= 50;
}
gpuCount = getGPUCount();
count = getCPUCount() + gpuCount;
t1 = Timer::get_tick();
keyRate = (double)(count - lastCount) / (t1 - t0);
gpuKeyRate = (double)(gpuCount - lastGPUCount) / (t1 - t0);
lastkeyRate[filterPos%FILTER_SIZE] = keyRate;
lastGpukeyRate[filterPos%FILTER_SIZE] = gpuKeyRate;
filterPos++;
// KeyRate smoothing
uint32_t nbSample;
for(nbSample = 0; (nbSample < FILTER_SIZE) && (nbSample < filterPos); nbSample++) {
avgKeyRate += lastkeyRate[nbSample];
avgGpuKeyRate += lastGpukeyRate[nbSample];
}
avgKeyRate /= (double)(nbSample);
avgGpuKeyRate /= (double)(nbSample);
double expectedTime = expectedNbOp / avgKeyRate;
// Display stats
if(isAlive(params) && !endOfSearch) {
if(clientMode) {
printf("\r[%.2f %s][GPU %.2f %s][Count 2^%.2f][%s][Server %6s] ",
avgKeyRate / 1000000.0,unit.c_str(),
avgGpuKeyRate / 1000000.0,unit.c_str(),
log2((double)count + offsetCount),
GetTimeStr(t1 - startTime + offsetTime).c_str(),
serverStatus.c_str()
);
} else {
printf("\r[%.2f %s][GPU %.2f %s][Count 2^%.2f][Dead %.0f][%s (Avg %s)][%s] ",
avgKeyRate / 1000000.0,unit.c_str(),
avgGpuKeyRate / 1000000.0,unit.c_str(),
log2((double)count + offsetCount),
(double)collisionInSameHerd,
GetTimeStr(t1 - startTime + offsetTime).c_str(),GetTimeStr(expectedTime).c_str(),
hashTable.GetSizeInfo().c_str()
);
}
}
// Save request
if(workFile.length() > 0 && !endOfSearch) {
if((t1 - lastSave) > saveWorkPeriod) {
SaveWork(count + offsetCount,t1 - startTime + offsetTime,params,nbCPUThread + nbGPUThread);
lastSave = t1;
}
}
// Abort
if(!clientMode && maxStep>0.0) {
double max = expectedNbOp * maxStep;
if( (double)count > max ) {
::printf("\nKey#%2d [XX]Pub: 0x%s \n",keyIdx,secp->GetPublicKeyHex(true,keysToSearch[keyIdx]).c_str());
::printf(" Aborted !\n");
endOfSearch = true;
Timer::SleepMillis(1000);
}
}
lastCount = count;
lastGPUCount = gpuCount;
t0 = t1;
}
count = getCPUCount() + getGPUCount();
t1 = Timer::get_tick();
if( !endOfSearch ) {
printf("\r[%.2f %s][GPU %.2f %s][Cnt 2^%.2f][%s] ",
avgKeyRate / 1000000.0,unit.c_str(),
avgGpuKeyRate / 1000000.0,unit.c_str(),
log2((double)count),
GetTimeStr(t1 - startTime).c_str()
);
}
}