1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
mod error; mod utils; pub use error::*; use sha2::{Digest, Sha256}; use std::fmt::Write; pub type BusId = u32; #[derive(Debug, Clone, Copy)] pub enum Brand { Amd, Nvidia, } pub struct Buffer<T> { buffer: ocl::Buffer<u8>, _phantom: std::marker::PhantomData<T>, } impl<T> Buffer<T> { pub fn length(&self) -> usize { self.buffer.len() / std::mem::size_of::<T>() } pub fn write_from(&mut self, data: &[T]) -> GPUResult<()> { assert!(data.len() <= self.length()); self.buffer .write(unsafe { std::slice::from_raw_parts( data.as_ptr() as *const T as *const u8, data.len() * std::mem::size_of::<T>(), ) }) .enq()?; Ok(()) } pub fn read_into(&self, data: &mut [T]) -> GPUResult<()> { assert!(data.len() <= self.length()); self.buffer .read(unsafe { std::slice::from_raw_parts_mut( data.as_mut_ptr() as *mut T as *mut u8, data.len() * std::mem::size_of::<T>(), ) }) .enq()?; Ok(()) } } impl Brand { pub fn platform_name(&self) -> &'static str { match self { Brand::Nvidia => "NVIDIA CUDA", Brand::Amd => "AMD Accelerated Parallel Processing", } } fn extract_bus_id(&self, d: ocl::Device) -> GPUResult<BusId> { Ok(match self { Brand::Nvidia => utils::get_nvidia_bus_id(d)?, Brand::Amd => utils::get_amd_bus_id(d)?, }) } } #[derive(Debug, Clone)] pub struct Device { brand: Brand, name: String, memory: u64, bus_id: BusId, platform: ocl::Platform, device: ocl::Device, } impl Device { pub fn brand(&self) -> Brand { self.brand } pub fn name(&self) -> String { self.name.clone() } pub fn memory(&self) -> u64 { self.memory } pub fn all() -> GPUResult<Vec<Device>> { let mut all = Vec::new(); for b in &[Brand::Nvidia, Brand::Amd] { all.append(&mut Device::by_brand(*b)?); } Ok(all) } pub fn by_bus_id(bus_id: BusId) -> GPUResult<Device> { Device::all().and_then(|devs| { devs.into_iter() .find(|d| d.bus_id == bus_id) .ok_or(GPUError::DeviceNotFound) }) } pub fn by_brand(brand: Brand) -> GPUResult<Vec<Device>> { match utils::find_platform(brand.platform_name())? { Some(plat) => ocl::Device::list_all(plat)? .into_iter() .map(|d| { (|| -> GPUResult<Device> { Ok(Device { brand, name: d.name()?, memory: get_memory(d)?, bus_id: brand.extract_bus_id(d)?, platform: plat, device: d, }) })() }) .collect(), None => Ok(Vec::new()), } } } pub fn get_memory(d: ocl::Device) -> ocl::Result<u64> { match d.info(ocl::enums::DeviceInfo::GlobalMemSize)? { ocl::enums::DeviceInfoResult::GlobalMemSize(sz) => Ok(sz), _ => panic!(), } } pub struct Program { device: Device, context: ocl::Context, program: ocl::Program, queue: ocl::Queue, } impl Program { pub fn device(&self) -> Device { self.device.clone() } pub fn from_opencl(device: Device, src: &str) -> GPUResult<Program> { let cached = utils::cache_path(&device, src)?; if std::path::Path::exists(&cached) { let bin = std::fs::read(cached)?; Program::from_binary(device, bin) } else { let context = ocl::Context::builder() .platform(device.platform) .devices(device.device) .build()?; let program = ocl::Program::builder() .src(src) .devices(ocl::builders::DeviceSpecifier::Single(device.device)) .build(&context)?; let queue = ocl::Queue::new(&context, device.device, None)?; let prog = Program { context, program, queue, device, }; std::fs::write(cached, prog.to_binary()?)?; Ok(prog) } } pub fn from_binary(device: Device, bin: Vec<u8>) -> GPUResult<Program> { let context = ocl::Context::builder() .platform(device.platform) .devices(device.device) .build()?; let bins = vec![&bin[..]]; let program = ocl::Program::builder() .binaries(&bins) .devices(ocl::builders::DeviceSpecifier::Single(device.device)) .build(&context)?; let queue = ocl::Queue::new(&context, device.device, None)?; Ok(Program { device, context, program, queue, }) } pub fn to_binary(&self) -> GPUResult<Vec<u8>> { match self.program.info(ocl::enums::ProgramInfo::Binaries)? { ocl::enums::ProgramInfoResult::Binaries(bins) => Ok(bins[0].clone()), _ => panic!(), } } pub fn create_buffer<T>(&self, length: usize) -> GPUResult<Buffer<T>> { assert!(length > 0); let buff = ocl::Buffer::<u8>::builder() .queue(self.queue.clone()) .flags(ocl::MemFlags::new().read_write()) .len(length * std::mem::size_of::<T>()) .build()?; buff.write(&vec![0u8]).enq()?; Ok(Buffer::<T> { buffer: buff, _phantom: std::marker::PhantomData, }) } pub fn create_buffer_flexible<T>(&self, max_length: usize) -> GPUResult<Buffer<T>> { let mut curr = max_length; let mut step = max_length / 2; let mut n = 1; while step > 0 && n < max_length { if self.create_buffer::<T>(curr).is_ok() { n = curr; curr = std::cmp::min(curr + step, max_length); } else { curr -= step; } step = step / 2; } self.create_buffer::<T>(n) } pub fn create_kernel(&self, name: &str, gws: usize, lws: Option<usize>) -> Kernel<'_> { let mut builder = ocl::Kernel::builder(); builder.name(name); builder.program(&self.program); builder.queue(self.queue.clone()); builder.global_work_size([gws]); if let Some(lws) = lws { builder.local_work_size([lws]); } Kernel::<'_> { builder } } } pub trait KernelArgument<'a> { fn push(&self, kernel: &mut Kernel<'a>); } impl<'a, T> KernelArgument<'a> for &'a Buffer<T> { fn push(&self, kernel: &mut Kernel<'a>) { kernel.builder.arg(&self.buffer); } } impl<T: ocl::OclPrm> KernelArgument<'_> for T { fn push(&self, kernel: &mut Kernel) { kernel.builder.arg(self.clone()); } } pub struct LocalBuffer<T> { length: usize, _phantom: std::marker::PhantomData<T>, } impl<T> LocalBuffer<T> { pub fn new(length: usize) -> Self { LocalBuffer::<T> { length, _phantom: std::marker::PhantomData, } } } impl<T> KernelArgument<'_> for LocalBuffer<T> { fn push(&self, kernel: &mut Kernel) { kernel .builder .arg_local::<u8>(self.length * std::mem::size_of::<T>()); } } #[derive(Debug)] pub struct Kernel<'a> { builder: ocl::builders::KernelBuilder<'a>, } impl<'a> Kernel<'a> { pub fn arg<T: KernelArgument<'a>>(mut self, t: T) -> Self { t.push(&mut self); self } pub fn run(self) -> GPUResult<()> { let kern = self.builder.build()?; unsafe { kern.enq()?; } Ok(()) } } #[macro_export] macro_rules! call_kernel { ($kernel:expr, $($arg:expr),*) => {{ $kernel $(.arg($arg))* .run() }}; }