wtl
wtl

Reputation: 332

Precision when reading image with CLK_FILTER_LINEAR in OpenCL

The code I used is from this question OpenCL image3d linear sampling , I've tested in 2d and 3d, both with huge diff between CPU and GPU.

Here is the result of 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

The result of 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    

You may see the diff between CPU and GPU is large, e.g. 10.000002 and 10.156250.

Upvotes: 4

Views: 1264

Answers (1)

Brian Cole
Brian Cole

Reputation: 41

The pertinent part of the OpenCL specification that actually states that it doesn't guarantee the precision of these calculations:

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. 

I would still bug the vendor though, because I think it would be beneficial to lock this down in future standards.

Upvotes: 4

Related Questions