Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / library / ml / src / main / rust / examples / report_copy_ceiling.rs

report_copy_ceiling.rs125 lines · 5.1 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.
22use std::sync::Arc;
23
24use ash::vk;
25use modelrunner::vulkan::buffers::Buffer;
26use modelrunner::vulkan::context::{self, Context};
27
28/// Bytes per buffer. Matches `imageprobe`'s sweep so the two are comparable.
29const BYTES: u64 = 256 * 1024 * 1024;
30
31/// Timed passes. The first is discarded: it pays for first-touch and lazy allocation.
32const PASSES: usize = 6;
33
34fn 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!("traffic {:.2} GB/s (read + write, {:.0} MB moved)", moved / seconds / 1e9, moved / 1e6);
49 println!("one-way {:.2} GB/s (compare against report_read_path's storage column)", BYTES as f64 / seconds / 1e9);
50 }
51 Err(why) => println!("the copy probe failed: {why}"),
52 }
53}
54
55/// Seconds for the fastest of [`PASSES`] copies, the first discarded.
56fn measure(context: &Arc<Context>) -> Result<f64, String> {
57 let device = &context.device;
58 let src = Buffer::device_local_usage(
59 context,
60 BYTES,
61 vk::BufferUsageFlags::TRANSFER_SRC | vk::BufferUsageFlags::TRANSFER_DST,
62 )?;
63 let dst = Buffer::device_local_usage(
64 context,
65 BYTES,
66 vk::BufferUsageFlags::TRANSFER_SRC | vk::BufferUsageFlags::TRANSFER_DST,
67 )?;
68
69 // SAFETY: every object created here is used only on this thread and destroyed below,
70 // after the fence has been waited on so nothing is still in flight.
71 unsafe {
72 let command_pool = device
73 .create_command_pool(
74 &vk::CommandPoolCreateInfo::default()
75 .queue_family_index(context.queue_family_index)
76 .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER),
77 None,
78 )
79 .map_err(|e| format!("command pool: {e:?}"))?;
80 let command_buffer = *device
81 .allocate_command_buffers(
82 &vk::CommandBufferAllocateInfo::default()
83 .command_pool(command_pool)
84 .level(vk::CommandBufferLevel::PRIMARY)
85 .command_buffer_count(1),
86 )
87 .map_err(|e| format!("allocate: {e:?}"))?
88 .first()
89 .ok_or("no command buffer")?;
90 let fence = device
91 .create_fence(&vk::FenceCreateInfo::default(), None)
92 .map_err(|e| format!("fence: {e:?}"))?;
93
94 device
95 .begin_command_buffer(command_buffer, &vk::CommandBufferBeginInfo::default())
96 .map_err(|e| format!("begin: {e:?}"))?;
97 let region = [vk::BufferCopy::default().src_offset(0).dst_offset(0).size(BYTES)];
98 device.cmd_copy_buffer(command_buffer, src.buffer, dst.buffer, &region);
99 device.end_command_buffer(command_buffer).map_err(|e| format!("end: {e:?}"))?;
100
101 let mut best = f64::MAX;
102 for pass in 0..PASSES {
103 let started = std::time::Instant::now();
104 device.reset_fences(&[fence]).map_err(|e| format!("reset: {e:?}"))?;
105 let buffers = [command_buffer];
106 let submit = vk::SubmitInfo::default().command_buffers(&buffers);
107 let guard = context.lock_queue();
108 let sent = device
109 .queue_submit(context.queue, std::slice::from_ref(&submit), fence)
110 .map_err(|e| format!("submit: {e:?}"));
111 drop(guard);
112 sent?;
113 device
114 .wait_for_fences(&[fence], true, 20_000_000_000)
115 .map_err(|e| format!("wait: {e:?}"))?;
116 if pass > 0 {
117 best = best.min(started.elapsed().as_secs_f64());
118 }
119 }
120
121 device.destroy_fence(fence, None);
122 device.destroy_command_pool(command_pool, None);
123 Ok(best)
124 }
125}