Compare commits

...

7 Commits

Author SHA1 Message Date
Aaron Fenyes
8cb73f88d0 Rust native benchmark: drop unused dependencies
Also, drop the commented-out beginnings of a `plotters-gtk4` version. I
can't use `plotters-gtk4` on my machine because it requires GTK 4.14 or
higher, and Ubuntu 22.04 is still at GTK 4.6.
2024-08-19 13:12:50 -07:00
Aaron Fenyes
eeb0f00534 Rust benchmark: write native version 2024-08-19 12:20:56 -07:00
Aaron Fenyes
8ce3e251d7 Drop unused dependency and use declaration 2024-08-13 14:00:02 -07:00
Aaron Fenyes
543f348cd8 Rust benchmark: drop old debug code 2024-08-13 13:50:58 -07:00
Aaron Fenyes
0abcb995b5 Rust benchmark: rename package 2024-08-13 13:40:33 -07:00
Aaron Fenyes
d864ab5abe Drop second attempt at static matrices
I couldn't get this one working, and the first attempt seems fine.
2024-08-13 13:34:26 -07:00
Aaron Fenyes
fb51e00503 Remove unnecessary type annotations
These annotations are only needed for statically sized matrices.
2024-08-13 13:14:54 -07:00
7 changed files with 233 additions and 66 deletions

View File

@ -0,0 +1,3 @@
target/*
dist/*
Cargo.lock

View File

@ -0,0 +1,16 @@
[package]
name = "rust-benchmark-native"
version = "0.1.0"
authors = ["Aaron"]
edition = "2021"
[dependencies]
cairo-rs = "0.20.1"
gtk = { package = "gtk4", version = "0.9.0" }
nalgebra = "0.33.0"
plotters = "0.3.6"
plotters-cairo = "0.7.0"
[profile.release]
opt-level = "s" # optimize for small code size
debug = true # include debug symbols

View File

@ -0,0 +1,105 @@
use nalgebra::{*, allocator::Allocator};
use std::f64::consts::{PI, E};
/* dynamic matrices */
pub fn rand_eigval_series(dim: usize, time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>> {
// initialize the random matrix
let mut rand_mat = DMatrix::<f64>::from_fn(dim, dim, |j, k| {
let n = j*dim + k;
E*((n*n) as f64) % 2.0 - 1.0
}) * (3.0 / (dim as f64)).sqrt();
// initialize the rotation step
let mut rot_step = DMatrix::<f64>::identity(dim, dim);
let max_freq = 4;
for n in (0..dim).step_by(2) {
let ang = PI * ((n % max_freq) as f64) / (time_res as f64);
let ang_cos = ang.cos();
let ang_sin = ang.sin();
rot_step[(n, n)] = ang_cos;
rot_step[(n+1, n)] = ang_sin;
rot_step[(n, n+1)] = -ang_sin;
rot_step[(n+1, n+1)] = ang_cos;
}
// find the eigenvalues
let mut eigval_series = Vec::<OVector<Complex<f64>, Dyn>>::with_capacity(time_res);
eigval_series.push(rand_mat.complex_eigenvalues());
for _ in 1..time_res {
rand_mat = &rot_step * rand_mat;
eigval_series.push(rand_mat.complex_eigenvalues());
}
eigval_series
}
/* dynamic single float matrices */
/*pub fn rand_eigval_series(dim: usize, time_res: usize) -> Vec<OVector<Complex<f32>, Dyn>> {
// initialize the random matrix
let mut rand_mat = DMatrix::<f32>::from_fn(dim, dim, |j, k| {
let n = j*dim + k;
(E as f32)*((n*n) as f32) % 2.0_f32 - 1.0_f32
}) * (3.0_f32 / (dim as f32)).sqrt();
// initialize the rotation step
let mut rot_step = DMatrix::<f32>::identity(dim, dim);
let max_freq = 4;
for n in (0..dim).step_by(2) {
let ang = (PI as f32) * ((n % max_freq) as f32) / (time_res as f32);
let ang_cos = ang.cos();
let ang_sin = ang.sin();
rot_step[(n, n)] = ang_cos;
rot_step[(n+1, n)] = ang_sin;
rot_step[(n, n+1)] = -ang_sin;
rot_step[(n+1, n+1)] = ang_cos;
}
// find the eigenvalues
let mut eigval_series = Vec::<OVector<Complex<f32>, Dyn>>::with_capacity(time_res);
eigval_series.push(rand_mat.complex_eigenvalues());
for _ in 1..time_res {
rand_mat = &rot_step * rand_mat;
eigval_series.push(rand_mat.complex_eigenvalues());
}
eigval_series
}*/
/* static matrices. should only be used when the dimension is really small */
/*pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f64>, N>>
where
N: ToTypenum + DimName + DimSub<U1>,
DefaultAllocator:
Allocator<N> +
Allocator<N, N> +
Allocator<<N as DimSub<U1>>::Output> +
Allocator<N, <N as DimSub<U1>>::Output>
{
// initialize the random matrix
let dim = N::try_to_usize().unwrap();
let mut rand_mat = OMatrix::<f64, N, N>::from_fn(|j, k| {
let n = j*dim + k;
E*((n*n) as f64) % 2.0 - 1.0
}) * (3.0 / (dim as f64)).sqrt();
/*let mut rand_mat = OMatrix::<f64, N, N>::identity();*/
// initialize the rotation step
let mut rot_step = OMatrix::<f64, N, N>::identity();
let max_freq = 4;
for n in (0..dim).step_by(2) {
let ang = PI * ((n % max_freq) as f64) / (time_res as f64);
let ang_cos = ang.cos();
let ang_sin = ang.sin();
rot_step[(n, n)] = ang_cos;
rot_step[(n+1, n)] = ang_sin;
rot_step[(n, n+1)] = -ang_sin;
rot_step[(n+1, n+1)] = ang_cos;
}
// find the eigenvalues
let mut eigval_series = Vec::<OVector<Complex<f64>, N>>::with_capacity(time_res);
eigval_series.push(rand_mat.complex_eigenvalues());
for _ in 1..time_res {
rand_mat = &rot_step * rand_mat;
eigval_series.push(rand_mat.complex_eigenvalues());
}
eigval_series
}*/

