Class OpenCLContext
- Namespace
- DotCompute.Backends.OpenCL
- Assembly
- DotCompute.Backends.OpenCL.dll
Represents an OpenCL context for a specific device. Manages the OpenCL context, command queue, and device state.
public sealed class OpenCLContext : IDisposable
- Inheritance
-
OpenCLContext
- Implements
- Inherited Members
- Extension Methods
Constructors
OpenCLContext(OpenCLDeviceInfo, ILogger<OpenCLContext>)
Initializes a new instance of the OpenCLContext class.
public OpenCLContext(OpenCLDeviceInfo deviceInfo, ILogger<OpenCLContext> logger)
Parameters
deviceInfoOpenCLDeviceInfoThe device information.
loggerILogger<OpenCLContext>Logger for diagnostic information.
Properties
CommandQueue
Gets the command queue handle.
public OpenCLTypes.CommandQueue CommandQueue { get; }
Property Value
Context
Gets the OpenCL context handle.
public OpenCLTypes.Context Context { get; }
Property Value
DeviceInfo
Gets the device information for this context.
public OpenCLDeviceInfo DeviceInfo { get; }
Property Value
IsDisposed
Gets whether this context has been disposed.
public bool IsDisposed { get; }
Property Value
Methods
BuildProgram(Program, string?)
Builds a program for the device.
public void BuildProgram(OpenCLTypes.Program program, string? options = null)
Parameters
programOpenCLTypes.ProgramProgram to build.
optionsstringBuild options (optional).
CreateBuffer(MemoryFlags, nuint, nint)
Creates a buffer in device memory.
public OpenCLTypes.MemObject CreateBuffer(MemoryFlags flags, nuint size, nint hostPtr = 0)
Parameters
flagsMemoryFlagsMemory flags for the buffer.
sizenuintSize of the buffer in bytes.
hostPtrnintOptional host pointer for buffer initialization.
Returns
- OpenCLTypes.MemObject
The created buffer handle.
CreateKernel(Program, string)
Creates a kernel from a built program.
public OpenCLTypes.Kernel CreateKernel(OpenCLTypes.Program program, string kernelName)
Parameters
programOpenCLTypes.ProgramBuilt program containing the kernel.
kernelNamestringName of the kernel function.
Returns
- OpenCLTypes.Kernel
The created kernel handle.
CreateProgramFromSource(string)
Creates a program from source code.
public OpenCLTypes.Program CreateProgramFromSource(string source)
Parameters
sourcestringOpenCL kernel source code.
Returns
- OpenCLTypes.Program
The created program handle.
Dispose()
Disposes the OpenCL context and associated resources.
public void Dispose()
EnqueueKernel(Kernel, uint, nuint[], nuint[]?)
Enqueues a kernel for execution.
public OpenCLTypes.Event EnqueueKernel(OpenCLTypes.Kernel kernel, uint workDimensions, nuint[] globalWorkSize, nuint[]? localWorkSize = null)
Parameters
kernelOpenCLTypes.KernelKernel to execute.
workDimensionsuintNumber of work dimensions (1, 2, or 3).
globalWorkSizenuint[]Global work size for each dimension.
localWorkSizenuint[]Local work size for each dimension (optional).
Returns
- OpenCLTypes.Event
Event handle for the operation.
EnqueueReadBuffer(MemObject, nint, nuint, bool)
Enqueues a buffer read operation.
public OpenCLTypes.Event EnqueueReadBuffer(OpenCLTypes.MemObject buffer, nint data, nuint size, bool blocking = false)
Parameters
bufferOpenCLTypes.MemObjectSource buffer.
datanintTarget data pointer.
sizenuintNumber of bytes to read.
blockingboolWhether to block until completion.
Returns
- OpenCLTypes.Event
Event handle for the operation.
EnqueueWriteBuffer(MemObject, nint, nuint, bool)
Enqueues a buffer write operation.
public OpenCLTypes.Event EnqueueWriteBuffer(OpenCLTypes.MemObject buffer, nint data, nuint size, bool blocking = false)
Parameters
bufferOpenCLTypes.MemObjectTarget buffer.
datanintSource data pointer.
sizenuintNumber of bytes to write.
blockingboolWhether to block until completion.
Returns
- OpenCLTypes.Event
Event handle for the operation.
Finish()
Finishes all commands in the queue (blocking).
public void Finish()
Flush()
Flushes the command queue.
public void Flush()
ReleaseObject(nint, Func<nint, OpenCLError>, string)
Releases an OpenCL object reference.
public static void ReleaseObject(nint handle, Func<nint, OpenCLError> releaseFunction, string objectType)
Parameters
handlenintreleaseFunctionFunc<nint, OpenCLError>objectTypestring
WaitForEvents(params Event[])
Waits for events to complete.
public void WaitForEvents(params OpenCLTypes.Event[] events)
Parameters
eventsEvent[]Events to wait for.