workaround for no pointers to image2d_t restriction

Hello,

I need to read value from one of two images, like that:


for (i = 1; i < N; i++)
{
p = foo(i);
if (cond)
  v = read_image_f(a, sampler, p);
else
  v = read_image_f(b, sampler, p);
}

I would like to optimize the branch to be outside the loop:

image_2d_t* im = cond ? a : b;
for (i = 1; i < N; i++)
{
  p = foo(i);
  v = read_image_f(*im, sampler, p);
}

But OpenCL does not allow pointers to image2d_t.

Any workaround for this?

In OpenCL 1.2 you can use an image array.

In older versions of OpenCL you can fission the loop:


if(cond)
{
    for (i = 1; i < N; i++)
    {
        p = foo(i);
        v = read_imagef(a, sampler, p);
    }
}
else
{
    for (i = 1; i < N; i++)
    {
        p = foo(i);
        v = read_imagef(b, sampler, p);
    }
}

I would personally let the compiler do that optimization instead so that the source code stays simple, unless this optimization was critical.

Thanks! (I work on Mac OS so no implementation for OpenCl 1.2 yet).
How can I ensure that the compiler does this optimization? Can I look at the assembly?

You can’t ensure that the compiler will make it, but it is a fairly common optimization. Some SDKs allow you to look at the machine code that is output by the compiler.