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.
1686 Discussions

OpenCL explicit vector alignment for structs

dpshamonin
Beginner
300 Views
Hi,

After checking OpenCL specification (Section 6) for me it is still unclear
whether you have to provide explicit vector alignment for structs initialization.

OpenCL example on different platforms (Intel, NVidia, AMD):

typedef struct
{
float4 f;
float2 g;
float4 h;
} A;

__constant A a = {0,1,2,3,4,5,6,7,8,9}; // Will compiles fine on Intel and NVidia. FAIL on AMD!!!
__constant A a = {{0,1,2,3},{4,5},{6,7,8,9}}; // Will compiles fine on Intel, NVidia and AMD.

Could you provide some explanation how you would prefer to see it according to the standard?

Thanks,
-Denis
0 Kudos
2 Replies
Guy_B_Intel
Employee
300 Views

Hi Denis,

C99 has its standard way to initialize structures by enclosing the initializing values in braces. OpenCL doesn't override this specification.

As for vector elements, the right way to initialize a float4 variable would be:

float4 f = (float4)(0.f,1.f,2.f,3.f);

as stated in the OpenCL 1.1 spec, section 6.1.6.

So the right way to do this whole initialization is:

__constant A d = {(float4)(0,1,2,3),(float2)(4,5),(float4)(6,7,8,9)};

Other forms of initialization might compile, but the result might not be as intended.

For example, __constant A a = {0,1,2,3,4,5,6,7,8,9}; will initialize the first vector (f) with 0 in all its elements, the second vector (g) will be initialized with the value (float2)(1,1), and the third (h) vector will hold (float4)(2,2,2,2). The rest of the initializer list will be thrown away.

Thanks

Guy

SergeyKostrov
Valued Contributor II
300 Views
...
Other forms of initialization might compile, but the result might not be as intended.
...


I decided to reproducethe casewith anMS C/C++ compiler ( VS 2005 ). A C structure lookslike:

typedef struct tagA
{
float f[4];
float g[2];
float h[4];
} A;

So,after initialization avariable 'v' of a type Alooked like:

v.f[0] = 0
v.f[1] = 1
v.f[2] = 2
v.f[3] = 3

v.g[0]= 4
v.g[1]= 5

v.h[0]= 6
v.h[1]= 7
v.h[2]= 8
v.h[3]= 9

in both cases ( described in the initial post ).

It doesn't matter if you declare a set of numbers for initialization with brackets or without brackets.

Best regards,
Sergey

Reply