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
use crate::error::{CudaResult, ToResult}; use crate::memory::device::AsyncCopyDestination; use crate::memory::device::{CopyDestination, DeviceBuffer}; use crate::memory::DeviceCopy; use crate::memory::DevicePointer; use crate::stream::Stream; use cuda_sys::cuda; use std::iter::{ExactSizeIterator, FusedIterator}; use std::mem; use std::ops::{ Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, }; use std::os::raw::c_void; use std::slice::{self, Chunks, ChunksMut}; /// Fixed-size device-side slice. #[derive(Debug)] #[repr(C)] pub struct DeviceSlice<T>([T]); // This works by faking a regular slice out of the device raw-pointer and the length and transmuting // I have no idea if this is safe or not. Probably not, though I can't imagine how the compiler // could possibly know that the pointer is not de-referenceable. I'm banking that we get proper // Dynamicaly-sized Types before the compiler authors break this assumption. impl<T> DeviceSlice<T> { /// Returns the number of elements in the slice. /// /// # Examples /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let a = DeviceBuffer::from_slice(&[1, 2, 3]).unwrap(); /// assert_eq!(a.len(), 3); /// ``` pub fn len(&self) -> usize { self.0.len() } /// Returns `true` if the slice has a length of 0. /// /// # Examples /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let a : DeviceBuffer<u64> = unsafe { DeviceBuffer::uninitialized(0).unwrap() }; /// assert!(a.is_empty()); /// ``` pub fn is_empty(&self) -> bool { self.0.is_empty() } /// Return a raw device-pointer to the slice's buffer. /// /// The caller must ensure that the slice outlives the pointer this function returns, or else /// it will end up pointing to garbage. The caller must also ensure that the pointer is not /// dereferenced by the CPU. /// /// Examples: /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let a = DeviceBuffer::from_slice(&[1, 2, 3]).unwrap(); /// println!("{:p}", a.as_ptr()); /// ``` pub fn as_ptr(&self) -> *const T { self.0.as_ptr() } /// Returns an unsafe mutable device-pointer to the slice's buffer. /// /// The caller must ensure that the slice outlives the pointer this function returns, or else /// it will end up pointing to garbage. The caller must also ensure that the pointer is not /// dereferenced by the CPU. /// /// Examples: /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let mut a = DeviceBuffer::from_slice(&[1, 2, 3]).unwrap(); /// println!("{:p}", a.as_mut_ptr()); /// ``` pub fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr() } /// Divides one DeviceSlice into two at a given index. /// /// The first will contain all indices from `[0, mid)` (excluding the index `mid` itself) and /// the second will contain all indices from `[mid, len)` (excluding the index `len` itself). /// /// # Panics /// /// Panics if `min > len`. /// /// Examples: /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let buf = DeviceBuffer::from_slice(&[0u64, 1, 2, 3, 4, 5]).unwrap(); /// let (left, right) = buf.split_at(3); /// let mut left_host = [0u64, 0, 0]; /// let mut right_host = [0u64, 0, 0]; /// left.copy_to(&mut left_host).unwrap(); /// right.copy_to(&mut right_host).unwrap(); /// assert_eq!([0u64, 1, 2], left_host); /// assert_eq!([3u64, 4, 5], right_host); /// ``` pub fn split_at(&self, mid: usize) -> (&DeviceSlice<T>, &DeviceSlice<T>) { let (left, right) = self.0.split_at(mid); unsafe { ( DeviceSlice::from_slice(left), DeviceSlice::from_slice(right), ) } } /// Divides one mutable DeviceSlice into two at a given index. /// /// The first will contain all indices from `[0, mid)` (excluding the index `mid` itself) and /// the second will contain all indices from `[mid, len)` (excluding the index `len` itself). /// /// # Panics /// /// Panics if `min > len`. /// /// Examples: /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let mut buf = DeviceBuffer::from_slice(&[0u64, 0, 0, 0, 0, 0]).unwrap(); /// /// { /// let (left, right) = buf.split_at_mut(3); /// let left_host = [0u64, 1, 2]; /// let right_host = [3u64, 4, 5]; /// left.copy_from(&left_host).unwrap(); /// right.copy_from(&right_host).unwrap(); /// } /// /// let mut host_full = [0u64; 6]; /// buf.copy_to(&mut host_full).unwrap(); /// assert_eq!([0u64, 1, 2, 3, 4, 5], host_full); /// ``` pub fn split_at_mut(&mut self, mid: usize) -> (&mut DeviceSlice<T>, &mut DeviceSlice<T>) { let (left, right) = self.0.split_at_mut(mid); unsafe { ( DeviceSlice::from_slice_mut(left), DeviceSlice::from_slice_mut(right), ) } } /// Returns an iterator over `chunk_size` elements of the slice at a time. The chunks are device /// slices and do not overlap. If `chunk_size` does not divide the length of the slice, then the /// last chunk will not have length `chunk_size`. /// /// See `exact_chunks` for a variant of this iterator that returns chunks of always exactly /// `chunk_size` elements. /// /// # Panics /// /// Panics if `chunk_size` is 0. /// /// # Examples /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let slice = DeviceBuffer::from_slice(&[1u64, 2, 3, 4, 5]).unwrap(); /// let mut iter = slice.chunks(2); /// /// assert_eq!(iter.next().unwrap().len(), 2); /// /// let mut host_buf = [0u64, 0]; /// iter.next().unwrap().copy_to(&mut host_buf).unwrap(); /// assert_eq!([3, 4], host_buf); /// /// assert_eq!(iter.next().unwrap().len(), 1); /// /// ``` pub fn chunks(&self, chunk_size: usize) -> DeviceChunks<T> { DeviceChunks(self.0.chunks(chunk_size)) } /// Returns an iterator over `chunk_size` elements of the slice at a time. The chunks are /// mutable device slices and do not overlap. If `chunk_size` does not divide the length of the /// slice, then the last chunk will not have length `chunk_size`. /// /// See `exact_chunks` for a variant of this iterator that returns chunks of always exactly /// `chunk_size` elements. /// /// # Panics /// /// Panics if `chunk_size` is 0. /// /// # Examples /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let mut slice = DeviceBuffer::from_slice(&[0u64, 0, 0, 0, 0]).unwrap(); /// { /// let mut iter = slice.chunks_mut(2); /// /// assert_eq!(iter.next().unwrap().len(), 2); /// /// let host_buf = [2u64, 3]; /// iter.next().unwrap().copy_from(&host_buf).unwrap(); /// /// assert_eq!(iter.next().unwrap().len(), 1); /// } /// /// let mut host_buf = [0u64, 0, 0, 0, 0]; /// slice.copy_to(&mut host_buf).unwrap(); /// assert_eq!([0u64, 0, 2, 3, 0], host_buf); /// ``` pub fn chunks_mut(&mut self, chunk_size: usize) -> DeviceChunksMut<T> { DeviceChunksMut(self.0.chunks_mut(chunk_size)) } /// Private function used to transmute a CPU slice (which must have the device pointer as it's /// buffer pointer) to a DeviceSlice. Completely unsafe. pub(super) unsafe fn from_slice(slice: &[T]) -> &DeviceSlice<T> { &*(slice as *const [T] as *const DeviceSlice<T>) } /// Private function used to transmute a mutable CPU slice (which must have the device pointer /// as it's buffer pointer) to a mutable DeviceSlice. Completely unsafe. pub(super) unsafe fn from_slice_mut(slice: &mut [T]) -> &mut DeviceSlice<T> { &mut *(slice as *mut [T] as *mut DeviceSlice<T>) } /// Returns a `DevicePointer<T>` to the buffer. /// /// The caller must ensure that the buffer outlives the returned pointer, or it will end up /// pointing to garbage. /// /// Modifying `DeviceBuffer` is guaranteed not to cause its buffer to be reallocated, so pointers /// cannot be invalidated in that manner, but other types may be added in the future which can /// reallocate. pub fn as_device_ptr(&mut self) -> DevicePointer<T> { unsafe { DevicePointer::wrap(self.0.as_mut_ptr()) } } /// Forms a slice from a `DevicePointer` and a length. /// /// The `len` argument is the number of _elements_, not the number of bytes. /// /// # Safety /// /// This function is unsafe as there is no guarantee that the given pointer is valid for `len` /// elements, nor whether the lifetime inferred is a suitable lifetime for the returned slice. /// /// # Caveat /// /// The lifetime for the returned slice is inferred from its usage. To prevent accidental misuse, /// it's suggested to tie the lifetime to whatever source lifetime is safe in the context, such /// as by providing a helper function taking the lifetime of a host value for the slice or /// by explicit annotation. /// /// # Examples /// /// ``` /// # let _context = rustacuda::quick_init().unwrap(); /// use rustacuda::memory::*; /// let mut x = DeviceBuffer::from_slice(&[0u64, 1, 2, 3, 4, 5]).unwrap(); /// // Manually slice the buffer (this is not recommended!) /// let ptr = unsafe { x.as_device_ptr().offset(1) }; /// let slice = unsafe { DeviceSlice::from_raw_parts(ptr, 2) }; /// let mut host_buf = [0u64, 0]; /// slice.copy_to(&mut host_buf).unwrap(); /// assert_eq!([1u64, 2], host_buf); /// ``` #[allow(clippy::needless_pass_by_value)] pub unsafe fn from_raw_parts<'a>(data: DevicePointer<T>, len: usize) -> &'a DeviceSlice<T> { DeviceSlice::from_slice(slice::from_raw_parts(data.as_raw(), len)) } /// Performs the same functionality as `from_raw_parts`, except that a /// mutable slice is returned. /// /// # Safety /// /// This function is unsafe as there is no guarantee that the given pointer is valid for `len` /// elements, nor whether the lifetime inferred is a suitable lifetime for the returned slice. /// /// This function is unsafe as there is no guarantee that the given pointer is valid for `len` /// elements, not whether the lifetime inferred is a suitable lifetime for the returned slice, /// as well as not being able to provide a non-aliasing guarantee of the returned /// mutable slice. `data` must be non-null and aligned even for zero-length /// slices as with `from_raw_parts`. /// /// See the documentation of `from_raw_parts` for more details. pub unsafe fn from_raw_parts_mut<'a>( mut data: DevicePointer<T>, len: usize, ) -> &'a mut DeviceSlice<T> { DeviceSlice::from_slice_mut(slice::from_raw_parts_mut(data.as_raw_mut(), len)) } } /// An iterator over a [`DeviceSlice`](struct.DeviceSlice.html) in (non-overlapping) chunks /// (`chunk_size` elements at a time). /// /// When the slice len is not evenly divided by the chunk size, the last slice of the iteration will /// be the remainder. /// /// This struct is created by the `chunks` method on `DeviceSlices`. #[derive(Debug, Clone)] pub struct DeviceChunks<'a, T: 'a>(Chunks<'a, T>); impl<'a, T> Iterator for DeviceChunks<'a, T> { type Item = &'a DeviceSlice<T>; fn next(&mut self) -> Option<&'a DeviceSlice<T>> { self.0 .next() .map(|slice| unsafe { DeviceSlice::from_slice(slice) }) } fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() } fn count(self) -> usize { self.0.len() } fn nth(&mut self, n: usize) -> Option<Self::Item> { self.0 .nth(n) .map(|slice| unsafe { DeviceSlice::from_slice(slice) }) } #[inline] fn last(self) -> Option<Self::Item> { self.0 .last() .map(|slice| unsafe { DeviceSlice::from_slice(slice) }) } } impl<'a, T> DoubleEndedIterator for DeviceChunks<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a DeviceSlice<T>> { self.0 .next_back() .map(|slice| unsafe { DeviceSlice::from_slice(slice) }) } } impl<'a, T> ExactSizeIterator for DeviceChunks<'a, T> {} impl<'a, T> FusedIterator for DeviceChunks<'a, T> {} /// An iterator over a [`DeviceSlice`](struct.DeviceSlice.html) in (non-overlapping) mutable chunks /// (`chunk_size` elements at a time). /// /// When the slice len is not evenly divided by the chunk size, the last slice of the iteration will /// be the remainder. /// /// This struct is created by the `chunks` method on `DeviceSlices`. #[derive(Debug)] pub struct DeviceChunksMut<'a, T: 'a>(ChunksMut<'a, T>); impl<'a, T> Iterator for DeviceChunksMut<'a, T> { type Item = &'a mut DeviceSlice<T>; fn next(&mut self) -> Option<&'a mut DeviceSlice<T>> { self.0 .next() .map(|slice| unsafe { DeviceSlice::from_slice_mut(slice) }) } fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() } fn count(self) -> usize { self.0.len() } fn nth(&mut self, n: usize) -> Option<Self::Item> { self.0 .nth(n) .map(|slice| unsafe { DeviceSlice::from_slice_mut(slice) }) } #[inline] fn last(self) -> Option<Self::Item> { self.0 .last() .map(|slice| unsafe { DeviceSlice::from_slice_mut(slice) }) } } impl<'a, T> DoubleEndedIterator for DeviceChunksMut<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a mut DeviceSlice<T>> { self.0 .next_back() .map(|slice| unsafe { DeviceSlice::from_slice_mut(slice) }) } } impl<'a, T> ExactSizeIterator for DeviceChunksMut<'a, T> {} impl<'a, T> FusedIterator for DeviceChunksMut<'a, T> {} macro_rules! impl_index { ($($t:ty)*) => { $( impl<T> Index<$t> for DeviceSlice<T> { type Output = DeviceSlice<T>; fn index(&self, index: $t) -> &Self { unsafe { DeviceSlice::from_slice(self.0.index(index)) } } } impl<T> IndexMut<$t> for DeviceSlice<T> { fn index_mut(&mut self, index: $t) -> &mut Self { unsafe { DeviceSlice::from_slice_mut( self.0.index_mut(index)) } } } )* } } impl_index! { Range<usize> RangeFull RangeFrom<usize> RangeInclusive<usize> RangeTo<usize> RangeToInclusive<usize> } impl<T> crate::private::Sealed for DeviceSlice<T> {} impl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> CopyDestination<I> for DeviceSlice<T> { fn copy_from(&mut self, val: &I) -> CudaResult<()> { let val = val.as_ref(); assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { unsafe { cuda::cuMemcpyHtoD_v2( self.0.as_mut_ptr() as u64, val.as_ptr() as *const c_void, size, ) .to_result()? } } Ok(()) } fn copy_to(&self, val: &mut I) -> CudaResult<()> { let val = val.as_mut(); assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { unsafe { cuda::cuMemcpyDtoH_v2(val.as_mut_ptr() as *mut c_void, self.as_ptr() as u64, size) .to_result()? } } Ok(()) } } impl<T: DeviceCopy> CopyDestination<DeviceSlice<T>> for DeviceSlice<T> { fn copy_from(&mut self, val: &DeviceSlice<T>) -> CudaResult<()> { assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { unsafe { cuda::cuMemcpyDtoD_v2(self.0.as_mut_ptr() as u64, val.as_ptr() as u64, size) .to_result()? } } Ok(()) } fn copy_to(&self, val: &mut DeviceSlice<T>) -> CudaResult<()> { assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { unsafe { cuda::cuMemcpyDtoD_v2(val.as_mut_ptr() as u64, self.as_ptr() as u64, size) .to_result()? } } Ok(()) } } impl<T: DeviceCopy> CopyDestination<DeviceBuffer<T>> for DeviceSlice<T> { fn copy_from(&mut self, val: &DeviceBuffer<T>) -> CudaResult<()> { self.copy_from(val as &DeviceSlice<T>) } fn copy_to(&self, val: &mut DeviceBuffer<T>) -> CudaResult<()> { self.copy_to(val as &mut DeviceSlice<T>) } } impl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> AsyncCopyDestination<I> for DeviceSlice<T> { unsafe fn async_copy_from(&mut self, val: &I, stream: &Stream) -> CudaResult<()> { let val = val.as_ref(); assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { cuda::cuMemcpyHtoDAsync_v2( self.0.as_mut_ptr() as u64, val.as_ptr() as *const c_void, size, stream.as_inner(), ) .to_result()? } Ok(()) } unsafe fn async_copy_to(&self, val: &mut I, stream: &Stream) -> CudaResult<()> { let val = val.as_mut(); assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { cuda::cuMemcpyDtoHAsync_v2( val.as_mut_ptr() as *mut c_void, self.as_ptr() as u64, size, stream.as_inner(), ) .to_result()? } Ok(()) } } impl<T: DeviceCopy> AsyncCopyDestination<DeviceSlice<T>> for DeviceSlice<T> { unsafe fn async_copy_from(&mut self, val: &DeviceSlice<T>, stream: &Stream) -> CudaResult<()> { assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { cuda::cuMemcpyDtoDAsync_v2( self.0.as_mut_ptr() as u64, val.as_ptr() as u64, size, stream.as_inner(), ) .to_result()? } Ok(()) } unsafe fn async_copy_to(&self, val: &mut DeviceSlice<T>, stream: &Stream) -> CudaResult<()> { assert!( self.len() == val.len(), "destination and source slices have different lengths" ); let size = mem::size_of::<T>() * self.len(); if size != 0 { cuda::cuMemcpyDtoDAsync_v2( val.as_mut_ptr() as u64, self.as_ptr() as u64, size, stream.as_inner(), ) .to_result()? } Ok(()) } } impl<T: DeviceCopy> AsyncCopyDestination<DeviceBuffer<T>> for DeviceSlice<T> { unsafe fn async_copy_from(&mut self, val: &DeviceBuffer<T>, stream: &Stream) -> CudaResult<()> { self.async_copy_from(val as &DeviceSlice<T>, stream) } unsafe fn async_copy_to(&self, val: &mut DeviceBuffer<T>, stream: &Stream) -> CudaResult<()> { self.async_copy_to(val as &mut DeviceSlice<T>, stream) } }