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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
//! CUDA Module (i.e. loaded PTX or cubin) use crate::{contexted_call, contexted_new, device::*, error::*, *}; use cuda::*; use num_traits::ToPrimitive; use std::{ffi::*, path::*, ptr::null_mut, sync::Arc}; /// Size of Block (thread block) in [CUDA thread hierarchy]( http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#programming-model ) /// /// Every input integer and float convert into `u32` using [ToPrimitive]. /// If the conversion is impossible, e.g. negative or too large integers, the conversion will panics. /// /// [ToPrimitive]: https://docs.rs/num-traits/0.2.11/num_traits/cast/trait.ToPrimitive.html /// /// Examples /// -------- /// /// - Explicit creation /// /// ``` /// # use accel::*; /// let block1d = Block::x(64); /// assert_eq!(block1d.x, 64); /// /// let block2d = Block::xy(64, 128); /// assert_eq!(block2d.x, 64); /// assert_eq!(block2d.y, 128); /// /// let block3d = Block::xyz(64, 128, 256); /// assert_eq!(block3d.x, 64); /// assert_eq!(block3d.y, 128); /// assert_eq!(block3d.z, 256); /// ``` /// /// - From single integer (unsigned and signed) /// /// ``` /// # use accel::*; /// let block1d: Block = 64_usize.into(); /// assert_eq!(block1d.x, 64); /// /// let block1d: Block = 64_i32.into(); /// assert_eq!(block1d.x, 64); /// ``` /// /// - From tuple /// /// ``` /// # use accel::*; /// let block1d: Block = (64,).into(); /// assert_eq!(block1d.x, 64); /// /// let block2d: Block = (64, 128).into(); /// assert_eq!(block2d.x, 64); /// assert_eq!(block2d.y, 128); /// /// let block3d: Block = (64, 128, 256).into(); /// assert_eq!(block3d.x, 64); /// assert_eq!(block3d.y, 128); /// assert_eq!(block3d.z, 256); /// ``` #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct Block { pub x: u32, pub y: u32, pub z: u32, } impl Block { /// 1D Block /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn x<I: ToPrimitive>(x: I) -> Self { Block { x: x.to_u32().expect("Cannot convert to u32"), y: 1, z: 1, } } /// 2D Block /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn xy<I1: ToPrimitive, I2: ToPrimitive>(x: I1, y: I2) -> Self { Block { x: x.to_u32().expect("Cannot convert to u32"), y: y.to_u32().expect("Cannot convert to u32"), z: 1, } } /// 3D Block /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn xyz<I1: ToPrimitive, I2: ToPrimitive, I3: ToPrimitive>(x: I1, y: I2, z: I3) -> Self { Block { x: x.to_u32().expect("Cannot convert to u32"), y: y.to_u32().expect("Cannot convert to u32"), z: z.to_u32().expect("Cannot convert to u32"), } } } impl<I: ToPrimitive> Into<Block> for (I,) { fn into(self) -> Block { Block::x(self.0) } } impl<I1: ToPrimitive, I2: ToPrimitive> Into<Block> for (I1, I2) { fn into(self) -> Block { Block::xy(self.0, self.1) } } impl<I1: ToPrimitive, I2: ToPrimitive, I3: ToPrimitive> Into<Block> for (I1, I2, I3) { fn into(self) -> Block { Block::xyz(self.0, self.1, self.2) } } macro_rules! impl_into_block { ($integer:ty) => { impl Into<Block> for $integer { fn into(self) -> Block { Block::x(self) } } }; } impl_into_block!(u8); impl_into_block!(u16); impl_into_block!(u32); impl_into_block!(u64); impl_into_block!(u128); impl_into_block!(usize); impl_into_block!(i8); impl_into_block!(i16); impl_into_block!(i32); impl_into_block!(i64); impl_into_block!(i128); impl_into_block!(isize); /// Size of Grid (grid of blocks) in [CUDA thread hierarchy]( http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#programming-model ) /// /// Every input integer and float convert into `u32` using [ToPrimitive]. /// If the conversion is impossible, e.g. negative or too large integers, the conversion will panics. /// /// [ToPrimitive]: https://docs.rs/num-traits/0.2.11/num_traits/cast/trait.ToPrimitive.html /// /// Examples /// -------- /// /// - Explicit creation /// /// ``` /// # use accel::*; /// let grid1d = Grid::x(64); /// assert_eq!(grid1d.x, 64); /// /// let grid2d = Grid::xy(64, 128); /// assert_eq!(grid2d.x, 64); /// assert_eq!(grid2d.y, 128); /// /// let grid3d = Grid::xyz(64, 128, 256); /// assert_eq!(grid3d.x, 64); /// assert_eq!(grid3d.y, 128); /// assert_eq!(grid3d.z, 256); /// ``` /// /// - From single integer (unsigned and signed) /// /// ``` /// # use accel::*; /// let grid1d: Grid = 64_usize.into(); /// assert_eq!(grid1d.x, 64); /// /// let grid1d: Grid = 64_i32.into(); /// assert_eq!(grid1d.x, 64); /// ``` /// /// - From tuple /// /// ``` /// # use accel::*; /// let grid1d: Grid = (64,).into(); /// assert_eq!(grid1d.x, 64); /// /// let grid2d: Grid = (64, 128).into(); /// assert_eq!(grid2d.x, 64); /// assert_eq!(grid2d.y, 128); /// /// let grid3d: Grid = (64, 128, 256).into(); /// assert_eq!(grid3d.x, 64); /// assert_eq!(grid3d.y, 128); /// assert_eq!(grid3d.z, 256); /// ``` #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct Grid { pub x: u32, pub y: u32, pub z: u32, } impl Grid { /// 1D Grid /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn x<I: ToPrimitive>(x: I) -> Self { Grid { x: x.to_u32().expect("Cannot convert to u32"), y: 1, z: 1, } } /// 2D Grid /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn xy<I1: ToPrimitive, I2: ToPrimitive>(x: I1, y: I2) -> Self { Grid { x: x.to_u32().expect("Cannot convert to u32"), y: y.to_u32().expect("Cannot convert to u32"), z: 1, } } /// 3D Grid /// /// Panic /// ----- /// - If input values cannot convert to u32 pub fn xyz<I1: ToPrimitive, I2: ToPrimitive, I3: ToPrimitive>(x: I1, y: I2, z: I3) -> Self { Grid { x: x.to_u32().expect("Cannot convert to u32"), y: y.to_u32().expect("Cannot convert to u32"), z: z.to_u32().expect("Cannot convert to u32"), } } } impl<I: ToPrimitive> Into<Grid> for (I,) { fn into(self) -> Grid { Grid::x(self.0) } } impl<I1: ToPrimitive, I2: ToPrimitive> Into<Grid> for (I1, I2) { fn into(self) -> Grid { Grid::xy(self.0, self.1) } } impl<I1: ToPrimitive, I2: ToPrimitive, I3: ToPrimitive> Into<Grid> for (I1, I2, I3) { fn into(self) -> Grid { Grid::xyz(self.0, self.1, self.2) } } macro_rules! impl_into_grid { ($integer:ty) => { impl Into<Grid> for $integer { fn into(self) -> Grid { Grid::x(self) } } }; } impl_into_grid!(u8); impl_into_grid!(u16); impl_into_grid!(u32); impl_into_grid!(u64); impl_into_grid!(u128); impl_into_grid!(usize); impl_into_grid!(i8); impl_into_grid!(i16); impl_into_grid!(i32); impl_into_grid!(i64); impl_into_grid!(i128); impl_into_grid!(isize); /// Represent the resource of CUDA middle-IR (PTX/cubin) #[derive(Debug)] pub enum Instruction { PTX(CString), PTXFile(PathBuf), Cubin(Vec<u8>), CubinFile(PathBuf), } impl Instruction { /// Constructor for `Instruction::PTX` pub fn ptx(s: &str) -> Instruction { let ptx = CString::new(s).expect("Invalid PTX string"); Instruction::PTX(ptx) } /// Constructor for `Instruction::Cubin` pub fn cubin(sl: &[u8]) -> Instruction { Instruction::Cubin(sl.to_vec()) } /// Constructor for `Instruction::PTXFile` pub fn ptx_file(path: &Path) -> Result<Self> { if !path.exists() { return Err(AccelError::FileNotFound { path: path.to_owned(), }); } Ok(Instruction::PTXFile(path.to_owned())) } /// Constructor for `Instruction::CubinFile` pub fn cubin_file(path: &Path) -> Result<Self> { if !path.exists() { return Err(AccelError::FileNotFound { path: path.to_owned(), }); } Ok(Instruction::CubinFile(path.to_owned())) } } impl Instruction { /// Get type of PTX/cubin pub fn input_type(&self) -> CUjitInputType { match *self { Instruction::PTX(_) | Instruction::PTXFile(_) => CUjitInputType_enum::CU_JIT_INPUT_PTX, Instruction::Cubin(_) | Instruction::CubinFile(_) => { CUjitInputType_enum::CU_JIT_INPUT_CUBIN } } } } /// CUDA Kernel function #[derive(Debug)] pub struct Kernel<'module> { func: CUfunction, module: &'module Module, } impl Contexted for Kernel<'_> { fn get_context(&self) -> Arc<Context> { self.module.get_context() } } /// Type which can be sent to the device as kernel argument /// /// ``` /// # use accel::*; /// # use std::ffi::*; /// let a: i32 = 10; /// let p = &a as *const i32; /// assert_eq!( /// DeviceSend::as_ptr(&p), /// &p as *const *const i32 as *const u8 /// ); /// assert!(std::ptr::eq( /// unsafe { *(DeviceSend::as_ptr(&p) as *mut *const i32) }, /// p /// )); /// ``` pub trait DeviceSend: Sized { /// Get the address of this value fn as_ptr(&self) -> *const u8 { self as *const Self as *const u8 } } // Use default impl impl<T> DeviceSend for *mut T {} impl<T> DeviceSend for *const T {} impl DeviceSend for bool {} impl DeviceSend for i8 {} impl DeviceSend for i16 {} impl DeviceSend for i32 {} impl DeviceSend for i64 {} impl DeviceSend for isize {} impl DeviceSend for u8 {} impl DeviceSend for u16 {} impl DeviceSend for u32 {} impl DeviceSend for u64 {} impl DeviceSend for usize {} impl DeviceSend for f32 {} impl DeviceSend for f64 {} /// Arbitary number of tuple of kernel arguments /// /// ``` /// # use accel::*; /// # use std::ffi::*; /// let a: i32 = 10; /// let b: f32 = 1.0; /// assert_eq!( /// Arguments::kernel_params(&(&a, &b)), /// vec![&a as *const i32 as *mut _, &b as *const f32 as *mut _, ] /// ); /// ``` pub trait Arguments<'arg> { /// Get a list of kernel parameters to be passed into [cuLaunchKernel] /// /// [cuLaunchKernel]: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__EXEC.html#group__CUDA__EXEC_1gb8f3dc3031b40da29d5f9a7139e52e15 fn kernel_params(&self) -> Vec<*mut c_void>; } macro_rules! impl_kernel_parameters { ($($name:ident),*; $($num:tt),*) => { impl<'arg, $($name : DeviceSend),*> Arguments<'arg> for ($( &'arg $name, )*) { fn kernel_params(&self) -> Vec<*mut c_void> { vec![$( self.$num.as_ptr() as *mut c_void ),*] } } } } impl_kernel_parameters!(;); impl_kernel_parameters!(D0; 0); impl_kernel_parameters!(D0, D1; 0, 1); impl_kernel_parameters!(D0, D1, D2; 0, 1, 2); impl_kernel_parameters!(D0, D1, D2, D3; 0, 1, 2, 3); impl_kernel_parameters!(D0, D1, D2, D3, D4; 0, 1, 2, 3, 4); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5; 0, 1, 2, 3, 4, 5); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6; 0, 1, 2, 3, 4, 5, 6); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6, D7; 0, 1, 2, 3, 4, 5, 6, 7); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6, D7, D8; 0, 1, 2, 3, 4, 5, 6, 7, 8); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6, D7, D8, D9; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); impl_kernel_parameters!(D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); /// Typed CUDA Kernel launcher /// /// This will be automatically implemented in [accel_derive::kernel] for autogenerated wrapper /// module of [Module]. /// /// ``` /// #[accel_derive::kernel] /// fn f(a: i32) {} /// ``` /// /// will create a submodule `f`: /// /// ``` /// mod f { /// pub const PTX_STR: &str = "PTX string generated by rustc/nvptx64-nvidia-cuda"; /// pub struct Module(::accel::Module); /// /* impl Module { ... } */ /// /* impl Launchable for Module { ... } */ /// } /// ``` /// /// Implementation of `Launchable` for `f::Module` is also generated by [accel_derive::kernel] /// proc-macro. /// /// [accel_derive::kernel]: https://docs.rs/accel-derive/0.3.0-alpha.1/accel_derive/attr.kernel.html /// [Module]: struct.Module.html pub trait Launchable<'arg> { /// Arguments for the kernel to be launched. /// This must be a tuple of [DeviceSend] types. /// /// [DeviceSend]: trait.DeviceSend.html type Args: Arguments<'arg>; fn get_kernel(&self) -> Result<Kernel>; /// Launch CUDA Kernel synchronously /// /// ``` /// use accel::*; /// /// #[accel_derive::kernel] /// fn f(a: i32) {} /// /// let device = Device::nth(0)?; /// let ctx = device.create_context(); /// let module = f::Module::new(ctx)?; /// let a = 12; /// module.launch((1,) /* grid */, (4,) /* block */, &(&a,))?; // wait until kernel execution ends /// # Ok::<(), ::accel::error::AccelError>(()) /// ``` fn launch<G: Into<Grid>, B: Into<Block>>( &self, grid: G, block: B, args: &Self::Args, ) -> Result<()> { let grid = grid.into(); let block = block.into(); let kernel = self.get_kernel()?; let mut params = args.kernel_params(); unsafe { contexted_call!( &kernel.get_context(), cuLaunchKernel, kernel.func, grid.x, grid.y, grid.z, block.x, block.y, block.z, 0, /* FIXME: no shared memory */ null_mut(), /* use default stream */ params.as_mut_ptr(), null_mut() /* no extra */ )?; } kernel.sync_context()?; Ok(()) } } /// OOP-like wrapper of `cuModule*` APIs #[derive(Debug)] pub struct Module { module: CUmodule, context: Arc<Context>, } impl Drop for Module { fn drop(&mut self) { if let Err(e) = unsafe { contexted_call!(&self.get_context(), cuModuleUnload, self.module) } { log::error!("Failed to unload module: {:?}", e); } } } impl Contexted for Module { fn get_context(&self) -> Arc<Context> { self.context.clone() } } impl Module { /// integrated loader of Instruction pub fn load(context: Arc<Context>, data: &Instruction) -> Result<Self> { match *data { Instruction::PTX(ref ptx) => { let module = unsafe { contexted_new!(&context, cuModuleLoadData, ptx.as_ptr() as *const _)? }; Ok(Module { module, context }) } Instruction::Cubin(ref bin) => { let module = unsafe { contexted_new!(&context, cuModuleLoadData, bin.as_ptr() as *const _)? }; Ok(Module { module, context }) } Instruction::PTXFile(ref path) | Instruction::CubinFile(ref path) => { let filename = path_to_cstring(path); let module = unsafe { contexted_new!(&context, cuModuleLoad, filename.as_ptr())? }; Ok(Module { module, context }) } } } pub fn from_str(context: Arc<Context>, ptx: &str) -> Result<Self> { let data = Instruction::ptx(ptx); Self::load(context, &data) } /// Wrapper of `cuModuleGetFunction` pub fn get_kernel(&self, name: &str) -> Result<Kernel> { let name = CString::new(name).expect("Invalid Kernel name"); let func = unsafe { contexted_new!( &self.get_context(), cuModuleGetFunction, self.module, name.as_ptr() ) }?; Ok(Kernel { func, module: self }) } } fn path_to_cstring(path: &Path) -> CString { CString::new(path.to_str().unwrap()).expect("Invalid Path") } #[cfg(test)] mod tests { use super::*; #[test] fn load_do_nothing() -> Result<()> { // generated by do_nothing example in accel-derive let ptx = r#" .version 3.2 .target sm_30 .address_size 64 .visible .entry do_nothing() { ret; } "#; let device = Device::nth(0)?; let ctx = device.create_context(); let _mod = Module::from_str(ctx, ptx)?; Ok(()) } }