gpu: port lock-free submit-thread watch/shadow/batch/card_steps/send optimizations

This commit is contained in:
theme613 committed 2026-11-28 13:54:54 +08:00
1 parent 84b4509fa3
commit 6f34121d31
5 files changed
+325 -143

No files matched your search

+49 -28
View File
@@ -219,7 +219,7 @@ pub(super) struct Own {
}
/// Runs whose pairs are one piece of work.
const RUNS_AT_A_TIME: usize = 4096;
const RUNS_AT_A_TIME: usize = 512;
/// Takes what `dirty` sends up out of the CPU sides.
pub(super) fn take(host: &Host, dirty: &[(std::sync::Arc<Shadow>, Vec<(usize, usize)>)]) -> Option<Own> {
@@ -227,42 +227,58 @@ pub(super) fn take(host: &Host, dirty: &[(std::sync::Arc<Shadow>, Vec<(usize, us
pass(host)?;
// Runs of whole words of the copy: only those are pairs. (A byte beside
// them in a word may be the card's.)
let short = |&(a, b): &(usize, usize)| short(a, b - a);
let is_short = |&(a, b): &(usize, usize)| short(a, b - a);
let mut at = 0u64;
let mut copies = Vec::with_capacity(dirty.len());
for (_, ranges) in dirty {
let mut word_chunks: Vec<(usize, &[(usize, usize)], u64)> = Vec::new();
let mut run_bytes = 0u64;
let mut run_count = 0u64;
let mut total_words = 0u64;
for (shadow, ranges) in dirty {
let mut runs = Vec::new();
for &(a, b) in ranges.iter().filter(|r| !short(r)) {
runs.push((a, at, b - a));
at += ((b - a) as u64).next_multiple_of(16);
let mut words = 0usize;
let from = shadow.card_data() as usize;
let chunk_start = word_chunks.len();
for chunk in ranges.chunks(RUNS_AT_A_TIME) {
let mut chunk_words = 0usize;
for &(a, b) in chunk {
let len = b - a;
if short(a, len) {
chunk_words += len / 4;
} else {
runs.push((a, at, len));
at += (len as u64).next_multiple_of(16);
run_bytes += len as u64;
run_count += 1;
}
}
if chunk_words > 0 {
word_chunks.push((from, chunk, words as u64));
words += chunk_words;
}
}
let words: usize = ranges.iter().filter(|r| short(r)).map(|r| (r.1 - r.0) / 4).sum();
copies.push(Copy { runs, pairs: (at, words as u32) });
let pairs_at = at;
for entry in &mut word_chunks[chunk_start..] {
entry.2 = pairs_at + entry.2 * 8;
}
copies.push(Copy { runs, pairs: (pairs_at, words as u32) });
total_words += words as u64;
at += (words as u64 * 8).next_multiple_of(16);
}
if at == 0 {
return None;
}
let memory = memory(host, at)?;
let mut moves: Vec<(usize, usize, usize)> = Vec::new();
let mut work: Vec<(usize, &[(usize, usize)], usize)> = Vec::new();
for ((shadow, ranges), copy) in dirty.iter().zip(&copies) {
let mut moves: Vec<(usize, usize, usize)> = Vec::with_capacity(run_count as usize);
for ((shadow, _), copy) in dirty.iter().zip(&copies) {
let from = shadow.card_data() as usize;
moves.extend(copy.runs.iter().map(|&(a, to, n)| (from + a, memory.data + to as usize, n)));
// (80,000 runs of some words a frame: in pieces of work.)
let mut to = memory.data + copy.pairs.0 as usize;
for ranges in ranges.chunks(RUNS_AT_A_TIME) {
let words: usize = ranges.iter().filter(|r| short(r)).map(|r| (r.1 - r.0) / 4).sum();
if words > 0 {
work.push((from, ranges, to));
to += words * 8;
}
}
}
let mem_data = memory.data;
let one = |i: usize| {
let (from, ranges, to) = work[i];
let mut to = to as *mut [u32; 2];
for &(a, b) in ranges.iter().filter(|r| short(r)) {
let (from, ranges, rel_to) = word_chunks[i];
let mut to = (mem_data + rel_to as usize) as *mut [u32; 2];
for &(a, b) in ranges.iter().filter(|r| is_short(r)) {
for offset in (a..b).step_by(4) {
// SAFETY: inside the copy's mapping (its CPU side, which
// the thread that asked, and waits, alone writes in these
@@ -276,15 +292,20 @@ pub(super) fn take(host: &Host, dirty: &[(std::sync::Arc<Shadow>, Vec<(usize, us
}
}
};
crate::land::each_under(work.len(), &one);
STATS[0].fetch_add(moves.iter().map(|m| m.2 as u64).sum(), Ordering::Relaxed);
STATS[1].fetch_add(moves.len() as u64, Ordering::Relaxed);
STATS[2].fetch_add(copies.iter().map(|c| u64::from(c.pairs.1)).sum(), Ordering::Relaxed);
crate::land::each_under(work_len(&word_chunks), &one);
STATS[0].fetch_add(run_bytes, Ordering::Relaxed);
STATS[1].fetch_add(run_count, Ordering::Relaxed);
STATS[2].fetch_add(total_words, Ordering::Relaxed);
crate::shadow::copy_all(moves);
STATS[3].fetch_add(began.elapsed().as_micros() as u64, Ordering::Relaxed);
Some(Own { memory, copies })
}
#[inline]
fn work_len<T>(chunks: &[T]) -> usize {
chunks.len()
}
impl Own {
/// Records the sends into `list`; the copies that `looked` says are in
/// `COPY_DEST`, the others in the common state. Those with a "before" are
@@ -315,7 +336,7 @@ impl Own {
}
state = Some(write);
}
let hashed = shadow.card_sides().is_some_and(|(_, before, _)| before.is_some());
let hashed = shadow.has_card_before();
if let Some(was) = state
&& hashed
&& (!copy.runs.is_empty() || copy.pairs.1 > 0)