1

I'm new to Halide so also kinda didn't know how to ask the question. Let me explain. Let's assume I have a simple code for Halide's generator like this:

class Blur : public Generator<Blur>{
public:
    Input<Buffer<float>> in_func{"in_func", 2};
    Output<Buffer<float>> forward{"forward", 2};

    Var x, y, n;
    void generate(){

        Expr m1 = in_func(x+1, y+2)+in_func(x+2, y+1);
        Expr m2 = in_func(x+1, y+2)-in_func(x+2, y+1);
        Expr m3 = in_func(x+2, y+1)+in_func(x+1, y+1);
        Expr m4 = in_func(x+2, y+1)-in_func(x+1, y+1);
        Expr w0010_2 = -in_func(x+2, y+2)+in_func(x, y+2);
        Expr w0111_2 = -in_func(x+3, y+2)+in_func(x+1, y+2);

        forward(0,0) = w0010_2+m4+m3+m2+m1;
        forward(1,0) = -w0111_2+m4+m3-m2-m1; 
        forward(0,1) = w0010_2-m4+m3-m2+m1;
        forward(1,1) = w0111_2-m4+m3+m2-m1;
    }
};

What I want to achieve is to define that output at index (0,0) should be the result of m1 + m2 but output at index (1,0) should be the result of different expression, for example, m1 - m2. I would be really grateful for help.

Shirakumo
  • 182
  • 1
  • 1
  • 10
  • Do you want _just_ those two points defined? If not, what should the rest of `forward` be? – Alex Reinking Aug 01 '22 at 13:56
  • No, let's say I have output array 4x4 but for example indices: (0,0), (0,2), (2,0) and (2,2) uses one expression, (0,1), (0,3), (2,1), (2,3) uses another one and so on. In total 4 expressions for different parts. – Shirakumo Aug 01 '22 at 14:06

1 Answers1

1

What I want to achieve is to define that output at index (0,0) should be the result of m1 + m2 but output at index (1,0) should be the result of different expression, for example, m1 - m2. [...] I want result[0][0] = expression1, result[0][1] = expression2, result[1][0] = expression3 and result[1][1] = expression4. But also result[0][2], result[0][4] and so on = expression1

Compute the values x%2 and y%2 and use their values in a select:

forward(x, y) = select(
  x % 2 == 0 && y % 2 == 0, m1 + m2,
  x % 2 == 1 && y % 2 == 0, m1 - m2,
  x % 2 == 0 && y % 2 == 1, expr3,
  /* otherwise, */ expr4
);

Select is a pure if-then-else. It evaluates all of its arguments and then picks the one corresponding to the first true predicate. If the expressions all use nearby points of in_func, this might not be too slow.

If you find that performance suffers, I'd try to create four funcs, one for each of the four expressions, and then select loads from those. If that's still too slow, you might be able to optimize the indexing to not compute any extra points. If you show all four expressions, I might be able to help you do that.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • Hmm I should read more about select then. Let me mix c++. I want result[0][0] = expression1, result[0][1] = expression2, result[1][0] = expression3 and result[1][1] = expression4. But also result[0][2], result[0][4] and so on = expression1 – Shirakumo Aug 01 '22 at 14:10
  • @Shirakumo - thanks for the additional context... I've updated my answer. – Alex Reinking Aug 01 '22 at 14:26
  • thank you so much for your effort, I put more details in my code example now. If you can explain to me how to make four functions for four expressions but still it'll go to one output I would be really grateful. In my code forward(0,0) means to which coordinates that expressions should go, but of course, it should be like earlier, forward (0,0), (0,2), (0,4) and so on have one expression and other coordinates in same way. – Shirakumo Aug 01 '22 at 17:40
  • I checked and it's working but the performance is not so great. I can't find how to make four functions to update one Output in my case "forward". By any chance you know any good resource to learn Halide other than their official website? – Shirakumo Aug 01 '22 at 21:21
  • The Halide tutorials are the main resource. I'm not aware of any books or anything that have been written about Halide. I am one of the open source maintainers for Halide, btw. You can try asking on our [Gitter](https://gitter.im/halide/Halide) if anyone there has time to help you optimize your code. – Alex Reinking Aug 01 '22 at 22:13
  • Ok, thank you so much for the help and information. I'll try to ask further questions there – Shirakumo Aug 02 '22 at 06:35