Skip to main content

bench/
bench.rs

1//! Wall-clock for the same model on both engines.
2
3use glazier::Simulation;
4use glazier::{CellType, Model};
5use std::time::Instant;
6
7fn model(size: usize) -> Model {
8    Model {
9        width: size,
10        height: size,
11        contact: vec![0.0, 16.0, 16.0, 8.0],
12        types: vec![
13            CellType::default(),
14            CellType {
15                target_volume: 64.0,
16                lambda_volume: 2.0,
17                ..Default::default()
18            },
19        ],
20        temperature: 10.0,
21        neighbour_order: 2,
22        seed: 1,
23        ..Default::default()
24    }
25}
26
27fn main() {
28    let mut args = std::env::args().skip(1);
29    let steps: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(100);
30    // Sizes follow the step count, so a sweep widens without a rebuild.
31    let sizes: Vec<usize> = {
32        let stated: Vec<usize> = args.filter_map(|s| s.parse().ok()).collect();
33        if stated.is_empty() {
34            vec![128, 256, 512, 1024]
35        } else {
36            stated
37        }
38    };
39
40    println!(
41        "{:>6}  {:>7}  {:>12}  {:>12}  {:>8}",
42        "size", "cells", "cpu (s)", "gpu (s)", "speedup"
43    );
44    for size in sizes {
45        let mut cpu = Simulation::tiled(model(size), 8).unwrap();
46        let cells = cpu.n_cells();
47
48        let t = Instant::now();
49        for _ in 0..steps {
50            cpu.step();
51        }
52        let cpu_s = t.elapsed().as_secs_f64();
53
54        #[cfg(feature = "cuda")]
55        let gpu_s = {
56            let seeded = Simulation::tiled(model(size), 8).unwrap();
57            match glazier::cuda::GpuSimulation::from_cpu(&seeded) {
58                Ok(mut g) => {
59                    // One step first, so the compile and the upload stay out of
60                    // the measurement.
61                    g.step(1).unwrap();
62                    let t = Instant::now();
63                    g.step(steps).unwrap();
64                    t.elapsed().as_secs_f64()
65                }
66                Err(e) => {
67                    eprintln!("no device: {e}");
68                    f64::NAN
69                }
70            }
71        };
72        #[cfg(not(feature = "cuda"))]
73        let gpu_s = f64::NAN;
74
75        println!(
76            "{size:>6}  {cells:>7}  {cpu_s:>12.3}  {gpu_s:>12.3}  {:>8.1}",
77            cpu_s / gpu_s
78        );
79    }
80    println!("\n{steps} Monte Carlo steps, one attempt per site per step.");
81}