Skip to content

Commit

Permalink
add neighbors_cpp_vs_neighbors_z_rust_benchmark.rs
Browse files Browse the repository at this point in the history
& minor refactoring
  • Loading branch information
technobeat committed Oct 31, 2024
1 parent 44635f6 commit d5833d5
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dev = ["clippy"]
name = "04"
path = "src/04.rs"

[[bin]]
name = "neighbors_z_rust_executable__inputfile_lj_cube_1000_timestep_0_0001_nbiterations_50k_cutoff_1_5"
path = "src/neighbors_z_rust_executable.rs"

[[bench]]
name = "lj_direct_summation_benchmark"
harness = false
Expand All @@ -50,6 +54,10 @@ harness = false
name = "neighbors_vs_neighbors_z_benchmark"
harness = false

[[bench]]
name = "neighbors_cpp_vs_neighbors_z_rust_benchmark"
harness = false

#[[bench]]
#name = "verlet_benchmark"
#harness = false
Expand Down
50 changes: 50 additions & 0 deletions benches/neighbors_cpp_vs_neighbors_z_rust_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::process::Command;
use std::time::Duration;

use mimalloc::MiMalloc;
#[global_allocator]
//static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static GLOBAL: MiMalloc = MiMalloc;

fn benchmark_binary(binary_path: &str) -> Duration {
// Run the binary and time its execution
let start = std::time::Instant::now();
let status = Command::new(binary_path)
.status()
.expect("Failed to execute binary");

if !status.success() {
panic!("Binary did not run successfully");
}

start.elapsed()
}

//Benchmark using the executable "neighbors_z_rust_executable__..." and "neighbors_cpp_executable__..."
fn neighbors_cpp_vs_neighbors_z_rust_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("neighbors_cpp_vs_neighbors_z_rust_benchmark");

let binary_folder = "target/release/";

let executables = ["neighbors_cpp_executable__inputfile_lj_cube_1000_timestep_0_0001_nbiterations_50k_cutoff_1_5", "neighbors_z_rust_executable__inputfile_lj_cube_1000_timestep_0_0001_nbiterations_50k_cutoff_1_5"];

group.sample_size(20);

for executable in executables {
let executable_path = binary_folder.to_owned() + executable;

println!("Benchmark executable: {}", executable_path);

group.bench_function(BenchmarkId::new(&executable_path, "__Benchmark"), |b| {
b.iter(|| {
benchmark_binary(&executable_path);
});
});
}

group.finish();
}

criterion_group!(benches, neighbors_cpp_vs_neighbors_z_rust_benchmark);
criterion_main!(benches);
35 changes: 11 additions & 24 deletions benches/neighbors_vs_neighbors_z_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,24 @@ use mimalloc::MiMalloc;
//static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static GLOBAL: MiMalloc = MiMalloc;

