Table of Contents

Class MetalEventManager

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

Advanced Metal event manager for timing, profiling, synchronization, and event pooling, following CUDA event patterns for cross-stream coordination.

public sealed class MetalEventManager : IDisposable
Inheritance
MetalEventManager
Implements
Inherited Members
Extension Methods

Constructors

MetalEventManager(nint, ILogger<MetalEventManager>)

public MetalEventManager(nint device, ILogger<MetalEventManager> logger)

Parameters

device nint
logger ILogger<MetalEventManager>

Methods

AddEventCallback(EventId, Func<EventId, Task>)

Adds an asynchronous callback for when an event completes

public void AddEventCallback(EventId eventId, Func<EventId, Task> callback)

Parameters

eventId EventId
callback Func<EventId, Task>

CreateEventBatchAsync(int, MetalEventType, CancellationToken)

Creates a batch of events for parallel operations

public Task<MetalEventHandle[]> CreateEventBatchAsync(int count, MetalEventType eventType = MetalEventType.Timing, CancellationToken cancellationToken = default)

Parameters

count int
eventType MetalEventType
cancellationToken CancellationToken

Returns

Task<MetalEventHandle[]>

CreateSyncEventAsync(CancellationToken)

Creates a synchronization event optimized for stream coordination

public Task<MetalEventHandle> CreateSyncEventAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<MetalEventHandle>

CreateTimingEventAsync(CancellationToken)

Creates a high-performance timing event for precise measurements

public Task<MetalEventHandle> CreateTimingEventAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<MetalEventHandle>

CreateTimingPairAsync(CancellationToken)

Creates a matched pair of timing events for measuring elapsed time

public Task<(MetalEventHandle start, MetalEventHandle end)> CreateTimingPairAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<(MetalEventHandle start, MetalEventHandle end)>

Dispose()

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

public void Dispose()

GetStatistics()

Gets comprehensive event manager statistics

public MetalEventStatistics GetStatistics()

Returns

MetalEventStatistics

IsEventComplete(EventId)

Checks if an event has completed

public bool IsEventComplete(EventId eventId)

Parameters

eventId EventId

Returns

bool

MeasureElapsedTimeAsync(EventId, EventId)

Measures elapsed time between two timing events using Metal Performance Counters

public Task<double> MeasureElapsedTimeAsync(EventId startEvent, EventId endEvent)

Parameters

startEvent EventId
endEvent EventId

Returns

Task<double>

MeasureOperationAsync(Func<nint, Task>, nint, string?, CancellationToken)

Performs high-precision timing measurement of an operation

public Task<MetalTimingResult> MeasureOperationAsync(Func<nint, Task> operation, nint commandQueue, string? operationName = null, CancellationToken cancellationToken = default)

Parameters

operation Func<nint, Task>
commandQueue nint
operationName string
cancellationToken CancellationToken

Returns

Task<MetalTimingResult>

PerformMaintenance(object?)

public void PerformMaintenance(object? state = null)

Parameters

state object

ProfileOperationAsync(Func<nint, Task>, int, nint, string?, CancellationToken)

Performs comprehensive profiling with statistical analysis

public Task<MetalProfilingResult> ProfileOperationAsync(Func<nint, Task> operation, int iterations = 50, nint commandQueue = 0, string? operationName = null, CancellationToken cancellationToken = default)

Parameters

operation Func<nint, Task>
iterations int
commandQueue nint
operationName string
cancellationToken CancellationToken

Returns

Task<MetalProfilingResult>

RecordEvent(EventId, nint)

Records an event on the specified command queue

public void RecordEvent(EventId eventId, nint commandQueue)

Parameters

eventId EventId
commandQueue nint

RecordEventFast(nint, nint)

Records an event using the handle directly for performance

public void RecordEventFast(nint eventHandle, nint commandQueue)

Parameters

eventHandle nint
commandQueue nint

WaitForEventAsync(EventId, TimeSpan?, CancellationToken)

Waits for an event asynchronously

public Task WaitForEventAsync(EventId eventId, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

eventId EventId
timeout TimeSpan?
cancellationToken CancellationToken

Returns

Task