Table of Contents

Class MetalExecutionContext

Namespace
DotCompute.Backends.Metal.Execution
Assembly
DotCompute.Backends.Metal.dll

Metal execution context for managing execution state, resource lifetime tracking, performance metrics collection, and command dependency resolution. Follows CUDA execution context patterns optimized for Metal.

public sealed class MetalExecutionContext : IDisposable, IAsyncDisposable
Inheritance
MetalExecutionContext
Implements
Inherited Members
Extension Methods

Constructors

MetalExecutionContext(nint, ILogger<MetalExecutionContext>, MetalExecutionContextOptions?)

public MetalExecutionContext(nint device, ILogger<MetalExecutionContext> logger, MetalExecutionContextOptions? options = null)

Parameters

device nint
logger ILogger<MetalExecutionContext>
options MetalExecutionContextOptions

Properties

CommandStream

Gets the command stream manager

public MetalCommandStream CommandStream { get; }

Property Value

MetalCommandStream

Device

Gets the Metal device handle

public nint Device { get; }

Property Value

nint

ErrorHandler

Gets the error handler

public MetalErrorHandler ErrorHandler { get; }

Property Value

MetalErrorHandler

EventManager

Gets the event manager

public MetalEventManager EventManager { get; }

Property Value

MetalEventManager

IsAppleSilicon

Gets whether the context is running on Apple Silicon

public bool IsAppleSilicon { get; }

Property Value

bool

IsExecutionPaused

Gets whether execution is currently paused

public bool IsExecutionPaused { get; }

Property Value

bool

TotalOperationsExecuted

Gets the total number of operations executed

public long TotalOperationsExecuted { get; }

Property Value

long

TotalResourcesTracked

Gets the total number of resources tracked

public long TotalResourcesTracked { get; }

Property Value

long

Methods

AddDependency(string, string)

Adds a dependency between operations

public void AddDependency(string dependentOperation, string dependencyOperation)

Parameters

dependentOperation string
dependencyOperation string

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

ExecuteOperationAsync<T>(string, Func<MetalOperationExecutionInfo, Task<T>>, MetalExecutionOptions?, CancellationToken)

Executes an operation with full context management

public Task<T> ExecuteOperationAsync<T>(string operationId, Func<MetalOperationExecutionInfo, Task<T>> operation, MetalExecutionOptions? options = null, CancellationToken cancellationToken = default)

Parameters

operationId string
operation Func<MetalOperationExecutionInfo, Task<T>>
options MetalExecutionOptions
cancellationToken CancellationToken

Returns

Task<T>

Type Parameters

T

GetStatistics()

Gets comprehensive execution statistics

public MetalExecutionStatistics GetStatistics()

Returns

MetalExecutionStatistics

PauseExecutionAsync(CancellationToken)

Pauses execution context (stops accepting new operations)

public Task PauseExecutionAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

PerformHealthCheckAsync(CancellationToken)

Performs a comprehensive health check

public Task<MetalHealthCheckResult> PerformHealthCheckAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<MetalHealthCheckResult>

ResumeExecution()

Resumes execution context

public void ResumeExecution()

TrackResource(string, nint, MetalResourceType, long)

Tracks a resource for lifetime management

public void TrackResource(string resourceId, nint resourceHandle, MetalResourceType resourceType, long sizeInBytes = 0)

Parameters

resourceId string
resourceHandle nint
resourceType MetalResourceType
sizeInBytes long

UntrackResource(string, bool)

Stops tracking a resource and optionally releases it

public void UntrackResource(string resourceId, bool releaseResource = false)

Parameters

resourceId string
releaseResource bool