Skip to main content

Device

Struct Device 

Source
pub struct Device { /* private fields */ }
Expand description

A device that may have a CUDA and/or OpenCL GPU associated with it.

Implementations§

Source§

impl Device

Source

pub fn vendor(&self) -> Vendor

Returns the Vendor of the GPU.

Source

pub fn name(&self) -> String

Returns the name of the GPU, e.g. “GeForce RTX 3090”.

Source

pub fn memory(&self) -> u64

Returns the memory of the GPU in bytes.

Source

pub fn compute_units(&self) -> u32

Returns the number of compute units of the GPU.

Source

pub fn compute_capability(&self) -> Option<(u32, u32)>

Returns the major and minor version of the compute capability (only available on Nvidia GPUs).

Source

pub fn unique_id(&self) -> UniqueId

Returns the best possible unique identifier, a UUID is preferred over a PCI ID.

Source

pub fn framework(&self) -> Framework

Returns the preferred framework (CUDA or OpenCL) to use.

CUDA will be be preferred over OpenCL. The returned framework will work on the device. E.g. it won’t return Framework::Cuda for an AMD device.

Source

pub fn cuda_device(&self) -> Option<&Device>

Returns the underlying CUDA device if it is available.

Examples found in repository?
examples/add.rs (line 8)
4fn cuda(device: &Device) -> Program {
5    // The kernel was compiled with:
6    // nvcc -fatbin -gencode=arch=compute_52,code=sm_52 -gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_61,code=sm_61 -gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_75,code=compute_75 --x cu add.cl
7    let cuda_kernel = include_bytes!("./add.fatbin");
8    let cuda_device = device.cuda_device().unwrap();
9    let cuda_program = cuda::Program::from_bytes(cuda_device, cuda_kernel).unwrap();
10    Program::Cuda(cuda_program)
11}
Source

pub fn opencl_device(&self) -> Option<&Device>

Returns the underlying OpenCL device if it is available.

Examples found in repository?
examples/add.rs (line 16)
14fn opencl(device: &Device) -> Program {
15    let opencl_kernel = include_str!("./add.cl");
16    let opencl_device = device.opencl_device().unwrap();
17    let opencl_program = opencl::Program::from_opencl(opencl_device, opencl_kernel).unwrap();
18    Program::Opencl(opencl_program)
19}
Source

pub fn all() -> Vec<&'static Device>

Returns all available GPUs that are supported.

Examples found in repository?
examples/add.rs (line 59)
21pub fn main() {
22    // Define some data that should be operated on.
23    let aa: Vec<u32> = vec![1, 2, 3, 4];
24    let bb: Vec<u32> = vec![5, 6, 7, 8];
25
26    // This is the core. Here we write the interaction with the GPU independent of whether it is
27    // CUDA or OpenCL.
28    let closures = program_closures!(|program, _args| -> Result<Vec<u32>, GPUError> {
29        // Make sure the input data has the same length.
30        assert_eq!(aa.len(), bb.len());
31        let length = aa.len();
32
33        // Copy the data to the GPU.
34        let aa_buffer = program.create_buffer_from_slice(&aa)?;
35        let bb_buffer = program.create_buffer_from_slice(&bb)?;
36
37        // The result buffer has the same length as the input buffers.
38        let result_buffer = unsafe { program.create_buffer::<u32>(length)? };
39
40        // Get the kernel.
41        let kernel = program.create_kernel("add", 1, 1)?;
42
43        // Execute the kernel.
44        kernel
45            .arg(&(length as u32))
46            .arg(&aa_buffer)
47            .arg(&bb_buffer)
48            .arg(&result_buffer)
49            .run()?;
50
51        // Get the resulting data.
52        let mut result = vec![0u32; length];
53        program.read_into_buffer(&result_buffer, &mut result)?;
54
55        Ok(result)
56    });
57
58    // Get the first available device.
59    let device = *Device::all().first().unwrap();
60
61    // First we run it on CUDA.
62    let cuda_program = cuda(device);
63    let cuda_result = cuda_program.run(closures, ()).unwrap();
64    assert_eq!(cuda_result, [6, 8, 10, 12]);
65    println!("CUDA result: {:?}", cuda_result);
66
67    // Then we run it on OpenCL.
68    let opencl_program = opencl(device);
69    let opencl_result = opencl_program.run(closures, ()).unwrap();
70    assert_eq!(opencl_result, [6, 8, 10, 12]);
71    println!("OpenCL result: {:?}", opencl_result);
72}
Source

pub fn by_pci_id(pci_id: PciId) -> Option<&'static Device>

Returns the device matching the PCI ID if there is one.

Source

pub fn by_uuid(uuid: DeviceUuid) -> Option<&'static Device>

Returns the device matching the UUID if there is one.

Source

pub fn by_unique_id(unique_id: UniqueId) -> Option<&'static Device>

Returns the device matching the unique ID if there is one.

Trait Implementations§

Source§

impl Clone for Device

Source§

fn clone(&self) -> Device

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Device

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Device

Source§

fn eq(&self, other: &Device) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Device

Source§

impl StructuralPartialEq for Device

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.