### RangeFrom Implementation for DeviceSliceIndex Source: https://docs.rs/cust/0.3.2/cust/memory/trait.DeviceSliceIndex.html Allows indexing into a `DeviceSlice` using a `std::ops::RangeFrom` (e.g., `start..`), selecting elements from `start` to the end. ```rust impl DeviceSliceIndex for RangeFrom ``` -------------------------------- ### Binary Search Example Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Performs a binary search on a sorted slice. Returns Ok(index) if found, or Err(insertion_index) if not found. ```rust let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; assert_eq!(s.binary_search(&13), Ok(9)); assert_eq!(s.binary_search(&4), Err(7)); assert_eq!(s.binary_search(&100), Err(13)); let r = s.binary_search(&1); assert!(match r { Ok(1..=4) => true, _ => false, }); ``` -------------------------------- ### Trim Suffix Example Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Demonstrates how to trim a suffix from a slice. If the suffix is present, it is removed. If not, the original slice is returned. ```rust #![feature(trim_prefix_suffix)] let v = &[10, 40, 30]; // Suffix present - removes it assert_eq!(v.trim_suffix(&[30]), &[10, 40][..]); assert_eq!(v.trim_suffix(&[40, 30]), &[10][..]); assert_eq!(v.trim_suffix(&[10, 40, 30]), &[][..]); // Suffix absent - returns original slice assert_eq!(v.trim_suffix(&[50]), &[10, 40, 30][..]); assert_eq!(v.trim_suffix(&[50, 30]), &[10, 40, 30][..]); ``` -------------------------------- ### Get Minor Version Number Source: https://docs.rs/cust/0.3.2/cust/struct.CudaApiVersion.html Extracts the minor version number from a CudaApiVersion instance. For example, in version 9.2, this would return 2. ```rust pub fn minor(self) -> i32 ``` -------------------------------- ### Example: Launching a Kernel for Element-wise Addition Source: https://docs.rs/cust/0.3.2/cust/prelude/macro.launch.html Demonstrates setting up the CUDA context, loading a PTX module, creating a stream, and launching a kernel for element-wise addition using both forms of the `launch!` macro. Kernel launches are asynchronous and require synchronization. ```rust use cust::memory::* use cust::module::Module use cust::stream::* use std::ffi::CString // Set up the context, load the module, and create a stream to run kernels in. let _ctx = cust::quick_init()?; let ptx = CString::new(include_str!("../resources/add.ptx"))?; let module = Module::load_from_string(&ptx)?; let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?; // Create buffers for data let mut in_x = DeviceBuffer::from_slice(&[1.0f32; 10])?; let mut in_y = DeviceBuffer::from_slice(&[2.0f32; 10])?; let mut out_1 = DeviceBuffer::from_slice(&[0.0f32; 10])?; let mut out_2 = DeviceBuffer::from_slice(&[0.0f32; 10])?; // This kernel adds each element in `in_x` and `in_y` and writes the result into `out`. unsafe { // Launch the kernel with one block of one thread, no dynamic shared memory on `stream`. let result = launch!(module.sum<<<1, 1, 0, stream>>>( in_x.as_device_ptr(), in_y.as_device_ptr(), out_1.as_device_ptr(), out_1.len() )); // `launch!` returns an error in case anything went wrong with the launch itself, but // kernel launches are asynchronous so errors caused by the kernel (eg. invalid memory // access) will show up later at some other CUDA API call (probably at `synchronize()` // below). result?; // Launch the kernel again using the `function` form: let sum = module.get_function("sum")?; // Launch with 1x1x1 (1) blocks of 10x1x1 (10) threads, to show that you can use tuples to // configure grid and block size. let result = launch!(sum<<<(1, 1, 1), (10, 1, 1), 0, stream>>>( in_x.as_device_ptr(), in_y.as_device_ptr(), out_2.as_device_ptr(), out_2.len() )); result?; } // Kernel launches are asynchronous, so we wait for the kernels to finish executing. stream.synchronize()?; // Copy the results back to host memory let mut out_host = [0.0f32; 20]; out_1.copy_to(&mut out_host[0..10])?; out_2.copy_to(&mut out_host[10..20])?; for x in out_host.iter() { assert_eq!(3.0, *x); } ``` -------------------------------- ### Get Major Version Number Source: https://docs.rs/cust/0.3.2/cust/struct.CudaApiVersion.html Extracts the major version number from a CudaApiVersion instance. For example, in version 9.2, this would return 9. ```rust pub fn major(self) -> i32 ``` -------------------------------- ### Example: Launching Kernels with cust Source: https://docs.rs/cust/0.3.2/cust/macro.launch.html Demonstrates setting up the CUDA context, loading a PTX module, creating device buffers, and launching kernels using both forms of the `launch!` macro. Kernel launches are asynchronous and require synchronization. ```rust use cust::memory::* use cust::module::Module use cust::stream::* use std::ffi::CString // Set up the context, load the module, and create a stream to run kernels in. let _ctx = cust::quick_init()? let ptx = CString::new(include_str!("../resources/add.ptx"))? let module = Module::load_from_string(&ptx)? let stream = Stream::new(StreamFlags::NON_BLOCKING, None)? // Create buffers for data let mut in_x = DeviceBuffer::from_slice(&[1.0f32; 10])? let mut in_y = DeviceBuffer::from_slice(&[2.0f32; 10])? let mut out_1 = DeviceBuffer::from_slice(&[0.0f32; 10])? let mut out_2 = DeviceBuffer::from_slice(&[0.0f32; 10])? // This kernel adds each element in `in_x` and `in_y` and writes the result into `out`. unsafe { // Launch the kernel with one block of one thread, no dynamic shared memory on `stream`. let result = launch!(module.sum<<<1, 1, 0, stream>>>( in_x.as_device_ptr(), in_y.as_device_ptr(), out_1.as_device_ptr(), out_1.len() )); // `launch!` returns an error in case anything went wrong with the launch itself, but // kernel launches are asynchronous so errors caused by the kernel (eg. invalid memory // access) will show up later at some other CUDA API call (probably at `synchronize()` // below). result? // Launch the kernel again using the `function` form: let sum = module.get_function("sum")?; // Launch with 1x1x1 (1) blocks of 10x1x1 (10) threads, to show that you can use tuples to // configure grid and block size. let result = launch!(sum<<<(1, 1, 1), (10, 1, 1), 0, stream>>>( in_x.as_device_ptr(), in_y.as_device_ptr(), out_2.as_device_ptr(), out_2.len() )); result? } // Kernel launches are asynchronous, so we wait for the kernels to finish executing. stream.synchronize()?; // Copy the results back to host memory let mut out_host = [0.0f32; 20] out_1.copy_to(&mut out_host[0..10])?; out_2.copy_to(&mut out_host[10..20])?; for x in out_host.iter() { assert_eq!(3.0, *x); } ``` -------------------------------- ### Repeat Slice Elements (Panic Example) Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Demonstrates a panic scenario when repeating slice elements with a size that would cause overflow. ```rust // this will panic at runtime b"0123456789abcdef".repeat(usize::MAX); ``` -------------------------------- ### Iterating Over Slice Chunks From the End (rchunks) Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `rchunks` to get an iterator over mutable slices of a specified size, starting from the end. The last chunk may be smaller if the slice length is not divisible by `chunk_size`. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert_eq!(iter.next().unwrap(), &['l']); assert!(iter.next().is_none()); ``` -------------------------------- ### quick_init Source: https://docs.rs/cust/0.3.2/cust/fn.quick_init.html Shortcut for initializing the CUDA Driver API and creating a CUDA context with default settings for the first device. You must keep this context alive while you do further operations or you will get an InvalidContext error. This is useful for testing or just setting up a basic CUDA context quickly. Users with more complex needs (multiple devices, custom flags, etc.) should use `init` and create their own context. ```APIDOC ## Function quick_init ### Description Shortcut for initializing the CUDA Driver API and creating a CUDA context with default settings for the first device. You must keep this context alive while you do further operations or you will get an InvalidContext error. This is useful for testing or just setting up a basic CUDA context quickly. Users with more complex needs (multiple devices, custom flags, etc.) should use `init` and create their own context. ### Signature ```rust pub fn quick_init() -> CudaResult ``` ### Usage Example ```rust let _ctx = quick_init()?; ``` ``` -------------------------------- ### Get Last Element Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `last` to get an immutable reference to the last element. Returns `None` if the slice is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last()); ``` -------------------------------- ### suggested_launch_configuration Source: https://docs.rs/cust/0.3.2/cust/function/struct.Function.html Provides a recommended block and grid size for optimal kernel launch. ```APIDOC ## suggested_launch_configuration ### Description Returns a reasonable block and grid size to achieve maximum capacity for the launch, balancing warp activity and blocks per multiprocessor. ### Method `pub fn suggested_launch_configuration(&self, dynamic_smem_size: usize, block_size_limit: BlockSize) -> CudaResult<(u32, u32)>` ### Parameters * `dynamic_smem_size` (usize) - The amount of dynamic shared memory required by this function. * `block_size_limit` (BlockSize) - The maximum block size the function is designed to handle. If 0, CUDA uses the device/function maximum. ### Response * `CudaResult<(u32, u32)>` - A tuple containing the suggested block size and grid size, or an error. ### Note Panics from `dynamic_smem_size` are ignored, and 0 will be used instead. ``` -------------------------------- ### Get Mutable Reference to Last Element Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `last_mut` to get a mutable reference to the last element. Returns `None` if the slice is empty. ```rust let x = &mut [0, 1, 2]; if let Some(last) = x.last_mut() { *last = 10; } assert_eq!(x, &[0, 1, 10]); let y: &mut [i32] = &mut []; assert_eq!(None, y.last_mut()); ``` -------------------------------- ### Suggest Launch Configuration Source: https://docs.rs/cust/0.3.2/cust/function/struct.Function.html Provides a suggested block and grid size for optimal launch performance, considering dynamic shared memory and block size limits. ```rust let (suggested_block_size, suggested_grid_size) = function.suggested_launch_configuration(dynamic_smem_size, block_size_limit)?; ``` -------------------------------- ### Initialize CUDA Context with quick_init Source: https://docs.rs/cust/0.3.2/cust/fn.quick_init.html Use this function as a shortcut to initialize the CUDA Driver API and create a CUDA context with default settings for the first device. Ensure the context remains alive for subsequent operations to avoid an InvalidContext error. ```rust pub fn quick_init() -> CudaResult ``` -------------------------------- ### Get Mutable Reference to First Element Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `first_mut` to get a mutable reference to the first element. Returns `None` if the slice is empty. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_mut() { *first = 5; } assert_eq!(x, &[5, 1, 2]); let y: &mut [i32] = &mut []; assert_eq!(None, y.first_mut()); ``` -------------------------------- ### Get First Fixed-Size Chunk Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `first_chunk` to get an array reference to the first `N` items. Returns `None` if the slice is shorter than `N`. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[10, 40]), u.first_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.first_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.first_chunk::<0>()); ``` -------------------------------- ### Quick Initialize CUDA Source: https://docs.rs/cust A shortcut for initializing the CUDA Driver API and creating a CUDA context with default settings for the first device. ```APIDOC ## quick_init ### Description Shortcut for initializing the CUDA Driver API and creating a CUDA context with default settings for the first device. ### Function Signature `pub fn quick_init() -> Result<()>` ``` -------------------------------- ### Create a new Linker Source: https://docs.rs/cust/0.3.2/cust/link/struct.Linker.html Instantiate a new Linker. This is the entry point for all linking operations. ```rust pub fn new() -> CudaResult ``` -------------------------------- ### Get Mutable References to Result Contents Source: https://docs.rs/cust/0.3.2/cust/error/type.DropResult.html Illustrates using `as_mut` to get mutable references to the values within a Result, allowing in-place modification. ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### Get Mutable First Fixed-Size Chunk Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `first_chunk_mut` to get a mutable array reference to the first `N` items. Returns `None` if the slice is shorter than `N`. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_chunk_mut::<2>() { first[0] = 5; first[1] = 4; } assert_eq!(x, &[5, 4, 2]); assert_eq!(None, x.first_chunk_mut::<4>()); ``` -------------------------------- ### ContextFlags Initialization Methods Source: https://docs.rs/cust/0.3.2/cust/context/legacy/struct.ContextFlags.html Provides methods to create new ContextFlags instances. `empty()` returns an empty set, while `all()` returns a set with all flags. ```rust pub const fn empty() -> Self ``` ```rust pub const fn all() -> Self ``` -------------------------------- ### Split Off Slice Starting from Third Element (Mutable) Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Use `split_off_mut` with a range to remove and return a sub-slice from a specific starting index. The original slice is modified in place. ```rust let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd']; let mut tail = slice.split_off_mut(2..).unwrap(); assert_eq!(slice, &mut ['a', 'b']); assert_eq!(tail, &mut ['c', 'd']); ``` -------------------------------- ### Quick Initialize CUDA Driver API and Create Context Source: https://docs.rs/cust/0.3.2/cust/index.html A shortcut function that initializes the CUDA Driver API and creates a CUDA context with default settings for the first available device. ```APIDOC ## quick_init ### Description Shortcut for initializing the CUDA Driver API and creating a CUDA context with default settings for the first device. ### Function Signature `pub fn quick_init() -> Result<(), CudaError>` ``` -------------------------------- ### Module::from_fatbin Source: https://docs.rs/cust/0.3.2/cust/module/struct.Module.html Creates a new module by loading a fatbin (fat binary) file. The driver selects the appropriate cubin or JIT compiles PTX if necessary. ```APIDOC ## Module::from_fatbin ### Description Creates a new module by loading a fatbin (fat binary) file. Fatbinary files are files that contain multiple ptx or cubin files. The driver will choose already-built cubin if it is present, and otherwise JIT compile any PTX in the file to cubin. ### Method `pub fn from_fatbin>(bytes: T, options: &[ModuleJitOption]) -> CudaResult` ### Parameters #### Request Body - **bytes** (T: AsRef<[u8]>) - Required - The byte slice representing the fatbin file. - **options** ([ModuleJitOption]) - Required - JIT compilation options. ### Request Example ```rust use cust::module::Module; let fatbin_bytes = std::fs::read("./resources/add.fatbin")?; // will return InvalidSource if the fatbin does not contain any compatible code, meaning, either // cubin compiled for the same device architecture OR PTX that can be JITted into valid code. let module = Module::from_fatbin(&fatbin_bytes, &[])?; ``` ``` -------------------------------- ### From Source: https://docs.rs/cust/0.3.2/cust/stream/struct.Stream.html Creates a new instance from another type. ```APIDOC ## fn from(t: T) -> T ### Description Returns the argument unchanged. ### Method `from` ### Parameters - **t** (T) - The value to convert from. ### Returns T ``` -------------------------------- ### RangeInclusive Implementation for DeviceSliceIndex Source: https://docs.rs/cust/0.3.2/cust/memory/trait.DeviceSliceIndex.html Allows indexing into a `DeviceSlice` using a `std::ops::RangeInclusive` (e.g., `start..=end`), including both the start and end indices. ```rust impl DeviceSliceIndex for RangeInclusive ``` -------------------------------- ### Linker::new Source: https://docs.rs/cust/0.3.2/cust/link/struct.Linker.html Creates a new linker instance. ```APIDOC ## Linker::new ### Description Creates a new linker. ### Method `pub fn new() -> CudaResult` ### Returns A new `Linker` instance wrapped in `CudaResult`. ``` -------------------------------- ### TypeId Source: https://docs.rs/cust/0.3.2/cust/stream/struct.Stream.html Gets the TypeId of the stream. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ### Parameters None ### Returns `TypeId` ``` -------------------------------- ### ContextFlags Initialization Methods Source: https://docs.rs/cust/0.3.2/cust/context/struct.ContextFlags.html Methods for creating and initializing ContextFlags. ```APIDOC ## ContextFlags Initialization Methods ### empty() Returns an empty set of flags. ### all() Returns the set containing all flags. ``` -------------------------------- ### len Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Gets the number of elements in the `DeviceBuffer`. ```APIDOC ## fn len(&self) -> usize ### Description Gets the number of elements in the `DeviceBuffer`. ``` -------------------------------- ### Linker::complete Source: https://docs.rs/cust/0.3.2/cust/link/struct.Linker.html Runs the linker to generate the final cubin bytes. ```APIDOC ## Linker::complete ### Description Runs the linker to generate the final cubin bytes. Also returns a duration for how long it took to run the linker. ### Method `pub fn complete(self) -> CudaResult>` ### Returns A `CudaResult` containing a `Vec` of the final cubin bytes. ``` -------------------------------- ### as_device_ptr Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Gets the device pointer for the `DeviceBuffer`. ```APIDOC ## fn as_device_ptr(&self) -> DevicePointer ### Description Gets the device pointer for the `DeviceBuffer`. ``` -------------------------------- ### descriptor Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayObject.html Gets the descriptor associated with this array. ```APIDOC ## descriptor ### Description Gets the descriptor associated with this array. ### Method Signature pub fn descriptor(&self) -> CudaResult ``` -------------------------------- ### launch! macro Source: https://docs.rs/cust/0.3.2/cust/macro.launch.html The `launch!` macro allows for asynchronous kernel function execution. It supports two syntax forms for specifying the kernel function and its launch configuration. ```APIDOC ## launch! macro ### Description Launches a kernel function asynchronously. The syntax is designed to resemble CUDA C's triple-chevron syntax. ### Syntax There are two forms: 1. `launch!(module.function_name<<>>(parameter1, parameter2...));` - Launches `function_name` from the specified `module`. - `module` and `stream` must be local variable names. 2. `launch!(function<<>>(parameter1, parameter2...));` - Launches the kernel referenced by the `function` variable. - Use this form when the kernel function is already available as a variable. ### Parameters - `module` (ident): The module containing the kernel function (only in the first form). - `function` (ident): The name of the kernel function or a variable holding the function. - `grid` (Into): The grid dimensions for the kernel launch. - `block` (Into): The block dimensions for the kernel launch. - `shared_memory_size` (usize): The size of dynamically allocated shared memory per thread in bytes (usually 0). - `stream` (Stream): The `Stream` object on which to launch the kernel. - `$(arg: expr),*`: A comma-separated list of arguments to pass to the kernel function. ### Safety Kernel launches must be performed within an `unsafe` block. The programmer is responsible for ensuring that the kernel parameters match the function signature and that memory accesses are valid. Host code must not access device memory that the kernel writes to until the stream is synchronized. ``` -------------------------------- ### as_raw_ptr Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Gets the raw CUDA device pointer for the `DeviceBuffer`. ```APIDOC ## fn as_raw_ptr(&self) -> CUdeviceptr ### Description Get the raw cuda device pointer ``` -------------------------------- ### Partial Ordering Methods Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedPointer.html Methods for partial comparison of UnifiedPointer instances. ```APIDOC ## partial_cmp ### Description Returns an ordering between `self` and `other` UnifiedPointer values if one exists. ### Signature `fn partial_cmp(&self, other: &UnifiedPointer) -> Option` ### Constraints `T: PartialOrd + ?Sized + DeviceCopy` ``` ```APIDOC ## lt ### Description Tests if `self` is less than `other`. Used by the `<` operator. ### Signature `fn lt(&self, other: &Rhs) -> bool` ``` ```APIDOC ## le ### Description Tests if `self` is less than or equal to `other`. Used by the `<=` operator. ### Signature `fn le(&self, other: &Rhs) -> bool` ``` ```APIDOC ## gt ### Description Tests if `self` is greater than `other`. Used by the `>` operator. ### Signature `fn gt(&self, other: &Rhs) -> bool` ``` ```APIDOC ## ge ### Description Tests if `self` is greater than or equal to `other`. Used by the `>=` operator. ### Signature `fn ge(&self, other: &Rhs) -> bool` ``` -------------------------------- ### Type Information Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedPointer.html Method to get the type ID of a type. ```APIDOC ## type_id ### Description Gets the `TypeId` of the current type. ### Signature `fn type_id(&self) -> TypeId` ### Constraints `T: 'static + ?Sized` ``` -------------------------------- ### Get ArrayDescriptor depth Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the depth of the ArrayDescriptor as a usize. ```rust pub fn depth(&self) -> usize ``` -------------------------------- ### Load and Drop Module Source: https://docs.rs/cust/0.3.2/cust/module/struct.Module.html Demonstrates loading a module from a PTX string and then dropping it. Handles potential errors during the drop operation. ```rust use cust::module::Module; use std::ffi::CString; let ptx = CString::new(include_str!("../resources/add.ptx"))?; let module = Module::load_from_string(&ptx)?; match Module::drop(module) { Ok(()) => println!("Successfully destroyed"), Err((e, module)) => { println!("Failed to destroy module: {:?}", e); // Do something with module }, } ``` -------------------------------- ### Get ArrayDescriptor height Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the height of the ArrayDescriptor as a usize. ```rust pub fn height(&self) -> usize ``` -------------------------------- ### Create All ContextFlags Source: https://docs.rs/cust/0.3.2/cust/context/struct.ContextFlags.html Returns a set containing all available flags, representing all possible CUDA context initialization options. ```rust pub const fn all() -> Self ``` -------------------------------- ### Load Module from Fatbinary Source: https://docs.rs/cust/0.3.2/cust/module/struct.Module.html Creates a module by loading a fatbinary file, which may contain multiple PTX or cubin files. The driver selects the most appropriate binary or JITs PTX if necessary. Returns `InvalidSource` if no compatible code is found. ```rust use cust::module::Module; let fatbin_bytes = std::fs::read("./resources/add.fatbin")?; // will return InvalidSource if the fatbin does not contain any compatible code, meaning, either // cubin compiled for the same device architecture OR PTX that can be JITted into valid code. let module = Module::from_fatbin(&fatbin_bytes, &[])?; ``` -------------------------------- ### Get ArrayDescriptor width Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the width of the ArrayDescriptor as a usize. ```rust pub fn width(&self) -> usize ``` -------------------------------- ### UnifiedBox::prefetch_to_host Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedBox.html Advises the driver to enqueue an operation on the stream to prefetch the memory to the CPU. ```APIDOC ## UnifiedBox::prefetch_to_host ### Description Advises the driver to enqueue an operation on the stream to prefetch the memory to the CPU. This will cause the driver to fetch the data back to the CPU as soon as the operation is reached on the stream. ### Method `MemoryAdvise` Trait Implementation ### Parameters - **self** (&self) - A shared reference to the UnifiedBox. - **stream** (&Stream) - The stream on which to enqueue the prefetch operation. ### Returns - **CudaResult<()>** - Ok(()) on success, or an error on failure. ``` -------------------------------- ### type_id Source: https://docs.rs/cust/0.3.2/cust/error/enum.CudaError.html Gets the `TypeId` of `self`. This is part of the blanket `Any` implementation. ```APIDOC #### fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. Read more Source§ ``` -------------------------------- ### starts_with Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Checks if the buffer starts with a given slice as a prefix. ```APIDOC ## pub fn starts_with(&self, needle: &[T]) -> bool ### Description Returns `true` if `needle` is a prefix of the slice or equal to the slice. ### Parameters - **needle** (*&[T]*) - The slice to check as a prefix. ### Returns - **bool** - `true` if `needle` is a prefix, `false` otherwise. ### Examples ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` Always returns `true` if `needle` is an empty slice: ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` ``` -------------------------------- ### Comparison Methods Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedPointer.html Methods for comparing UnifiedPointer instances. ```APIDOC ## cmp ### Description Compares `self` and `other` UnifiedPointer instances and returns an `Ordering`. ### Signature `fn cmp(&self, other: &UnifiedPointer) -> Ordering` ``` ```APIDOC ## max ### Description Compares two UnifiedPointer values and returns the maximum. ### Signature `fn max(self, other: Self) -> Self` ### Constraints `Self: Sized` ``` ```APIDOC ## min ### Description Compares two UnifiedPointer values and returns the minimum. ### Signature `fn min(self, other: Self) -> Self` ### Constraints `Self: Sized` ``` ```APIDOC ## clamp ### Description Restricts a UnifiedPointer value to a specified interval. ### Signature `fn clamp(self, min: Self, max: Self) -> Self` ### Constraints `Self: Sized` ``` -------------------------------- ### starts_with Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedBuffer.html Checks if the slice starts with the given prefix slice. ```APIDOC ## pub fn starts_with(&self, needle: &[T]) -> bool ### Description Returns `true` if `needle` is a prefix of the slice or equal to the slice. ### Parameters * `needle` (*[T]) - The slice to check as a prefix. ### Returns * `bool` - `true` if `needle` is a prefix, `false` otherwise. ### Examples ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` Always returns `true` if `needle` is an empty slice: ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` ``` -------------------------------- ### Create DeviceBuffer from Slice Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Allocates a DeviceBuffer and initializes it with a copy of the data from a host slice. Returns an error if allocation fails. ```rust use cust::memory::*; let values = [0u64; 5]; let mut buffer = DeviceBuffer::from_slice(&values).unwrap(); ``` -------------------------------- ### Load Module from PTX String Source: https://docs.rs/cust/0.3.2/cust/module/struct.Module.html Creates a module from a PTX string. The driver will JIT the PTX into cubin. Panics if the input string contains a null byte. ```rust use cust::module::Module; let ptx = std::fs::read("./resources/add.ptx")?; let module = Module::from_ptx(&ptx, &[])?; ``` -------------------------------- ### size_in_bytes Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Gets the size of the `DeviceBuffer`'s memory region in bytes. ```APIDOC ## fn size_in_bytes(&self) -> usize ### Description Get the size of the memory region in bytes ``` -------------------------------- ### Create Kernel Invocation for Graphs Source: https://docs.rs/cust/0.3.2/cust/index.html Creates a kernel invocation using the same syntax as `launch`, intended for use within graphs. Returns a Result of a kernel invocation object. ```APIDOC ## kernel_invocation ### Description Creates a kernel invocation using the same syntax as [`launch`] to be used to insert kernel launches inside graphs. This returns a Result of a kernel invocation object you can then pass to a graph. ### Macro Usage `#[macro_export] macro_rules! kernel_invocation { ... }` ``` -------------------------------- ### Get Device Source: https://docs.rs/cust/0.3.2/cust/context/struct.CurrentContext.html Retrieves the device identifier for the current CUDA context. ```APIDOC ## CurrentContext::get_device ### Description Return the device ID for the current context. ### Method `CurrentContext::get_device()` ### Returns - `CudaResult`: A `CudaResult` containing the `Device` if successful, or an error. ### Example ```rust let context = Context::new(device)?; let device = CurrentContext::get_device()?; ``` ``` -------------------------------- ### into_ok Source: https://docs.rs/cust/0.3.2/cust/error/type.CudaResult.html Returns the contained Ok value, never panics. This is a nightly-only experimental API. ```APIDOC ## into_ok ### Description Returns the contained `Ok` value, but never panics. This method is known to never panic on the result types it is implemented for, making it a maintainability safeguard. ### Method Signature `pub const fn into_ok(self) -> T where E: Into` ### Note This is a nightly-only experimental API. ### Examples ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); ``` ``` -------------------------------- ### iter Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedBuffer.html Returns an iterator over the slice, yielding items from start to end. ```APIDOC ## iter ### Description Returns an iterator over the slice. ### Method `pub fn iter(&self) -> Iter<'_, T>` ### Yields The iterator yields all items from start to end. ### Examples ``` let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` ``` -------------------------------- ### Implementing DeviceCopy with Derive Source: https://docs.rs/cust/0.3.2/cust/memory/trait.DeviceCopy.html Demonstrates how to implement the DeviceCopy trait for a custom struct using the derive macro. This is the recommended and safest approach, as the macro automatically checks if all fields also implement DeviceCopy. ```APIDOC ## Implement DeviceCopy with Derive To implement `DeviceCopy` for your custom type, you can use the `derive` attribute. This is the simplest and safest method, as the derive macro will verify that all fields within your type also implement `DeviceCopy`. ### Example ```rust use cust::DeviceCopy; #[derive(Clone, DeviceCopy)] struct MyStruct(u64); ``` **Note:** If any field within your struct, enum, or union does not implement `DeviceCopy` (e.g., `Vec`), the compilation will fail, preventing unsafe device memory operations. ``` -------------------------------- ### iter Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Returns an iterator over the slice, yielding elements from start to end. ```APIDOC ## pub fn iter(&self) -> Iter<'_, T> ### Description Returns an iterator over the slice. The iterator yields all items from start to end. ### Examples ``` let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` ``` -------------------------------- ### Convert Result to Option (Ok value) Source: https://docs.rs/cust/0.3.2/cust/error/type.DropResult.html Explains how to use the `ok` method to convert a Result into an Option, extracting the Ok value and discarding any error. ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` -------------------------------- ### UnifiedBox::deref Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedBox.html Dereferences the UnifiedBox to get a shared reference to the contained data. ```APIDOC ## UnifiedBox::deref ### Description Dereferences the value. ### Method `Deref` Trait Implementation ### Parameters - **self** (&self) - A shared reference to the UnifiedBox. ### Returns - **&T** - A shared reference to the contained data. ``` -------------------------------- ### Get Device Pointer Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceVariable.html Returns the device pointer to the variable stored on the device. ```rust pub fn as_device_ptr(&self) -> DevicePointer ``` -------------------------------- ### RangeFull Implementation for DeviceSliceIndex Source: https://docs.rs/cust/0.3.2/cust/memory/trait.DeviceSliceIndex.html Enables using `..` to select the entire `DeviceSlice`. ```rust impl DeviceSliceIndex for RangeFull ``` -------------------------------- ### Binary Search By Example Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Performs a binary search on a sorted slice using a custom comparison function. Returns Ok(index) if found, or Err(insertion_index) if not found. ```rust let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1..=4) => true, _ => false, }); ``` -------------------------------- ### Get ArrayDescriptor flags Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the ArrayObjectFlags of the ArrayDescriptor. Requires the `ArrayObjectFlags` type. ```rust pub fn flags(&self) -> ArrayObjectFlags ``` -------------------------------- ### clone_from Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceSlice.html Performs copy-assignment from a source `DeviceSlice` into this `DeviceSlice`. ```APIDOC ## fn clone_from(&mut self, source: &Self) ### Description Performs copy-assignment from `source`. ``` -------------------------------- ### Get ArrayDescriptor format Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the ArrayFormat of the ArrayDescriptor. Requires the `ArrayFormat` type. ```rust pub fn format(&self) -> ArrayFormat ``` -------------------------------- ### Create and Push Context Source: https://docs.rs/cust/0.3.2/cust/context/legacy/struct.Context.html Creates a new CUDA context for a given device and pushes it onto the current thread's context stack. This context will be automatically destroyed when it goes out of scope. ```APIDOC ## pub fn create_and_push( flags: ContextFlags, device: Device, ) -> CudaResult ### Description Create a CUDA context for the given device. ### Parameters #### Path Parameters - **flags** (ContextFlags) - Required - Flags to configure the context. - **device** (Device) - Required - The device for which to create the context. ### Returns - **CudaResult** - A `CudaResult` containing the created `Context` on success. ### Example ```rust cust::init(cust::CudaFlags::empty())?; let device = Device::get_device(0)?; let context = Context::create_and_push(ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO, device)?; ``` ``` -------------------------------- ### Get ArrayDescriptor dimensions Source: https://docs.rs/cust/0.3.2/cust/memory/array/struct.ArrayDescriptor.html Returns the dimensions of the ArrayDescriptor as a 3-element array of usize. ```rust pub fn dims(&self) -> [usize; 3] ``` -------------------------------- ### Debugging and Formatting Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBox.html Includes methods for debugging and formatting the DeviceBox, such as implementing the `Debug` trait. ```APIDOC ## fmt ### Description Formats the value using the given formatter. ### Method `fn fmt(&self, f: &mut Formatter<'_>) -> Result` ### Parameters - `f` (*&mut Formatter<'_>*): The formatter to use. ### Response - `Result`: Ok(()) on success, or a fmt::Error on failure. ``` -------------------------------- ### Get Context Flags Source: https://docs.rs/cust/0.3.2/cust/context/struct.CurrentContext.html Retrieves the flags associated with the current CUDA context. ```APIDOC ## CurrentContext::get_flags ### Description Return the context flags for the current context. ### Method `CurrentContext::get_flags()` ### Returns - `CudaResult`: A `CudaResult` containing the `ContextFlags` if successful, or an error. ### Example ```rust let context = Context::new(device)?; let flags = CurrentContext::get_flags()?; ``` ``` -------------------------------- ### init Source: https://docs.rs/cust/0.3.2/cust/fn.init.html Initializes the CUDA Driver API. This function must be called before any other custa function. It takes a `CudaFlags` parameter, which currently must be `CudaFlags::empty()` as no flags are defined. ```APIDOC ## init ### Description Initialize the CUDA Driver API. This must be called before any other custa function is called. Typically, this should be at the start of your program. All other functions will fail unless the API is initialized first. The `flags` parameter is used to configure the CUDA API. Currently no flags are defined, so it must be `CudaFlags::empty()`. ### Function Signature ```rust pub fn init(flags: CudaFlags) -> CudaResult<()> ``` ### Parameters #### Argument `flags` - **flags** (CudaFlags) - Required - Used to configure the CUDA API. Currently must be `CudaFlags::empty()`. ``` -------------------------------- ### Type ID of UnownedContext Source: https://docs.rs/cust/0.3.2/cust/context/legacy/struct.UnownedContext.html Gets the TypeId of the UnownedContext. This is a blanket implementation for the Any trait. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### as_rchunks Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Splits the slice into N-element arrays starting from the end, and a remainder slice. ```APIDOC ## pub fn as_rchunks(&self) -> (&[T], &[[T; N]]) ### Description Splits the slice into a slice of `N`-element arrays, starting at the end of the slice, and a remainder slice with length strictly less than `N`. The remainder is meaningful in the division sense. ### Method `as_rchunks` ### Parameters #### Path Parameters - **N** (const usize) - Required - The size of each chunk array. ### Panics Panics if `N` is zero. ### Examples ``` let slice = ['l', 'o', 'r', 'e', 'm']; let (remainder, chunks) = slice.as_rchunks(); assert_eq!(remainder, &['l']); assert_eq!(chunks, &[['o', 'r'], ['e', 'm']]); ``` ``` -------------------------------- ### Create Separate Contexts for Multiple Devices Source: https://docs.rs/cust/0.3.2/cust/context/legacy/index.html Iterate through available devices, creating and pushing a new context for each. After creation, the context is popped from the stack, and then explicitly set as current for subsequent operations. This is useful when managing multiple GPUs. ```rust // Create and pop contexts for each device let mut contexts = vec![]; for device in Device::devices()? { let device = device?; let ctx = Context::create_and_push(ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO, device)?; ContextStack::pop()?; contexts.push(ctx); } CurrentContext::set_current(&contexts[0])?; // Call cust functions which will use the context ``` -------------------------------- ### Create Zeroed DeviceBuffer Asynchronously Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Asynchronously allocates a DeviceBuffer and fills it with zeroes. Requires explicit stream synchronization to ensure completion before use. ```rust use cust::{memory::*, stream::*}; let stream = Stream::new(StreamFlags::DEFAULT, None)?; let mut values = [1u8, 2, 3, 4]; unsafe { let mut zero = DeviceBuffer::zeroed_async(4, &stream)?; zero.async_copy_to(&mut values, &stream)?; zero.drop_async(&stream)?; } stream.synchronize()?; assert_eq!(values, [0; 4]); ``` -------------------------------- ### UnifiedBox::deref_mut Source: https://docs.rs/cust/0.3.2/cust/memory/struct.UnifiedBox.html Dereferences the UnifiedBox mutably to get a mutable reference to the contained data. ```APIDOC ## UnifiedBox::deref_mut ### Description Mutably dereferences the value. ### Method `DerefMut` Trait Implementation ### Parameters - **self** (&mut self) - A mutable reference to the UnifiedBox. ### Returns - **&mut T** - A mutable reference to the contained data. ``` -------------------------------- ### Get Resource Limit Source: https://docs.rs/cust/0.3.2/cust/context/struct.CurrentContext.html Retrieves a specific resource limit for the current CUDA context. ```APIDOC ## CurrentContext::get_resource_limit ### Description Return resource limits for the current context. ### Method `CurrentContext::get_resource_limit(resource: ResourceLimit)` ### Parameters #### Path Parameters - **resource** (ResourceLimit) - Required - The type of resource limit to retrieve. ### Returns - `CudaResult`: A `CudaResult` containing the resource limit value if successful, or an error. ### Example ```rust let context = Context::new(device)?; let stack_size = CurrentContext::get_resource_limit(ResourceLimit::StackSize)?; ``` ``` -------------------------------- ### DeviceBox::from_device Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBox.html Constructs a DeviceBox from a DevicePointer. The DeviceBox takes ownership and will manage the memory. ```APIDOC ## DeviceBox::from_device ### Description Constructs a DeviceBox from a DevicePointer. After calling this function, the pointer and the memory it points to is owned by the DeviceBox. The DeviceBox destructor will free the allocated memory, but will not call the destructor of `T`. This function may accept any pointer produced by the `cuMemAllocManaged` CUDA API call, such as one taken from `DeviceBox::into_device`. ### Safety This function is unsafe because improper use may lead to memory problems. For example, a double free may occur if this function is called twice on the same pointer, or a segfault may occur if the pointer is not one returned by the appropriate API call. ### Method `unsafe fn from_device(ptr: DevicePointer) -> Self` ### Examples ```rust use cust::memory::*; let x = DeviceBox::new(&5).unwrap(); let ptr = DeviceBox::into_device(x); let x = unsafe { DeviceBox::from_device(ptr) }; ``` ``` -------------------------------- ### Create DeviceBuffer from Slice Asynchronously Source: https://docs.rs/cust/0.3.2/cust/memory/struct.DeviceBuffer.html Asynchronously allocates a DeviceBuffer and initializes it with a copy of the data from a host slice. Requires stream synchronization. ```rust use cust::memory::*; use cust::stream::{Stream, StreamFlags}; let stream = Stream::new(StreamFlags::NON_BLOCKING, None).unwrap(); let values = [0u64; 5]; unsafe { let mut buffer = DeviceBuffer::from_slice_async(&values, &stream).unwrap(); stream.synchronize(); // Perform some operation on the buffer } ``` -------------------------------- ### rchunks Source: https://docs.rs/cust/0.3.2/cust/memory/struct.LockedBuffer.html Returns an iterator over chunk_size elements of the slice at a time, starting at the end of the slice. ```APIDOC ## rchunks(&self, chunk_size: usize) -> RChunks<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are 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`. ### Method `rchunks` ### Parameters - `chunk_size`: `usize` - The size of each chunk. ### Panics Panics if `chunk_size` is zero. ```