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

OpenCL C++ HelloWorld
EN

Stack Overflow用户
提问于 2022-01-29 14:09:24
回答 1查看 671关注 0票数 0

下午好!我正在学习本教程中的OpenCL C++:点击 (这不是必要的)

该视频使用CL API版本1.2,因此我从答复:https://stackoverflow.com/a/57017982/11968932中的链接下载了OpenCL 1.2报头。

Visual 2022没有显示错误,但程序输出以下符号:

代码语言:javascript
复制
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

应该说是"Hello World!"

这是程序本身。主机:

代码语言:javascript
复制
#define CL_USE_DEPRECATED_OPENCL_1_2_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_ALL, &devices);

    auto device = devices.front();

    std::ifstream helloWorldFile("HelloWorld.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_READ_ONLY, sizeof(buf));
    cl::Kernel kernel(program, "HelloWorld", &err);
    kernel.setArg(0, memBuf);

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

    std::cout << buf << " - buf" << std::endl;
}

HelloWorld.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

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-30 09:22:14

三个错误:

  1. 它要么是__kernel,要么是kernel,但不是带一个下划线的_kernel;对于__global也是如此。
  2. cl::Buffer memBuf(context, CL_MEM_READ_WRITE, 16*sizeof(buf)); --这里有两件事是错误的:CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY标志意味着设备端的缓冲区完全不可访问,它只为第一个字符分配内存(忘记了16*sizeof(buf))。
  3. queue.enqueueReadBuffer(memBuf, CL_TRUE, 0, 16*sizeof(buf), (void*)buf); -忘了16*sizeof(buf)

我也不得不在没有任何争论的情况下做auto err = program.build();

还请注意:

  • 当它工作时,堆栈分配(char buf[16];)限制缓冲区大小。使用堆分配(char* buf = new char[16];)代替(并且不要忘记delete[] buf;)。
  • 不要使用queue.enqueueTask(kernel);,而是使用queue.enqueueNDRangeKernel(cl_kernel, cl::NullRange, cl::NDRange(...), cl::NDRange(32));。这样,您就可以指定全局和本地范围。

最后,做一些广告:我创建了一个OpenCL包装器,以极大地简化学习和使用OpenCL。这个包装器不需要跟踪缓冲区大小,也不需要为CPU和设备提供重复的缓冲区。您需要为HelloWorld示例编写的代码要短得多,而且更容易:

代码语言:javascript
复制
#include "opencl.hpp"
int main() {
    const Device device(select_device_with_most_flops()); // compile OpenCL C code for the fastest available device
    const uint N = 16u; // size of vectors
    Memory<char> buf(device, N); // allocate memory on both host and device
    const Kernel HelloWorld(device, N, "HelloWorld", buf); // kernel that runs on the device
    HelloWorld.run(); // run add_kernel on the device
    buf.read_from_device(); // copy data from device memory to host memory
    println(buf.data());
}
代码语言:javascript
复制
#include "kernel.hpp" // note: string literals can't be arbitrarily long, so periodically interrupt with )+R(
string opencl_c_container() { return R( // ########################## begin of OpenCL C code ####################################################################

kernel void HelloWorld(global char* data) {
    data[0] = 'H';
    data[1] = 'e';
    data[2] = 'l';
    data[3] = 'l';
    data[4] = 'o';
    data[5] = 32; // spaces are wrongly converted with stringification macro, so use ascii code here instead of ' '
    data[6] = 'W';
    data[7] = 'o';
    data[8] = 'r';
    data[9] = 'l';
    data[10] = 'd';
    data[11] = '!';
    data[12] = '\n';
}

);} // ############################################################### end of OpenCL C code #####################################################################
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70906254

复制
相关文章

相似问题

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