Class KernelUtilities
- Namespace
- DotCompute.Abstractions
- Assembly
- DotCompute.Abstractions.dll
Utility methods for common kernel compilation patterns to reduce code duplication.
public static class KernelUtilities
- Inheritance
-
KernelUtilities
- Inherited Members
Methods
ClearCacheSafely(Dictionary<string, ICompiledKernel>, ILogger, string)
Clears a kernel compilation cache safely.
public static void ClearCacheSafely(Dictionary<string, ICompiledKernel> cache, ILogger logger, string backendType)
Parameters
cacheDictionary<string, ICompiledKernel>Cache to clear
loggerILoggerLogger instance
backendTypestringBackend type for logging
CompileWithCachingAsync(KernelDefinition, CompilationOptions?, ILogger, string, bool, Dictionary<string, ICompiledKernel>, Func<KernelDefinition, CompilationOptions, CancellationToken, ValueTask<ICompiledKernel>>, Func<KernelDefinition, CompilationOptions, string>, CancellationToken)
Common pattern for kernel compilation with caching, error handling, and logging.
public static ValueTask<ICompiledKernel> CompileWithCachingAsync(KernelDefinition definition, CompilationOptions? options, ILogger logger, string backendType, bool enableCaching, Dictionary<string, ICompiledKernel> cache, Func<KernelDefinition, CompilationOptions, CancellationToken, ValueTask<ICompiledKernel>> compileFunc, Func<KernelDefinition, CompilationOptions, string> cacheKeyFunc, CancellationToken cancellationToken = default)
Parameters
definitionKernelDefinitionKernel definition to compile
optionsCompilationOptionsCompilation options
loggerILoggerLogger instance
backendTypestringType of backend for logging
enableCachingboolWhether to enable caching
cacheDictionary<string, ICompiledKernel>Cache dictionary for compiled kernels
compileFuncFunc<KernelDefinition, CompilationOptions, CancellationToken, ValueTask<ICompiledKernel>>The actual compilation function
cacheKeyFuncFunc<KernelDefinition, CompilationOptions, string>Function to generate cache keys
cancellationTokenCancellationTokenCancellation token
Returns
- ValueTask<ICompiledKernel>
The compiled kernel
DisposeCompilerWithCacheCleanup(ILogger, string, Dictionary<string, ICompiledKernel>?, params object?[])
Common pattern for kernel compiler disposal with cache cleanup (synchronous).
public static void DisposeCompilerWithCacheCleanup(ILogger logger, string backendType, Dictionary<string, ICompiledKernel>? cache, params object?[] disposables)
Parameters
loggerILoggerLogger instance
backendTypestringType of backend for logging
cacheDictionary<string, ICompiledKernel>Cache to clear
disposablesobject[]Objects to dispose
DisposeCompilerWithCacheCleanupAsync(ILogger, string, Dictionary<string, ICompiledKernel>?, params object?[])
Common pattern for kernel compiler disposal with cache cleanup.
public static ValueTask DisposeCompilerWithCacheCleanupAsync(ILogger logger, string backendType, Dictionary<string, ICompiledKernel>? cache, params object?[] disposables)
Parameters
loggerILoggerLogger instance
backendTypestringType of backend for logging
cacheDictionary<string, ICompiledKernel>Cache to clear
disposablesobject[]Objects to dispose
Returns
GenerateStandardCacheKey(KernelDefinition, CompilationOptions)
Generates a standard cache key for kernel compilation.
public static string GenerateStandardCacheKey(KernelDefinition definition, CompilationOptions options)
Parameters
definitionKernelDefinitionKernel definition
optionsCompilationOptionsCompilation options
Returns
- string
Cache key string
GetCompilationStatistics(Dictionary<string, ICompiledKernel>, string, bool, int)
Gets compilation statistics for a cache.
public static Dictionary<string, object> GetCompilationStatistics(Dictionary<string, ICompiledKernel> cache, string backendType, bool enableCaching, int maxCacheSize)
Parameters
cacheDictionary<string, ICompiledKernel>Cache to analyze
backendTypestringBackend type
enableCachingboolWhether caching is enabled
maxCacheSizeintMaximum cache size
Returns
- Dictionary<string, object>
Statistics dictionary
InitializeCompilerWithLogging<T>(ILogger, string, Func<T>, string?)
Common pattern for kernel compiler initialization.
public static T InitializeCompilerWithLogging<T>(ILogger logger, string backendType, Func<T> initFunc, string? compilerInfo = null)
Parameters
loggerILoggerLogger instance
backendTypestringType of backend for logging
initFuncFunc<T>Initialization function
compilerInfostringOptional compiler information for logging
Returns
- T
Result of initialization
Type Parameters
T
ValidateKernelDefinition(KernelDefinition)
Validates a kernel definition before compilation.
public static void ValidateKernelDefinition(KernelDefinition definition)
Parameters
definitionKernelDefinitionKernel definition to validate