bancs: mesures de vitesse (mmap, remplissage de chunk, delta rt-live, rayon)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
9690961cd0
révision
1beb7b993c
3 fichiers modifiés
+407
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,263 @@
|
||||
//! Bancs de vitesse (rapport 13).
|
||||
//! Usage : bancs <dossier tuiles> <tx0> <tz0> <ntx> <ntz> [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::<Pmc>() 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<String> = 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<ChunkEntree> = 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");
|
||||
}
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur