aeon

Programming Language designed for Program Synthesis with SMT-validation.


Project maintained by alcides Hosted on GitHub Pages — Theme by mattgraham

Cuda: explicit GPU buffers with linear types and refinements

Cuda.ae wraps the CUDA Driver API for explicit device memory: upload host arrays, launch fixed elementwise kernels, synchronize, download snapshots, and free allocations. It is not imported by the @gpu decorator — that path uses Gpu and tensors. Use Cuda when you want compile-time proofs about devices, sizes, byte budgets, memory kind, access mode, launch shape, shared/warp legality, and optional Status-aware sync.


Resource lifecycle

Every host array and GPU handle is linear (let 1, parameter (1 …)): each value is consumed exactly once. The typical Int vector-add path:

Array ──upload──► ReadyBuffer ──add──► Pending ──synchronize──► ReadyBuffer
                      │                                              │
                      └──────────────── download ──► I32Download ──unpack──► Array + ReadyBuffer
                                                                                    │
                                                                                 free ──► Unit
Handle Role
Device Immutable CUDA device descriptor
Launch1D / Launch2D Grid descriptors (1-D items/threads; 2-D width×height + block dims)
Stream CUDA stream for ordered kernel enqueue
ReadyBuffer a Device allocation ready for kernels or download
Pending a In-flight kernel result (must synchronize or discard)
Status / StatusBuffer a Optional sync-with-status path (check_ok / discard_status)
I32Download / Float64Download Linear token: host snapshot + preserved buffer
*DownloadPair / StatusPair Unrestricted pairs after unpack

discard abandons a Pending without reading results. free releases a ReadyBuffer. The main lifecycle above is unchanged; Status and read-only views are parallel APIs.


Refinement guarantees

The bindings prove (at compile time):

Violations are type errors, not runtime surprises.


Supported operations

Category Functions
Discovery num_devices, device, default_device, default_stream
Launch launch_1d, launch_1d_shared, launch_1d_warped, launch_2d, launch_2d_shared
Host → device upload_i32, upload_float64
Access as_read_only
Kernels add_i32, add_float64 (elementwise vector add)
Sync synchronize, synchronize_with_status, discard
Status unpack_status_buffer, status_pair_*, check_ok, discard_status
Device → host download_i32, download_float64, unpack_*, download_values_*, download_buffer_*
Release free

Aeon’s surface Float maps to CUDA float64 in this module; there is no silent float32 narrowing.

Parallel APIs (optional)

Read-only view before a kernel (both inputs may be RO):

let 1 left := as_read_only left0 in
let 1 right := as_read_only right0 in
let 1 pending := add_i32 launch stream left right in

Shared / warp-aware / 2-D launch descriptors (kernels still use 1-D Launch1D today):

let l1 := launch_1d_shared d 8 4 0 in
let lw := launch_1d_warped d 64 32 in
let l2 := launch_2d d 33 17 16 8 in

Status-aware sync (must consume Status linearly):

let 1 sb := synchronize_with_status pending in
let pair := unpack_status_buffer sb in
let 1 st := status_pair_status pair in
let 1 buf := status_pair_buffer pair in
let _ := check_ok st in

Minimal example

import Array;
import Cuda;

open Cuda

def vector_add (u: Unit) : Int :=
    let d := default_device u in
    let stream := default_stream d in
    let launch := launch_1d d 3 1 in
    let 1 xs := Array.append (Array.append (Array.append (Array.new{Int} u) 11) 22) 33 in
    let 1 ys := Array.append (Array.append (Array.append (Array.new{Int} u) 1) 2) 3 in
    let 1 left := upload_i32 d xs in
    let 1 right := upload_i32 d ys in
    let 1 pending := add_i32 launch stream left right in
    let 1 ready := synchronize pending in
    let 1 downloaded := download_i32 ready in
    let pair := unpack_i32_download downloaded in
    let 1 result := download_values_i32 pair in
    let 1 buf := download_buffer_i32 pair in
    let total := Array.sum result in
    let _ := free buf in
    total;

Requires a CUDA-capable GPU and driver at runtime. See examples/llvm/gpu/cuda_vector_add.ae.