//Benchmark using the milestone
fn neighbors_vs_neighbors_z_benchmark(c: &mut Criterion) {
const TIMESTEP: f64 = 0.0001;
const NB_ITERATIONS: u32 = 10_000;
const CUTOFF_RADIUS:f64 = 1.5;
const CUTOFF_RADIUS: f64 = 1.5;
const INPUT_PATH: &str = "input_files/lj_cube_1000.xyz";

let folder = "input_files";
let json_file_path = folder.to_owned() + "/benchmark_lj_direct_summation.json";
let content = fs::read_to_string(&json_file_path).expect("JSON file \"benchmark_lj_direct_summation.json\" could not be loaded to benchmark with the specified input files.");
let json: serde_json::Value =
serde_json::from_str(&content).expect("JSON was not well-formatted");

let input_files = json.as_array().expect(
&("The JSON file \"".to_owned()
+ &json_file_path
+ "\" doesn't contain a valid JSON array with the filenames of the input files."),
);

let path = folder.to_owned() + &"/".to_owned() + input_files[5].as_str().unwrap();
if !fs::metadata(&path).is_ok() {
panic!("input file \"{}\" doesn't exist!", path);
if !fs::metadata(INPUT_PATH).is_ok() {
panic!("input file \"{}\" doesn't exist!", INPUT_PATH);
}
println!("input_file path {}", &path.to_string());
println!("input_file path {}", INPUT_PATH.to_string());

let mut group = c.benchmark_group("neighbors_vs_neighbors_z");

group.sample_size(20);
let iterations_string = &("neighbors_".to_owned()+&NB_ITERATIONS.to_string()+"_iters");
let iterations_string = &("neighbors_".to_owned() + &NB_ITERATIONS.to_string() + "_iters");

group.bench_function(BenchmarkId::new(iterations_string, &path), |b| {
let mut atoms = md_implementation::xyz::read_xyz(path.to_string())
group.bench_function(BenchmarkId::new(iterations_string, INPUT_PATH), |b| {
let mut atoms = md_implementation::xyz::read_xyz(INPUT_PATH.to_string())
.expect("Failed to load atoms configuration.");
let mut neighbor_list: NeighborList = NeighborList::new();
b.iter(|| {
Expand All @@ -49,10 +36,10 @@ fn neighbors_vs_neighbors_z_benchmark(c: &mut Criterion) {
});
});

let iterations_string = &("neighbors_z_".to_owned()+&NB_ITERATIONS.to_string()+"_iters");
let iterations_string = &("neighbors_z_".to_owned() + &NB_ITERATIONS.to_string() + "_iters");

group.bench_function(BenchmarkId::new(iterations_string, &path), |b| {
let mut atoms = md_implementation::xyz::read_xyz(path.to_string())
group.bench_function(BenchmarkId::new(iterations_string, INPUT_PATH), |b| {
let mut atoms = md_implementation::xyz::read_xyz(INPUT_PATH.to_string())
.expect("Failed to load atoms configuration.");
let mut neighbor_list: NeighborListZ = NeighborListZ::new();
b.iter(|| {
Expand Down
6 changes: 2 additions & 4 deletions src/04.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use rsmd::md_implementation::{self, xyz};

// use morton_encoding;

use mimalloc::MiMalloc;
#[global_allocator]
//static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
Expand All @@ -26,8 +24,8 @@ fn main() {
//delete old trajectory if it exists
_ = std::fs::remove_file(OUTPUT_FILE);

let ekin: f64 = atoms.kinetic_energy();
let epot: f64 = atoms.lj_direct_summation(None, None);
let _ekin: f64 = atoms.kinetic_energy();
let _epot: f64 = atoms.lj_direct_summation(None, None);

for i in 0..NB_ITERATIONS {
atoms.verlet_step1(TIMESTEP.into());
Expand Down
1 change: 0 additions & 1 deletion src/md_implementation/neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl NeighborList {

// Origin stores the bottom left corner of the enclosing rectangles and
// lengths the three Cartesian lengths.

let mut origin = Array1::<f64>::from_elem(3, 3.0);
let mut lengths = Array1::<f64>::from_elem(3, 3.0);

Expand Down
28 changes: 28 additions & 0 deletions src/neighbors_z_rust_executable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rsmd::md_implementation::{neighbors_z::NeighborListZ, xyz::read_xyz};

use mimalloc::MiMalloc;
#[global_allocator]
//static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static GLOBAL: MiMalloc = MiMalloc;

const TIMESTEP: f64 = 0.0001;
const NB_ITERATIONS: u32 = 50_000;
const CUTOFF_RADIUS: f64 = 1.5;
const INPUT_PATH: &str = "input_files/lj_cube_1000.xyz";

//Executable for benchmark "neighbors_cpp_vs_neighbors_z_rust_benchmark.rs"
fn main() {
let mut atoms = read_xyz(INPUT_PATH.to_string()).expect("Failed to load atoms configuration.");

let mut neighbor_list: NeighborListZ = NeighborListZ::new();

for i in 0..NB_ITERATIONS {
atoms.verlet_step1(TIMESTEP.into());
if i % 100 != 0 {
neighbor_list.update(&mut atoms, CUTOFF_RADIUS, false);
} else {
neighbor_list.update(&mut atoms, CUTOFF_RADIUS, true);
}
atoms.verlet_step2(TIMESTEP.into());
}
}

0 comments on commit d5833d5

Please # to comment.