Compute is an experimental OpenCL acceleration library for C#.
It provides:
- OpenCL platform and device discovery.
- OpenCL Context and memory abstractions.
- Kernel execution helpers.
- C# to OpenCL kernel generation through IL/AST compilation.
This project is experimental and under active iteration.
- API shape may still change.
- Not all IL patterns are supported yet.
- Some valid C# methods may fail to compile as kernels.
- Latest .NET SDK.
- A working OpenCL runtime/driver (NVIDIA, AMD, Intel, etc).
Build samples:
dotnet build Compute.Samples/Compute.Samples.csprojRun the sample suite:
dotnet run --project Compute.Samples/Compute.Samples.csprojThe suite currently runs representative examples for:
- GEMM with local memory tiling
- 1D/2D/3D grid execution
- Type-safe kernel invocation
- N-body simulation
- Atomics
- Images
- Reductions
Each sample reports correctness and timing (CPU, GPU, and speedup when available).
[Kernel]publicstaticvoidSaxpy([Global]float[]x,[Global]float[]y,[Const]uintcount){varid=BuiltIn.GetGlobalId(0);if(id>=count)return;y[id]=2.0f*x[id]+y[id];}usingvarcontext=accelerator.CreateContext();usingvarkernel=Parallel.Prepare(context,()=>{varid=KernelThread.Global.X;if(id>=length)return;output[id]=input[id]*input[id];});kernel.Run(Grid.Size((uint)length));vartile=LocalMemory.Allocate2D<float>(16,16);tile[KernelThread.Local.Y,KernelThread.Local.X]=value;Sync.Local();varworkers=Grid.Size(N,M).Tile((uint)tile,(uint)tile);usingvarparallel=Parallel.Prepare(context,()=>{vartileA=LocalMemory.Allocate2D<float>(TILE,TILE);vartileB=LocalMemory.Allocate2D<float>(TILE,TILE);introw=KernelThread.Local.Y;intcol=KernelThread.Local.X;intglobalRow=KernelThread.Group.Y*tile+row;intglobalCol=KernelThread.Group.X*tile+col;floatsum=0.0f;intnumTiles=K/tile;for(intt=0;t<numTiles;t++){tileA[row,col]=A[globalRow*K+t*tile+col];tileB[row,col]=B[(t*tile+row)*N+globalCol];Sync.Local();for(intk=0;k<tile;k++){sum+=tileA[row,k]*tileB[k,col];}Sync.Local();}C[globalRow*N+globalCol]=sum;});parallel.Run(workers);- Image kernels and atomic operations are supported in the sample suite.
- Image readback precision/format behavior may vary by driver/runtime.
- For realistic validation, always test on your target GPU vendor stack.
- Core library:
Compute/ - Sample suite:
Compute.Samples/ - Entry point for samples:
Compute.Samples/Program.cs
Contributions are welcome.