这是我的代码:
#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;
}
用gcc 4.6编译时,报错
test.c:14:11: error: invalid operands to binary + (have ‘cl_float3’ and ‘cl_float3’)
我很奇怪,因为 OpenCL Specification在第 6.4 节中仅演示了添加两个 floatn
。我需要包含任何其他 header 吗?
但更奇怪的是,当使用 -std=c99
编译时,我得到类似的错误
test.c:16:26: error: ‘cl_float3’ has no member named ‘x’
..对于所有组件(x、y 和 z)...
最佳答案
结构下标编译出问题的原因可以看AMD SDK中标准的实现。
如果您查看 <CL/cl_platform.h>
来自 AMD 工具包的 header ,您可以看到结构是如何定义的。
typedef cl_float4 cl_float3;
typedef union
{
cl_float CL_ALIGNED(16) s[4];
#if (defined( __GNUC__) || defined( __IBMC__ )) && ! defined( __STRICT_ANSI__ )
__extension__ struct{ cl_float x, y, z, w; };
....
#endif
}cl_float4;
#if
当使用 --std=c99
调用 gcc 时,子句被忽略.
要使您的代码与 --std=c99 一起工作,您可以替换对 f3.x
的引用与 f3.s[0]
等等。
关于c - 如何在主机端代码中使用 OpenCL vector 类型进行算术运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979487/