Halide学习笔记----Halide tutorial源码阅读6

来源:互联网 发布:郭天祥十天学会单片机4 编辑:程序博客网 时间:2024/06/05 18:15

Halide入门教程06


// Halide tutorial lesson 6: Realizing Funcs over arbitrary domains// Halide入门教程第六课:在指定区域上执行函数// This lesson demonstrates how to evaluate a Func over a domain that// does not start at (0, 0).// 本课演示了如何在指定区域上执行函数,而不是默认的以(0,0)起点在整个图像上执行操作// On linux, you can compile and run it like so:// g++ lesson_06*.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -o lesson_06 -std=c++11// LD_LIBRARY_PATH=../bin ./lesson_06#include "Halide.h"#include <stdio.h>using namespace Halide;int main(int argc, char **argv) {    // We define our familiar gradient function.    Func gradient("gradient");    Var x("x"), y("y");    gradient(x, y) = x + y;    // And turn on tracing so we can see how it is being evaluated.    gradient.trace_stores();    // 之前,按照如下方式进行梯度计算    // gradient.realize(8, 8);    // 它隐含地做了如下三件事    // 1) 生成可以在任何矩形上进行计算的代码    // 2) 分配一个新的8x8的图像存储空间    // 3) 遍历x, y从(0, 0) 到 (7, 7) 把生成的结果保存到图像中    // 4) 返回结果图像    // 如果不想Halide分配新的图像空间,可以采用其他的方式调用realize成员函数。将图像以参数的形式传给realize    // 将结果填充到对应的参数图像当中。下面的例子就是将计算结果填充到一幅已经存在的图像当中。    printf("Evaluating gradient from (0, 0) to (7, 7)\n");    Buffer<int> result(8, 8);    gradient.realize(result);    // Let's check it did what we expect:    for (int y = 0; y < 8; y++) {        for (int x = 0; x < 8; x++) {            if (result(x, y) != x + y) {                printf("Something went wrong!\n");                return -1;            }        }    }    // 现在,想要在5x7的矩形区域上计算梯度,而且起始坐标为(100, 50)    // We start by creating an image that represents that rectangle:    // 创建一个表示5x7图像的矩形区域    Buffer<int> shifted(5, 7); // 在构造函数中指定尺寸    shifted.set_min(100, 50); // 指定,计算区域的其实坐标(左上角坐标)    printf("Evaluating gradient from (100, 50) to (104, 56)\n");    // 这里不需要重新编译代码,原因是halide生成的是可以在任何矩形上操作的代码    gradient.realize(shifted);    // From C++, we also access the image object using coordinates    // that start at (100, 50).    for (int y = 50; y < 57; y++) {        for (int x = 100; x < 105; x++) {            if (shifted(x, y) != x + y) {                printf("Something went wrong!\n");                return -1;            }        }    }    // 如果想要在非矩形的区域上操作呢?非常遗憾,Halide值提供了矩形区域的操作。    printf("Success!\n");    return 0;}

编译并运行程序:

$ g++ lesson_06*.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -o lesson_06 -std=c++11$ ./lesson_06

运行结果

Begin pipeline gradient.0()Store gradient.0(0, 0) = 0Store gradient.0(1, 0) = 1Store gradient.0(2, 0) = 2Store gradient.0(3, 0) = 3Store gradient.0(4, 0) = 4Store gradient.0(5, 0) = 5Store gradient.0(6, 0) = 6Store gradient.0(7, 0) = 7Store gradient.0(0, 1) = 1Store gradient.0(1, 1) = 2Store gradient.0(2, 1) = 3Store gradient.0(3, 1) = 4Store gradient.0(4, 1) = 5Store gradient.0(5, 1) = 6Store gradient.0(6, 1) = 7Store gradient.0(7, 1) = 8Store gradient.0(0, 2) = 2Store gradient.0(1, 2) = 3Store gradient.0(2, 2) = 4Store gradient.0(3, 2) = 5Store gradient.0(4, 2) = 6Store gradient.0(5, 2) = 7Store gradient.0(6, 2) = 8Store gradient.0(7, 2) = 9Store gradient.0(0, 3) = 3Store gradient.0(1, 3) = 4Store gradient.0(2, 3) = 5Store gradient.0(3, 3) = 6Store gradient.0(4, 3) = 7Store gradient.0(5, 3) = 8Store gradient.0(6, 3) = 9Store gradient.0(7, 3) = 10Store gradient.0(0, 4) = 4Store gradient.0(1, 4) = 5Store gradient.0(2, 4) = 6Store gradient.0(3, 4) = 7Store gradient.0(4, 4) = 8Store gradient.0(5, 4) = 9Store gradient.0(6, 4) = 10Store gradient.0(7, 4) = 11Store gradient.0(0, 5) = 5Store gradient.0(1, 5) = 6Store gradient.0(2, 5) = 7Store gradient.0(3, 5) = 8Store gradient.0(4, 5) = 9Store gradient.0(5, 5) = 10Store gradient.0(6, 5) = 11Store gradient.0(7, 5) = 12Store gradient.0(0, 6) = 6Store gradient.0(1, 6) = 7Store gradient.0(2, 6) = 8Store gradient.0(3, 6) = 9Store gradient.0(4, 6) = 10Store gradient.0(5, 6) = 11Store gradient.0(6, 6) = 12Store gradient.0(7, 6) = 13Store gradient.0(0, 7) = 7Store gradient.0(1, 7) = 8Store gradient.0(2, 7) = 9Store gradient.0(3, 7) = 10Store gradient.0(4, 7) = 11Store gradient.0(5, 7) = 12Store gradient.0(6, 7) = 13Store gradient.0(7, 7) = 14End pipeline gradient.0()Begin pipeline gradient.0()Store gradient.0(100, 50) = 150Store gradient.0(101, 50) = 151Store gradient.0(102, 50) = 152Store gradient.0(103, 50) = 153Store gradient.0(104, 50) = 154Store gradient.0(100, 51) = 151Store gradient.0(101, 51) = 152Store gradient.0(102, 51) = 153Store gradient.0(103, 51) = 154Store gradient.0(104, 51) = 155Store gradient.0(100, 52) = 152Store gradient.0(101, 52) = 153Store gradient.0(102, 52) = 154Store gradient.0(103, 52) = 155Store gradient.0(104, 52) = 156Store gradient.0(100, 53) = 153Store gradient.0(101, 53) = 154Store gradient.0(102, 53) = 155Store gradient.0(103, 53) = 156Store gradient.0(104, 53) = 157Store gradient.0(100, 54) = 154Store gradient.0(101, 54) = 155Store gradient.0(102, 54) = 156Store gradient.0(103, 54) = 157Store gradient.0(104, 54) = 158Store gradient.0(100, 55) = 155Store gradient.0(101, 55) = 156Store gradient.0(102, 55) = 157Store gradient.0(103, 55) = 158Store gradient.0(104, 55) = 159Store gradient.0(100, 56) = 156Store gradient.0(101, 56) = 157Store gradient.0(102, 56) = 158Store gradient.0(103, 56) = 159Store gradient.0(104, 56) = 160End pipeline gradient.0()Evaluating gradient from (0, 0) to (7, 7)Evaluating gradient from (100, 50) to (104, 56)Success!

可以清晰看到,指定计算区域之后的梯度计算区域成空移到了(100,50)开始5x7的矩形区域

要点提炼:

// 算法描述gradient = x + y;// 指定计算区域大小Buffer<int> shifted(5, 7);// 指定起始点坐标shifted.min(100, 50);// 函数实现,结果保存到shifted中gradient.realize(shifted);
原创粉丝点击