Module Device.Graph

Backend interface for batched replay of a fixed call sequence.

A graph records a sequence of kernel launches and buffer copies once and replays them with a single dispatch, eliminating per-call launch overhead. The engine builds the node list, tracks node dependencies, and patches per-replay state (rebound buffer arguments, variable values, launch dimensions) through exec before each launch.

type node =
  1. | Kernel of {
    1. handle : nativeint;
      (*

      Kernel handle from prog.handle.

      *)
    2. global : int array;
      (*

      Global launch dimensions (3 entries).

      *)
    3. local : int array;
      (*

      Local launch dimensions (3 entries).

      *)
    4. bufs : nativeint array;
      (*

      Buffer argument addresses.

      *)
    5. vals : int array;
      (*

      Scalar arguments.

      *)
    6. deps : int array;
      (*

      Indices of nodes this node must wait on.

      *)
    }
  2. | Copy of {
    1. dest : nativeint;
      (*

      Destination address.

      *)
    2. src : nativeint;
      (*

      Source address.

      *)
    3. nbytes : int;
      (*

      Copied byte count.

      *)
    4. deps : int array;
      (*

      Indices of nodes this node must wait on.

      *)
    }
    (*

    One recorded call. Node indices follow build order.

    *)
type exec = {
  1. set_buf : int -> int -> nativeint -> unit;
    (*

    set_buf node pos addr stages buffer argument pos of node to addr. For Copy nodes position 0 is the destination and position 1 the source.

    *)
  2. set_val : int -> int -> int -> unit;
    (*

    set_val node idx v stages scalar argument idx of node.

    *)
  3. set_launch_dims : int -> global:int array -> local:int array -> unit;
    (*

    set_launch_dims node ~global ~local stages new launch dimensions for kernel node.

    *)
  4. set_params : int -> unit;
    (*

    set_params node commits the staged state of node into the instantiated graph.

    *)
  5. launch : wait:bool -> float option;
    (*

    launch ~wait replays the graph. Returns the elapsed device time in seconds when wait is true and the backend supports timing.

    *)
}

An instantiated graph.

type t = {
  1. supports_copy : bool;
    (*

    true iff Copy nodes are supported, allowing the engine to batch buffer copies alongside kernels.

    *)
  2. build : node array -> exec;
    (*

    build nodes records and instantiates a graph over nodes.

    *)
}

The type for backend graph capabilities.