How to do arithmetic with OpenCL vector types in host-side code?

c, gcc, opencl, types

Solution

OpenCL programs consist of two parts.

- A program which runs on the host. This is normally written in C or C++, but it's nothing special except that it uses the API described in sections 4 & 5 of the OpenCL Specification.

- A kernel which runs on the OpenCL device (normally a GPU). This is written in the language specified in section 6. This isn't C, but it's close. It adds things like vector operations (like you're trying to use). This is compiled by the host program passing a string which contains the kernel code to OpenCL via the API.

You've confused the two, and tried to use features of the kernel language in the host code.

Problem

Here's my code: ``` #include <stdio.h> #include <CL/cl.h> #include <CL/cl_platform.h> int main(){ cl_float3 f3 = (cl_float3){1, 1, 1}; cl_float3 f31 = (cl_float3) {2, 2, 2}; cl_float3 f32 = (cl_float3) {2, 2, 2}; f3 = f31 + f32; printf("%g %g %g \n", f3.x, f3.y, f3.z); return 0; } ``` When compiling with gcc 4.6, it produces the error ``` test.c:14:11: error: invalid operands to binary + (have ‘cl_float3’ and ‘cl_float3’) ``` Very strange to me, because the OpenCL Specification demontrates in section 6.4 just that, an addition of two `floatn`. Do I need to include any other headers? But even more strange is that when compiling with `-std=c99` I get errors like ``` test.c:16:26: error: ‘cl_float3’ has no member named ‘x’ ``` ..for all components (x, y and z)...

Original source

Related problems