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 1 commit
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
49 changes: 28 additions & 21 deletions clients/cli/src/flops.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
// 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; // Число повторов для усреднения

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()
pub fn measure_flops() -> f64 {
let num_threads = rayon::current_num_threads();

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
let start = Instant::now();

let total_flops: u64 = (0..num_threads)
.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();

let duration = start.elapsed().as_secs_f64();
total_flops as f64 / duration
})
.sum::<u64>() as f64;
.sum::<f64>()
/ NUM_REPEATS as f64; // Усреднение результатов

let duration = start.elapsed();
avg_flops
}

let flops = total_flops / duration.as_secs_f64();
flops as f32
fn main() {
let flops = measure_flops();
println!("CPU FLOPS: {:.3} GFLOP/s", flops / 1e9);
}