OpenCL HelloWorld [英] OpenCL HelloWorld

查看:69
本文介绍了OpenCL HelloWorld的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've just started working in opencl and I'm currently working on what should be a relatively basic hello_world program in opencl. Unfortunately the program is not outputting the proper phrase or anything at all it instead hangs with no output.

关于为什么会这样的任何想法?

Any idea on why that is the case?

下面是:openglsource.cpp和hello.cl

Below is: openglsource.cpp and 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); 

那里的字符串不只是我所推断的任意名称,它应该是您要从指定内核调用的函数.可以说一个内核可以容纳多个功能,这是有道理的!

The string that goes there is not just an arbitrary name as I had inferred it should be the function that you want to call from the specified kernel. Which makes sense give that a kernel can hold multiple functions!

这样一个简单的解决方法.....我觉得不好张贴!

Such a simple fix.....I feel bad for posting!

这篇关于OpenCL HelloWorld的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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