Table of Contents

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

cache Dictionary<string, ICompiledKernel>

Cache to clear

logger ILogger

Logger instance

backendType string

Backend 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

definition KernelDefinition

Kernel definition to compile

options CompilationOptions

Compilation options

logger ILogger

Logger instance

backendType string

Type of backend for logging

enableCaching bool

Whether to enable caching

cache Dictionary<string, ICompiledKernel>

Cache dictionary for compiled kernels

compileFunc Func<KernelDefinition, CompilationOptions, CancellationToken, ValueTask<ICompiledKernel>>

The actual compilation function

cacheKeyFunc Func<KernelDefinition, CompilationOptions, string>

Function to generate cache keys

cancellationToken CancellationToken

Cancellation 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

logger ILogger

Logger instance

backendType string

Type of backend for logging

cache Dictionary<string, ICompiledKernel>

Cache to clear

disposables object[]

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

logger ILogger

Logger instance

backendType string

Type of backend for logging

cache Dictionary<string, ICompiledKernel>

Cache to clear

disposables object[]

Objects to dispose

Returns

ValueTask

GenerateStandardCacheKey(KernelDefinition, CompilationOptions)

Generates a standard cache key for kernel compilation.

public static string GenerateStandardCacheKey(KernelDefinition definition, CompilationOptions options)

Parameters

definition KernelDefinition

Kernel definition

options CompilationOptions

Compilation 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

cache Dictionary<string, ICompiledKernel>

Cache to analyze

backendType string

Backend type

enableCaching bool

Whether caching is enabled

maxCacheSize int

Maximum 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

logger ILogger

Logger instance

backendType string

Type of backend for logging

initFunc Func<T>

Initialization function

compilerInfo string

Optional 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

definition KernelDefinition

Kernel definition to validate