Skip to content

Commit

Permalink
feat: add build feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kilb committed Aug 9, 2019
1 parent 539622a commit 116c407
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 180 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ tags
.vagrant

# runtime folder
/ckb.toml
/ckb-miner.toml
/data
/specs
/ckb-dev
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ ckb-logger = { path = "util/logger" }
sentry = "^0.15.4"
ckb-build-info = { path = "util/build-info" }
serde = "1.0"
serde_derive = "1.0"
serde_derive = "1.0"

[features]
gpu = ["miner/gpu"]
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
A cuckoo miner for avx2 CPUs, avx512 CPUs and Nvidia GPUs.

## How to build
Cuda is essential to build this repo.
By default, GPU miner is not built, you can build GPU miner with this parameters:
```
cargo build --release --features gpu
```
Cuda is essential to build and run GPU miners.

## How to use
1. Modify `ckb-miner.toml` in this repo and copy it to your working directory. You can specify the CPUs and GPUs you want to use.
Expand Down
17 changes: 5 additions & 12 deletions ckb-miner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ block_on_submit = true
# block template polling interval in milliseconds
poll_interval = 1000

[gpus]
# the gpu you want to use, can be 0,1,2,3,..
gpuids = [0]
# the gpu you want to use runing miner, can be 0,1,2,3,..
# only work if a gpu miner was built in.
gpus = [0]

# cpu miner, the architecture you want to use, 0 is avx2, 1 is avx512
# avx2 miner can run on most intel CPUs, but slower than avx512
[[cpus]]
arch = 0
threads = 2

[[cpus]]
arch = 1
threads = 2
# how many cpu miners you want to run
cpus = 2
5 changes: 4 additions & 1 deletion miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ console = "0.7.5"
proptest = "0.9"

[build-dependencies]
cc = "1.0"
cc = "1.0"

[features]
gpu = []
19 changes: 10 additions & 9 deletions miner/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ extern crate cc;

fn main() {
cc::Build::new()
.file("src/worker/blake2b.c")
.include("src/worker")
.file("src/worker/include/blake2b.c")
.include("src/worker/include")
.flag("-O3")
.flag("-msse4")
.static_flag(true)
.compile("libblake2b.a");

cc::Build::new()
.file("src/worker/cuckoo-avx512.c")
.include("src/worker")
.file("src/worker/include/cuckoo-avx512.c")
.include("src/worker/include")
.flag("-O3")
.flag("-lcrypto")
.flag("-mavx512f")
Expand All @@ -21,18 +21,19 @@ fn main() {
.compile("libcuckoo.a.1");

cc::Build::new()
.file("src/worker/cuckoo.c")
.include("src/worker")
.file("src/worker/include/cuckoo.c")
.include("src/worker/include")
.flag("-O3")
.flag("-lcrypto")
.flag("-mavx2")
.flag("-msse4")
.static_flag(true)
.compile("libcuckoo.a.2");


#[cfg(feature = "gpu")]
cc::Build::new()
.file("src/worker/cuckoo.cu")
.include("src/worker")
.file("src/worker/include/cuckoo.cu")
.include("src/worker/include")
.flag("-O3")
.flag("-lcrypto")
.cuda(true)
Expand Down
15 changes: 2 additions & 13 deletions miner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ pub struct MinerConfig {
pub rpc_url: String,
pub poll_interval: u64,
pub block_on_submit: bool,
pub cpus: Vec<CpuConfig>,
pub gpus: GpuConfig,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GpuConfig {
pub gpuids: Vec<u32>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Copy)]
pub struct CpuConfig {
pub arch: u32,
pub threads: u32,
pub cpus: u32,
pub gpus: Vec<u32>,
}
119 changes: 1 addition & 118 deletions miner/src/worker/cuckoo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Worker, WorkerMessage};
use super::{Worker, WorkerMessage, CYCLE_LEN};
use byteorder::{ByteOrder, LittleEndian};
use ckb_core::header::Seal;
use ckb_logger::{debug, error};
Expand All @@ -8,8 +8,6 @@ use numext_fixed_hash::H256;
use std::thread;
use std::time::{Duration, Instant};

const CYCLE_LEN: usize = 12;

const STATE_UPDATE_DURATION_MILLIS: u128 = 300;

extern "C" {
Expand All @@ -20,13 +18,6 @@ extern "C" {
input: *const u8,
target: *const u8,
) -> u32;
pub fn c_solve_gpu(
output: *mut u32,
nonce: *mut u64,
input: *const u8,
target: *const u8,
gpuid: u32,
) -> u32;
}

pub struct CuckooCpu {
Expand Down Expand Up @@ -145,111 +136,3 @@ impl Worker for CuckooCpu {
}
}
}

