Skip to content

Instantly share code, notes, and snippets.

@ishitatsuyuki
Last active December 30, 2023 14:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishitatsuyuki/e86aa70879a8de8ed5b398b68b1ddfbc to your computer and use it in GitHub Desktop.
Save ishitatsuyuki/e86aa70879a8de8ed5b398b68b1ddfbc to your computer and use it in GitHub Desktop.
// Put this in your Cargo.toml:
// [dependencies]
// vulkano = "0.27.1"
use std::time::Instant;
use vulkano::{app_info_from_cargo_toml, Version};
use vulkano::device::{Device, Features};
use vulkano::device::physical::PhysicalDevice;
use vulkano::instance::{Instance, InstanceExtensions};
use vulkano::memory::DeviceMemory;
fn main() {
// Builds an `ApplicationInfo` by looking at the content of the `Cargo.toml` file at
// compile-time.
let app_infos = app_info_from_cargo_toml!();
let instance = Instance::new(Some(&app_infos), Version::V1_1, &InstanceExtensions::none(), None).unwrap();
let physical_device = PhysicalDevice::from_index(&instance, 0).unwrap();
let queue_family = physical_device.queue_families().filter(|x| x.supports_compute() && !x.supports_graphics()).next().unwrap();
let (device, mut queues) = Device::new(
physical_device,
&Features::none(),
&physical_device
.required_extensions(),
[(queue_family, 0.5)].iter().cloned(),
)
.unwrap();
let types: Vec<_> = device.physical_device().memory_types().filter(|x|x.is_device_local() && x.is_host_coherent()).collect();
let mtype = types[0].clone();
const LEN: u64 = 8 * 1024 * 1024;
for i in 0..2048 * 9 / 10 {
let mem = DeviceMemory::alloc_and_map(device.clone(), mtype, LEN).expect("Failed to allocate memory");
let start = Instant::now();
let ptr;
unsafe {
let mut content = mem.read_write::<[u8]>(0..LEN); // flushing ranges is free on radv, who cares
content.fill(0);
ptr = content.as_ptr();
}
std::mem::forget(mem);
println!("{:3} {:>8.3?} {:?}", i, start.elapsed(), ptr);
}
let mut buffer = String::new();
let mut stdin = std::io::stdin();
stdin.read_line(&mut buffer).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment