OpenCL - 多个worker可以同时write_image同一个coord吗?

时间:2014-07-03 18:56:03

标签: macos image-processing kernel opencl

我有一个内核,它根据亮度拍摄图像并生成waveform out of it。在这个内核中,工作者最终必须将像素写入与其他工作者完全相同的位置。这样安全吗?

在我看来它看起来不错(英特尔5000图形),但在朋友的计算机上使用独立GPU,似乎是在内存泄漏或从GPU内存中的其他区域获取内存(可能在GPU中缓存的随机图像显示在渲染图像)。甚至问题出在哪里?开源回购是GCVideoWaveform。对此有任何想法都非常感谢!

int clampInt(int value, int lowerBound, int upperBound){
    if(value < lowerBound){
        return lowerBound;
    }
    if (value > upperBound){
        return upperBound;
    }
    return value;
}
kernel void waveformLuma(read_only image2d_t input, write_only image2d_t output, uint maxValue) {
    size_t x = get_global_id(0);
    size_t y = get_global_id(1);

    uint4 inputColor = read_imageui(input, sampler, (int2)(x,y));

    float luminance01 = (((float)inputColor.x + (float)inputColor.y + (float)inputColor.z) / 3.0) / (float)maxValue;

    int newY = (get_image_dim(output).y - (luminance01*(float)(get_image_dim(output).y-1)));
    newY = clampInt(newY, 0, get_image_dim(output).y-1);

    write_imageui(output, (int2)(x, newY), (uint4)(maxValue, maxValue, maxValue, maxValue));
}

第一台计算机/集成GPU

enter image description here

第二台计算机/离散GPU

enter image description here

1 个答案:

答案 0 :(得分:0)

write_imageuiread_imageui可以由具有相同参数的多个线程调用,没有问题。它们在OpenCL中用作任何其他赋值操作数。 例如:

__private float b = tanh[(global int *)a[2]]; //like read_image, reads from a global memory
(global int *)a[2] = b; //like write image, writes lo a global memory

注意: 我用了[2]来说明多个WI会写到同一个位置


write_imageui问题不是您遇到的问题,问题是写操作本身。结果完全未知,因为它们严重依赖于WI执行顺序。在CPU中,它提供了良好的结果(并行线程,但是一些底层同步),而在GPU(完全并行设备)上却没有。

但是,由于结果是“奇怪的”,我建议您考虑其他问题。我个人认为垃圾输出完全基于这种无序的执行行为。