Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / examples / report_copy_ceiling.rs
report_copy_ceiling.rs132 lines · 5.2 KiBRaw
| 1 | //! What a plain `vkCmdCopyBuffer` achieves on this device. |
| 2 | //! |
| 3 | //! ```text |
| 4 | //! cargo run --offline --release -p modelrunner --example report_copy_ceiling |
| 5 | //! ``` |
| 6 | //! |
| 7 | //! # Why this is the denominator |
| 8 | //! |
| 9 | //! Every kernel bandwidth figure in this crate is quoted against a claim that "a plain |
| 10 | //! `vkCmdCopyBuffer` reaches 5.0 GB/s" (`crate::vulkan::imageprobe`, header). That number was |
| 11 | //! taken on a Tensor G4. This measures the same thing on whatever device it is run on, so the |
| 12 | //! claim can be checked rather than inherited. |
| 13 | //! |
| 14 | //! A copy moves bytes twice - one read and one write - so the figure reported here is |
| 15 | //! `2 * size / seconds`, the total traffic the memory system carried. The one-directional |
| 16 | //! read rate a shader can hope for is roughly half of it, which is the number to compare |
| 17 | //! against `report_read_path`'s storage-buffer column. |
| 18 | //! |
| 19 | //! Deliberately uses the transfer path and nothing else: no shader, no descriptors, no |
| 20 | //! pipeline. Whatever this reports is the floor under which no explanation involving shader |
| 21 | //! quality can hide. |
| 22 | use std::sync::Arc; |
| 23 | |
| 24 | use ash::vk; |
| 25 | use modelrunner::vulkan::buffers::Buffer; |
| 26 | use modelrunner::vulkan::context::{self, Context}; |
| 27 | |
| 28 | /// Bytes per buffer. Matches `imageprobe`'s sweep so the two are comparable. |
| 29 | const BYTES: u64 = 256 * 1024 * 1024; |
| 30 | |
| 31 | /// Timed passes. The first is discarded: it pays for first-touch and lazy allocation. |
| 32 | const PASSES: usize = 6; |
| 33 | |
| 34 | fn main() { |
| 35 | let context = match context::shared() { |
| 36 | Ok(context) => context, |
| 37 | Err(why) => { |
| 38 | println!("no usable Vulkan device: {why}"); |
| 39 | return; |
| 40 | } |
| 41 | }; |
| 42 | match measure(&context) { |
| 43 | Ok(seconds) => { |
| 44 | let moved = (BYTES * 2) as f64; |
| 45 | println!("copy {} MB src -> dst", BYTES / (1024 * 1024)); |
| 46 | println!("best {:.2} ms", seconds * 1000.0); |
| 47 | println!(); |
| 48 | println!( |
| 49 | "traffic {:.2} GB/s (read + write, {:.0} MB moved)", |
| 50 | moved / seconds / 1e9, |
| 51 | moved / 1e6 |
| 52 | ); |
| 53 | println!( |
| 54 | "one-way {:.2} GB/s (compare against report_read_path's storage column)", |
| 55 | BYTES as f64 / seconds / 1e9 |
| 56 | ); |
| 57 | } |
| 58 | Err(why) => println!("the copy probe failed: {why}"), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Seconds for the fastest of [`PASSES`] copies, the first discarded. |
| 63 | fn measure(context: &Arc<Context>) -> Result<f64, String> { |
| 64 | let device = &context.device; |
| 65 | let src = Buffer::device_local_usage( |
| 66 | context, |
| 67 | BYTES, |
| 68 | vk::BufferUsageFlags::TRANSFER_SRC | vk::BufferUsageFlags::TRANSFER_DST, |
| 69 | )?; |
| 70 | let dst = Buffer::device_local_usage( |
| 71 | context, |
| 72 | BYTES, |
| 73 | vk::BufferUsageFlags::TRANSFER_SRC | vk::BufferUsageFlags::TRANSFER_DST, |
| 74 | )?; |
| 75 | |
| 76 | // SAFETY: every object created here is used only on this thread and destroyed below, |
| 77 | // after the fence has been waited on so nothing is still in flight. |
| 78 | unsafe { |
| 79 | let command_pool = device |
| 80 | .create_command_pool( |
| 81 | &vk::CommandPoolCreateInfo::default() |
| 82 | .queue_family_index(context.queue_family_index) |
| 83 | .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER), |
| 84 | None, |
| 85 | ) |
| 86 | .map_err(|e| format!("command pool: {e:?}"))?; |
| 87 | let command_buffer = *device |
| 88 | .allocate_command_buffers( |
| 89 | &vk::CommandBufferAllocateInfo::default() |
| 90 | .command_pool(command_pool) |
| 91 | .level(vk::CommandBufferLevel::PRIMARY) |
| 92 | .command_buffer_count(1), |
| 93 | ) |
| 94 | .map_err(|e| format!("allocate: {e:?}"))? |
| 95 | .first() |
| 96 | .ok_or("no command buffer")?; |
| 97 | let fence = device |
| 98 | .create_fence(&vk::FenceCreateInfo::default(), None) |
| 99 | .map_err(|e| format!("fence: {e:?}"))?; |
| 100 | |
| 101 | device |
| 102 | .begin_command_buffer(command_buffer, &vk::CommandBufferBeginInfo::default()) |
| 103 | .map_err(|e| format!("begin: {e:?}"))?; |
| 104 | let region = [vk::BufferCopy::default().src_offset(0).dst_offset(0).size(BYTES)]; |
| 105 | device.cmd_copy_buffer(command_buffer, src.buffer, dst.buffer, ®ion); |
| 106 | device.end_command_buffer(command_buffer).map_err(|e| format!("end: {e:?}"))?; |
| 107 | |
| 108 | let mut best = f64::MAX; |
| 109 | for pass in 0..PASSES { |
| 110 | let started = std::time::Instant::now(); |
| 111 | device.reset_fences(&[fence]).map_err(|e| format!("reset: {e:?}"))?; |
| 112 | let buffers = [command_buffer]; |
| 113 | let submit = vk::SubmitInfo::default().command_buffers(&buffers); |
| 114 | let guard = context.lock_queue(); |
| 115 | let sent = device |
| 116 | .queue_submit(context.queue, std::slice::from_ref(&submit), fence) |
| 117 | .map_err(|e| format!("submit: {e:?}")); |
| 118 | drop(guard); |
| 119 | sent?; |
| 120 | device |
| 121 | .wait_for_fences(&[fence], true, 20_000_000_000) |
| 122 | .map_err(|e| format!("wait: {e:?}"))?; |
| 123 | if pass > 0 { |
| 124 | best = best.min(started.elapsed().as_secs_f64()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | device.destroy_fence(fence, None); |
| 129 | device.destroy_command_pool(command_pool, None); |
| 130 | Ok(best) |
| 131 | } |
| 132 | } |