Halide學習筆記----Halide tutorial源碼閱讀1

Halide入門教程01


// Halide tutorial lesson 1: Getting started with Funcs, Vars, and Exprs
// Halide入門教程第一課:瞭解Funcs(函數),Vars(變量)和Exprs(表達式)

// This lesson demonstrates basic usage of Halide as a JIT compiler for imaging.
// 本課演示了Halide作爲圖像處理JIT compiler(即時編譯器)的一些基本用法

// On linux, you can compile and run it like so:
// 在linux操作系統上,你可以按照如下方式進行編譯和運行
// g++ lesson_01*.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -o lesson_01 -std=c++11
// LD_LIBRARY_PATH=../bin ./lesson_01

// On os x:
// g++ lesson_01*.cpp -g -I ../include -L ../bin -lHalide -o lesson_01 -std=c++11
// DYLD_LIBRARY_PATH=../bin ./lesson_01

// If you have the entire Halide source tree, you can also build it by
// 如果你有整個Halide代碼樹,你可以按照如下方式進行編譯
// running:
//    make tutorial_lesson_01_basics
// in a shell with the current directory at the top of the halide
// source tree.

// The only Halide header file you need is Halide.h. It includes all of Halide.
// Halide.h包含了整個Halide, 只需要include這個頭文件即可
#include "Halide.h"

// We'll also include stdio for printf.
#include <stdio.h>

int main(int argc, char **argv) {

    // This program defines a single-stage imaging pipeline that
    // outputs a grayscale diagonal gradient.

    // A 'Func' object represents a pipeline stage. It's a pure
    // function that defines what value each pixel should have. You
    // can think of it as a computed image.
    // Func對象表示了一個pipeline階段。它是一個純函數,定義了每個像素點對應的值。
    // 可以理解爲要進行計算的圖像。
    Halide::Func gradient;

    // Var objects are names to use as variables in the definition of
    // a Func. They have no meaning by themselves.
    // Var對象是Func的定義域,或者說是Func的參數。它們本身沒有任何意義。Var用來索引
    // 函數(圖像)對應的像素點
    Halide::Var x, y;

    // We typically use Vars named 'x' and 'y' to correspond to the x
    // and y axes of an image, and we write them in that order. If
    // you're used to thinking of images as having rows and columns,
    // then x is the column index, and y is the row index.
    // x和y分別對應着圖像的x軸和y軸,x對應的是列索引,y對應着行索引
    // -------------> x axes
    // |
    // |
    // |
    // \/ y axes

    // Funcs are defined at any integer coordinate of its variables as
    // an Expr in terms of those variables and other functions.
    // Here, we'll define an Expr which has the value x + y. Vars have
    // appropriate operator overloading so that expressions like
    // 'x + y' become 'Expr' objects.
    // 函數定義在整數座標處,函數值是變量和其他函數的表達式的結果。
    // 在此我們定義了一個 x + y的表達式。變量重載了算數運算符,因此變量進行運算的結果是一個Expr對象
    Halide::Expr e = x + y;

    // Now we'll add a definition for the Func object. At pixel x, y,
    // the image will have the value of the Expr e. On the left hand
    // side we have the Func we're defining and some Vars. On the right
    // hand side we have some Expr object that uses those same Vars.
    // 現在我們將給函數對象一個定義的實現。在像素點x,y處,圖像的像素值爲表達式e的結果。
    // 函數對象和一些變量位於表達式的左邊,表達式的右邊是一些Expr對象。
    // gradient(x, y) = e 相當於在x,y處的像素值是表達式x+y的運算結果。
    gradient(x, y) = e;

    // This is the same as writing:
    //
    //   gradient(x, y) = x + y;
    //
    // which is the more common form, but we are showing the
    // intermediate Expr here for completeness.

    // That line of code defined the Func, but it didn't actually
    // compute the output image yet. At this stage it's just Funcs,
    // Exprs, and Vars in memory, representing the structure of our
    // imaging pipeline. We're meta-programming. This C++ program is
    // constructing a Halide program in memory. Actually computing
    // pixel data comes next.
    // 上述幾行代碼定義了Func,但實際上並沒有計算輸出圖像。在這個階段,它僅僅是內存函數,表達式和變量,
    // 我們在進行元編程。C++程序正在內存中構造Halide程序。實際上進行像素數據計算的在下一階段進行。

    // Now we 'realize' the Func, which JIT compiles some code that
    // implements the pipeline we've defined, and then runs it.  We
    // also need to tell Halide the domain over which to evaluate the
    // Func, which determines the range of x and y above, and the
    // resolution of the output image. Halide.h also provides a basic
    // templatized image type we can use. We'll make an 800 x 600
    // image.
    // 在此,我們realize前一個階段定義的Func,即時編譯器編譯我們定義的算法流程代碼,然後運行代碼。
    // 我們需要告訴Halide在在指定的區域進行計算。這裏的domain可以理解爲像素點的範圍。domain決定了
    // 前面的x,y變量的範圍,輸出圖像的分辨率等。Halide.h提供了供使用的一些基本的圖像類型模板。
    // 在此,我們是哦用一個800x600的圖像。
    Halide::Buffer<int32_t> output = gradient.realize(800, 600);

    // Halide does type inference for you. Var objects represent
    // 32-bit integers, so the Expr object 'x + y' also represents a
    // 32-bit integer, and so 'gradient' defines a 32-bit image, and
    // so we got a 32-bit signed integer image out when we call
    // 'realize'. Halide types and type-casting rules are equivalent
    // to C.
    // Halide能夠進行數據類型推斷。Var對象由32位(有符號)整數表示,Expr對象x+y也是32位有符號整數
    // 因此,gradient定義了一幅32位的圖像。在我們調用了realize之後,得到一幅32爲有符號整數圖像輸出。
    // Halide的類型轉換規則和C語言的類型轉換一致。

    // Let's check everything worked, and we got the output we were
    // expecting:
    for (int j = 0; j < output.height(); j++) {
        for (int i = 0; i < output.width(); i++) {
            // We can access a pixel of an Buffer object using similar
            // syntax to defining and using functions.
            if (output(i, j) != i + j) {
                printf("Something went wrong!\n"
                       "Pixel %d, %d was supposed to be %d, but instead it's %d\n",
                       i, j, i+j, output(i, j));
                return -1;
            }
        }
    }

    // Everything worked! We defined a Func, then called 'realize' on
    // it to generate and run machine code that produced an Buffer.
    printf("Success!\n");

    return 0;
}

在linux終端中編譯並執行:

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

結果如下:

Success!

則說明Halide配置環境搭建成功,並能夠進行代碼編譯執行.

Halide代碼流程:

1. 聲明函數,變量,表達式
2. 用變量、表達式等實現算法
3. 使用Halide的算法調度策略對算法進行調度調優
4. 調用函數的realize成員函數,對函數進行實現
5. 寫出數據
發佈了32 篇原創文章 · 獲贊 18 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章