Skip to content

Commit f5a567f

Browse files
committed
feat: rework demo
Signed-off-by: Martin Kröning <[email protected]>
1 parent 87a5b8c commit f5a567f

File tree

8 files changed

+245
-326
lines changed

8 files changed

+245
-326
lines changed

examples/demo/src/fs.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::path::Path;
2+
use std::{fs, io};
3+
4+
pub fn read_dir(path: &str) -> io::Result<()> {
5+
eprintln!();
6+
7+
assert!(Path::new(path).is_dir());
8+
eprintln!("Reading {path:?} directory entries");
9+
let entries = fs::read_dir(path)?.collect::<Result<Vec<_>, _>>()?;
10+
11+
assert!(!entries.is_empty());
12+
for entry in entries {
13+
let path = entry.path();
14+
eprintln!("Found {path:?}");
15+
}
16+
17+
Ok(())
18+
}
19+
20+
pub fn read_version() -> io::Result<()> {
21+
eprintln!();
22+
let path = "/proc/version";
23+
24+
eprint!("{path} contains");
25+
let version = fs::read_to_string(path).unwrap();
26+
eprintln!(" {version:?}");
27+
28+
Ok(())
29+
}
30+
31+
fn test_file(path: &str) -> io::Result<()> {
32+
let contents = "Hello, world!";
33+
34+
eprint!("{path:15} : writing");
35+
fs::write(path, contents)?;
36+
37+
eprint!(", reading");
38+
let read = fs::read_to_string(path)?;
39+
assert_eq!(contents, read);
40+
41+
// FIXME: this fails on Uhyve
42+
// eprintln!(", deleting");
43+
// fs::remove_file(path)?;
44+
45+
Ok(())
46+
}
47+
48+
pub fn file() -> io::Result<()> {
49+
eprintln!();
50+
test_file("/tmp/hello.txt")?;
51+
if cfg!(target_os = "hermit") && cfg!(feature = "fs") {
52+
test_file("/root/hello.txt")?;
53+
}
54+
Ok(())
55+
}
56+
57+
pub fn dir() -> io::Result<()> {
58+
read_dir("/proc")?;
59+
// FIXME: this fails on both QEMU and Uhyve
60+
// read_dir("/root")?;
61+
Ok(())
62+
}
63+
64+
pub fn fs() -> io::Result<()> {
65+
read_version()?;
66+
file()?;
67+
dir()?;
68+
Ok(())
69+
}

examples/demo/src/laplace.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
use std::time::Instant;
2-
use std::{thread, vec};
2+
use std::vec;
33

44
use rayon::prelude::*;
55

6-
pub fn laplace(size_x: usize, size_y: usize) -> Result<(), ()> {
7-
let ncpus = thread::available_parallelism().unwrap().get();
8-
let pool = rayon::ThreadPoolBuilder::new()
9-
.num_threads(ncpus)
10-
.build()
11-
.unwrap();
12-
let matrix = matrix_setup(size_x, size_y);
6+
const SIZE: usize = if cfg!(debug_assertions) { 16 } else { 64 };
137

8+
pub fn laplace() {
9+
eprintln!();
10+
11+
let matrix = matrix_setup(SIZE, SIZE);
12+
13+
eprintln!("Laplace iterations");
1414
let now = Instant::now();
15-
let (iterations, res) = pool.install(|| compute(matrix, size_x, size_y));
16-
println!(
17-
"Time to solve {} s, iterations {}, residuum {}",
18-
now.elapsed().as_secs_f64(),
19-
iterations,
20-
res
21-
);
22-
23-
if res < 0.01 {
24-
Ok(())
25-
} else {
26-
Err(())
27-
}
15+
let (i, residual) = compute(matrix, SIZE, SIZE);
16+
let elapsed = now.elapsed();
17+
eprintln!("{i} iterations: {elapsed:?} (residual: {residual})");
18+
19+
assert!(residual < 0.001);
2820
}
2921

3022
fn matrix_setup(size_x: usize, size_y: usize) -> vec::Vec<vec::Vec<f64>> {
@@ -97,7 +89,6 @@ fn iteration(cur: &[f64], next: &mut [f64], size_x: usize, size_y: usize) {
9789
});
9890
}
9991

