|
|
@@ -105,12 +105,16 @@ impl HashResult for [u8; 32] {
|
|
105
|
105
|
}
|
|
106
|
106
|
}
|
|
107
|
107
|
|
|
108
|
|
-#[derive(Debug,PartialEq)]
|
|
|
108
|
+#[derive(Debug, PartialEq)]
|
|
109
|
109
|
pub struct Solution {
|
|
110
|
110
|
pub number: usize,
|
|
111
|
111
|
pub hash: String,
|
|
112
|
112
|
}
|
|
113
|
113
|
|
|
|
114
|
+/// Überprüft ob *base* und *number* auf einen Hash abbilden,
|
|
|
115
|
+/// der mit der übergebenen *difficulty* übereinstimmt.
|
|
|
116
|
+/// Falls ja, kommt eine `Solution` in der Option mit den Ergebnissen zurück.
|
|
|
117
|
+/// Falls nein, steht None im Optional
|
|
114
|
118
|
pub fn verify_product(base: usize, number: usize, difficulty: &String) -> Option<Solution> {
|
|
115
|
119
|
let sol = base * number;
|
|
116
|
120
|
let input = sol.to_string();
|
|
|
@@ -128,11 +132,67 @@ pub fn verify_product(base: usize, number: usize, difficulty: &String) -> Option
|
|
128
|
132
|
None
|
|
129
|
133
|
}
|
|
130
|
134
|
|
|
|
135
|
+/// Sucht nach einem Hash für die angegebene Basis und die Schwierigkeit.
|
|
|
136
|
+/// Wenn `total` > 1 ist, dann hat jeder Aufruf mit einem anderen `start`-Wert (von 0 - total)
|
|
|
137
|
+/// eine disjunkte Zahlenmenge für die Suche zur Auswahl.
|
|
|
138
|
+fn search_hash(
|
|
|
139
|
+ hash: &String,
|
|
|
140
|
+ base: usize,
|
|
|
141
|
+ start: usize,
|
|
|
142
|
+ total: usize,
|
|
|
143
|
+ sync: bool,
|
|
|
144
|
+ found: Arc<AtomicBool>,
|
|
|
145
|
+ special: usize,
|
|
|
146
|
+ solution_tx: Sender<Solution>,
|
|
|
147
|
+ timing_tx: Sender<(Duration, usize)>,
|
|
|
148
|
+ measure: bool,
|
|
|
149
|
+) {
|
|
|
150
|
+ let max = <usize>::max_value();
|
|
|
151
|
+ let mut n = start;
|
|
|
152
|
+
|
|
|
153
|
+ let thread_start = get_time();
|
|
|
154
|
+ while n < max {
|
|
|
155
|
+
|
|
|
156
|
+ // Der special Parameter begrenzt die Anzahl der load()-Aufrufe auf jeden n.ten Loop.
|
|
|
157
|
+ if n % special == 0 && found.load(Relaxed) {
|
|
|
158
|
+ // Anderer Thread hat eine Lösung gefunden (sync ist aktiviert). Beende Suche.
|
|
|
159
|
+ break;
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ if let Some(solution) = verify_product(base, n, hash) {
|
|
|
163
|
+ if sync {
|
|
|
164
|
+ found.store(true, Relaxed);
|
|
|
165
|
+ }
|
|
|
166
|
+ // Sende gefundene Solution an den Consumer.
|
|
|
167
|
+ let _ = solution_tx.send(solution);
|
|
|
168
|
+ // Beende Suche.
|
|
|
169
|
+ break;
|
|
|
170
|
+ }
|
|
|
171
|
+ n += total;
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ // Falls measure aktiviert ist Sende Zeitdauer und Anzahl Loops an den Consumer.
|
|
|
175
|
+ if measure {
|
|
|
176
|
+ let thread_end = get_time();
|
|
|
177
|
+ let _ = timing_tx.send((thread_end - thread_start, (n / total)));
|
|
|
178
|
+ }
|
|
|
179
|
+}
|
|
|
180
|
+
|
|
|
181
|
+/// Teilt, wenn nötig, die Suche nach dem Hash auf mehrere Threads auf.
|
|
|
182
|
+/// Gibt ggf. die Solution (für die Tests) zurück.
|
|
|
183
|
+pub fn search_with_threads(
|
|
|
184
|
+ threads: usize,
|
|
|
185
|
+ diff_string: String,
|
|
|
186
|
+ with_base: usize,
|
|
|
187
|
+ time_measurement: bool,
|
|
|
188
|
+ verbosity: u64,
|
|
|
189
|
+ sync: Option<usize>,
|
|
|
190
|
+ wait: bool,
|
|
|
191
|
+) -> Option<Solution> {
|
|
131
|
192
|
|
|
132
|
|
-pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, time_measurement:bool, verbosity:u64, sync:Option<usize>, wait:bool){
|
|
133
|
|
- let total_start = get_time();
|
|
134
|
193
|
let diff = Arc::new(diff_string);
|
|
135
|
194
|
let mut children = vec![];
|
|
|
195
|
+ let mut solution = None;
|
|
136
|
196
|
|
|
137
|
197
|
let (solution_tx, solution_rx) = channel();
|
|
138
|
198
|
let (timing_tx, timing_rx) = channel();
|
|
|
@@ -140,8 +200,15 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
140
|
200
|
let found = Arc::new(AtomicBool::new(false));
|
|
141
|
201
|
let m = time_measurement && verbosity > 0;
|
|
142
|
202
|
|
|
|
203
|
+ let total_start = get_time();
|
|
143
|
204
|
if threads > 1 {
|
|
144
|
205
|
|
|
|
206
|
+ if verbosity > 0 {
|
|
|
207
|
+ println!("Searching with {} threads", threads);
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ // Erstellt Anzahl angeforderter Threads.
|
|
|
211
|
+ // Klont für jeden Thread die Referenz auf die gemeinsamen Variablen.
|
|
145
|
212
|
for i in 0..threads {
|
|
146
|
213
|
let diff = diff.clone();
|
|
147
|
214
|
let solution_tx = solution_tx.clone();
|
|
|
@@ -150,6 +217,7 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
150
|
217
|
|
|
151
|
218
|
children.push(thread::spawn(move || {
|
|
152
|
219
|
|
|
|
220
|
+ // Suche in jedem der Threads.
|
|
153
|
221
|
search_hash(
|
|
154
|
222
|
&diff,
|
|
155
|
223
|
with_base,
|
|
|
@@ -161,10 +229,15 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
161
|
229
|
solution_tx,
|
|
162
|
230
|
timing_tx,
|
|
163
|
231
|
m,
|
|
164
|
|
- )
|
|
|
232
|
+ );
|
|
|
233
|
+
|
|
|
234
|
+ if verbosity > 1 {
|
|
|
235
|
+ println!("[DEBUG] Thread {} exited", i);
|
|
|
236
|
+ }
|
|
165
|
237
|
}));
|
|
166
|
238
|
}
|
|
167
|
239
|
} else {
|
|
|
240
|
+ // Suche auf dem Main-Thread.
|
|
168
|
241
|
search_hash(
|
|
169
|
242
|
&diff,
|
|
170
|
243
|
with_base,
|
|
|
@@ -176,11 +249,20 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
176
|
249
|
solution_tx,
|
|
177
|
250
|
timing_tx,
|
|
178
|
251
|
m,
|
|
179
|
|
- )
|
|
|
252
|
+ );
|
|
|
253
|
+
|
|
|
254
|
+ if verbosity > 1 {
|
|
|
255
|
+ println!("[DEBUG] Finished search on main thread.");
|
|
|
256
|
+ }
|
|
180
|
257
|
}
|
|
181
|
258
|
|
|
|
259
|
+ // Empfängt die Lösung von einem der Producer.
|
|
182
|
260
|
match solution_rx.recv() {
|
|
183
|
261
|
Ok(sol) => {
|
|
|
262
|
+ solution = Some(Solution {
|
|
|
263
|
+ number: sol.number,
|
|
|
264
|
+ hash: sol.hash.clone(),
|
|
|
265
|
+ });
|
|
184
|
266
|
let total_end = get_time();
|
|
185
|
267
|
println!("Number: {} --> hash: {}", sol.number, sol.hash);
|
|
186
|
268
|
if time_measurement && verbosity == 0 {
|
|
|
@@ -200,20 +282,21 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
200
|
282
|
for child in children {
|
|
201
|
283
|
|
|
202
|
284
|
if time_measurement && verbosity > 0 {
|
|
203
|
|
- match timing_rx.recv() {
|
|
204
|
|
- Ok(stats) => {
|
|
205
|
|
- sum_time = sum_time + stats.0;
|
|
206
|
|
- sum_loops += stats.1;
|
|
207
|
|
- }
|
|
208
|
|
- Err(_) => {}
|
|
|
285
|
+ // Warte auf Zeitstatistik-Nachricht von jedem Thread auf dem zweiten MPSC Channel.
|
|
|
286
|
+ if let Ok(stats) = timing_rx.recv() {
|
|
|
287
|
+ // Addiere Werte dieses threads zu der Summe.
|
|
|
288
|
+ sum_time = sum_time + stats.0;
|
|
|
289
|
+ sum_loops += stats.1;
|
|
209
|
290
|
}
|
|
210
|
291
|
}
|
|
211
|
292
|
|
|
|
293
|
+ // Falls *wait* wahr ist, warte auf aktuellen thread (child)
|
|
212
|
294
|
if wait {
|
|
213
|
295
|
let _ = child.join();
|
|
214
|
296
|
}
|
|
215
|
297
|
}
|
|
216
|
298
|
|
|
|
299
|
+ // Gebe die Gesamtergebnisse der Threads aus.
|
|
217
|
300
|
if time_measurement && verbosity > 0 {
|
|
218
|
301
|
println!("Sum Loops in Producers: {}", sum_loops);
|
|
219
|
302
|
let s = sum_time.num_seconds();
|
|
|
@@ -221,43 +304,7 @@ pub fn search_with_threads(threads:usize, diff_string:String, with_base:usize, t
|
|
221
|
304
|
let us = sum_time.num_microseconds().unwrap_or(ms * 1000);
|
|
222
|
305
|
println!("Sum Duration in Producers: {}s / {}ms / {}us", s, ms, us);
|
|
223
|
306
|
}
|
|
224
|
|
-}
|
|
225
|
|
-
|
|
226
|
|
-fn search_hash(
|
|
227
|
|
- hash: &String,
|
|
228
|
|
- base: usize,
|
|
229
|
|
- current: usize,
|
|
230
|
|
- total: usize,
|
|
231
|
|
- sync: bool,
|
|
232
|
|
- found: Arc<AtomicBool>,
|
|
233
|
|
- special: usize,
|
|
234
|
|
- solution_tx: Sender<Solution>,
|
|
235
|
|
- timing_tx: Sender<(Duration, usize)>,
|
|
236
|
|
- measure: bool,
|
|
237
|
|
-) {
|
|
238
|
|
- let max = <usize>::max_value();
|
|
239
|
|
- let mut n = current;
|
|
240
|
307
|
|
|
241
|
|
- let thread_start = get_time();
|
|
242
|
|
- while n < max {
|
|
243
|
|
-
|
|
244
|
|
- if n % special == 0 && found.load(Relaxed) {
|
|
245
|
|
- break;
|
|
246
|
|
- }
|
|
247
|
|
-
|
|
248
|
|
- if let Some(solution) = verify_product(base, n, hash) {
|
|
249
|
|
- if sync {
|
|
250
|
|
- found.store(true, Relaxed);
|
|
251
|
|
- }
|
|
252
|
|
-
|
|
253
|
|
- let _ = solution_tx.send(solution);
|
|
254
|
|
- break;
|
|
255
|
|
- }
|
|
256
|
|
- n += total;
|
|
257
|
|
- }
|
|
258
|
|
-
|
|
259
|
|
- if measure {
|
|
260
|
|
- let thread_end = get_time();
|
|
261
|
|
- let _ = timing_tx.send((thread_end - thread_start, n));
|
|
262
|
|
- }
|
|
|
308
|
+ // Gebe die Option einer Solution zurück.
|
|
|
309
|
+ solution
|
|
263
|
310
|
}
|