c - 如何在主机端代码中使用 OpenCL vector 类型进行算术运算?

标签 c gcc types opencl

这是我的代码:

#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/

相关文章:

Java - 特定泛型实例的实例方法

sql - (Oracle/SQL) 将所有数据类型合并到单个列中

c - Ubuntu gcc 中无法执行 "make"

objective-c - 使用ExtAudioFile read读取音频文件时,是否可以不连续读取音频 float ?

c - 保证每个线程的执行

Cairo(图形库)可以用来渲染数学公式和图片(latex风格)吗?

C 程序编译没有错误但不会运行并且没有警告或错误

ios - 为动态链接提供后备符号

C++类型转换

c - 我怎样才能从那个循环之外杀死一个处于无限循环中的 pthread?