-
Notifications
You must be signed in to change notification settings - Fork 29
/
Thread.cpp
235 lines (176 loc) · 5.3 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
/*
* This file is part of the BSGS distribution (https://github.com/JeanLucPons/BSGS).
* 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 "BSGS.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 BSGS::LaunchThread(LPTHREAD_START_ROUTINE func, TH_PARAM *p) {
p->obj = this;
return CreateThread(NULL, 0, func, (void*)(p), 0, NULL);
}
void BSGS::JoinThreads(THREAD_HANDLE *handles,int nbThread) {
WaitForMultipleObjects(nbThread, handles, TRUE, INFINITE);
}
void BSGS::FreeHandles(THREAD_HANDLE *handles, int nbThread) {
for (int i = 0; i < nbThread; i++)
CloseHandle(handles[i]);
}
#else
THREAD_HANDLE BSGS::LaunchThread(void *(*func) (void *), TH_PARAM *p) {
THREAD_HANDLE h;
p->obj = this;
pthread_create(&h, NULL, func, (void*)(p));
return h;
}
void BSGS::JoinThreads(THREAD_HANDLE *handles, int nbThread) {
for (int i = 0; i < nbThread; i++)
pthread_join(handles[i], NULL);
}
void BSGS::FreeHandles(THREAD_HANDLE *handles, int nbThread) {
}
#endif
// ----------------------------------------------------------------------------
bool BSGS::isAlive(TH_PARAM *p) {
bool isAlive = false;
int total = nbCPUThread;
for(int i=0;i<total;i++)
isAlive = isAlive || p[i].isRunning;
return isAlive;
}
// ----------------------------------------------------------------------------
bool BSGS::hasStarted(TH_PARAM *p) {
bool hasStarted = true;
int total = nbCPUThread;
for (int i = 0; i < total; i++)
hasStarted = hasStarted && p[i].hasStarted;
return hasStarted;
}
// ----------------------------------------------------------------------------
bool BSGS::isWaiting(TH_PARAM *p) {
bool isWaiting = true;
int total = nbCPUThread;
for (int i = 0; i < total; i++)
isWaiting = isWaiting && p[i].isWaiting;
return isWaiting;
}
// ----------------------------------------------------------------------------
uint64_t BSGS::getCPUCount() {
uint64_t count = 0;
for(int i=0;i<nbCPUThread;i++)
count += counters[i];
return count;
}
// ----------------------------------------------------------------------------
string BSGS::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 threads and display stats
void BSGS::Process(TH_PARAM *params,std::string unit) {
double t0;
double t1;
#ifndef WIN64
setvbuf(stdout,NULL,_IONBF,0);
#endif
uint64_t lastCount = 0;
double avgKeyRate;
uint64_t count;
// Key rate smoothing filter
#define FILTER_SIZE 8
double lastkeyRate[FILTER_SIZE];
uint32_t filterPos = 0;
double keyRate = 0.0;
memset(lastkeyRate,0,sizeof(lastkeyRate));
// Wait that all threads have started
while(!hasStarted(params))
Timer::SleepMillis(50);
t0 = Timer::get_tick();
startTime = t0;
while(isAlive(params)) {
int delay = 2000;
while(isAlive(params) && delay > 0) {
Timer::SleepMillis(50);
delay -= 50;
}
count = getCPUCount();
t1 = Timer::get_tick();
keyRate = (double)(count - lastCount) / (t1 - t0);
lastkeyRate[filterPos%FILTER_SIZE] = keyRate;
filterPos++;
// KeyRate smoothing
avgKeyRate = 0.0;
uint32_t nbSample;
for(nbSample = 0; (nbSample < FILTER_SIZE) && (nbSample < filterPos); nbSample++) {
avgKeyRate += lastkeyRate[nbSample];
}
avgKeyRate /= (double)(nbSample);
if(isAlive(params) && !endOfSearch) {
::printf("\r[%.2f %s][Cnt 2^%.2f][%s][%.1fMB] ",
avgKeyRate / 1000000.0,unit.c_str(),
log2((double)count),
GetTimeStr(t1 - startTime).c_str(),
hashTable.GetSizeMB()
);
}
lastCount = count;
t0 = t1;
}
count = getCPUCount();
t1 = Timer::get_tick();
if( !endOfSearch ) {
::printf("\r[%.2f %s][Cnt 2^%.2f][%s][%.1fMB] \n",
avgKeyRate / 1000000.0,unit.c_str(),
log2((double)count),
GetTimeStr(t1 - startTime).c_str(),
hashTable.GetSizeMB()
);
}
}