Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ x64/
Release/
Debug/
build/
Cargo.lock
target/
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "randomx"
version = "1.1.11"
homepage = "https://github.com/tevador/RandomX/"
authors = [
"Dyne.org Foundation <foundation@dyne.org>",
"tevador <tevador@gmail.com>",
"The Monero Project",
]
license = "BSD-3-Clause"
edition = "2021"

[target.'cfg(not(target = "x86_64-unknown-linux-musl"))'.build-dependencies]
bindgen = "0.69.4"

[target.'cfg(target = "x86_64-unknown-linux-musl")'.build-dependencies]
bindgen = {version = "0.69.4", default-features = false, features = ["static"]}

[dependencies]
bitflags = "1"
81 changes: 81 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::env;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

fn main() {
let target = env::var("TARGET").unwrap();
let n_threads = std::thread::available_parallelism()
.unwrap()
.get()
.to_string();

let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let build_dir = &cargo_dir.join("build");
std::fs::create_dir_all(build_dir).unwrap();
env::set_current_dir(build_dir).unwrap();

// Generate CMake cache files
let b = Command::new("cmake")
.arg("-DARCH=native")
.arg("..")
.output()
.expect("Failed to generate Makefile with CMake");
std::io::stdout().write_all(&b.stdout).unwrap();
std::io::stderr().write_all(&b.stderr).unwrap();
assert!(b.status.success());

// Build the library
let b = Command::new("cmake")
.arg("--build")
.arg(".")
.arg("--config")
.arg("Release")
.arg("-j")
.arg(n_threads)
.output()
.expect("Failed to build RandomX library with CMake");
std::io::stdout().write_all(&b.stdout).unwrap();
std::io::stderr().write_all(&b.stderr).unwrap();
assert!(b.status.success());

env::set_current_dir(cargo_dir).unwrap();

// Tell cargo how to find the static library
println!(
"cargo:rustc-link-search=native={}",
build_dir.to_string_lossy()
);
println!("cargo:rustc-link-lib=static=randomx");

if target.contains("apple") {
println!("cargo:rustc-link-lib=dylib=c++");
} else if target.contains("linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
} else if target.contains("freebsd") {
println!("cargo:rustc-link-lib=dylib=c++");
} else {
unimplemented!()
}

// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("src/randomx.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
52 changes: 52 additions & 0 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! randomx example that calculates many hashes using multiple threads

use randomx::*;
use std::sync::Arc;
use std::thread;
use std::time::Instant;
use std::vec::Vec;

fn main() {
const NUM_THREADS: u32 = 8;
// number of hashes to perform in each thread, not the total.
const NUM_HASHES: u32 = 5000;

let start = Instant::now();

// Try adding `| RandomXFlags::LARGEPAGES`.
let flags = RandomXFlags::default() | RandomXFlags::FULLMEM;
let dataset = Arc::new(RandomXDataset::new(flags, b"key", NUM_THREADS as usize).unwrap());

println!("Dataset initialised in {}ms", start.elapsed().as_millis());

let mut handles = Vec::new();

let start = Instant::now();

for i in 0..NUM_THREADS {
let dataset = dataset.clone();

handles.push(thread::spawn(move || {
let mut nonce: u32 = i;
let vm = RandomXVM::new_fast(flags, &dataset).unwrap();

for _ in 0..NUM_HASHES {
let _ = vm.hash(&nonce.to_be_bytes());

// e.g. thread 0 will use nonces 0, 8, 16, ...
// and thread 1 will use nonces 1, 9, 17, ...
nonce += NUM_THREADS;
}
}));
}

for handle in handles {
let _ = handle.join();
}

println!(
"Completed {} hashes in {}ms",
NUM_THREADS * NUM_HASHES,
start.elapsed().as_millis()
);
}
Loading