diff --git a/bancs/Cargo.lock b/bancs/Cargo.lock new file mode 100644 index 0000000..ba50ed5 --- /dev/null +++ b/bancs/Cargo.lock @@ -0,0 +1,126 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bancs" +version = "0.1.0" +dependencies = [ + "biomes", + "rayon", + "rt-core", + "rt-live", + "rt-tiles", +] + +[[package]] +name = "biomes" +version = "0.1.0" +dependencies = [ + "physique", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "crc32fast" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "memmap2" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" +dependencies = [ + "libc", +] + +[[package]] +name = "physique" +version = "0.1.0" + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "rt-core" +version = "0.1.0" + +[[package]] +name = "rt-live" +version = "0.1.0" +dependencies = [ + "physique", +] + +[[package]] +name = "rt-tiles" +version = "0.1.0" +dependencies = [ + "crc32fast", + "memmap2", + "rt-core", +] diff --git a/bancs/Cargo.toml b/bancs/Cargo.toml new file mode 100644 index 0000000..cbda50b --- /dev/null +++ b/bancs/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "bancs" +version = "0.1.0" +edition = "2021" +license = "GPL-3.0-only" +description = "Mesures de vitesse du moteur (lecture mmap, remplissage de chunk, delta rt-live), mono-thread et rayon" + +[dependencies] +rt-core = { path = "../relief/rt-core" } +rt-tiles = { path = "../relief/rt-tiles" } +biomes = { path = "../biomes" } +rt-live = { path = "../rt-live" } +rayon = "1.10" + +[profile.release] +debug = false + +[workspace] diff --git a/bancs/src/main.rs b/bancs/src/main.rs new file mode 100644 index 0000000..13ce88b --- /dev/null +++ b/bancs/src/main.rs @@ -0,0 +1,263 @@ +//! Bancs de vitesse (rapport 13). +//! Usage : bancs [repetitions] +use biomes::{arbre_en, biome, palette, Colonne as BCol, Essence as BEss}; +use rayon::prelude::*; +use rt_core::TileId; +use rt_live::*; +use rt_tiles::TileStore; +use std::path::PathBuf; +use std::time::Instant; + +const MIN_Y: i32 = -1024; +const H: usize = 2400; +const SEED: u64 = 11; + +#[repr(C)] +struct Pmc { + cb: u32, + pf: u32, + peak_ws: usize, + ws: usize, + a: usize, + b: usize, + c: usize, + d: usize, + pagefile: usize, + peak_pagefile: usize, +} +extern "system" { + fn GetCurrentProcess() -> isize; + fn K32GetProcessMemoryInfo(h: isize, p: *mut Pmc, cb: u32) -> i32; +} +fn mem_mo() -> (f64, f64) { + unsafe { + let mut p: Pmc = std::mem::zeroed(); + p.cb = std::mem::size_of::() as u32; + K32GetProcessMemoryInfo(GetCurrentProcess(), &mut p, p.cb); + (p.ws as f64 / 1048576.0, p.peak_ws as f64 / 1048576.0) + } +} + +fn colonne(store: &TileStore, x: i32, z: i32) -> Option<(BCol, i32)> { + let id = TileId::of_block(x, z); + let t = store.get(id).ok()?; + let s = t.sample(x - id.tx * 512, z - id.tz * 512); + if s.ground_dm == 0 { + return None; + } + let o = t.origin(); + let alt = s.ground_dm as f32 / 10.0 + o.alt_zero_dm() as f32; + let c = BCol { alt_m: alt, pente_deg: s.slope_deg as f32, aspect_deg: s.aspect as f32 * 360.0 / 256.0, sol: s.landcover, eau: (s.water_dm > 0) as u8, route: 0, bati: 0 }; + Some((c, s.ground_y(&o))) +} + +/// Remplit un chunk complet 16x16x2400 : relief, couches de surface, eau, biome, arbres (tronc + couronne). +fn remplir(store: &TileStore, cx: i32, cz: i32, buf: &mut [u16]) -> (usize, ChunkEntree) { + buf.fill(0); + let mut entree = ChunkEntree { cx, cz, colonnes: Vec::with_capacity(256), feuilles: Vec::new() }; + let mut n = 0usize; + let mut biomes_sum = 0u32; + let idx = |lx: i32, y: i32, lz: i32| ((y - MIN_Y) as usize * 16 + lz as usize) * 16 + lx as usize; + for lz in 0..16 { + for lx in 0..16 { + let (x, z) = (cx * 16 + lx, cz * 16 + lz); + let Some((c, sol)) = colonne(store, x, z) else { + entree.colonnes.push(rt_live::Colonne { sol_y: MIN_Y, surface: Surface::Roche, pente_deg: 0.0, alt_m: 0.0, eau_y: MIN_Y }); + continue; + }; + biomes_sum += biome(&c, x, z, SEED) as u32; + let p = palette(&c, x, z, SEED); + let (haut, sous) = (1 + p.haut.len() as u16, 1 + p.sous.len() as u16); + let ep = p.epaisseur as i32; + let hs = ((sol - MIN_Y) as usize).min(H - 1); + let base = lz as usize * 16 + lx as usize; + buf[base] = 1000; + for y in 1..=hs { + let yy = y as i32 + MIN_Y; + buf[y * 256 + base] = if yy == sol { haut } else if yy >= sol - ep { sous } else { 1 }; + } + n += hs + 1; + let mut eau_y = sol; + if c.eau == 1 { + for k in 1..=2usize { + if hs + k < H { + buf[(hs + k) * 256 + base] = 2000; + n += 1; + } + } + eau_y = sol + 2; + } + entree.colonnes.push(rt_live::Colonne { + sol_y: sol, + surface: if c.eau == 1 { Surface::Eau { lac: false } } else { Surface::Sol }, + pente_deg: c.pente_deg, + alt_m: c.alt_m, + eau_y, + }); + if let Some(a) = arbre_en(&c, x, z, SEED) { + let h = a.hauteur as usize; + for k in 1..=h.min(H - hs - 1) { + buf[(hs + k) * 256 + base] = 3000; + n += 1; + } + for dy in 0..4i32 { + let r = if dy == 3 { 1 } else { 2 }; + let y = sol + h as i32 - 2 + dy; + if y - MIN_Y >= H as i32 { + continue; + } + for dz in -r..=r { + for dx in -r..=r { + let (ax, az) = (lx + dx, lz + dz); + if (0..16).contains(&ax) && (0..16).contains(&az) { + let i = idx(ax, y, az); + if buf[i] == 0 { + buf[i] = 4000; + n += 1; + if matches!(a.essence, BEss::Hetre | BEss::Chene) { + entree.feuilles.push(FeuilleEntree { x: x + dx, y, z: z + dz, essence: rt_live::Essence::Hetre, alt_m: c.alt_m }); + } + } + } + } + } + } + } + } + } + std::hint::black_box(biomes_sum); + (n, entree) +} + +fn pool(n: usize) -> rayon::ThreadPool { + rayon::ThreadPoolBuilder::new().num_threads(n).build().unwrap() +} + +fn manteau(store: &TileStore, x0: i32, z0: i32, w: usize, h: usize) -> Manteau { + Manteau::nouveau(Params::default(), 32, x0, z0, w, h, |ix, iz| { + let (x, z) = (x0 + ix as i32 * 32 + 16, z0 + iz as i32 * 32 + 16); + match colonne(store, x, z) { + Some((c, _)) => Cellule { alt_m: c.alt_m, pente_deg: c.pente_deg, aspect_deg: c.aspect_deg }, + None => Cellule { alt_m: 0.0, pente_deg: 0.0, aspect_deg: 0.0 }, + } + }) +} + +fn main() { + let a: Vec = std::env::args().collect(); + let dir = PathBuf::from(&a[1]); + let (tx0, tz0, ntx, ntz): (i32, i32, i32, i32) = (a[2].parse().unwrap(), a[3].parse().unwrap(), a[4].parse().unwrap(), a[5].parse().unwrap()); + let reps: usize = a.get(6).and_then(|s| s.parse().ok()).unwrap_or(3); + let store = TileStore::new(dir.clone()); + let cpu = std::thread::available_parallelism().unwrap().get(); + println!("# region tuiles tx {tx0}..{} tz {tz0}..{} ; {} chunks ; {cpu} threads logiques", tx0 + ntx - 1, tz0 + ntz - 1, ntx * ntz * 1024); + let (x0, z0) = (tx0 * 512, tz0 * 512); + let (bw, bh) = ((ntx * 512) as usize, (ntz * 512) as usize); + let m0 = mem_mo(); + + let t = Instant::now(); + for tx in tx0..tx0 + ntx { + for tz in tz0..tz0 + ntz { + let _ = store.get(TileId { tx, tz }).map(|t| t.origin()); + } + } + println!("ouverture mmap {} tuiles : {:.2} ms", ntx * ntz, t.elapsed().as_secs_f64() * 1e3); + + for &th in &[1usize, 4, 8] { + let th = th.min(cpu); + let p = pool(th); + let mut best = f64::MAX; + let mut sum = 0i64; + for _ in 0..reps { + let t = Instant::now(); + sum = p.install(|| { + (0..bh as i32) + .into_par_iter() + .map(|dz| { + let mut s = 0i64; + for dx in 0..bw as i32 { + s += store.hauteur_colonne(x0 + dx, z0 + dz).unwrap_or(0) as i64; + } + s + }) + .sum() + }); + best = best.min(t.elapsed().as_secs_f64()); + } + let cols = (bw * bh) as f64; + println!("hauteur_colonne {th:>2} thr : {:.1} Mcol/s ({:.1} ns/col, {:.0} us/chunk-equiv) [somme {sum}]", cols / best / 1e6, best * 1e9 / cols, best * 1e6 / (cols / 256.0)); + } + + let chunks: Vec<(i32, i32)> = (0..ntz * 32).flat_map(|dz| (0..ntx * 32).map(move |dx| (tx0 * 32 + dx, tz0 * 32 + dz))).collect(); + let nch = chunks.len(); + let mut threads = vec![1usize, 2, 4, 8, 10, 16]; + threads.retain(|&t| t <= cpu); + let mut base_cps = 0.0; + for &th in &threads { + let p = pool(th); + let mut best = f64::MAX; + let mut tot = 0usize; + for _ in 0..reps { + let t = Instant::now(); + tot = p.install(|| chunks.par_iter().map_init(|| vec![0u16; 16 * 16 * H], |buf, &(cx, cz)| remplir(&store, cx, cz, buf).0).sum()); + best = best.min(t.elapsed().as_secs_f64()); + } + let cps = nch as f64 / best; + if th == 1 { + base_cps = cps; + } + println!("remplissage chunk {th:>2} thr : {cps:.0} chunks/s ({:.0} us/chunk par thread, accel x{:.2}) [{} blocs non air/chunk]", th as f64 * 1e6 / cps, cps / base_cps, tot / nch); + } + let (ws, peak) = mem_mo(); + println!("memoire : RSS {ws:.0} Mo, pic {peak:.0} Mo (avant : {:.0} Mo) ; tampon chunk 1,2 Mo/thread", m0.0); + + let (mw, mh) = (bw / 32, bh / 32); + let tm = Instant::now(); + let man = manteau(&store, x0, z0, mw, mh); + println!("construction manteau {mw}x{mh} cellules : {:.1} ms", tm.elapsed().as_secs_f64() * 1e3); + let mut couche = CoucheLive::new(Mode::Climat, man, Climat::nouveau(7, 1.0), None); + let ts = Instant::now(); + couche.avancer_a(Date::new(2027, 1, 15)); + println!("simulation neige 1er aout -> 15 janv ({} cellules) : {:.1} ms", mw * mh, ts.elapsed().as_secs_f64() * 1e3); + let entrees: Vec = pool(cpu.min(8)).install(|| chunks.par_iter().map_init(|| vec![0u16; 16 * 16 * H], |buf, &(cx, cz)| remplir(&store, cx, cz, buf).1).collect()); + let nf: usize = entrees.iter().map(|e| e.feuilles.len()).sum(); + let masque = MasqueJoueur::default(); + for &th in &threads { + let p = pool(th); + let mut best = f64::MAX; + let mut nm = 0usize; + for _ in 0..reps { + let t = Instant::now(); + nm = p.install(|| { + entrees + .par_iter() + .map(|e| { + let actuel = |_x: i32, _y: i32, _z: i32| Bloc::Air; + couche.delta_chunk(e, &masque, &actuel).len() + }) + .sum() + }); + best = best.min(t.elapsed().as_secs_f64()); + } + let cps = nch as f64 / best; + println!("delta rt-live {th:>2} thr : {cps:.0} chunks/s ({:.1} us/chunk par thread) [{} modifs au total, {} feuilles en entree]", th as f64 * 1e6 / cps, nm, nf); + } + + let th = (cpu / 2).clamp(2, 16); + let p = pool(th); + let t = Instant::now(); + let n: usize = p.install(|| { + chunks + .par_iter() + .map_init(|| vec![0u16; 16 * 16 * H], |buf, &(cx, cz)| { + let (_, e) = remplir(&store, cx, cz, buf); + let actuel = |_x: i32, _y: i32, _z: i32| Bloc::Air; + couche.delta_chunk(&e, &masque, &actuel).len() + }) + .sum() + }); + let d = t.elapsed().as_secs_f64(); + println!("pipeline remplissage+delta {th} thr (cores/2) : {:.0} chunks/s ({n} modifs)", nch as f64 / d); + let (ws, peak) = mem_mo(); + println!("memoire finale : RSS {ws:.0} Mo, pic {peak:.0} Mo"); +}