Multi GPU APIs
To learn more about the theory of Multi GPU programming refer to this part of documentation.
Here we will cover the core multi GPU apis and a example
A Multi GPU example
In this example we will display how you can
- Fetch the number of devices installed on a machine
- For every GPU launch a thread and set an active device per thread.
- Execute a MSM on each GPU
...
let device_count = get_device_count().unwrap();
(0..device_count)
.into_par_iter()
.for_each(move |device_id| {
set_device(device_id).unwrap();
// you can allocate points and scalars_d here
let mut cfg = MSMConfig::default_for_device(device_id);
cfg.ctx.stream = &stream;
cfg.is_async = true;
cfg.are_scalars_montgomery_form = true;
msm(&scalars_d, &HostOrDeviceSlice::on_host(points), &cfg, &mut msm_results).unwrap();
// collect and process results
})
...
We use get_device_count
to fetch the number of connected devices, device IDs will be 0, 1, 2, ..., device_count - 1
into_par_iter
is a parallel iterator, you should expect it to launch a thread for every iteration.
We then call set_device(device_id).unwrap();
it should set the context of that thread to the selected device_id
.
Any data you now allocate from the context of this thread will be linked to the device_id
. We create our MSMConfig
with the selected device ID let mut cfg = MSMConfig::default_for_device(device_id);
, behind the scene this will create for us a DeviceContext
configured for that specific GPU.
We finally call our msm
method.
Device management API
To streamline device management we offer as part of icicle-cuda-runtime
package methods for dealing with devices.
set_device
Sets the current CUDA device by its ID, when calling set_device
it will set the current thread to a CUDA device.
Parameters:
device_id: usize
: The ID of the device to set as the current device. Device IDs start from 0.
Returns:
CudaResult<()>
: An empty result indicating success if the device is set successfully. In case of failure, returns aCudaError
.
Errors:
- Returns a
CudaError
if the specified device ID is invalid or if a CUDA-related error occurs during the operation.
Example:
let device_id = 0; // Device ID to set
match set_device(device_id) {
Ok(()) => println!("Device set successfully."),
Err(e) => eprintln!("Failed to set device: {:?}", e),
}