OpenCL 内核代码编译错误 - Visual Studio 2019 [英] OpenCL Kernel code compile error - Visual Studio 2019

查看:191
本文介绍了OpenCL 内核代码编译错误 - Visual Studio 2019的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 OpenCL 编程有点陌生,正在尝试在 VS 2019 中运行一个简单的向量加法代码.但是,我无法编译 .cl 代码.尝试构建程序时显示这 6 个错误:

I'm kind of new to OpenCL programming and am trying to run a simple vector addition code in VS 2019. However, I can't get the .cl code to compile. It's showing these 6 errors when trying to build the program:

C2144 语法错误:'void' 前面应该有';'

错误 C4430 缺少类型说明符 - 假设为 int.注意:C++ 不支持 default-int

错误 C2065__global":未声明的标识符

C2146 语法错误:在标识符float4"之前缺少)"

C2143 语法错误:缺少;"'{'

错误 C2447{":缺少函数头(旧式正式列表?)

这是我的内核代码:

__kernel void add_numbers(__global float4* data,
    __local float* local_result, __global float* group_result) {

    float sum;
    float4 input1, input2, sum_vector;
    uint global_addr, local_addr;

    global_addr = get_global_id(0) * 2;
    input1 = data[global_addr];
    input2 = data[global_addr + 1];
    sum_vector = input1 + input2;

    local_addr = get_local_id(0);
    local_result[local_addr] = sum_vector.s0 + sum_vector.s1 +
        sum_vector.s2 + sum_vector.s3;
    barrier(CLK_LOCAL_MEM_FENCE);

    if (get_local_id(0) == 0) {
        sum = 0.0f;
        for (int i = 0; i < get_local_size(0); i++) {
            sum += local_result[i];
        }
        group_result[get_group_id(0)] = sum;
    }
}

我已经添加了 include 和 lib 目录并正确链接了它们.谷歌搜索后,我找不到针对此错误的许多修复程序.请帮帮我...

I have added the include and lib directories and linked them properly. I couldn't find many fixes for this error after googling. Please help me out...

更新:我修好了

大家好,我找到了这个问题的解决方案.我从 VS 项目中删除了 .cl 文件,然后重新添加了它(可选).我还将文件打开选项更改为rb"而不是r"( fopen(filename,rb") ).现在我可以运行它了!

Hello everyone, I found the solution to this problem. I removed the .cl file from VS projects and then re-added it (optional). I also changed file open option to have "rb" instead of "r" ( fopen(filename,"rb") ). Now I'm able to run it!

推荐答案

您的问题是 C++ 编译器想要编译 OpenCL 代码.您可以从 VS 项目中排除该文件并在运行时使用 fstream 读取它以获取内核代码字符串,或者您可以通过字符串化宏将内核代码字符串直接嵌入到可执行文件中:

Your issue was that the C++ compiler wanted to compile the OpenCL code. You can exclude the file from the VS project and read it with fstream at runtime to get the kernel code string, or you can embed the kernel code string right into the executable via stringification macro:

#include <string>
#define R(...) string(#__VA_ARGS__" ")
string get_opencl_code() { return R(

// put your OpenCL code here

);}

这篇关于OpenCL 内核代码编译错误 - Visual Studio 2019的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