View File

@ -0,0 +1,104 @@
// based on Olivier Pelhatre's GTK 3 example, ported to GTK 4
//
// https://github.com/Ouam74/RUST_Real-time_plots_using_GTK-rs_and_Plotters-rs
//
// a self-contained component might draw on the example below, by StackOverflow
// user Nicolas
//
// https://stackoverflow.com/a/76548487
//
// here's a crash course in `plotters`
//
// https://plotters-rs.github.io/book/basic/basic_data_plotting.html
//
extern crate cairo;
use plotters::prelude::*;
use plotters_cairo::CairoBackend;
use gtk::{
glib,
prelude::*,
Adjustment,
Align,
Application,
ApplicationWindow,
Box,
DrawingArea,
Label,
Orientation,
Scale
};
use std::time::Instant;
mod engine;
fn main() -> glib::ExitCode {
let app = Application::builder()
.application_id("org.studioinfinity.rust-benchmark-native")
.build();
app.connect_activate(|app| {
const TIME_RES: usize = 100;
let start_time = Instant::now();
let eigval_series = engine::rand_eigval_series(60, TIME_RES);
let run_time = start_time.elapsed().as_millis();
// application state
let time_step = Adjustment::new(0.0, 0.0, TIME_RES as f64, 1.0, 0.0, 0.0);
// create the window.
let window = ApplicationWindow::builder()
.application(app)
.title("The circular law")
.build();
// create a vertical box
let container = Box::new(Orientation::Vertical, 5);
window.set_child(Some(&container));
// create the run time readout
let run_time_readout = Label::builder()
.margin_top(5)
.margin_start(10)
.halign(Align::Start)
.label(glib::gformat!("{} ms", run_time))
.build();
container.append(&run_time_readout);
// set up the drawing area
let drawing_area = DrawingArea::builder()
.content_width(600)
.content_height(600)
.build();
let time_step_for_draw = time_step.clone();
let draw_eigvals = move |_: &DrawingArea, context: &cairo::Context, width: i32, height: i32| {
let root = CairoBackend::new(&context, (width as u32, height as u32)).unwrap().into_drawing_area();
let _ = root.fill(&BLACK);
const R_DISP: f64 = 1.5;
let mut chart = ChartBuilder::on(&root)
.build_cartesian_2d(-R_DISP..R_DISP, -R_DISP..R_DISP)
.unwrap();
let time_step_val = (time_step_for_draw.value() as usize).min(TIME_RES-1);
let eigval_iter = eigval_series[time_step_val].iter();
let _ = chart.draw_series(
eigval_iter.map(|z| Circle::new((z.re, z.im), 3, WHITE.filled()))
);
let _ = root.present();
};
DrawingAreaExtManual::set_draw_func(&drawing_area, draw_eigvals);
container.append(&drawing_area);
// set up the time step slider
let time_step_scale = Scale::new(Orientation::Horizontal, Some(&time_step));
time_step_scale.connect_value_changed(move |_: &Scale| {
drawing_area.queue_draw();
});
container.append(&time_step_scale);
// show the window
window.present();
});
app.run()
}

View File

