OpenCL 你好世界

OpenCL HelloWorld

我刚刚开始使用 opencl,目前正在研究 opencl 中应该是相对基础的 hello_world 程序。不幸的是,程序没有输出正确的短语或任何东西,而是挂起没有输出。

知道为什么会这样吗?

下面是:openglsource.cpp和hello.cl

#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

__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';
}

问题在

 cl::Kernel kernel(program, "Hello", &err);

应该是

 cl::Kernel kernel(program, "HelloWorld", &err); 

那里的字符串不仅仅是我推断的任意名称,它应该是您要从指定内核调用的函数。考虑到一个内核可以容纳多个函数,这是有道理的!

如此简单的修复.....我觉得不好发帖!

您还应该将 GL_TRUE 更改为 CL_TRUE