我正在使用 Halide 来优化模板计算,但调度部分是一个挑战!这是我的卤化物代码,我正在使用 AOT 编译:
//Halide declarations:
Param<int> max_x, max_y, max_z;
Param<float> r;
Var x("x"), y("y"), z("z"), s, xi("xi"), yi("yi"), m("n"), xo("xo"), yo("yo"), index("idx");
Func f1_unbound("f_ub"), f1_bound("f_b"), result("res");
ImageParam input1(type_of<float>(), 3);
ImageParam input2(type_of<float>(), 3);
Expr t_h;
//The algorithm:
t_h = input2(x, y, z) / (r * input1(x, y, z));
f1_unbound(x, y, z) = 100.0f / (t_h) * pow((t_h / 200.0f), 1.5f);
f1_bound(x, y, z) = BoundaryConditions::repeat_edge(f1_unbound, 1, max_x - 2, 1, max_y - 2, 1, max_z - 2)(x, y, z);
result(x, y, z) = 0.125f * (f1_bound(x, y, z) + f1_bound(x + 1, y, z) +
f1_bound(x, y + 1, z) + f1_bound(x + 1, y + 1, z) +
f1_bound(x, y, z + 1) + f1_bound(x + 1, y, z + 1) +
f1_bound(x, y + 1, z + 1) + f1_bound(x + 1, y + 1, z + 1));
f1_bound.split(x, x, xi, 32).unroll(y, 2).unroll(xi, 2).vectorize(xi, 16).compute_at(result, y).store_at(result, y);
//f1_unbound.compute_root();
//f1_bound.vectorize(x, 16);
result.tile(x, y, x, y, xi, yi, 32, 8).vectorize(xi, 16).bound(x, 0, ((max_x + 1) / 2) * 2).bound(y, 0, ((max_y + 1) / 2) * 2).bound(z, 0, (max_z + 1)).parallel(y);
result.print_loop_nest();
result.compile_to_static_library("v_halide", {input1, input2, r, max_x, max_y, max_z}, "v_halide");
std::cout << "Compiled to static library!" << std::endl;
我了解在拆分/平铺和指定每个函数的评估位置时,使用不同的时间表时性能如何变化。但是,我在并行性能方面存在一些问题。我尝试了不同的并行化调度,例如上面的那个。然而,我还没有找到一个有效的并行计划,并且分析数字有点令人困惑。对于上述计划,随着线程数量的增加,“结果”变得更慢,f1_bound 变得更快,而报告为线程的数量(如果我没记错的话是每个区域中活动线程的平均数量)都在增加:
4 个线程:
使用的平均线程数:3.586322堆分配:19500 峰值堆使用量:116640 字节
res:0.946ms (33%) 线程:3.119
f1_b:1.873ms (66%) 线程:3.823 峰值:116640 数量:19500 平均:29160
2 个线程:使用的平均线程数:1.934264
堆分配:19500 峰值堆使用量:58320 字节
res: 0.769ms (19%) 线程: 1.794
f1_b:3.152ms (80%) 线程:1.968 峰值:58320 数量:19500 平均:29160
当我同时安排 f1_bound 和 unbound 时,随着线程数量的增加,我得到了更好的扩展,但我认为局部性较少,因此具有单线程的代码比没有并行化的代码要慢。
f1_bound.split(x, x, xi, 32).unroll(y, 2).unroll(xi, 2).vectorize(xi, 16).compute_at(result, y);
f1_unbound.split(x, x, xi, 32).unroll(y, 2).unroll(xi, 2).vectorize(xi, 16).compute_at(result, y).store_at(result, y);
对更好的时间表有什么建议吗?