decuda 0.1.1

CUDA to HIP, SYCL, OpenCL, and Rust GPU migration tool — automatic source-code translator for porting CUDA C++ kernels to AMD ROCm HIP, Intel oneAPI SYCL, Khronos OpenCL, and Rust GPU (cust / rust-gpu)
Documentation
//! Programmatic migration example: parse a CUDA source in memory and emit
//! each backend's output to stdout, without going through the CLI.
//!
//! Run with: `cargo run --example migrate -- examples/cu/saxpy.cu`

use std::path::PathBuf;

use decuda::cli::Target;
use decuda::parser::translate_source;
use decuda::targets::for_target;

fn main() {
    let path = std::env::args_os()
        .nth(1)
        .map(PathBuf::from)
        .unwrap_or_else(|| {
            PathBuf::from(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/examples/cu/saxpy.cu"
            ))
        });

    let src = std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    let unit = translate_source(src, path);

    println!("=== IR nodes ({} total) ===", unit.nodes.len());
    for n in &unit.nodes {
        println!("  {}", n.describe());
    }

    for t in Target::iter_real() {
        let backend = for_target(t);
        let out = backend.emit(&unit);
        let banner = format!("\n=== {} output ({} bytes) ===", t.as_str(), out.len());
        println!("{banner}");
        // Print the first 12 lines so the example stays readable; the full
        // output is what `decuda migrate` would write to disk.
        for line in out.lines().take(12) {
            println!("{line}");
        }
        let rest = out.lines().count().saturating_sub(12);
        if rest > 0 {
            println!("... ({rest} more lines)");
        }
    }
}