Skip to content

Minimal GPU example

dgajaria-ADI edited this page Nov 20, 2019 · 7 revisions

The following is the simplest program that does something interesting on the GPU. It uses JIT compilation.

#include <Halide.h>
#include <stdio.h>

int main(int argc, char **argv) {
    // Define a gradient function.
    Halide::Func f;
    Halide::Var x, y, xo, xi, yo, yi;
    f(x, y) = x + y;

    // Schedule f on the GPU in 16x16 tiles.
    f.gpu_tile(x, y, xo, yo, xi, yi, 16, 16);

    // Construct a target that uses the GPU.
    Halide::Target target = Halide::get_host_target();

    // Enable OpenCL as the GPU backend.
    target.set_feature(Halide::Target::OpenCL);

    // Enable debugging so that you can see what OpenCL API calls we do.
    target.set_feature(Halide::Target::Debug);

    // JIT-compile the pipeline.
    f.compile_jit(target);

    // Run it.
    Halide::Buffer<int> result = f.realize(32, 32);

    // Print the result.
    for (int y = 0; y < result.height(); y++) {
        for (int x = 0; x < result.width(); x++) {
            printf("%3d ", result(x, y));
        }
        printf("\n");
    }

    return 0;
}

For a longer worked example of using the GPU, see tutorial lesson 12