C++ cout未在控制台或OpenCL HelloWorld中的输出缓冲区上显示字符数组(char buf)
这是C++ cout未在控制台或OpenCL HelloWorld中的输出缓冲区上显示字符数组(char buf),c++,opencl,C++,Opencl,这是HelloWorld.cl内核文件。调试时,我可以看到buf具有helloworld\n,但它不会显示在控制台上 __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
HelloWorld.cl
内核文件。调试时,我可以看到buf
具有helloworld\n
,但它不会显示在控制台上
__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';
}
我第一次尝试OpenCL。我能够编译代码,但输出没有显示在控制台上
CreateProgram
设置初始上下文和程序
cl::Program CreateProgram(const std::string& file)
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
auto platform = platforms.front();
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
auto device = devices.front();
std::ifstream helloWorldFile(file);
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");
return program;
}
int main()
{
auto program = CreateProgram("HelloWorld.cl");
auto context = program.getInfo<CL_PROGRAM_CONTEXT>();
auto devices = context.getInfo<CL_CONTEXT_DEVICES>();
auto device = devices.front();
char buf[16];// output buffer where helloWorld is stored
cl::Buffer memBuf = cl::Buffer(context, CL_MEM_WRITE_ONLY |
CL_MEM_HOST_READ_ONLY, sizeof(buf));
cl::Kernel kernel(program, "HelloWorld");
kernel.setArg(0, memBuf);
cl::CommandQueue queue(context, device);
queue.enqueueTask(kernel);
queue.enqueueReadBuffer(memBuf, CL_TRUE, 0, sizeof(buf), buf);
std::cout << buf << std::endl; //I want buf to display on console but it is always blank
std::cin.get();
return 0;
}
cl::Program CreateProgram(const std::string&file)
{
向量平台;
cl::平台::获取(&platforms);
自动平台=platforms.front();
std::矢量设备;
平台。获取设备(CL\U设备\U类型\U GPU和设备);
自动设备=devices.front();
std::ifstream helloWorldFile(文件);
std::string src(std::istreambuf_迭代器(helloWorldFile),
(std::istreambuf_迭代器());
cl::Program::Sources(1,std::make_pair(src.c_str()),
src.length()+1));
cl::上下文(设备);
cl::程序(上下文、源);
auto err=program.build(“-cl std=CL1.2”);
返回程序;
}
int main()
{
auto program=CreateProgram(“HelloWorld.cl”);
自动上下文=program.getInfo();
自动设备=context.getInfo();
自动设备=devices.front();
char buf[16];//存储helloWorld的输出缓冲区
cl::Buffer memBuf=cl::Buffer(上下文,cl_MEM_WRITE_ONLY |
CL_MEM_HOST_READ_ONLY,sizeof(buf));
cl::内核内核(程序,“HelloWorld”);
setArg(0,memBuf);
cl::CommandQueue队列(上下文、设备);
排队任务(内核);
enqueueReadBuffer(memBuf,CL_TRUE,0,sizeof(buf),buf);
std::cout在初始化内核中的字符串时缺少nul终止字符
我不确定这个错误是否会完全阻止任何输出;您的程序是否会崩溃而不是打印输出
无论如何,要修复此特定错误(可能还有其他错误),请添加
到您的内核。使用以下设置使用cl2.hpp
强制执行opencl1.2
兼容性它对我有效:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include "cl2.hpp"
我把它改成:
cl::Program::Sources sources;
sources.push_back(src);
检查OpenCL函数返回的错误代码-其中很可能有不起作用的信息。如果它仍然不起作用,则向我们展示HelloWorld.cl
和CreateProgram
的实现。我添加了CreateProgram。没有错误。我使用了#define#cl\u ENABLE_异常。仍然没有HelloWorld.cl
.Include。我自己构建了它,它对我来说运行良好:它生成了预期的输出。我对代码进行了一些更改,因为我有编译错误-这可能是因为我使用了最新的cl2.hpp
。这只是为了说明小细节有时有多么重要。为了增加帮助您解决此问题的机会,您可以真的需要包括。谢谢,我不能使用openCL 2.0,因为我的卡只支持1.2。我添加了调试窗口的图片。我可以在buf中看到值,但无法在控制台上显示。如果使用(&buf),我可以显示地址,但不能显示values@redPanda我刚注意到你的cin.get()
。你试过把std::cout
cl::Program::Sources sources(1, std::make_pair(src.c_str(),
src.length() + 1));
cl::Program::Sources sources;
sources.push_back(src);