use crate::ffi;
use crate::runtime::Future;
type Result<T> = std::result::Result<T, crate::error::Error>;
pub async fn num_devices() -> Result<usize> {
Future::new(ffi::device::num_devices).await
}
pub type DeviceId = i32;
pub struct Device;
impl Device {
pub async fn get() -> Result<DeviceId> {
Future::new(ffi::device::Device::get).await
}
pub async fn set(id: DeviceId) -> Result<()> {
Future::new(move || ffi::device::Device::set(id)).await
}
pub async fn synchronize() -> Result<()> {
Future::new(ffi::device::Device::synchronize).await
}
pub async fn memory_info() -> Result<MemoryInfo> {
Future::new(ffi::device::Device::memory_info).await
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MemoryInfo {
pub free: usize,
pub total: usize,
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_num_devices() {
assert!(matches!(num_devices().await, Ok(num) if num > 0));
}
#[tokio::test]
async fn test_get_device() {
assert!(matches!(Device::get().await, Ok(0)));
}
#[tokio::test]
async fn test_set_device() {
assert!(Device::set(0).await.is_ok());
assert!(matches!(Device::get().await, Ok(0)));
}
#[tokio::test]
async fn test_synchronize() {
assert!(Device::synchronize().await.is_ok());
}
#[tokio::test]
async fn test_memory_info() {
let memory_info = Device::memory_info().await.unwrap();
assert!(memory_info.free > 0);
assert!(memory_info.total > 0);
}
}