harness: the lcg rows bind their increment from a Managed buffer, as the recordings do; burst_160_shared keeps the Shared row

The recordings bind small constant arguments from Managed buffers
(216,574 dispatches in one, against 94 Shared-backed), so the frame,
burst_160, burst_2000 and burst_2000x5 rows now keep their increment in
a Managed buffer, written through contents and flushed with
didModifyRange:. burst_160_shared keeps it Shared, for the record.

The harness gains Storage::Managed and Buffer::did_modify_range. The old
runtime makes a real Managed buffer. The new runtime has no Managed
storage, so its harness keeps the GPU copy in a Private buffer, written
as a Private one is (through a Shared buffer and the copy kernel), as
didModifyRange: updates a Managed buffer GPU copy on a Mac with a
discrete GPU.
This commit is contained in:
Anees Iqbal committed 2026-11-17 12:41:44 -08:00
1 parent 2dfb2f2041
commit 58891b9ce5
6 files changed
+75 -23

No files matched your search

+30 -18
View File
@@ -19,7 +19,7 @@ pub struct Workload {
}
/// The authored workloads.
pub const WORKLOADS: [Workload; 15] = [
pub const WORKLOADS: [Workload; 16] = [
Workload {
name: "bandwidth",
plan: bandwidth,
@@ -52,6 +52,10 @@ pub const WORKLOADS: [Workload; 15] = [
name: "burst_160",
plan: burst_160,
},
Workload {
name: "burst_160_shared",
plan: burst_160_shared,
},
Workload {
name: "burst_2000",
plan: burst_2000,
@@ -401,15 +405,6 @@ fn private(contents: Vec<u8>) -> BufferPlan {
}
}
/// A `Shared` buffer starting with `contents`, written through `contents`.
fn shared(contents: Vec<u8>) -> BufferPlan {
BufferPlan {
length: contents.len() as u64,
storage: Storage::Shared,
contents: Some(contents),
}
}
/// A `Private` buffer of `length` bytes whose first run writes before it reads.
fn unset(length: u64) -> BufferPlan {
BufferPlan {
@@ -423,34 +418,47 @@ fn unset(length: u64) -> BufferPlan {
/// a frame, two at the 90th percentile, a few dispatches each), in compute alone: a run is one
/// command buffer of four `lcg_step` dispatches ([`lcg_frame`]).
fn frame() -> Plan {
lcg_frame("frame", 1, 4, None)
lcg_frame("frame", 1, 4, None, Storage::Managed)
}
/// A burst of small command buffers: a run is 160 command buffers of two `lcg_step` dispatches
/// each ([`lcg_frame`]).
fn burst_160() -> Plan {
lcg_frame("burst_160", 160, 2, None)
lcg_frame("burst_160", 160, 2, None, Storage::Managed)
}
/// `burst_160` with its increment in a `Shared` buffer, kept for the record: the recordings bind
/// few small constant arguments from `Shared` memory ([`lcg_frame`]).
fn burst_160_shared() -> Plan {
lcg_frame("burst_160_shared", 160, 2, None, Storage::Shared)
}
/// The burst a recording commits most: a run is 2,000 command buffers of one compute encoder of
/// one `lcg_step` dispatch each ([`lcg_frame`]).
fn burst_2000() -> Plan {
lcg_frame("burst_2000", 2000, 1, None)
lcg_frame("burst_2000", 2000, 1, None, Storage::Managed)
}
/// That burst at five compute encoders a command buffer: a run is 2,000 command buffers of five
/// compute encoders of one `lcg_step` dispatch each ([`lcg_frame`]).
fn burst_2000x5() -> Plan {
lcg_frame("burst_2000x5", 2000, 5, Some(1))
lcg_frame("burst_2000x5", 2000, 5, Some(1), Storage::Managed)
}
/// A run of `command_buffers` command buffers of `per` tiny dispatches each, in compute encoders
/// of `per_encoder` dispatches (or one of them all), committed with no wait between and waited
/// for once: `lcg_step` over 256 words of `Private` storage, each dispatch setting its bindings,
/// since an encoder starts with none. Both are buffers (`setBuffer:offset:atIndex:`): the words,
/// and the increment in a `Shared` buffer written once; the run makes no `setBytes` call
/// (`dispatch_chain` measures those).
fn lcg_frame(name: &str, command_buffers: usize, per: usize, per_encoder: Option<usize>) -> Plan {
/// and the increment in a buffer of `storage` written once, a `Managed` one then flushed with
/// `didModifyRange:`, as the recordings bind their small constant arguments; the run makes no
/// `setBytes` call (`dispatch_chain` measures those).
fn lcg_frame(
name: &str,
command_buffers: usize,
per: usize,
per_encoder: Option<usize>,
storage: Storage,
) -> Plan {
const WORDS: usize = 256;
let steps = per * command_buffers;
let increment = words(14, 1)[0];
@@ -473,7 +481,11 @@ fn lcg_frame(name: &str, command_buffers: usize, per: usize, per_encoder: Option
library: Source::Authored,
buffers: vec![
private(word_bytes(&input)),
shared(word_bytes(&[increment, 0, 0, 0])),
BufferPlan {
length: 16,
storage,
contents: Some(word_bytes(&[increment, 0, 0, 0])),
},
],
dispatches: vec![dispatch; steps],
expect: vec![Expect {
+8
View File
@@ -131,6 +131,10 @@ pub enum Storage {
Shared,
/// `MTLStorageModePrivate`: the GPU's alone.
Private,
/// `MTLStorageModeManaged`: the CPU writes it through `contents` and calls
/// `didModifyRange:`, which brings the GPU's copy up to date. The new runtime has no such
/// mode; its harness keeps the GPU's copy in a `Private` buffer (`rt`).
Managed,
}
/// A library's code.
@@ -228,6 +232,10 @@ pub trait ComputeDevice: Device {
pub trait Buffer {
/// `contents`: the CPU address of the buffer's bytes; `None` for `Private` storage.
fn contents(&self) -> Option<NonNull<u8>>;
/// `didModifyRange:` of the `length` bytes at `offset`: the CPU wrote them. Nothing for
/// storage other than `Managed`.
fn did_modify_range(&self, offset: u64, length: u64) -> Result<()>;
}
/// `MTLLibrary`.
+12
View File
@@ -31,6 +31,11 @@ impl crate::Device for runtime::Device {
let options = match storage {
Storage::Shared => ResourceOptions::shared(),
Storage::Private => ResourceOptions::private(),
Storage::Managed => ResourceOptions::new(
runtime::StorageMode::Managed,
runtime::CpuCacheMode::DefaultCache,
runtime::HazardTrackingMode::Default,
),
};
Ok(runtime::Device::new_buffer(self, length, options)?)
}
@@ -132,6 +137,13 @@ impl crate::Buffer for runtime::Buffer {
fn contents(&self) -> Option<NonNull<u8>> {
runtime::Buffer::contents(self)
}
fn did_modify_range(&self, offset: u64, length: u64) -> Result<()> {
Ok(runtime::Buffer::did_modify_range(
self,
offset..offset + length,
)?)
}
}
impl crate::Library<runtime::Device> for runtime::Library {
+11 -1
View File
@@ -9,6 +9,11 @@
//! buffer's memory only then. So a buffer made after one of the same length was dropped takes
//! the same range, which the runtime zeroes as `newBufferWithLength:options:` does.
//!
//! The new runtime has no `Managed` storage, so a `Managed` buffer is a `Private` one here: the
//! GPU's copy in device memory, which the harness writes as it writes a `Private` buffer, through
//! a `Shared` buffer and its copy kernel, as `didModifyRange:` brings a `Managed` buffer's GPU copy
//! up to date on a Mac with a discrete GPU. It has no CPU copy: its `contents` is `None`.
//!
//! Its known gaps and the device's limits (`Error::Unsupported`, `Error::Limit`) are typed
//! refusals ([`Refused`]); its other errors are failures. It makes no texture or sampler state
//! yet: a plan with either is refused when it is made, so its texture and sampler types have no
@@ -234,7 +239,7 @@ impl crate::Device for Device {
fn new_buffer(&self, length: u64, storage: Storage) -> Result<Buffer> {
let class = match storage {
Storage::Shared => rt::MemoryClass::Shared,
Storage::Private => rt::MemoryClass::Private,
Storage::Private | Storage::Managed => rt::MemoryClass::Private,
};
let placed = self.heap.borrow_mut().place(class, length)?;
let start = placed.range.as_ref().map_or(0, |range| range.start);
@@ -448,4 +453,9 @@ impl crate::Buffer for Buffer {
fn contents(&self) -> Option<NonNull<u8>> {
self.buffer.contents()
}
/// Nothing: a `Managed` buffer has no CPU copy here (the module's docs).
fn did_modify_range(&self, _: u64, _: u64) -> Result<()> {
Ok(())
}
}
+10 -4
View File
@@ -420,16 +420,22 @@ impl<D: ComputeDevice> Harness<D> {
}
/// Every buffer's starting contents: a `Shared` one's written through `contents`, a
/// `Private` one's copied in from a `Shared` buffer, all in one command buffer, waited for.
/// `Managed` one's too, then `didModifyRange:` over them, and a `Private` one's, or a
/// `Managed` one's without a CPU copy, copied in from a `Shared` buffer, all in one command
/// buffer, waited for.
fn fill(&self, plans: &[BufferPlan], buffers: &[D::Buffer]) -> Result<()> {
let mut staged = Vec::new();
for (plan, buffer) in plans.iter().zip(buffers) {
let Some(contents) = &plan.contents else {
continue;
};
match plan.storage {
Storage::Shared => write(buffer, contents)?,
Storage::Private => {
match (plan.storage, buffer.contents()) {
(Storage::Shared, _) => write(buffer, contents)?,
(Storage::Managed, Some(_)) => {
write(buffer, contents)?;
buffer.did_modify_range(0, plan.length)?;
}
(Storage::Private | Storage::Managed, _) => {
let stage = self.device.new_buffer(plan.length, Storage::Shared)?;
write(&stage, contents)?;
staged.push((stage, buffer, plan.length));
+4
View File
@@ -393,6 +393,10 @@ impl Buffer for HostBuffer {
fn contents(&self) -> Option<NonNull<u8>> {
NonNull::new(self.0.as_ptr().cast_mut().cast())
}
fn did_modify_range(&self, _: u64, _: u64) -> Result<()> {
Ok(())
}
}
#[test]