在OpenCL中使用CLK_FILTER_LINEAR读取图像时的精度

时间:2013-11-17 04:42:12

标签: image opencl gpu precision

我使用的代码来自这个问题OpenCL image3d linear sampling,我已经在2d和3d中进行了测试,两者都在CPU和GPU之间存在巨大差异。

以下是CPU的结果:

coordinate:0.000000, result: 0.000000
coordinate:0.100000, result: 0.000000
coordinate:0.200000, result: 0.000000
coordinate:0.300000, result: 10.156250
coordinate:0.400000, result: 30.078125
coordinate:0.500000, result: 50.000000
coordinate:0.600000, result: 69.921875
coordinate:0.700000, result: 89.843750
coordinate:0.800000, result: 100.000000
coordinate:0.900000, result: 100.000000
coordinate:1.000000, result: 100.000000

CPU的结果:

coordinate:0.000000, result: 0.000000
coordinate:0.000000, result: 0.000000
coordinate:0.100000, result: 0.000000
coordinate:0.200000, result: 0.000000
coordinate:0.300000, result: 10.000002
coordinate:0.400000, result: 30.000002
coordinate:0.500000, result: 50.000000
coordinate:0.600000, result: 70.000008
coordinate:0.700000, result: 90.000000
coordinate:0.800000, result: 100.000000
coordinate:0.900000, result: 100.000000
coordinate:1.000000, result: 100.000000    

你可能会看到CPU和GPU之间的差异很大,例如10.000002和10.156250。

1 个答案:

答案 0 :(得分:3)

OpenCL规范的相关部分实际上表明它并不能保证这些计算的精确性:

If the sampler is specified as using unnormalized coordinates (floating-point or integer
coordinates), filter mode set to CLK_FILTER_NEAREST and addressing mode set to one of the
following modes - CLK_ADDRESS_NONE, CLK_ADDRESS_CLAMP_TO_EDGE or
CLK_ADDRESS_CLAMP, the location of the image element in the image given by (i, j, k) in
section 8.2 will be computed without any loss of precision.

For all other sampler combinations of normalized or unnormalized coordinates, filter and
addressing modes, the relative error or precision of the addressing mode calculations and the
image filter operation are not defined by this revision of the OpenCL specification. To ensure a
minimum precision of image addressing and filter calculations across any OpenCL device, for
these sampler combinations, developers should unnormalize the image coordinate in the kernel
and implement the linear filter in the kernel with appropriate calls to read_image{f|i|ui} with a
sampler that uses unnormalized coordinates, filter mode set to CLK_FILTER_NEAREST,
addressing mode set to CLK_ADDRESS_NONE, CLK_ADDRESS_CLAMP_TO_EDGE or
CLK_ADDRESS_CLAMP and finally performing the interpolation of color values read from the
image to generate the filtered color value. 

我仍然会对供应商造成错误,因为我认为将其锁定在未来的标准中是有益的。