Skip to content

MandelBrot

Portalez Régis edited this page May 11, 2026 · 25 revisions

Mandelbrot

The Mandelbrot set computed on a 2D grid and saved to an image. Demonstrates:

  • Splitting a [Kernel] device function from the [EntryPoint] that drives it.
  • 2D thread-block / grid indexing (threadIdx.x / threadIdx.y etc.).
  • IntResidentArray — a GPU-resident buffer the runtime never copies back unless asked.

Source: src/1.Simple/Mandelbrot/Program.cs

[Kernel]
public static int IterCount(float cx, float cy)
{
    int result = 0;
    float x = 0.0f, y = 0.0f, xx = 0.0f, yy = 0.0f;
    while (xx + yy <= 4.0f && result < maxiter)
    {
        xx = x * x; yy = y * y;
        float xtmp = xx - yy + cx;
        y = 2.0f * x * y + cy;
        x = xtmp;
        result++;
    }
    return result;
}

[EntryPoint]
public static void Run(IntResidentArray light, int lineFrom, int lineTo)
{
    for (int line = lineFrom + threadIdx.y + blockDim.y * blockIdx.y; line < lineTo; line += gridDim.y * blockDim.y)
        for (int j = threadIdx.x + blockIdx.x * blockDim.x; j < N; j += blockDim.x * gridDim.x)
        {
            float x = fromX + line * h, y = fromY + j * h;
            light[line * N + j] = IterCount(x, y);
        }
}

The [Kernel] attribute marks IterCount as device-callable; without it, hybridizer wouldn't generate device code for that method. The [EntryPoint] wraps the host-callable kernel.

mandelbrot rendering

Performance discussion: hybridizer-fractal-demo-application.

Clone this wiki locally