Class OpenCLProfiler
- Namespace
- DotCompute.Backends.OpenCL.Profiling
- Assembly
- DotCompute.Backends.OpenCL.dll
Production-grade OpenCL profiler with event-based profiling, hardware counter integration, and comprehensive statistical analysis.
public sealed class OpenCLProfiler : IAsyncDisposable
- Inheritance
-
OpenCLProfiler
- Implements
- Inherited Members
Remarks
This profiler provides detailed performance metrics for OpenCL operations including:
- Kernel execution timing with hardware counters
- Memory transfer profiling (host-to-device, device-to-host, device-to-device)
- Kernel compilation and buffer allocation tracking
- Statistical aggregation (min/max/avg/percentiles)
- Thread-safe session management
- JSON and CSV export capabilities
Constructors
OpenCLProfiler(OpenCLEventManager, ILogger<OpenCLProfiler>)
Initializes a new instance of the OpenCLProfiler class.
public OpenCLProfiler(OpenCLEventManager eventManager, ILogger<OpenCLProfiler> logger)
Parameters
eventManagerOpenCLEventManagerThe OpenCL event manager for profiling integration.
loggerILogger<OpenCLProfiler>Logger for diagnostic information.
Exceptions
- ArgumentNullException
Thrown if eventManager or logger is null.
Methods
BeginSessionAsync(string, CancellationToken)
Begins a new profiling session with the specified name.
public Task<ProfilingSession> BeginSessionAsync(string name, CancellationToken cancellationToken = default)
Parameters
namestringDescriptive name for the profiling session.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
- Task<ProfilingSession>
A profiling session handle for recording events.
Exceptions
- ArgumentException
Thrown if name is null or whitespace.
DisposeAsync()
Asynchronously disposes the profiler and releases all resources.
public ValueTask DisposeAsync()
Returns
EndSessionAsync(ProfilingSession, CancellationToken)
Ends a profiling session, finalizes statistics, and returns the session for export.
public Task<ProfilingSession> EndSessionAsync(ProfilingSession session, CancellationToken cancellationToken = default)
Parameters
sessionProfilingSessionThe profiling session to end.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
- Task<ProfilingSession>
The completed profiling session with finalized statistics.
Exceptions
- ArgumentNullException
Thrown if session is null.
ExportSessionToCsvAsync(ProfilingSession, string, CancellationToken)
Exports a profiling session to CSV format for analysis tools.
public Task ExportSessionToCsvAsync(ProfilingSession session, string filePath, CancellationToken cancellationToken = default)
Parameters
sessionProfilingSessionThe profiling session to export.
filePathstringDestination file path for CSV output.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
Exceptions
- ArgumentNullException
Thrown if session or filePath is null.
- IOException
Thrown if file write fails.
ExportSessionToJsonAsync(ProfilingSession, string, CancellationToken)
Exports a profiling session to JSON format.
public Task ExportSessionToJsonAsync(ProfilingSession session, string filePath, CancellationToken cancellationToken = default)
Parameters
sessionProfilingSessionThe profiling session to export.
filePathstringDestination file path for JSON output.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
Exceptions
- ArgumentNullException
Thrown if session or filePath is null.
- IOException
Thrown if file write fails.
GetStatistics()
Gets comprehensive profiler statistics.
public ProfilerStatistics GetStatistics()
Returns
- ProfilerStatistics
Profiler statistics including session and event counts.
ProfileKernelExecutionAsync(OpenCLKernel, OpenCLEventHandle, Dictionary<string, object>?, CancellationToken)
Profiles kernel execution by extracting timing information from an OpenCL event.
public Task<ProfiledEvent> ProfileKernelExecutionAsync(OpenCLKernel kernel, OpenCLEventHandle executionEvent, Dictionary<string, object>? metadata = null, CancellationToken cancellationToken = default)
Parameters
kernelOpenCLKernelThe kernel handle being profiled.
executionEventOpenCLEventHandleThe event associated with kernel execution.
metadataDictionary<string, object>Optional metadata to attach to the profiled event.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
- Task<ProfiledEvent>
A profiled event containing detailed timing information.
Exceptions
- ArgumentException
Thrown if kernel handle is invalid.
- OpenCLProfilingException
Thrown if profiling information cannot be retrieved.
ProfileMemoryTransferAsync(ProfiledOperation, ulong, OpenCLEventHandle, Dictionary<string, object>?, CancellationToken)
Profiles memory transfer operations with bandwidth calculations.
public Task<ProfiledEvent> ProfileMemoryTransferAsync(ProfiledOperation transferType, ulong sizeBytes, OpenCLEventHandle transferEvent, Dictionary<string, object>? metadata = null, CancellationToken cancellationToken = default)
Parameters
transferTypeProfiledOperationType of memory transfer (host-to-device, device-to-host, etc.).
sizeBytesulongSize of the memory transfer in bytes.
transferEventOpenCLEventHandleThe event associated with the memory transfer.
metadataDictionary<string, object>Optional metadata to attach to the profiled event.
cancellationTokenCancellationTokenCancellation token for async operations.
Returns
- Task<ProfiledEvent>
A profiled event with bandwidth information in metadata.
Exceptions
- ArgumentException
Thrown if sizeBytes is zero or negative.
- OpenCLProfilingException
Thrown if profiling information cannot be retrieved.
RecordOperation(ProfilingSession, ProfiledOperation, string, long, Dictionary<string, object>?)
Records a non-event-based profiled operation (e.g., kernel compilation, buffer allocation).
public void RecordOperation(ProfilingSession session, ProfiledOperation operation, string name, long durationNanoseconds, Dictionary<string, object>? metadata = null)
Parameters
sessionProfilingSessionThe profiling session to record to.
operationProfiledOperationType of operation being profiled.
namestringDescriptive name for the operation.
durationNanosecondslongDuration of the operation in nanoseconds.
metadataDictionary<string, object>Optional metadata to attach.
TryGetHardwareCounters(OpenCLDeviceId, out Dictionary<string, long>)
Attempts to query hardware counters from the OpenCL device (vendor-specific).
public bool TryGetHardwareCounters(OpenCLDeviceId device, out Dictionary<string, long> counters)
Parameters
deviceOpenCLDeviceIdThe OpenCL device to query.
countersDictionary<string, long>Output dictionary of counter name-value pairs.
Returns
- bool
True if hardware counters were successfully retrieved, false otherwise.
Remarks
This method attempts to query vendor-specific performance counters. Support varies by vendor (AMD, NVIDIA, Intel) and may require special extensions.