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
impl Device
Sourcepub fn compute_units(&self) -> u32
pub fn compute_units(&self) -> u32
Returns the number of compute units of the GPU.
Sourcepub fn compute_capability(&self) -> Option<(u32, u32)>
pub fn compute_capability(&self) -> Option<(u32, u32)>
Returns the major and minor version of the compute capability (only available on Nvidia GPUs).
Sourcepub fn unique_id(&self) -> UniqueId
pub fn unique_id(&self) -> UniqueId
Returns the best possible unique identifier, a UUID is preferred over a PCI ID.
Sourcepub fn framework(&self) -> Framework
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.
Sourcepub fn cuda_device(&self) -> Option<&Device>
pub fn cuda_device(&self) -> Option<&Device>
Returns the underlying CUDA device if it is available.
Sourcepub fn opencl_device(&self) -> Option<&Device>
pub fn opencl_device(&self) -> Option<&Device>
Returns the underlying OpenCL device if it is available.
Sourcepub fn all() -> Vec<&'static Device>
pub fn all() -> Vec<&'static Device>
Returns all available GPUs that are supported.
Examples found in repository?
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}
Sourcepub fn by_pci_id(pci_id: PciId) -> Option<&'static Device>
pub fn by_pci_id(pci_id: PciId) -> Option<&'static Device>
Returns the device matching the PCI ID if there is one.
Sourcepub fn by_uuid(uuid: DeviceUuid) -> Option<&'static Device>
pub fn by_uuid(uuid: DeviceUuid) -> Option<&'static Device>
Returns the device matching the UUID if there is one.
Sourcepub fn by_unique_id(unique_id: UniqueId) -> Option<&'static Device>
pub fn by_unique_id(unique_id: UniqueId) -> Option<&'static Device>
Returns the device matching the unique ID if there is one.