Skip to content

Commit d8404ef

Browse files
committed
thread pinning
1 parent 6ca72eb commit d8404ef

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cached = "0.46.1"
2626
chrono = "0.4.38"
2727
clap = { version = "4.4.12", features = ["derive"] }
2828
colored = "2.0"
29+
core_affinity = "0.8.1"
2930
drillx = "2.0.0"
3031
futures = "0.3.30"
3132
num_cpus = "1.16.0"

src/mine.rs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Miner {
2828
self.open().await;
2929

3030
// Check num threads
31-
self.check_num_cores(args.threads);
31+
self.check_num_cores(args.cores);
3232

3333
// Start mining loop
3434
loop {
@@ -45,13 +45,9 @@ impl Miner {
4545
let cutoff_time = self.get_cutoff(proof, args.buffer_time).await;
4646

4747
// 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;
5551

5652
// Submit most difficult hash
5753
let mut compute_budget = 500_000;
@@ -75,60 +71,68 @@ impl Miner {
7571
async fn find_hash_par(
7672
proof: Proof,
7773
cutoff_time: u64,
78-
threads: u64,
74+
cores: u64,
7975
min_difficulty: u32,
8076
) -> Solution {
8177
// Dispatch job to each thread
8278
let progress_bar = Arc::new(spinner::new_progress_bar());
8379
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()
8583
.map(|i| {
8684
std::thread::spawn({
8785
let proof = proof.clone();
8886
let progress_bar = progress_bar.clone();
8987
let mut memory = equix::SolverMemory::new();
9088
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+
}
108110
}
109-
}
110111

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+
));
117124
}
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-
));
123125
}
126+
127+
// Increment nonce
128+
nonce += 1;
124129
}
125130

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())
128135
}
129-
130-
// Return the best nonce
131-
(best_nonce, best_difficulty, best_hash)
132136
}
133137
})
134138
})
@@ -158,14 +162,12 @@ impl Miner {
158162
Solution::new(best_hash.d, best_nonce.to_le_bytes())
159163
}
160164

161-
pub fn check_num_cores(&self, threads: u64) {
162-
// Check num threads
165+
pub fn check_num_cores(&self, cores: u64) {
163166
let num_cores = num_cpus::get() as u64;
164-
if threads.gt(&num_cores) {
167+
if cores.gt(&num_cores) {
165168
println!(
166-
"{} Number of threads ({}) exceeds available cores ({})",
169+
"{} Cannot exceeds available cores ({})",
167170
"WARNING".bold().yellow(),
168-
threads,
169171
num_cores
170172
);
171173
}

0 commit comments

Comments
 (0)