我只是想知道Halide中是否可以将Buffer解释为Func(即更好的标识函数)。另外,我的许多代码似乎非常多余,因此,如果可以用更好的样式编写代码,那会是什么样?
以下是我所讨论的示例(注意:出于其他原因,我必须使用行优先的表示形式):
当前:
#define HEIGHT dim(0).extent()
#define WIDTH dim(1).extent()
//modular addition (needs to accept Funcs as inputs
//because it will be part of a pipeline)
Halide::Func add(Halide::Func f1, Halide::Func f2, int64_t modulus) {
Halide::Var x, y;
Halide::Func add_temp;
if (modulus > 1)
add_temp(x, y) = (f1(x, y) + f2(x, y)) % Halide::Expr(modulus);
else
add_temp(x, y) = f1(x, y) + f2(x, y);
return add_temp;
}
int main() {//testing the "add" function
Halide::Buffer<int64_t> in1(4, 4, "in1");
Halide::Buffer<int64_t> in2(4, 4, "in2");
//initialize in1 and in2 to random numbers
Halide::Buffer<int64_t> sum(4, 4, "sum");
//side note: what does the name in the constructor even do?
//can I just omit it? I was told to keep it.
Halide::Func in1_id;
Halide::Func in2_id;
Halide::Var x, y;
in1_id(x, y) = in1(x, y);
in2_id(x, y) = in2(x, y);
sum = add(in1_id, in2_id, 503).realize(4, 4);
//print the values of sum
}
理想(第1版):
Halide::Func add(Halide::Func f1, Halide::Func f2, int64_t modulus) {
//the same definition
}
int main() {
Halide::Buffer<int64_t> in1(4, 4);
Halide::Buffer<int64_t> in2(4, 4);
//initialize in1 and in2 to random numbers
Halide::Buffer<int64_t> sum(4, 4);
//in1 is automatically interpreted as
//the identity function on in1
sum = add(in1, in2, 503).realize(4,4);
//print the values of sum
}
理想(第2版):
Halide::Func add(Halide::Func f1, Halide::Func f2, int64_t modulus) {
//the same definition
}
int main() {
Halide::Buffer<int64_t> in1(4, 4);
Halide::Buffer<int64_t> in2(4, 4);
//initialize in1 and in2 to random numbers
Halide::Buffer<int64_t> sum(4, 4);
//h_id is something included in the Halide
//library which acts as the identity on anything
sum = add(h_id(in1), h_id(in2), 503).realize(4,4);
//print the values of sum
}
起初似乎没什么大不了的,但是真正的问题是当我必须制作具有多个输入和多个输出的多级管道,并且必须为每个输入定义单独的标识函数时,这似乎非常多余和不必要的。预先感谢所有答复者!
编辑:第二天我实际上自己解决了这个问题。事实证明,有一个简单的解决方案。 h_id不必包含在Halide中;我可以自己定义它。这是我对h_id的定义:
Halide::Func h_id(Halide::Buffer<int64_t> in) {
Halide::Var x, y;
Halide::Func ret;
ret(x,y) = in(x,y);
return ret;
}
然后,就像在理想版本2中一样,我可以轻松地在任何缓冲区上调用它,而不必定义新的Func和创建新的Vars。