Files
leleka/crates/native-cpu-expert/src/rust_pool.rs
T

101 lines
3.0 KiB
Rust

//! Persistent Original epoch/claim/park protocol over closed Rust-owned jobs.
//! Unsafe sharing is restricted to admitted disjoint row cells. No raw owner escapes.
use super::rust_simd::{dispatch, silu};
use super::rust_types::{ActQ, NativeFmt, Q8K, SplitBuf};
use super::{Error, FF, H, MAX_JOBS, MAX_ROWS, PreparedJob, Result};
use std::{
cell::UnsafeCell,
sync::{
Arc, Condvar, Mutex,
atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering},
},
thread,
time::{Duration, Instant},
};
#[repr(align(64))]
struct Line<T>(T);
#[repr(C, align(64))]
struct NativeAct {
q: [Q8K; 10],
padding: [u8; 1176],
}
#[repr(C, align(64))]
struct Scratch {
a1: ActQ,
a2: ActQ,
ff: [f32; FF],
}
#[repr(C)]
#[derive(Clone, Copy)]
struct Job {
gate: *const u8,
up: *const u8,
down: *const u8,
rows: i32,
act: [*const ActQ; MAX_ROWS],
output: [*mut f32; MAX_ROWS],
nact: [*const u8; MAX_ROWS],
}
#[repr(C, align(64))]
struct SingleSplit {
ff: [f32; FF],
a2: ActQ,
}
const _: () = assert!(std::mem::size_of::<Job>() == 224);
const _: () = assert!(std::mem::size_of::<Scratch>() == 9728);
const _: () = assert!(std::mem::size_of::<SingleSplit>() == 6144);
struct Batch {
jobs: [Job; MAX_JOBS],
format: NativeFmt,
count: usize,
mode: u32,
rows: usize,
tasks: usize,
}
struct Shared {
head: Line<AtomicU64>,
done: Line<AtomicU32>,
parked: Line<AtomicU32>,
epoch: Line<AtomicU32>,
stop: Line<AtomicBool>,
sleepers: Line<AtomicU32>,
sleep_mu: Mutex<()>,
sleep_cv: Condvar,
spin: Duration,
n: usize,
batch: UnsafeCell<Batch>,
act: Vec<UnsafeCell<[ActQ; MAX_ROWS]>>,
nact: Vec<UnsafeCell<[NativeAct; MAX_ROWS]>>,
split: Vec<UnsafeCell<SplitBuf>>,
// Borrowed groups quantize every token once (expert_source.cpp act_multi/nact_multi), not once per job row.
tok_act: Vec<UnsafeCell<[ActQ; MAX_ROWS]>>,
tok_nact: Vec<UnsafeCell<[NativeAct; MAX_ROWS]>>,
// Keep the original per-worker scratch reservation even though native phases
// require only split storage; it remains owned until all worker joins complete.
_scratch: Vec<Scratch>,
_single_split: Vec<SingleSplit>,
}
/// `LELEKA_POOL_TIMING=1`: wall time of the host's stages of `compute_borrowed`, summed over calls until taken (ms):
/// 0 pre (validation, token quantization, job table), 1-5 gate/up phase (park wait before, publish, host drain, wait
/// done, park wait after), 6 host requantization, 7-11 down phase (same five), 12 post (output check). Off: no clock is
/// read.
pub const TIMING_STAGES: usize = 13;
pub(super) struct NativePool {
shared: Arc<Shared>,
workers: Vec<thread::JoinHandle<()>>,
timing: Option<[f64; TIMING_STAGES]>,
}
mod exec;
mod impl_default_for_nativeact;
#[allow(unused_imports)]
use impl_default_for_nativeact::all_finite;
#[allow(unused_imports)]
use impl_default_for_nativeact::allocated;
mod impl_shared;
mod stage;
#[allow(unused_imports)]
use impl_shared::tasks_per_thread;
mod impl_nativepool;
mod impl_nativepool_2;