Table of Contents

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

deviceInfo OpenCLDeviceInfo

The device information.

logger ILogger<OpenCLContext>

Logger for diagnostic information.

Properties

CommandQueue

Gets the command queue handle.

public OpenCLTypes.CommandQueue CommandQueue { get; }

Property Value

OpenCLTypes.CommandQueue

Context

Gets the OpenCL context handle.

public OpenCLTypes.Context Context { get; }

Property Value

OpenCLTypes.Context

DeviceInfo

Gets the device information for this context.

public OpenCLDeviceInfo DeviceInfo { get; }

Property Value

OpenCLDeviceInfo

IsDisposed

Gets whether this context has been disposed.

public bool IsDisposed { get; }

Property Value

bool

Methods

BuildProgram(Program, string?)

Builds a program for the device.

public void BuildProgram(OpenCLTypes.Program program, string? options = null)

Parameters

program OpenCLTypes.Program

Program to build.

options string

Build options (optional).

CreateBuffer(MemoryFlags, nuint, nint)

Creates a buffer in device memory.

public OpenCLTypes.MemObject CreateBuffer(MemoryFlags flags, nuint size, nint hostPtr = 0)

Parameters

flags MemoryFlags

Memory flags for the buffer.

size nuint

Size of the buffer in bytes.

hostPtr nint

Optional 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

program OpenCLTypes.Program

Built program containing the kernel.

kernelName string

Name 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

source string

OpenCL 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

kernel OpenCLTypes.Kernel

Kernel to execute.

workDimensions uint

Number of work dimensions (1, 2, or 3).

globalWorkSize nuint[]

Global work size for each dimension.

localWorkSize nuint[]

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

buffer OpenCLTypes.MemObject

Source buffer.

data nint

Target data pointer.

size nuint

Number of bytes to read.

blocking bool

Whether 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

buffer OpenCLTypes.MemObject

Target buffer.

data nint

Source data pointer.

size nuint

Number of bytes to write.

blocking bool

Whether 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

handle nint
releaseFunction Func<nint, OpenCLError>
objectType string

WaitForEvents(params Event[])

Waits for events to complete.

public void WaitForEvents(params OpenCLTypes.Event[] events)

Parameters

events Event[]

Events to wait for.