Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize FLOPs calculation #1438

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 22 additions & 20 deletions clients/cli/src/flops.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
// Measure the FLOPS of the CPU
// use num_cpus;
use rayon::prelude::*;
use std::hint::black_box;
use std::time::Instant;

const NTESTS: u64 = 1_000_000;
const OPERATIONS_PER_ITERATION: u64 = 4; // sin, add, multiply, divide
const NUM_REPEATS: usize = 5; // Number of repeats to average the results

pub fn measure_flops() -> f32 {
let num_cores = num_cpus::get() as u64;
let start = Instant::now();

let total_flops: f64 = (0..num_cores)
.into_par_iter()
println!("Using {} logical cores for FLOPS measurement", num_cores);

let avg_flops: f64 = (0..NUM_REPEATS)
.map(|_| {
let mut x: f64 = 1.0;
for _ in 0..NTESTS {
x = (x.sin() + 1.0) * 0.5 / 1.1;
}
// Prevent compiler from optimizing out the loop
if x.is_nan() {
println!("This should never happen");
}
NTESTS * OPERATIONS_PER_ITERATION
})
.sum::<u64>() as f64;
let start = Instant::now();

let duration = start.elapsed();
let total_flops: u64 = (0..num_cores)
.into_par_iter()
.map(|_| {
let mut x: f64 = 1.0;
for _ in 0..NTESTS {
x = black_box((x.sin() + 1.0) * 0.5 / 1.1);
}
NTESTS * OPERATIONS_PER_ITERATION
})
.sum();

total_flops as f64 / start.elapsed().as_secs_f64()
})
.sum::<f64>()
/ NUM_REPEATS as f64; // Average the FLOPS over all repeats

let flops = total_flops / duration.as_secs_f64();
flops as f32
(avg_flops / 1e9) as f32 // Convert to GFLOP/s and cast to f32
}