@@ -28,7 +28,7 @@ impl Miner {
28
28
self . open ( ) . await ;
29
29
30
30
// Check num threads
31
- self . check_num_cores ( args. threads ) ;
31
+ self . check_num_cores ( args. cores ) ;
32
32
33
33
// Start mining loop
34
34
loop {
@@ -45,13 +45,9 @@ impl Miner {
45
45
let cutoff_time = self . get_cutoff ( proof, args. buffer_time ) . await ;
46
46
47
47
// Run drillx
48
- let solution = Self :: find_hash_par (
49
- proof,
50
- cutoff_time,
51
- args. threads ,
52
- config. min_difficulty as u32 ,
53
- )
54
- . await ;
48
+ let solution =
49
+ Self :: find_hash_par ( proof, cutoff_time, args. cores , config. min_difficulty as u32 )
50
+ . await ;
55
51
56
52
// Submit most difficult hash
57
53
let mut compute_budget = 500_000 ;
@@ -75,60 +71,68 @@ impl Miner {
75
71
async fn find_hash_par (
76
72
proof : Proof ,
77
73
cutoff_time : u64 ,
78
- threads : u64 ,
74
+ cores : u64 ,
79
75
min_difficulty : u32 ,
80
76
) -> Solution {
81
77
// Dispatch job to each thread
82
78
let progress_bar = Arc :: new ( spinner:: new_progress_bar ( ) ) ;
83
79
progress_bar. set_message ( "Mining..." ) ;
84
- let handles: Vec < _ > = ( 0 ..threads)
80
+ let core_ids = core_affinity:: get_core_ids ( ) . unwrap ( ) ;
81
+ let handles: Vec < _ > = core_ids
82
+ . into_iter ( )
85
83
. map ( |i| {
86
84
std:: thread:: spawn ( {
87
85
let proof = proof. clone ( ) ;
88
86
let progress_bar = progress_bar. clone ( ) ;
89
87
let mut memory = equix:: SolverMemory :: new ( ) ;
90
88
move || {
91
- let timer = Instant :: now ( ) ;
92
- let mut nonce = u64:: MAX . saturating_div ( threads) . saturating_mul ( i) ;
93
- let mut best_nonce = nonce;
94
- let mut best_difficulty = 0 ;
95
- let mut best_hash = Hash :: default ( ) ;
96
- loop {
97
- // Create hash
98
- if let Ok ( hx) = drillx:: hash_with_memory (
99
- & mut memory,
100
- & proof. challenge ,
101
- & nonce. to_le_bytes ( ) ,
102
- ) {
103
- let difficulty = hx. difficulty ( ) ;
104
- if difficulty. gt ( & best_difficulty) {
105
- best_nonce = nonce;
106
- best_difficulty = difficulty;
107
- best_hash = hx;
89
+ let res = core_affinity:: set_for_current ( i) ;
90
+ if res {
91
+ let timer = Instant :: now ( ) ;
92
+ let mut nonce =
93
+ u64:: MAX . saturating_div ( cores) . saturating_mul ( i. id as u64 ) ;
94
+ let mut best_nonce = nonce;
95
+ let mut best_difficulty = 0 ;
96
+ let mut best_hash = Hash :: default ( ) ;
97
+ loop {
98
+ // Create hash
99
+ if let Ok ( hx) = drillx:: hash_with_memory (
100
+ & mut memory,
101
+ & proof. challenge ,
102
+ & nonce. to_le_bytes ( ) ,
103
+ ) {
104
+ let difficulty = hx. difficulty ( ) ;
105
+ if difficulty. gt ( & best_difficulty) {
106
+ best_nonce = nonce;
107
+ best_difficulty = difficulty;
108
+ best_hash = hx;
109
+ }
108
110
}
109
- }
110
111
111
- // Exit if time has elapsed
112
- if nonce % 100 == 0 {
113
- if timer. elapsed ( ) . as_secs ( ) . ge ( & cutoff_time) {
114
- if best_difficulty. ge ( & min_difficulty) {
115
- // Mine until min difficulty has been met
116
- break ;
112
+ // Exit if time has elapsed
113
+ if nonce % 100 == 0 {
114
+ if timer. elapsed ( ) . as_secs ( ) . ge ( & cutoff_time) {
115
+ if best_difficulty. ge ( & min_difficulty) {
116
+ // Mine until min difficulty has been met
117
+ break ;
118
+ }
119
+ } else if cores == 0 {
120
+ progress_bar. set_message ( format ! (
121
+ "Mining... ({} sec remaining)" ,
122
+ cutoff_time. saturating_sub( timer. elapsed( ) . as_secs( ) ) ,
123
+ ) ) ;
117
124
}
118
- } else if i == 0 {
119
- progress_bar. set_message ( format ! (
120
- "Mining... ({} sec remaining)" ,
121
- cutoff_time. saturating_sub( timer. elapsed( ) . as_secs( ) ) ,
122
- ) ) ;
123
125
}
126
+
127
+ // Increment nonce
128
+ nonce += 1 ;
124
129
}
125
130
126
- // Increment nonce
127
- nonce += 1 ;
131
+ // Return the best nonce
132
+ ( best_nonce, best_difficulty, best_hash)
133
+ } else {
134
+ ( 0 , 0 , Hash :: default ( ) )
128
135
}
129
-
130
- // Return the best nonce
131
- ( best_nonce, best_difficulty, best_hash)
132
136
}
133
137
} )
134
138
} )
@@ -158,14 +162,12 @@ impl Miner {
158
162
Solution :: new ( best_hash. d , best_nonce. to_le_bytes ( ) )
159
163
}
160
164
161
- pub fn check_num_cores ( & self , threads : u64 ) {
162
- // Check num threads
165
+ pub fn check_num_cores ( & self , cores : u64 ) {
163
166
let num_cores = num_cpus:: get ( ) as u64 ;
164
- if threads . gt ( & num_cores) {
167
+ if cores . gt ( & num_cores) {
165
168
println ! (
166
- "{} Number of threads ({}) exceeds available cores ({})" ,
169
+ "{} Cannot exceeds available cores ({})" ,
167
170
"WARNING" . bold( ) . yellow( ) ,
168
- threads,
169
171
num_cores
170
172
) ;
171
173
}
0 commit comments