为什么我的表现不好? (Noob日程安排)

时间:2015-10-24 04:28:16

标签: halide

我主要是一个非常高级的程序员,因此思考像CPU本地这样的事情对我来说是一个新手。

我正在研究一个基本的双线性去马赛克(对于RGGB传感器数据)并且我已经算法正确(根据结果判断),但它的表现不如我&#39 ; d希望(~210Mpix / s)。

这是我的代码(输入是带有单个RGGB通道的4640x3472图像):

def get_bilinear_debayer(input_raw, print_nest=False):
    x, y, c = Var(), Var(), Var()

    # Clamp and move to 32 bit for lots of space for averaging.
    input = Func()
    input[x,y] = cast(
        UInt(32),
        input_raw[
            clamp(x,0,input_raw.width()-1),
            clamp(y,0,input_raw.height()-1)]
    )

    # Interpolate vertically
    vertical = Func()
    vertical[x,y] = (input[x,y-1] + input[x,y+1])/2

    # Interpolate horizontally
    horizontal = Func()
    horizontal[x,y] = (input[x-1,y] + input[x+1,y])/2

    # Interpolate on diagonals
    diagonal_average = Func()
    diagonal_average[x, y] = (
        input[x+1,y-1] + 
        input[x+1,y+1] +
        input[x-1,y-1] +
        input[x-1,y+1])/4

    # Interpolate on adjacents
    adjacent_average = Func()
    adjacent_average[x, y] = (horizontal[x,y] + vertical[x,y])/2

    red, green, blue = Func(), Func(), Func()

    # Calculate the red channel
    red[x, y, c] = select(
        # Red photosite
        c == 0, input[x, y],
        # Green photosite
        c == 1, select(x%2 == 0, vertical[x,y],
                                 horizontal[x,y]),
        # Blue photosite
        diagonal_average[x,y]
    )

    # Calculate the blue channel
    blue[x, y, c] = select(
        # Blue photosite
        c == 2, input[x, y],
        # Green photosite
        c == 1, select(x%2 == 1, vertical[x,y],
                                 horizontal[x,y]),
        # Red photosite
        diagonal_average[x,y]
    )

    # Calculate the green channel
    green[x, y, c] = select(
        # Green photosite
        c == 1, input[x,y],
        # Red/Blue photosite
        adjacent_average[x,y]
    )

    # Switch color interpolator based on requested color.
    # Specify photosite as third argument, calculated as [x, y, z] = (0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 2)
    # Happily works out to a sum of x mod 2 and y mod 2.
    debayer = Func()
    debayer[x, y, c] = select(c == 0, red[x, y, x%2 + y%2],
                              c == 1, green[x, y, x%2 + y%2],
                                      blue[x, y, x%2 + y%2])


    # Scheduling
    x_outer, y_outer, x_inner, y_inner, tile_index = Var(), Var(), Var(), Var(), Var()

    bits = input_raw.get().type().bits

    output = Func()
    # Cast back to the original colour space
    output[x,y,c] = cast(UInt(bits), debayer[x,y,c])
    # Reorder so that colours are calculated in order (red runs, then green, then blue)

    output.reorder_storage(c, x, y)
    # Tile in 128x128 squares
    output.tile(x, y, x_outer, y_outer, x_inner, y_inner, 128, 128)
    # Vectorize based on colour
    output.bound(c, 0, 3)
    output.vectorize(c)
    # Fuse and parallelize
    output.fuse(x_outer, y_outer, tile_index)
    output.parallel(tile_index)

    # Debugging
    if print_nest:
        output.print_loop_nest()
        debayer.print_loop_nest()
        red.print_loop_nest()
        green.print_loop_nest()
        blue.print_loop_nest()

    return output

老实说,我不知道我在这里做了什么,而且我对此过于陌生,不知道在哪里或者应该看什么。

有关如何改进计划的任何建议都很有帮助。我还在学习,但反馈很难找到。

我所拥有的时间表是我能做的最好的,但它完全是试验和错误。

编辑:我通过直接在函数中进行整个相邻的平均求和,并在x_inner而不是颜色上进行矢量化,增加了额外的30Mpix / s。

编辑:新时间表:

# Set input bounds.
output.bound(x, 0, (input_raw.width()/2)*2)
output.bound(y, 0, (input_raw.height()/2)*2)
output.bound(c, 0, 3)

# Reorder so that colours are calculated in order (red runs, then green, then blue)
output.reorder_storage(c, x, y)
output.reorder(c, x, y)

# Tile in 128x128 squares
output.tile(x, y, x_outer, y_outer, x_inner, y_inner, 128, 128)
output.unroll(x_inner, 2).unroll(y_inner,2)

# Vectorize based on colour
output.unroll(c)
output.vectorize(c)

# Fuse and parallelize
output.fuse(x_outer, y_outer, tile_index)
output.parallel(tile_index)

编辑:最终时间表现在超过(640MP / s)在CPU Intel Performance Primitive benchmark上运行的twice as powerful as mine

output = Func()

# Cast back to the original colour space
output[x,y,c] = cast(UInt(bits), debayer[x,y,c])

# Set input bounds.
output.bound(x, 0, (input_raw.width()/2)*2)
output.bound(y, 0, (input_raw.height()/2)*2)
output.bound(c, 0, 3)

# Tile in 128x128 squares
output.tile(x, y, x_outer, y_outer, x_inner, y_inner, 128, 128)
output.unroll(x_inner, 2).unroll(y_inner, 2)

# Vectorize based on colour
output.vectorize(x_inner, 16)

# Fuse and parallelize
output.fuse(x_outer, y_outer, tile_index)
output.parallel(tile_index)

target = Target()
target.arch = X86
target.os = OSX
target.bits = 64
target.set_feature(AVX)
target.set_feature(AVX2)
target.set_feature(SSE41)

output.compile_jit(target)

1 个答案:

答案 0 :(得分:3)

确保使用展开(c)使每通道选择逻辑优化。在x和y中展开2也会有所帮助:

output.unroll(x, 2).unroll(y,2)

目标是优化偶数/奇数行和列之间的选择逻辑。为了充分利用这一点,你可能还需要告诉Halide min和extent是2的倍数:

output.output_buffer().set_bounds(0,
                                  (f.output_buffer().min(0) / 2) * 2,
                                  (output.output_buffer().extent(0) / 2) * 2)
output.output_buffer().set_bounds(1,
                                  (f.output_buffer().min(1) / 2) * 2,
                                  (output.output_buffer().extent(1) / 2) * 2)

虽然可能值得说明更严格的约束,例如使用128而不是2来断言磁贴大小的倍数,或者只是硬连接最小值和范围以反映实际传感器参数(如果您只支持单个摄像头)。