opencl c 文件编译失败

发布于 2024-12-08 13:24:15 字数 2092 浏览 0 评论 0原文

问题:这个函数/内核声明中缺少什么导致其无法编译?


信息:

__kernel void square(
   __global float* input,
   __global float* output,
   const unsigned int count)
{
int i = get_global_id(0);
if(i < count)
    output[i] = input[i] * input[i];
}

上面的代码片段在我的显卡上编译。我已经让它工作了,但出于维护原因,我决定最好将代码从文件中的字符串移动到单独的文件中,然后简单地读入它,没有什么异常。我认为,理论上,我应该能够编译这段代码,无论它是否运行,但至少在 gcc 和命令行中进行调试,然后在运行时使用我的 GPU 进行调试。

但上面的代码只是给了我错误:

GalGenCL.cl.c:51:10:错误:“void”之前应有“=”、“,”、“;”、“asm”或“属性

,这与我的错误相同得到了我想使用的实际代码:

__kernel void force(__global float* Galaxy, const unsigned int count)
{
    int i = get_global_id(0);
    float x,y,z,d,force;
    int j;
    for(j = 0; j < starc; j++)
    {    
        if (j == i) continue;
        //find relative distance
        x = Galaxy[i][1] - Galaxy[j][1];
        y = Galaxy[i][2] - Galaxy[j][2];
        z = Galaxy[i][3] - Galaxy[j][3];
        d = x*x+y*y+z*z;
        if (d == 0) continue;
        force = ((0.00000066742799999999995)*Galaxy[i][0]*Galaxy[j][0])/(d);
        Galaxy[i][7] = (x*x)*force*(-1)/d;
        Galaxy[i][8] = (y*y)*force*(-1)/d;
        Galaxy[i][9] = (z*z)*force*(-1)/d;
    }//end for loop
}

所以我尝试更改为:

__kernel __attribute__((vec_type_hint(float))); 
void force(__global float* Galaxy, const unsigned int count)

并得到了这个:

GalGenCL.cl.c:8:1:警告:数据定义没有类型或存储 类 GalGenCL.cl.c:8:39: 错误:“float”之前的预期表达式 GalGenCL.cl.c:8:45:错误:预期为“,”或“;”在 ')' 标记之前 GalGenCL.cl.c:9:21:错误:“浮动”之前应有“)”

所以我再次更改,将浮动取出:

__kernel __attribute__((vec_type_hint())); 
void force(__global float* Galaxy, const unsigned int count)

这使它更快乐一点:

GalGenCL.cl.c:8:1:警告:数据定义没有类型或存储类 GalGenCL.cl.c:8:1:警告:忽略“vec_type_hint”属性指令 GalGenCL.cl.c:9:21:错误:“浮动”之前应有“)”

但它仍然不接受函数头中的“浮动”,所以我摆脱了分号。然后它抱怨道:

GalGenCL.cl.c:9:1:错误:应为“,”或“;”在“无效”之前

所以现在我只是一无所知。它到底在寻找什么?

Question: What is missing from this function/kernel declaration that is keeping it from compiling?


Info:

__kernel void square(
   __global float* input,
   __global float* output,
   const unsigned int count)
{
int i = get_global_id(0);
if(i < count)
    output[i] = input[i] * input[i];
}

The code snippet above compiles on my graphics card. I've gotten this to work, but i decided for maintenance reasons it would be better to move the code from a string in my file to a separate file and simply read it in, nothing out of the ordinary. I figured that, in theory, i should be able to compile this code, whether or not it runs, but at least debug in gcc and command line then at run time with my gpu.

but the code above simply gives me the error:

GalGenCL.cl.c:51:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘void’

and that was the same error I got with the actual code I wanted to use:

__kernel void force(__global float* Galaxy, const unsigned int count)
{
    int i = get_global_id(0);
    float x,y,z,d,force;
    int j;
    for(j = 0; j < starc; j++)
    {    
        if (j == i) continue;
        //find relative distance
        x = Galaxy[i][1] - Galaxy[j][1];
        y = Galaxy[i][2] - Galaxy[j][2];
        z = Galaxy[i][3] - Galaxy[j][3];
        d = x*x+y*y+z*z;
        if (d == 0) continue;
        force = ((0.00000066742799999999995)*Galaxy[i][0]*Galaxy[j][0])/(d);
        Galaxy[i][7] = (x*x)*force*(-1)/d;
        Galaxy[i][8] = (y*y)*force*(-1)/d;
        Galaxy[i][9] = (z*z)*force*(-1)/d;
    }//end for loop
}

so i tried changing to this:

__kernel __attribute__((vec_type_hint(float))); 
void force(__global float* Galaxy, const unsigned int count)

and got this:

GalGenCL.cl.c:8:1: warning: data definition has no type or storage
class GalGenCL.cl.c:8:39: error: expected expression before ‘float’
GalGenCL.cl.c:8:45: error: expected ‘,’ or ‘;’ before ‘)’ token
GalGenCL.cl.c:9:21: error: expected ‘)’ before ‘float’

so i changed again, taking the float out:

__kernel __attribute__((vec_type_hint())); 
void force(__global float* Galaxy, const unsigned int count)

that made it a little happier:

GalGenCL.cl.c:8:1: warning: data definition has no type or storage class
GalGenCL.cl.c:8:1: warning: ‘vec_type_hint’ attribute directive ignored
GalGenCL.cl.c:9:21: error: expected ‘)’ before ‘float’

but still it wouldn't accept "float" from the function header, so i got ride of the semicolon. then it complained:

GalGenCL.cl.c:9:1: error: expected ‘,’ or ‘;’ before ‘void’

so now I'm just all kinds of clueless. what exactly it looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2024-12-15 13:24:15

OpenCL 是 C 的扩展,因此,如果使用任何扩展,C 编译器将不会解析和编译它。

因此,让 C 编译器处理 GalGenCL.cl.c 文件是没有意义的,除非明确考虑了扩展名 - 可能通过 #define 来隐藏或删除它们。

看来 __kernel 修饰符在编译中存在问题。

使用什么命令行来编译您的程序?

OpenCL is a extension to C, and as such, a C compiler will not parse and compile it if any extensions are used.

It therefore does not make sense to have a GalGenCL.cl.c file to be processed by a C compiler, unless the extensions are explicitly accounted for - possibly via #define to hide or remove them.

It would seem the the __kernel modifier is being problematic in compilation.

What command line is being used to compile your program?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文