Synchronous C# loops on CUDA or OpenCL, powered by ILGPU. Write a lambda over captured collections; the library compiles it, transfers the data, and copies results back before returning.
Build ParallelGpu from source or install the NuGet package in your project.
| Assembly | Specification | OS | Download | Package |
|---|---|---|---|---|
| ParallelGpu | .NET Standard 2.1 | Cross-platform | Release | NuGet |
dotnet add package ParallelGpuILGPU dependencies are restored automatically.
using ParallelGpu;
using var gpu = new Gpu();
int[] input = { 1, 2, 3 };
int[] output = new int[input.Length];
gpu.For(0, input.Length, i => output[i] = input[i] * 2);
// output: 2, 4, 6Reuse a Gpu instance to retain the device and compiled kernels. GpuParallel.For(...) provides a static API; use Gpu for repeated calls with custom options.
GpuOptions.Backend |
Behavior |
|---|---|
Auto (default) |
Tries CUDA, then OpenCL. CPU fallback requires AllowCpuFallback = true. |
Cuda |
Requires an NVIDIA GPU and a compatible CUDA driver. |
OpenCL |
Requires OpenCL C 2.0 or newer with generic address-space support. An OpenCL 3.0 label alone is insufficient. |
Cpu |
Uses the ILGPU CPU accelerator for debugging and correctness checks. |
using var gpu = new Gpu(new GpuOptions
{
Backend = GpuBackend.Auto,
AllowCpuFallback = true
});Explicit Cuda and OpenCL selections never fall back to CPU. DeviceIndex selects a device; gpu.Backend, gpu.DeviceName, and gpu.IsGpu report the selection. CPU and CUDA are tested. OpenCL remains unverified because the local driver lacks the required generic address-space support.
| Feature | API and details |
|---|---|
| Automatic collection transfers | Arrays of any rank, jagged arrays, lists, and memory slices; shared storage is preserved. See supported values and collections. |
| Data kept on the device | Upload<T>, Allocate<T>, and GpuBuffer<T>.DeviceView; see the example below. |
| Reductions and synchronization | Reduce, built-in sums, and GpuAtomic; see reductions and atomics. |
| Variable output | Bounded append buffers and Collect; see bounded output. |
| Custom data models | Collection adapters and object graphs. |
| Numeric methods | Supported Math and MathF overloads. |
| Explicit kernels | Public value types implementing IGpuForKernel or IGpuForEachKernel<T>; see native buffers and kernels. |
Loop bodies and helpers must use device-compatible operations. Managed allocation, boxing, reflection, exception handlers, checked integer arithmetic, and arbitrary .NET calls are unsupported. Automatic lambda compilation requires Reflection.Emit and method IL, so Native AOT is unsupported.
Iterations are unordered: use independent writes, atomics, or reductions to avoid races. Scalar captures are read-only snapshots; collection sizes cannot change during a loop. See the execution rules for error handling and other constraints.
Automatic loops transfer captured collections on every call. For repeated work, upload once and download when needed. This example reuses gpu from the quick start:
using var buffer = gpu.Upload(new float[] { 1, 2, 3 });
var values = buffer.DeviceView;
gpu.For(0, values.Length, i => values[i] *= 2);
gpu.For(0, values.Length, i => values[i] *= 3);
float[] scaled = buffer.ToArray();
// scaled: 6, 12, 18Native buffers use unmanaged ILGPU-compatible types, without automatic value mappings. Views borrow their owner's memory; keep the owner alive, use its executor, and keep indices in range. See buffer types, transfers, and lifetime.
Transfers and compilation can outweigh GPU execution for small workloads. Measure complete calls after warm-up in Release mode.
The samples cover matrix multiplication, FFT, hash search, Monte Carlo, factorization, digit recognition, Mandelbrot, ray tracing, and image denoising.
Run a sample with the .NET 8 SDK:
dotnet run --project samples/FloatMatrixMultiplication -c ReleaseDigit recognition requires .artifacts/digit-recognizer.zip; dataset and training settings are in its Program.cs. Image denoising includes an input image.
dotnet build ParallelGpu.sln -c Release -warnaserror
$env:PARALLEL_GPU_TEST_BACKEND = 'Cpu'
dotnet test tests/ParallelGpu.Tests.csproj -c Release --no-build
$env:PARALLEL_GPU_TEST_BACKEND = 'Cuda'
dotnet test tests/ParallelGpu.Tests.csproj -c Release --no-buildTests default to Cpu; the environment variable also accepts Cuda, OpenCL, and Auto (Auto requires a GPU). build.bat builds just the library and NuGet package. Release packages in sources/bin/Release/ include the license, package README, logo, and XML API documentation.
MIT
