首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCL HelloWorld

OpenCL HelloWorld
EN

Stack Overflow用户
提问于 2016-11-18 16:52:40
回答 2查看 7.8K关注 0票数 8

我刚刚开始在opencl中工作,目前我正在开发一个应该是opencl中相对基本的hello_world程序。不幸的是,程序没有输出正确的短语或任何东西,而是挂起,没有输出。

知道为什么会这样吗?

以下是: openglsource.cpp和hello.cl

代码语言:javascript
复制
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS

#include<CL/cl.hpp>
#include<iostream>
#include <fstream>

int main() 
{
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);

    auto platform = platforms.front();
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_CPU, &devices);

    auto device = devices.front();

    std::ifstream helloWorldFile("hello.cl");
    std::string src(std::istreambuf_iterator<char>(helloWorldFile), (std::istreambuf_iterator<char>()));

    cl::Program::Sources sources( 1, std::make_pair(src.c_str(), src.length() + 1));

    cl::Context context(device);
    cl::Program program(context, sources);

    auto err = program.build("-cl-std=CL1.2");

    char buf[16];
    cl::Buffer memBuf(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));
    cl::Kernel kernel(program, "Hello", &err);
    kernel.setArg(0, memBuf);

    cl::CommandQueue queue(context, device);
    queue.enqueueTask(kernel);
    queue.enqueueReadBuffer(memBuf, GL_TRUE, 0, sizeof(buf), buf);

    std::cout << "hello";
    std::cin.get();

}

hello.cl

代码语言:javascript
复制
__kernel void HelloWorld(__global char* data)
{
    data[0] = 'H';
    data[1] = 'E';
    data[2] = 'L';
    data[3] = 'L';
    data[4] = 'O';
    data[5] = ' ';
    data[6] = 'W';
    data[7] = 'O';
    data[8] = 'R';
    data[9] = 'L';
    data[10] = 'D';
    data[11] = '!';
    data[12] = '\n';
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-20 23:08:22

问题出在

代码语言:javascript
复制
 cl::Kernel kernel(program, "Hello", &err);

它应该是

代码语言:javascript
复制
 cl::Kernel kernel(program, "HelloWorld", &err); 

那里的字符串不只是一个任意名称,正如我之前推断的那样,它应该是您希望从指定内核调用的函数。这是合理的,一个内核可以容纳多个函数!

这么简单的修正.我很抱歉发帖!

票数 11
EN

Stack Overflow用户

发布于 2021-12-08 01:06:03

您还应该将GL_TRUE更改为CL_TRUE

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40682279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档