@ -1,5 +1,5 @@
[package]
name = "sycamore-trial"
name = "rust-benchmark"
version = "0.1.0"
authors = ["Aaron"]
edition = "2021"
@ -10,7 +10,6 @@ default = ["console_error_panic_hook"]
[dependencies]
nalgebra = "0.33.0"
sycamore = "0.9.0-beta.2"
typenum = "1.17.0"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires

View File

@ -1,20 +1,9 @@
use nalgebra::{*, allocator::Allocator};
use std::f64::consts::{PI, E};
/*use std::ops::Sub;*/
/*use typenum::{B1, UInt, UTerm};*/
/* dynamic matrices */
pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>>
where
N: ToTypenum + DimName + DimSub<U1>,
DefaultAllocator:
Allocator<N> +
Allocator<N, N> +
Allocator<<N as DimSub<U1>>::Output> +
Allocator<N, <N as DimSub<U1>>::Output>
{
pub fn rand_eigval_series(dim: usize, time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>> {
// initialize the random matrix
let dim = N::try_to_usize().unwrap();
let mut rand_mat = DMatrix::<f64>::from_fn(dim, dim, |j, k| {
let n = j*dim + k;
E*((n*n) as f64) % 2.0 - 1.0
@ -44,17 +33,8 @@ pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>>
}
/* dynamic single float matrices */
/*pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f32>, Dyn>>
where
N: ToTypenum + DimName + DimSub<U1>,
DefaultAllocator:
Allocator<N> +
Allocator<N, N> +
Allocator<<N as DimSub<U1>>::Output> +
Allocator<N, <N as DimSub<U1>>::Output>
{
/*pub fn rand_eigval_series(dim: usize, time_res: usize) -> Vec<OVector<Complex<f32>, Dyn>> {
// initialize the random matrix
let dim = N::try_to_usize().unwrap();
let mut rand_mat = DMatrix::<f32>::from_fn(dim, dim, |j, k| {
let n = j*dim + k;
(E as f32)*((n*n) as f32) % 2.0_f32 - 1.0_f32
@ -99,7 +79,6 @@ pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>>
let n = j*dim + k;
E*((n*n) as f64) % 2.0 - 1.0
}) * (3.0 / (dim as f64)).sqrt();
/*let mut rand_mat = OMatrix::<f64, N, N>::identity();*/
// initialize the rotation step
let mut rot_step = OMatrix::<f64, N, N>::identity();
@ -123,42 +102,3 @@ pub fn rand_eigval_series<N>(time_res: usize) -> Vec<OVector<Complex<f64>, Dyn>>
}
eigval_series
}*/
/* another attempt at static matrices. i couldn't get the types to work out */
/*pub fn random_eigval_series<const N: usize>(time_res: usize) -> Vec<OVector<Complex<f64>, Const<N>>>
where
Const<N>: ToTypenum,
<Const<N> as ToTypenum>::Typenum: Sub<UInt<UTerm, B1>>,
<<Const<N> as ToTypenum>::Typenum as Sub<UInt<UTerm, B1>>>::Output: ToConst
{
// initialize the random matrix
/*let mut rand_mat = SMatrix::<f64, N, N>::zeros();
for n in 0..N*N {
rand_mat[n] = E*((n*n) as f64) % 2.0 - 1.0;
}*/
let rand_mat = OMatrix::<f64, Const<N>, Const<N>>::from_fn(|j, k| {
let n = j*N + k;
E*((n*n) as f64) % 2.0 - 1.0
});
// initialize the rotation step
let mut rot_step = OMatrix::<f64, Const<N>, Const<N>>::identity();
let max_freq = 4;
for n in (0..N).step_by(2) {
let ang = PI * ((n % max_freq) as f64) / (time_res as f64);
let ang_cos = ang.cos();
let ang_sin = ang.sin();
rot_step[(n, n)] = ang_cos;
rot_step[(n+1, n)] = ang_sin;
rot_step[(n, n+1)] = -ang_sin;
rot_step[(n+1, n+1)] = ang_cos;
}
// find the eigenvalues
let mut eigvals = Vec::<OVector<Complex<f64>, Const<N>>>::with_capacity(time_res);
unsafe { eigvals.set_len(time_res); }
for t in 0..time_res {
eigvals[t] = rand_mat.complex_eigenvalues();
}
eigvals
}*/

View File

@ -1,4 +1,3 @@
use nalgebra::*;
use std::f64::consts::PI as PI;
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
use web_sys::window;
@ -19,7 +18,8 @@ fn main() {
on_mount(move || {
let performance = window().unwrap().performance().unwrap();
let start_time = performance.now();
let eigval_series = engine::rand_eigval_series::<U60>(time_res);
/*let eigval_series = engine::rand_eigval_series::<U60>(time_res);*/
let eigval_series = engine::rand_eigval_series(60, time_res);
let run_time = performance.now() - start_time;
run_time_report.set(run_time);