Realm

Device capability canary

Source-owned programs from the current OA SDK. Rust and Python examples follow their respective public APIs.

Rust · Python

Example

Tu_core_device_canary.rs

1use oa::{DeviceSelection, Engine, matrix};
2
3fn main() -> anyhow::Result<()> {
4 let mut selection = DeviceSelection::Automatic;
5 let mut arguments = std::env::args().skip(1);
6 while let Some(argument) = arguments.next() {
7 match argument.as_str() {
8 "--list-devices" => {
9 for device in Engine::available_devices()? {
10 println!(
11 "[{}] {} · {:?} · Vulkan {}",
12 device.index(),
13 device.name(),
14 device.kind(),
15 device.api_version()
16 );
17 }
18 return Ok(());
19 }
20 "--device" => {
21 let index = arguments
22 .next()
23 .ok_or_else(|| anyhow::anyhow!("--device requires an index"))?;
24 selection = DeviceSelection::Index(
25 index
26 .parse()
27 .map_err(|_| anyhow::anyhow!("--device requires a nonnegative integer"))?,
28 );
29 }
30 _ => {
31 return Err(anyhow::anyhow!(
32 "unknown argument {argument}; use --list-devices or --device INDEX"
33 ));
34 }
35 }
36 }
37 let engine = Engine::builder().devices(selection).build().map_err(|e| {
38 eprintln!("device canary: Engine construction failed: {e}");
39 e
40 })?;
41
42 // Capture a minimal plan: ones + full(2) → add. The plan diagnostics
43 // and debug report are the device-admission evidence.
44 let (plan, result) = engine.capture(|| {
45 let a = matrix::ones([64, 128])?;
46 let b = matrix::full([64, 128], 2.0_f32)?;
47 matrix::add(&a, &b)
48 })?;
49
50 let event = engine.submit(&plan)?;
51 event.wait()?;
52
53 let values = result.read_f32()?;
54 let first = values.first().copied().unwrap_or(f32::NAN);
55
56 let diagnostics = plan.diagnostics();
57 let report_json = plan.debug_report_json("device_canary");
58
59 println!("OA device canary");
60 println!(" nodes: {}", diagnostics.node_count());
61 println!(" barriers: {}", diagnostics.barrier_count());
62 println!(" schema: {}", diagnostics.schema_owned_node_count());
63 println!(" add(Ones, full(2))[0] = {first:.1}");
64 println!();
65 println!("{report_json}");
66
67 assert!(
68 (first - 3.0_f32).abs() <= 1e-5,
69 "device canary arithmetic failed: expected 3.0 but got {first}"
70 );
71 assert_eq!(diagnostics.node_count(), 1, "expected one compute node");
72 Ok(())
73}
74

Run Rust tutorials from the OA checkout with cargo run --release --example <name>. Run Python with the environment containing the OA native extension. Consult each program's arguments and capability checks before running it.