pub struct CuckooGpu {
start: bool,
pow_info: Option<(H256, H256)>,
seal_tx: Sender<(H256, Seal)>,
worker_rx: Receiver<WorkerMessage>,
seal_candidates_found: u64,
gpuid: u32,
}

impl CuckooGpu {
pub fn new(
seal_tx: Sender<(H256, Seal)>,
worker_rx: Receiver<WorkerMessage>,
gpuid: u32,
) -> Self {
Self {
start: true,
pow_info: None,
seal_candidates_found: 0,
seal_tx,
worker_rx,
gpuid,
}
}

fn poll_worker_message(&mut self) {
if let Ok(msg) = self.worker_rx.try_recv() {
match msg {
WorkerMessage::NewWork(pow_info) => {
self.pow_info = Some(pow_info);
}
WorkerMessage::Stop => {
self.start = false;
}
WorkerMessage::Start => {
self.start = true;
}
}
}
}

#[inline]
fn solve(&mut self, pow_hash: &H256, target: &H256) -> usize {
unsafe {
let mut nonce = 0u64;
let mut output = vec![0u32; CYCLE_LEN + 1];
let ns = c_solve_gpu(
output.as_mut_ptr(),
&mut nonce,
pow_hash[..].as_ptr(),
target[..].as_ptr(),
self.gpuid,
);
if ns > 0 && output[CYCLE_LEN] == 1 {
let mut proof_u8 = vec![0u8; CYCLE_LEN << 2];
LittleEndian::write_u32_into(&output[0..CYCLE_LEN], &mut proof_u8);
let seal = Seal::new(nonce, proof_u8.into());
debug!(
"send new found seal, pow_hash {:x}, seal {:?}",
pow_hash, seal
);
if let Err(err) = self.seal_tx.send((pow_hash.clone(), seal)) {
error!("seal_tx send error {:?}", err);
}
self.seal_candidates_found += 1;
}

return ns as usize;
}
}
}

impl Worker for CuckooGpu {
fn run(&mut self, progress_bar: ProgressBar) {
let mut state_update_counter = 0usize;
let mut start = Instant::now();
loop {
self.poll_worker_message();
if self.start {
if let Some((pow_hash, target)) = self.pow_info.clone() {
state_update_counter += self.solve(&pow_hash, &target);

let elapsed = start.elapsed();
if elapsed.as_millis() > STATE_UPDATE_DURATION_MILLIS {
let elapsed_nanos: f64 = (elapsed.as_secs() * 1_000_000_000
+ u64::from(elapsed.subsec_nanos()))
as f64
/ 1_000_000_000.0;
progress_bar.set_message(&format!(
"gps: {:>10.3} / cycles found: {:>10}",
state_update_counter as f64 / elapsed_nanos,
self.seal_candidates_found,
));
progress_bar.inc(1);
state_update_counter = 0;
start = Instant::now();
}
}
} else {
// reset state and sleep
state_update_counter = 0;
start = Instant::now();
thread::sleep(Duration::from_millis(100));
}
}
}
}
Loading

0 comments on commit 116c407

Please sign in to comment.