1

I am new in Halide.

I am trying to do some calculation where pixel position 'x' should be set as the limit of the for loop. For that, I wrote the following code but it seems i can not use halide variable as a limit of a for loop.

Is there a solution for that?

My code:

Halide::Var x, y;

Halide::Expr L;

Halide::Func mat,A;

L = 0;

for (int k=1; k<=x-1; k++)

        L +=  mat(k,y) * mat(k,x);

mat(x,y) = Halide::select(x==y, (A(x, y) - L),

                              y>x, (A(x,y) - L)/mat(x,x),

                              0);

it gives the error message:

"error: could not convert ‘Halide::operator<=(int, Halide::Expr) Halide::operator-(Halide::Expr, int)(1))’ from ‘Halide::Expr’ to ‘bool’ for (int k=1; k<=x-1; k++)"

Ripi2
  • 7,031
  • 1
  • 17
  • 33

1 Answers1

1

Halide::Expr and Halide::Var don't have explicit values at C++ compilation time; they are placeholders for values that are expressed in the resulting Halide code. Thus, you can't use them in a C++ for loop. The equivalent in Halide is to use an RDom to specify an explicit range, e.g.,

    Halide::RDom k(1, x-1);  // RDom is [min, extent], not [min, max]
    Halide::Expr L;
    L = 0;
    L += mat(k,y) * mat(k,x);

or, more simply,

    Halide::RDom k(1, x-1);  // RDom is [min, extent], not [min, max]
    Halide::Expr L = Halide::sum(mat(k,y) * mat(k,x));
Steven Johnson
  • 266
  • 1
  • 4
  • Thanks a lot Steven Johnson! I will try with your suggestion. :-) – Nusrat Jemy Aug 29 '19 at 17:18
  • I don't think you can use Vars in the bounds of an RDom. You probably want to put a plausible upper bound, then use RDom::where to trim it down to the range you actually want: RDom r(k, 1, 1024); r.where(r < x); – Andrew Adams Aug 29 '19 at 20:10
  • Yup, you are right Andrew Adams! I can't use a Var as the bounds of an RDom. I need to give specific values as bound of an RDom. So, I need to find another solution for it. – Nusrat Jemy Aug 30 '19 at 13:27
  • Thank you Steven Johnson and Andrew Adams! it works fine with RDom::where. But there is another problem should I write a new Question? As in my Question, some later mat(x,y) needs the previous value of mat(x,y). i.e. It is a recursive function. But it gives an error: "Can't call Func "mat" because it has not yet been defined. The program has unexpectedly finished." ------what to do now? – Nusrat Jemy Aug 30 '19 at 16:28