diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index 1754c1af1..9a30d423c 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -926,15 +926,21 @@ that touch blocks first. with the state, each one tile of the grid, which its region carries. A thread's position is the tile's origin plus its index masked and shifted by the tile's sides, with no division. - The translator's `pipeline::grid_tile` picks the tile from the grid's extents alone: - power-of-two sides whose product is 256, launching the fewest threads past the grid, then with - the fewest tiles partly past it, then the shallowest, then (where the grid has two rows or more) - the height nearest 2, then the widest. Where the workgroups do not divide the grid, an - invocation past the grid returns before the kernel's body. An indirect dispatch runs at its own - threadgroup size. A kernel that observes its partition is lowered plainly: its whole grids, - `dispatch_threadgroups` and indirect dispatches run that module at their own size, and a - `dispatch_threads` grid that is not a whole number of threadgroups runs its partial-threadgroups - variant, which the reaper lowers for the first such grid, which waits for it (a first-use wait). + A tile has power-of-two sides whose product is 256. The translator's `pipeline::grid_tile` + picks a tiling kernel's from the grid's extents alone: launching the fewest threads past the + grid, then with the fewest tiles partly past it, then the shallowest, then (where the grid has + two rows or more) the height nearest 2, then the widest. An image-only kernel's tile is picked + from the grid and the call's threadgroup size: the fewest threads launched past the grid, then + the fewest tiles partly past it, then the depth nearest the threadgroup's (the shallower of two + as near), then the most square in `x` and `y`, then the wider. So threadgroups of 8x8x4 over a + grid that 8x8x4 tiles divide run in 8x8x4, and threadgroups one deep in the square 16x16x1, + the pipeline made with the state, wherever 16x16x1 tiles divide the grid. Where the workgroups + do not divide the grid, an invocation past the grid returns before the kernel's body. An + indirect dispatch runs at its own threadgroup size. A kernel that observes its partition is + lowered plainly: its whole grids, `dispatch_threadgroups` and indirect dispatches run that + module at their own size, and a `dispatch_threads` grid that is not a whole number of + threadgroups runs its partial-threadgroups variant, which the reaper lowers for the first such + grid, which waits for it (a first-use wait). - A threadgroup deeper than 64, the floor's `maxComputeWorkGroupSize[2]`, runs as one row of its threads, a workgroup of `x·y·z` by 1 by 1, each thread's position its index unflattened in Metal's order (the translator's `SpecKey::DeepThreadgroup`), on every device alike; any other diff --git a/crates/metal2vulkan-rt/src/encoder.rs b/crates/metal2vulkan-rt/src/encoder.rs index 7ed6da135..20656b580 100644 --- a/crates/metal2vulkan-rt/src/encoder.rs +++ b/crates/metal2vulkan-rt/src/encoder.rs @@ -236,8 +236,8 @@ pub(crate) struct Choice { /// indirect, the same on every device: /// - indirect: the whole module at its own size; /// - an image-only kernel ([`Form::Square`]): the whole module at `MultiRegion` 0 in workgroups -/// of [`SQUARE`] over a grid one deep, and of the grid's `grid_tile` tile over a deeper one, -/// the region carrying them, flagged where they do not divide the grid; +/// of [`SQUARE`] over a grid one deep, and of its [`image_tile`] over a deeper one, the region +/// carrying them, flagged where they do not divide the grid; /// - a tiling kernel ([`Form::Tiled`]): its tiles, workgroups of `GRID_TILE_THREADS`, each one /// `grid_tile` tile of the grid, which its region carries; /// - an observing kernel ([`Form::Sized`]): its own size, a ragged grid on its @@ -273,7 +273,7 @@ pub(crate) fn choose(form: &Form, direct: Option, size: [u32; 3]) -> Cho }; match form { Form::Square if threads[2] == 1 => whole(SQUARE), - Form::Square => whole(grid_tile(threads)), + Form::Square => whole(image_tile(threads, size)), Form::Tiled => { let tile = grid_tile(threads); Choice { @@ -295,6 +295,41 @@ pub(crate) fn choose(form: &Form, direct: Option, size: [u32; 3]) -> Cho } } +/// The tile an image-only kernel's grid of `threads`, more than one deep, runs in at threadgroup +/// size `size` ([`Form::Square`]): sides `2^a, 2^b, 2^c` with `a + b + c = 8`, so that one +/// workgroup of [`GRID_TILE_THREADS`] is one tile. Of every such tile it is the first in one total +/// order: the fewest threads launched, `∏ ⌈threads_i / side_i⌉ · side_i`; then the fewest tiles +/// partly past the grid; then the depth nearest the threadgroup's, `|2^c − size[2]|` smallest, +/// the shallower of two as near; then the most square in `x` and `y`, `|a − b|` smallest; then +/// the wider. A pure function of the grid and the threadgroup size: threadgroups of 8x8x4 over a +/// grid that 8x8x4 tiles divide run in 8x8x4, and threadgroups one deep in 16x16x1 where 16x16x1 +/// tiles divide the grid. Each tile is one of 45, a `VkPipeline` of the state's each. +pub(crate) fn image_tile(threads: [u32; 3], size: [u32; 3]) -> [u32; 3] { + const BITS: u32 = GRID_TILE_THREADS.trailing_zeros(); + // The tiles of side `2^k` an extent of `n` takes, and those of them it fills. + let counts = |n: u32, k: u32| ((u64::from(n) + (1 << k) - 1) >> k, u64::from(n) >> k); + let depth = u64::from(size[2].max(1)); + let mut best = ( + [0; 3], + (u128::MAX, u128::MAX, u64::MAX, u32::MAX, u32::MAX, u32::MAX), + ); + for c in 0..=BITS { + let (tz, wz) = counts(threads[2], c); + let near = (1u64 << c).abs_diff(depth); + for b in 0..=BITS - c { + let a = BITS - c - b; + let ((tx, wx), (ty, wy)) = (counts(threads[0], a), counts(threads[1], b)); + let tiles = u128::from(tx) * u128::from(ty) * u128::from(tz); + let partly = tiles - u128::from(wx) * u128::from(wy) * u128::from(wz); + let key = (tiles, partly, near, c, a.abs_diff(b), b); + if key < best.1 { + best = ([a, b, c], key); + } + } + } + best.0.map(|k| 1 << k) +} + // A workgroup one deep at most `DEEP_THREADGROUP`, or one row of at most Metal's 1,024 // threads, is within the floor's sizes. const _: () = assert!( diff --git a/crates/metal2vulkan-rt/src/pipeline.rs b/crates/metal2vulkan-rt/src/pipeline.rs index 8416ee1d9..4e869da09 100644 --- a/crates/metal2vulkan-rt/src/pipeline.rs +++ b/crates/metal2vulkan-rt/src/pipeline.rs @@ -7,8 +7,9 @@ //! every direct dispatch runs in workgroups of 256 threads, each one tile of the grid whose sides //! the translator's rule picks from the grid's extents (`grid_tile`) and its region carries, on //! one `VkPipeline` made with the state. An image-only kernel's (`CompiledFunction::is_image_only`) -//! runs instead in workgroups of 16x16x1 over a grid one deep, on that one, and of the grid's tile -//! over a deeper one, on one `VkPipeline` per tile made at its first dispatch. An indirect +//! runs instead in workgroups of 16x16x1 over a grid one deep, on that one, and of a tile the +//! grid and the threadgroup size pick (`encoder::image_tile`) over a deeper one, on one +//! `VkPipeline` per tile made at its first dispatch. An indirect //! dispatch runs at its own threadgroup size. A kernel that observes its partition is lowered //! once, plainly: its whole grids, threadgroup dispatches and indirect dispatches run that module //! at their own threadgroup size, and a `dispatchThreads` grid that is not a whole number of @@ -250,9 +251,9 @@ pub(crate) enum Form { Tiled, /// The kernel does not observe its partition and is image-only /// (`CompiledFunction::is_image_only`): `whole` runs a direct dispatch at `MultiRegion` 0, - /// whatever the threadgroup size, in workgroups of [`SQUARE`] over a grid one deep and of the - /// grid's `grid_tile` tile over a deeper one, a tile deeper than `DEEP_THREADGROUP` as one - /// row ([`Constant::Deep`]). + /// whatever the threadgroup size, in workgroups of [`SQUARE`] over a grid one deep and of its + /// `encoder::image_tile` over a deeper one, a tile deeper than `DEEP_THREADGROUP` as one row + /// ([`Constant::Deep`]). Square, /// The kernel observes its partition: a dispatch runs at its own threadgroup size, and a /// `dispatchThreads` grid that is not a whole number of threadgroups on the diff --git a/crates/metal2vulkan-rt/src/tests/choice.rs b/crates/metal2vulkan-rt/src/tests/choice.rs index 51f2a6365..3f954a7fd 100644 --- a/crates/metal2vulkan-rt/src/tests/choice.rs +++ b/crates/metal2vulkan-rt/src/tests/choice.rs @@ -7,7 +7,7 @@ //! its threads, so every workgroup is within the floor's `maxComputeWorkGroupSize`, whatever the //! threadgroup size. -use crate::encoder::{choose, Choice, Direct, Module}; +use crate::encoder::{choose, image_tile, Choice, Direct, Module}; use crate::floor::{WORKGROUP_INVOCATIONS, WORKGROUP_SIZE}; use crate::pipeline::{Form, SQUARE}; use metal2vulkan::pipeline::{grid_tile, DEEP_THREADGROUP, GRID_TILE_THREADS}; @@ -59,12 +59,12 @@ fn an_image_only_kernels_grid_one_deep_runs_in_square_workgroups() { } } -/// An image-only kernel's grid more than one deep runs its whole module in workgroups of the -/// grid's tile at `MultiRegion` 0, its regions carrying them: one pipeline per tile. +/// An image-only kernel's grid more than one deep runs its whole module in workgroups of its tile +/// at `MultiRegion` 0, its regions carrying them: one pipeline per tile. #[test] fn an_image_only_kernels_deeper_grid_runs_in_workgroups_of_its_tile() { for threads in [[4, 16, 312], [24, 24, 252], [256, 256, 8], [7, 5, 3]] { - let tile = grid_tile(threads); + let tile = image_tile(threads, [4, 4, 4]); let choice = choose( &Form::Square, Some(threads_of(threads, [4, 4, 4])), @@ -83,6 +83,53 @@ fn an_image_only_kernels_deeper_grid_runs_in_workgroups_of_its_tile() { } } +/// An image-only kernel's tile over a deeper grid: of the 256-thread tiles, those launching the +/// fewest threads, then with the fewest tiles partly past the grid, then the depth nearest the +/// threadgroup's (the shallower of two as near), then the most square, then the wider. Over a grid +/// every such tile divides, the threadgroup's depth decides: 8x8x4 for 8x8x4 and 4x4x4, the square +/// 16x16x1 for any one deep, as deep as the grid for a deeper one. Fewer threads launched comes +/// first: a column runs in one tile of its own, and a grid four wide in tiles four wide. +#[test] +fn an_image_only_kernels_tile_follows_the_threadgroups_depth() { + for (threads, size, tile) in [ + ([512, 512, 16], [8, 8, 4], [8, 8, 4]), + ([512, 512, 16], [4, 4, 4], [8, 8, 4]), + ([512, 512, 16], [8, 8, 1], [16, 16, 1]), + ([512, 512, 16], [16, 16, 1], [16, 16, 1]), + ([512, 512, 16], [32, 32, 1], [16, 16, 1]), + ([512, 512, 16], [8, 8, 2], [16, 8, 2]), + ([512, 512, 16], [8, 8, 3], [16, 8, 2]), + ([512, 512, 16], [1, 1, 64], [4, 4, 16]), + ([64, 64, 16], [8, 8, 4], [8, 8, 4]), + ([1, 1, 192], [8, 8, 4], [1, 1, 256]), + ([4, 16, 252], [8, 8, 4], [4, 16, 4]), + ([4, 16, 252], [8, 8, 1], [4, 16, 4]), + ] { + assert_eq!(image_tile(threads, size), tile, "{threads:?} at {size:?}"); + } + // Every tile is 256 threads, whatever the grid and threadgroup. + for threads in [ + [152, 152, 6], + [40, 40, 90], + [7, 5, 3], + [1, 1, 2], + [1000, 3, 7], + ] { + for size in [[8, 8, 4], [1, 1, 1], [32, 1, 32], [4, 4, 64]] { + let tile = image_tile(threads, size); + assert_eq!( + tile.iter().product::(), + GRID_TILE_THREADS, + "{threads:?}" + ); + assert!( + tile.iter().all(|side| side.is_power_of_two()), + "{threads:?}" + ); + } + } +} + /// A kernel that is not image-only runs every grid, one deep or deeper, in the tiles its regions /// carry, on its tiles' one pipeline. #[test]