Class MetalComputeGraph
- Namespace
- DotCompute.Backends.Metal.Execution.Graph
- Assembly
- DotCompute.Backends.Metal.dll
Represents a Metal computational graph that can be constructed, optimized, and executed efficiently. Provides high-performance execution through kernel fusion and optimization for Apple Silicon architecture.
public sealed class MetalComputeGraph : IDisposable
- Inheritance
-
MetalComputeGraph
- Implements
- Inherited Members
- Extension Methods
Remarks
Metal compute graphs enable efficient execution of complex computation pipelines by reducing CPU-GPU synchronization overhead and enabling advanced optimizations like kernel fusion, memory coalescing, and command buffer batching.
Constructors
MetalComputeGraph(string, ILogger<MetalComputeGraph>?)
Initializes a new instance of the MetalComputeGraph class.
public MetalComputeGraph(string name, ILogger<MetalComputeGraph>? logger = null)
Parameters
namestringThe human-readable name for this graph.
loggerILogger<MetalComputeGraph>Optional logger instance for debugging and monitoring.
Exceptions
- ArgumentException
Thrown when name is null or whitespace.
Properties
Configuration
Gets the configuration options for this graph.
public MetalGraphConfiguration Configuration { get; }
Property Value
CreatedAt
Gets the timestamp when this graph was created.
public DateTimeOffset CreatedAt { get; }
Property Value
EstimatedMemoryFootprint
Gets the total estimated memory footprint of this graph in bytes.
public long EstimatedMemoryFootprint { get; }
Property Value
Id
Gets the unique identifier for this Metal compute graph.
public string Id { get; }
Property Value
IsBuilt
Gets a value indicating whether the graph has been built and is ready for execution.
public bool IsBuilt { get; }
Property Value
IsEmpty
Gets a value indicating whether the graph is empty (contains no nodes).
public bool IsEmpty { get; }
Property Value
IsOptimized
Gets a value indicating whether the graph has been optimized for execution.
public bool IsOptimized { get; }
Property Value
Name
Gets the human-readable name for this Metal compute graph.
public string Name { get; }
Property Value
NodeCount
Gets the current count of nodes in the graph.
public int NodeCount { get; }
Property Value
Nodes
Gets the thread-safe collection of graph nodes that comprise this Metal compute graph.
public IReadOnlyList<MetalGraphNode> Nodes { get; }
Property Value
Statistics
Gets the current statistics for this graph.
public MetalGraphStatistics Statistics { get; }
Property Value
Methods
AddBarrierNode(MetalGraphNode[])
Adds a synchronization barrier node to the graph.
public MetalGraphNode AddBarrierNode(MetalGraphNode[] dependencies)
Parameters
dependenciesMetalGraphNode[]The nodes that must complete before subsequent operations.
Returns
- MetalGraphNode
The created barrier node.
AddKernelNode(ICompiledKernel, MTLSize, MTLSize, object[], MetalGraphNode[]?)
Adds a kernel computation node to the graph.
public MetalGraphNode AddKernelNode(ICompiledKernel kernel, MTLSize threadgroupsPerGrid, MTLSize threadsPerThreadgroup, object[] arguments, MetalGraphNode[]? dependencies = null)
Parameters
kernelICompiledKernelThe compiled Metal kernel to execute.
threadgroupsPerGridMTLSizeThe number of threadgroups to dispatch.
threadsPerThreadgroupMTLSizeThe number of threads per threadgroup.
argumentsobject[]The kernel arguments.
dependenciesMetalGraphNode[]Optional array of nodes this kernel depends on.
Returns
- MetalGraphNode
The created kernel node.
Exceptions
- ArgumentNullException
Thrown when kernel is null.
- ObjectDisposedException
Thrown when the graph has been disposed.
AddMemoryCopyNode(nint, nint, long, MetalGraphNode[]?)
Adds a memory copy node to the graph.
public MetalGraphNode AddMemoryCopyNode(nint sourceBuffer, nint destinationBuffer, long size, MetalGraphNode[]? dependencies = null)
Parameters
sourceBuffernintThe source Metal buffer.
destinationBuffernintThe destination Metal buffer.
sizelongThe number of bytes to copy.
dependenciesMetalGraphNode[]Optional array of nodes this copy depends on.
Returns
- MetalGraphNode
The created memory copy node.
AddMemorySetNode(nint, byte, long, MetalGraphNode[]?)
Adds a memory set node to the graph.
public MetalGraphNode AddMemorySetNode(nint buffer, byte value, long size, MetalGraphNode[]? dependencies = null)
Parameters
buffernintThe Metal buffer to fill.
valuebyteThe value to fill with.
sizelongThe number of bytes to fill.
dependenciesMetalGraphNode[]Optional array of nodes this operation depends on.
Returns
- MetalGraphNode
The created memory set node.
AnalyzeOptimizationOpportunities()
Analyzes the graph for optimization opportunities.
public MetalGraphAnalysis AnalyzeOptimizationOpportunities()
Returns
- MetalGraphAnalysis
Analysis results containing optimization information.
Build()
Builds the graph by analyzing dependencies and preparing for execution.
public void Build()
Exceptions
- InvalidOperationException
Thrown when circular dependencies are detected.
Clear()
Clears all nodes from the graph.
public void Clear()
Dispose()
Releases all resources used by the Metal compute graph.
public void Dispose()
~MetalComputeGraph()
Finalizer for the MetalComputeGraph class.
protected ~MetalComputeGraph()
FindNode(string)
Finds a node in the graph by its ID.
public MetalGraphNode? FindNode(string nodeId)
Parameters
nodeIdstringThe ID of the node to find.
Returns
- MetalGraphNode
The node with the specified ID, or
nullif not found.
GetExecutionOrder()
Gets the execution order of nodes using topological sorting.
public IReadOnlyList<MetalGraphNode> GetExecutionOrder()
Returns
- IReadOnlyList<MetalGraphNode>
A list of nodes in execution order.
Exceptions
- InvalidOperationException
Thrown when the graph hasn't been built.
RemoveNode(string)
Removes a node from the graph by its ID.
public bool RemoveNode(string nodeId)
Parameters
nodeIdstringThe ID of the node to remove.
Returns
- bool
trueif the node was found and removed; otherwise,false.
Exceptions
- ArgumentException
Thrown when nodeId is null or whitespace.
- ObjectDisposedException
Thrown when the graph has been disposed.