Realm

Engine and device introduction

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

Rust · Python

Example

Tu_core_engine_intro.rs

1use oa::{Engine, matrix};
2
3fn main() -> oa::Result<()> {
4 let engine = Engine::new()?;
5
6 println!("OA engine");
7
8 // Domain operations record into the engine's private eager session. No
9 // public graph or context object is required. The matrix values are
10 // returned immediately as semantic handles.
11 let a = matrix::ones([64, 128])?;
12 let b = matrix::full([64, 128], 2.0_f32)?;
13 let output = matrix::add(&a, &b)?;
14
15 // `engine.checkpoint()` submits all pending eager work and returns an
16 // Event without blocking the host. Calling it is optional — host
17 // observation (read_f32 below) also flushes and waits.
18 let checkpoint = engine.checkpoint()?;
19 checkpoint.wait()?;
20
21 // `read_f32` is the explicit device-to-host boundary. It flushes any
22 // remaining eager work, waits for the result, and copies to a Vec.
23 let values = output.read_f32()?;
24 let first = values.first().copied().unwrap_or(f32::NAN);
25
26 println!(" add(Ones, full(2))[0] = {first:.1}");
27 println!(" shape: {:?}", output.shape());
28 println!(" elements: {}", values.len());
29
30 assert!(
31 (first - 3.0_f32).abs() <= 1e-5,
32 "unexpected value: expected 3.0 but got {first}"
33 );
34 assert_eq!(output.shape(), [64, 128]);
35 assert_eq!(values.len(), 64 * 128);
36 Ok(())
37}
38

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.