host_ab: feature kernels, a host-clock latency split, and per-variant encode runs
Content: the tree of `development` at 61b5179154, unchanged.
This commit is contained in:
1 parent
9810aff5e0
commit
3a02b6330a
6 files changed
+218
-6
No files matched your search
@@ -149,6 +149,9 @@ pub trait Gpu {
|
||||
fn on_completed(&self, command_buffer: &Self::CommandBuffer, handler: Box<dyn FnOnce() + Send>);
|
||||
/// `GPUEndTime - GPUStartTime`, in seconds, where the side measures it on the GPU.
|
||||
fn gpu_seconds(&self, command_buffer: &Self::CommandBuffer) -> Option<f64>;
|
||||
/// `kernelStartTime`, `kernelEndTime`, `GPUStartTime` and `GPUEndTime`, in seconds of the
|
||||
/// host clock [`crate::host_seconds`] reads.
|
||||
fn times(&self, command_buffer: &Self::CommandBuffer) -> [f64; 4];
|
||||
|
||||
/// Run `f` inside an autorelease pool (native), as a Metal program's frame runs.
|
||||
fn pool<R>(&self, f: impl FnOnce() -> R) -> R;
|
||||
|
||||
@@ -86,6 +86,107 @@ kernel void heavy_kernel(device float4 *out [[buffer(0)]],
|
||||
out[tid] = float4(acc * p.scale, float(hacc), 0.0, 1.0);
|
||||
}
|
||||
|
||||
// The feature kernels: each one Metal feature of the heavy kernel in the same loop, so their
|
||||
// ratios say which feature its cost comes from. Each takes the heavy kernel's bindings
|
||||
// (`out`, `p`, `tex`) and `src`, a device buffer as large as `out`.
|
||||
kernel void k_fma(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
float a = float(tid) * 1.0e-6 + float(NONCE) * 1.0e-12;
|
||||
float b = 1.0;
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
a = fma(a, 0.999, b);
|
||||
b = fma(b, 1.0001, -a * 0.5);
|
||||
}
|
||||
out[tid] = float4(a, b, 0.0, 1.0);
|
||||
}
|
||||
|
||||
kernel void k_sin(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
float a = float(tid) * 1.0e-6 + float(NONCE) * 1.0e-12;
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
a = sin(a * 1.3 + 0.7);
|
||||
}
|
||||
out[tid] = float4(a, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
kernel void k_half(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
half a = half(tid & 255u) * 0.01h + half(float(NONCE) * 1.0e-12);
|
||||
half b = 0.5h;
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
a = fma(a, 0.999h, b);
|
||||
b = b * 0.75h + a * 0.125h;
|
||||
}
|
||||
out[tid] = float4(float(a), float(b), 0.0, 1.0);
|
||||
}
|
||||
|
||||
kernel void k_constant(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
float4 acc = float4(float(NONCE) * 1.0e-12);
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
acc = fma(acc, float4(0.5), p.coeffs[(i + tid) & 63u]);
|
||||
}
|
||||
out[tid] = acc;
|
||||
}
|
||||
|
||||
kernel void k_device(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
device const float4 *src [[buffer(2)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
float4 acc = float4(float(NONCE) * 1.0e-12);
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
acc = fma(acc, float4(0.5), src[(tid + i * 4099u) & 1048575u]);
|
||||
}
|
||||
out[tid] = acc;
|
||||
}
|
||||
|
||||
kernel void k_texture(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
texture2d<half> tex [[texture(0)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
half4 acc = half4(half(float(NONCE) * 1.0e-12));
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
acc = fma(acc, half4(0.5h), tex.read(uint2((tid + i * 7u) & 255u, ((tid >> 8) + i) & 255u)));
|
||||
}
|
||||
out[tid] = float4(acc);
|
||||
}
|
||||
|
||||
kernel void k_texture_float(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
texture2d<float> tex [[texture(0)]],
|
||||
uint tid [[thread_position_in_grid]]) {
|
||||
float4 acc = float4(float(NONCE) * 1.0e-12);
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
acc = fma(acc, float4(0.5), tex.read(uint2((tid + i * 7u) & 255u, ((tid >> 8) + i) & 255u)));
|
||||
}
|
||||
out[tid] = acc;
|
||||
}
|
||||
|
||||
kernel void k_threadgroup_float(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]],
|
||||
uint lid [[thread_index_in_threadgroup]]) {
|
||||
threadgroup float shared_f[256];
|
||||
float acc = float(tid) * 1.0e-6 + float(NONCE) * 1.0e-12;
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
shared_f[lid] = acc;
|
||||
threadgroup_barrier(mem_flags::mem_threadgroup);
|
||||
acc = fma(shared_f[(lid + 1u) & 255u], 0.5, acc);
|
||||
threadgroup_barrier(mem_flags::mem_threadgroup);
|
||||
}
|
||||
out[tid] = float4(acc, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
kernel void k_threadgroup_half(device float4 *out [[buffer(0)]], constant HeavyParams &p [[buffer(1)]],
|
||||
uint tid [[thread_position_in_grid]],
|
||||
uint lid [[thread_index_in_threadgroup]]) {
|
||||
threadgroup half shared_h[256];
|
||||
half acc = half(tid & 255u) * 0.01h + half(float(NONCE) * 1.0e-12);
|
||||
for (uint i = 0; i < p.iterations; i++) {
|
||||
shared_h[lid] = acc;
|
||||
threadgroup_barrier(mem_flags::mem_threadgroup);
|
||||
acc = fma(shared_h[(lid + 7u) & 255u], 0.5h, acc);
|
||||
threadgroup_barrier(mem_flags::mem_threadgroup);
|
||||
}
|
||||
out[tid] = float4(float(acc), 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// The small dispatches' kernels: two, so a dispatch can change the pipeline state.
|
||||
kernel void tiny_kernel(device uint *out [[buffer(0)]],
|
||||
constant uint &value [[buffer(1)]],
|
||||
|
||||
@@ -29,7 +29,14 @@
|
||||
//! - `gpu`: a full-screen heavy fragment (`heavy_fragment`, 2048x2048 `RGBA16Float`) and a heavy
|
||||
//! kernel (`heavy_kernel`, 4096 threadgroups of 256), one command buffer in flight: commit to
|
||||
//! the wait's return, and native's `GPUEndTime - GPUStartTime`. `--heavy-iterations` sets
|
||||
//! the fragment's loop, and 16 times it the kernel's.
|
||||
//! the fragment's loop, and 16 times it the kernel's. Then each feature kernel (`k_*`, one
|
||||
//! feature of the heavy kernel each: fma, sin, half, constant buffer, device buffer, texture
|
||||
//! read as `half` and as `float`, threadgroup float, threadgroup half) on the kernel's grid
|
||||
//! and loop.
|
||||
//! - `latency_split` (with `latency`): the same command buffers' host-clock timeline, commit to
|
||||
//! `kernelStartTime` to `kernelEndTime` to `GPUStartTime` to `GPUEndTime` to the wait's return.
|
||||
//! The runtime's GPU times on MoltenVK are the host's (its docs): its GPU start is its kernel
|
||||
//! end and its GPU end the moment it saw the command buffer complete.
|
||||
//! - `pipeline`: the library, then each of four states' creation and first use, then each made
|
||||
//! again in the process. Each round runs it in two processes per side over a metallib of a
|
||||
//! fresh `NONCE` (`host_ab.metal`): `cold_*` (no cache holds its functions) and `warm_*` (a
|
||||
@@ -43,7 +50,9 @@
|
||||
//! `--heavy-iterations N` (64), `--heavy-runs N` (15), `--scratch DIR` (metallibs and the
|
||||
//! runtime's shader caches; a directory under the OS's temporary directory by default, removed at
|
||||
//! the end), `--metallib PATH` (skip the compile; not with `pipeline`), `--cache DIR` (the
|
||||
//! runtime's shader cache, with `--side`).
|
||||
//! runtime's shader cache, with `--side`), `--variants a,b,...` (the encode workload's variants
|
||||
//! alone: `draw_static`, `draw_bind`, `draw_pipeline`, `dispatch_static`, `dispatch_churn`; for a
|
||||
//! profile of one).
|
||||
//!
|
||||
//! On a Mac, the runtime's device opens the Vulkan loader (`/opt/homebrew/lib` among the places
|
||||
//! it looks) and MoltenVK's `MVK_CONFIG_*` variables apply. Run timings on an otherwise idle GPU.
|
||||
@@ -71,6 +80,8 @@ struct Options {
|
||||
scratch: Option<PathBuf>,
|
||||
metallib: Option<PathBuf>,
|
||||
cache: Option<PathBuf>,
|
||||
/// The encode workload's variants to run (`--variants`); all when empty.
|
||||
variants: Vec<String>,
|
||||
/// Arguments passed on to each child.
|
||||
passed: Vec<String>,
|
||||
}
|
||||
@@ -90,6 +101,7 @@ fn options() -> Options {
|
||||
scratch: None,
|
||||
metallib: None,
|
||||
cache: None,
|
||||
variants: vec![],
|
||||
passed: vec![],
|
||||
};
|
||||
let mut args = std::env::args().skip(1);
|
||||
@@ -118,6 +130,11 @@ fn options() -> Options {
|
||||
"--scratch" => options.scratch = Some(PathBuf::from(value())),
|
||||
"--metallib" => options.metallib = Some(PathBuf::from(value())),
|
||||
"--cache" => options.cache = Some(PathBuf::from(value())),
|
||||
"--variants" => {
|
||||
let v = value();
|
||||
options.variants = v.split(',').map(str::to_owned).collect();
|
||||
options.passed.extend(["--variants".to_owned(), v]);
|
||||
}
|
||||
"--help" | "-h" => {
|
||||
eprintln!("see the module docs of examples/host_ab/main.rs");
|
||||
std::process::exit(0);
|
||||
@@ -210,7 +227,7 @@ fn run<G: Gpu>(g: &G, opened: Instant, metallib: &Path, options: &Options) {
|
||||
);
|
||||
for workload in rest {
|
||||
match workload {
|
||||
"encode" => workloads::encode(g, &scene, sizes),
|
||||
"encode" => workloads::encode(g, &scene, sizes, &options.variants),
|
||||
"fixed" => workloads::fixed(g, &scene, sizes),
|
||||
"latency" => workloads::latency(g, &scene, sizes),
|
||||
"gpu" => workloads::gpu(g, &scene, sizes),
|
||||
@@ -220,6 +237,12 @@ fn run<G: Gpu>(g: &G, opened: Instant, metallib: &Path, options: &Options) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The host clock Metal's and the runtime's command buffer times count: `CLOCK_UPTIME_RAW`
|
||||
/// (`mach_absolute_time`) on macOS.
|
||||
pub fn host_seconds() -> f64 {
|
||||
metal2vulkan_runtime::host_time()
|
||||
}
|
||||
|
||||
fn scratch_default() -> PathBuf {
|
||||
std::env::temp_dir().join(format!("host-ab-{}", std::process::id()))
|
||||
}
|
||||
|
||||
@@ -394,6 +394,15 @@ impl Gpu for Native {
|
||||
Some(command_buffer.GPUEndTime() - command_buffer.GPUStartTime())
|
||||
}
|
||||
|
||||
fn times(&self, command_buffer: &Self::CommandBuffer) -> [f64; 4] {
|
||||
[
|
||||
command_buffer.kernelStartTime(),
|
||||
command_buffer.kernelEndTime(),
|
||||
command_buffer.GPUStartTime(),
|
||||
command_buffer.GPUEndTime(),
|
||||
]
|
||||
}
|
||||
|
||||
fn pool<R>(&self, f: impl FnOnce() -> R) -> R {
|
||||
autoreleasepool(|_| f())
|
||||
}
|
||||
|
||||
@@ -353,6 +353,15 @@ impl Gpu for Runtime {
|
||||
None
|
||||
}
|
||||
|
||||
fn times(&self, command_buffer: &rt::CommandBuffer) -> [f64; 4] {
|
||||
[
|
||||
command_buffer.kernel_start_time(),
|
||||
command_buffer.kernel_end_time(),
|
||||
command_buffer.gpu_start_time().unwrap_or(0.0),
|
||||
command_buffer.gpu_end_time().unwrap_or(0.0),
|
||||
]
|
||||
}
|
||||
|
||||
fn pool<R>(&self, f: impl FnOnce() -> R) -> R {
|
||||
f()
|
||||
}
|
||||
|
||||
@@ -70,6 +70,18 @@ const HEAVY_TEXTURE_SIDE: usize = 1024;
|
||||
const KERNEL_TEXTURE_SIDE: usize = 256;
|
||||
/// The heavy kernel's grid: threadgroups of 256 threads.
|
||||
const HEAVY_GROUPS: usize = 4096;
|
||||
/// The feature kernels (`host_ab.metal`).
|
||||
const FEATURES: [&str; 9] = [
|
||||
"k_fma",
|
||||
"k_sin",
|
||||
"k_half",
|
||||
"k_constant",
|
||||
"k_device",
|
||||
"k_texture",
|
||||
"k_texture_float",
|
||||
"k_threadgroup_float",
|
||||
"k_threadgroup_half",
|
||||
];
|
||||
const HEAVY_THREADS: usize = 256;
|
||||
|
||||
/// What the workloads draw and dispatch with, made once per process.
|
||||
@@ -79,6 +91,8 @@ pub struct Scene<G: Gpu> {
|
||||
/// `quad_vertex` + `heavy_fragment` into `RGBA16Float`.
|
||||
pub heavy_fragment: G::RenderPipeline,
|
||||
pub heavy_kernel: G::ComputePipeline,
|
||||
/// The feature kernels (`host_ab.metal`), by name.
|
||||
pub features: Vec<(&'static str, G::ComputePipeline)>,
|
||||
/// `tiny_kernel` and `tiny_kernel_b`.
|
||||
pub tiny: [G::ComputePipeline; 2],
|
||||
pub textures: Vec<G::Texture>,
|
||||
@@ -96,6 +110,8 @@ pub struct Scene<G: Gpu> {
|
||||
pub tiny_out: G::Buffer,
|
||||
pub tiny_values: G::Buffer,
|
||||
pub heavy_out: G::Buffer,
|
||||
/// The feature kernels' `src`.
|
||||
pub heavy_in: G::Buffer,
|
||||
pub fill: G::Buffer,
|
||||
}
|
||||
|
||||
@@ -156,6 +172,10 @@ impl<G: Gpu> Scene<G> {
|
||||
false,
|
||||
);
|
||||
let heavy_kernel = g.compute_pipeline(library, "heavy_kernel");
|
||||
let features = FEATURES
|
||||
.iter()
|
||||
.map(|&name| (name, g.compute_pipeline(library, name)))
|
||||
.collect();
|
||||
let tiny = [
|
||||
g.compute_pipeline(library, "tiny_kernel"),
|
||||
g.compute_pipeline(library, "tiny_kernel_b"),
|
||||
@@ -187,6 +207,7 @@ impl<G: Gpu> Scene<G> {
|
||||
textured,
|
||||
heavy_fragment,
|
||||
heavy_kernel,
|
||||
features,
|
||||
tiny,
|
||||
textures,
|
||||
heavy_texture: shader_texture(HEAVY_TEXTURE_SIDE, 7),
|
||||
@@ -206,6 +227,7 @@ impl<G: Gpu> Scene<G> {
|
||||
tiny_out: g.buffer(RECTS * RECT_STRIDE),
|
||||
tiny_values: g.buffer_with(&vec![1u8; RECTS * RECT_STRIDE]),
|
||||
heavy_out: g.buffer(HEAVY_GROUPS * HEAVY_THREADS * 16),
|
||||
heavy_in: g.buffer_with(&vec![0x3c; HEAVY_GROUPS * HEAVY_THREADS * 16]),
|
||||
fill: g.buffer(4096),
|
||||
}
|
||||
}
|
||||
@@ -287,9 +309,13 @@ fn encode_dispatches<G: Gpu>(
|
||||
/// The CPU cost of encoding: per draw and per dispatch, on the encoding thread (wall) and in the
|
||||
/// whole process (CPU time of every thread, from the command buffer's creation until its wait
|
||||
/// returned, which takes in work moved off the encoding thread to commit or to other threads).
|
||||
pub fn encode<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
pub fn encode<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes, variants: &[String]) {
|
||||
let n = sizes.draws;
|
||||
let runs = |name: String| variants.is_empty() || variants.contains(&name);
|
||||
for churn in [Churn::Static, Churn::Bind, Churn::Pipeline] {
|
||||
if !runs(format!("draw_{}", churn.name())) {
|
||||
continue;
|
||||
}
|
||||
let (mut wall, mut cpu, mut commit, mut total) = (vec![], vec![], vec![], vec![]);
|
||||
for run in 0..sizes.timed + 3 {
|
||||
g.pool(|| {
|
||||
@@ -333,6 +359,12 @@ pub fn encode<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
emit("encode", &format!("draw_{name}_cb_total"), "ms", &mut total);
|
||||
}
|
||||
for churn in [false, true] {
|
||||
if !runs(format!(
|
||||
"dispatch_{}",
|
||||
if churn { "churn" } else { "static" }
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
let (mut wall, mut cpu, mut total) = (vec![], vec![], vec![]);
|
||||
for run in 0..sizes.timed + 3 {
|
||||
g.pool(|| {
|
||||
@@ -458,21 +490,43 @@ pub fn latency<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
("draw", Tiny::Draw),
|
||||
] {
|
||||
let (mut wait, mut commit) = (vec![], vec![]);
|
||||
// The host-clock timeline: commit to kernel start, kernel start to end, kernel end to
|
||||
// GPU start, GPU start to end, GPU end to the wait's return.
|
||||
let mut split: [Vec<f64>; 5] = Default::default();
|
||||
for run in 0..sizes.latency + 10 {
|
||||
g.pool(|| {
|
||||
let command_buffer = g.command_buffer();
|
||||
encode_tiny(g, s, &command_buffer, tiny);
|
||||
let h0 = crate::host_seconds();
|
||||
let t0 = Instant::now();
|
||||
g.commit(&command_buffer);
|
||||
let t1 = Instant::now();
|
||||
g.wait(&command_buffer);
|
||||
let t2 = Instant::now();
|
||||
let h2 = crate::host_seconds();
|
||||
if run >= 10 {
|
||||
commit.push(us(t1 - t0));
|
||||
wait.push(us(t2 - t0));
|
||||
let [ks, ke, gs, ge] = g.times(&command_buffer);
|
||||
let points = [h0, ks, ke, gs, ge, h2];
|
||||
for (k, part) in split.iter_mut().enumerate() {
|
||||
part.push((points[k + 1] - points[k]) * 1e6);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (part, values) in [
|
||||
"commit_to_kernel_start",
|
||||
"kernel_start_to_end",
|
||||
"kernel_end_to_gpu_start",
|
||||
"gpu_start_to_end",
|
||||
"gpu_end_to_wait_return",
|
||||
]
|
||||
.iter()
|
||||
.zip(&mut split)
|
||||
{
|
||||
emit("latency_split", &format!("{name}_{part}"), "us", values);
|
||||
}
|
||||
emit(
|
||||
"latency",
|
||||
&format!("{name}_commit_to_wait_return"),
|
||||
@@ -509,8 +563,9 @@ pub fn latency<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
}
|
||||
}
|
||||
|
||||
/// GPU time of the heavy shaders, one command buffer in flight: from the commit until the wait
|
||||
/// returned, and `GPUEndTime - GPUStartTime` where the side measures it on the GPU.
|
||||
/// GPU time of the heavy shaders and the feature kernels, one command buffer in flight: from the
|
||||
/// commit until the wait returned, and `GPUEndTime - GPUStartTime` where the side measures it on
|
||||
/// the GPU.
|
||||
pub fn gpu<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
let run = |name: &str, encode: &dyn Fn(&G::CommandBuffer)| {
|
||||
let (mut wall, mut times) = (vec![], vec![]);
|
||||
@@ -554,6 +609,18 @@ pub fn gpu<G: Gpu>(g: &G, s: &Scene<G>, sizes: &Sizes) {
|
||||
g.dispatch(&mut encoder, HEAVY_GROUPS, HEAVY_THREADS);
|
||||
g.end_compute(encoder);
|
||||
});
|
||||
for (name, state) in &s.features {
|
||||
run(name, &|command_buffer| {
|
||||
let mut encoder = g.compute_encoder(command_buffer);
|
||||
g.set_compute_pipeline(&mut encoder, state);
|
||||
g.set_buffer(&mut encoder, &s.heavy_out, 0, 0);
|
||||
g.set_buffer(&mut encoder, &s.kernel_params, 0, 1);
|
||||
g.set_buffer(&mut encoder, &s.heavy_in, 0, 2);
|
||||
g.set_texture(&mut encoder, &s.kernel_texture, 0);
|
||||
g.dispatch(&mut encoder, HEAVY_GROUPS, HEAVY_THREADS);
|
||||
g.end_compute(encoder);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Pipeline creation, in a process whose caches are as the caller left them: the library, each
|
||||
|
||||
Reference in new issue
Block a user