OpenCL* for CPU
Ask questions and share information on Intel® SDK for OpenCL™ Applications and OpenCL™ implementations for Intel® CPU
Announcements
This forum covers OpenCL* for CPU only. OpenCL* for GPU questions can be asked in the GPU Compute Software forum. Intel® FPGA SDK for OpenCL™ questions can be ask in the FPGA Intel® High Level Design forum.
1663 Discussions

vector component addressing with operator [] is legal?

dpshamonin
Beginner
635 Views
Hi,

After checking OpenCL 1.1, 1,2 specification (6.1.7 Vector Components)
http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf
http://www.khronos.org/registry/cl/specs/opencl-1.2.pdf
I was unable to find that following vector component addressing construction is legal in OpenCL:

float4 var;
for(uint i=0; i<4; i++)
{
var = 0.0f;
}

I can understand that for Intel creating overloaded operator [] could make some code elegant.
And personally as programmer, I would prefer to have it as well. Nowadays with OpenCL 1.1
sometimes you forced to write something like this:

float foo(float8 v)
{
float[8] v1;
v1[0] = v.s0; v1[1] = v.s1; v1[2] = v.s2; v1[3] = v.s3;
v1[4] = v.s4; v1[5] = v.s5; v1[6] = v.s6; v1[7] = v.s7;

// compute something using loop, just an example
float res;
for (uint i=0, i<8; i++)
{
res += v1;
}
return res;
}

But strictly saying, if that is not legal according to OpenCL 1.1 specification
standard, I would vote to remove this operator from Intel OpenCL as well.
The main argument would be that code become not portable to other platforms.
For example try it on NVidia or AMD OpenCL 1.1 and you will get en error like this:

Build Log:
:27:3: error: subscripted value is not an array, pointer, or vector
var = 0.0f;
^ ~
It could be that I've missed some document.
Could you provide reference that such constructions are legal in OpenCL 1.1, 1.2?

Thanks,
-Denis
0 Kudos
3 Replies
Doron_S_Intel
Employee
635 Views
Hello Denis,

Allowing a square bracket notation for indexing vectors means the components accessed are not known during compile time. So, the change is not just syntactic in nature - it would actually be a full-fledged feature.

Doron Singer
Doron_S_Intel
Employee
635 Views
Hello Denis,

Allowing a square bracket notation for indexing vectors means the components accessed are not known during compile time. So, the change is not just syntactic in nature - it would actually be a full-fledged feature.

Doron Singer
Guy_B_Intel
Employee
635 Views
Hi Denis,
Using operator [] to access a single vector element is not supported according to the OpenCL spec, and it's also inefficient. This usage will be disabled in subsequent versions of the Intel OpenCL SDK.

Thanks
Guy
Reply