Search code examples
c++optimizationhalide

Halide check if split is possible


I am writing a Halide program which takes different image sizes. When I optimize I split a loop in a number of "sub loops" so I can parallelize this with a given factor. For small images however, this can be a problem if the image is smaller than the split factor. Or, to be more accurate, when the number of iterations in the loop is smaller than the splitfactor.

Reading out of bounds is handled using the Halide::BoundaryConditions, Sure I can manually check the split factor with an if statement, but does Halide have something similar to the BoundaryConditions for the optimizations?


Solution

  • If I understand correctly what you're saying, then this is what Func::specialize is for. You can do something like:

    // Only vectorize if output is large enough
    f.specialize(f.output_buffer().width() > 8).vectorize(x, 8);
    

    The Halide sgemm uses this a bunch: https://github.com/halide/Halide/blob/master/apps/linear_algebra/src/blas_l3_generators.cpp