100-
#[inline(never)]
10192
pub fn compute(mut matrix: vec::Vec<vec::Vec<f64>>, size_x: usize, size_y: usize) -> (usize, f64) {
10293
let mut counter = 0;
10394

examples/demo/src/main.rs

Lines changed: 49 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,60 @@
1+
use std::{env, f64, hint, io};
2+
13
#[cfg(target_os = "hermit")]
24
use hermit as _;
35

6+
mod fs;
47
mod laplace;
58
mod matmul;
6-
mod tests;
7-
mod thread_local;
9+
mod pi;
10+
mod thread;
11+
12+
fn main() -> io::Result<()> {
13+
hello();
14+
print_env();
15+
arithmetic();
16+
thread::sleep();
17+
thread::spawn()?;
18+
fs::fs()?;
19+
pi::pi();
20+
matmul::matmul();
21+
laplace::laplace();
22+
Ok(())
23+
}
24+
25+
pub fn hello() {
26+
eprintln!();
27+
eprintln!("Hello, Hermit! 🦀");
28+
eprintln!("Hello, world!");
29+
eprintln!("Привет, мир!");
30+
eprintln!("こんにちは世界!");
31+
eprintln!("你好世界!");
32+
eprintln!("สวัสดีชาวโลก!");
33+
eprintln!("Chào thế giới!");
34+
}
835

9-
use tests::*;
36+
pub fn print_env() {
37+
eprintln!();
38+
eprintln!("Arguments:");
39+
for argument in env::args() {
40+
eprintln!("{argument}");
41+
}
1042

11-
fn test_result<T>(result: Result<(), T>) -> &'static str {
12-
match result {
13-
Ok(_) => "ok",
14-
Err(_) => "failed!",
43+
eprintln!();
44+
eprintln!("Environment variables:");
45+
for (key, value) in env::vars() {
46+
eprintln!("{key}: {value}");
1547
}
1648
}
1749

18-
fn main() {
19-
println!("Test {} ... {}", stringify!(hello), test_result(hello()));
20-
println!(
21-
"Test {} ... {}",
22-
stringify!(sleep),
23-
test_result(test_sleep())
24-
);
25-
println!(
26-
"Test {} ... {}",
27-
stringify!(test_thread_local),
28-
test_result(thread_local::test_thread_local())
29-
);
30-
println!(
31-
"Test {} ... {}",
32-
stringify!(arithmetic),
33-
test_result(arithmetic())
34-
);
35-
println!(
36-
"Test {} ... {}",
37-
stringify!(print_argv),
38-
test_result(print_argv())
39-
);
40-
println!(
41-
"Test {} ... {}",
42-
stringify!(print_env),
43-
test_result(print_env())
44-
);
45-
println!(
46-
"Test {} ... {}",
47-
stringify!(read_file),
48-
test_result(read_file())
49-
);
50-
println!(
51-
"Test {} ... {}",
52-
stringify!(read_dir),
53-
test_result(read_dir())
54-
);
55-
println!(
56-
"Test {} ... {}",
57-
stringify!(create_file),
58-
test_result(create_file())
59-
);
60-
println!(
61-
"Test {} ... {}",
62-
stringify!(threading),
63-
test_result(threading())
64-
);
65-
println!(
66-
"Test {} ... {}",
67-
stringify!(pi_sequential),
68-
test_result(pi_sequential(5000000))
69-
);
70-
println!(
71-
"Test {} ... {}",
72-
stringify!(pi_parallel),
73-
test_result(pi_parallel(5000000))
74-
);
75-
println!(
76-
"Test {} ... {}",
77-
stringify!(laplace),
78-
test_result(laplace::laplace(128, 128))
79-
);
80-
println!(
81-
"Test {} ... {}",
82-
stringify!(test_matmul_strassen),
83-
test_result(matmul::test_matmul_strassen())
84-
);
85-
println!(
86-
"Test {} ... {}",
87-
stringify!(thread_creation),
88-
test_result(thread_creation())
89-
);
50+
pub fn arithmetic() {
51+
eprintln!();
52+
53+
let x = hint::black_box(f64::consts::PI) * 2.0;
54+
let y: f64 = hint::black_box(x).exp();
55+
let z: f64 = hint::black_box(y).ln();
56+
57+
eprintln!("x = {x}");
58+
eprintln!("e^x = {y}");
59+
eprintln!("ln(e^x) = {z}");
9060
}

examples/demo/src/matmul.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn timed_matmul<F: FnOnce(&[f32], &[f32], &mut [f32])>(size: usize, f: F, name:
372372
f(&a[..], &b[..], &mut dest[..]);
373373
let dur = Instant::now() - start;
374374
let nanos = u64::from(dur.subsec_nanos()) + dur.as_secs() * 1_000_000_000u64;
375-
println!(
375+
eprintln!(
376376
"{}:\t{}x{} matrix: {} s",
377377
name,
378378
size,
@@ -384,7 +384,9 @@ fn timed_matmul<F: FnOnce(&[f32], &[f32], &mut [f32])>(size: usize, f: F, name:
384384

385385
const SIZE: usize = if cfg!(debug_assertions) { 64 } else { 256 };
386386

387-
pub fn test_matmul_strassen() -> Result<(), ()> {
387+
pub fn matmul() {
388+
eprintln!();
389+
eprintln!("Matrix multiplication");
388390
if SIZE <= 1024 {
389391
// Crappy algorithm takes several minutes on larger inputs.
390392
timed_matmul(SIZE, seq_matmul, "seq row-major");
@@ -397,7 +399,5 @@ pub fn test_matmul_strassen() -> Result<(), ()> {
397399
let par = timed_matmul(SIZE, matmulz, "par z-order");
398400
timed_matmul(SIZE, matmul_strassen, "par strassen");
399401
let speedup = seq as f64 / par as f64;
400-
println!("speedup: {:.2}x", speedup);
401-
402-
Ok(())
402+
eprintln!("speedup: {:.2}x", speedup);
403403
}

examples/demo/src/pi.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::time::Instant;
2+
use std::{f64, hint};
3+
4+
use rayon::prelude::*;
5+
6+
const STEPS: u64 = if cfg!(debug_assertions) {
7+
50_000
8+
} else {
9+
5_000_000
10+
};
11+
const STEP_SIZE: f64 = 1.0 / STEPS as f64;
12+
13+
#[derive(Debug)]
14+
pub enum Mode {
15+
Sequential,
16+
Parallel,
17+
}
18+
19+
fn calculate_pi(mode: Mode) {
20+
eprintln!();
21+
eprint!("Calculating Pi {:14}", format!("({mode:?}): "));
22+
23+
let steps = hint::black_box(STEPS);
24+
let map_step = |i| {
25+
let x = (i as f64 + 0.5) * STEP_SIZE;
26+
4.0 / (1.0 + x * x)
27+
};
28+
29+
let now = Instant::now();
30+
let sum = match mode {
31+
Mode::Sequential => (0..steps).map(map_step).sum::<f64>(),
32+
Mode::Parallel => (0..steps).into_par_iter().map(map_step).sum::<f64>(),
33+
};
34+
let mypi = sum * STEP_SIZE;
35+
let elapsed = now.elapsed();
36+
37+
eprintln!("{elapsed:?}");
38+
39+
assert!((mypi - f64::consts::PI).abs() < 1e-10);
40+
}
41+
42+
pub fn pi() {
43+
eprintln!();
44+
calculate_pi(Mode::Sequential);
45+
calculate_pi(Mode::Parallel);
46+
}

0 commit comments

Comments
 (0)