cout在OpenCL的HelloWorld问题中未在控制台或输出缓冲区上显示char数组(char buf)

问题描述 投票:0回答:1

这是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;
    }
c++ opencl
1个回答
0
投票

您在内核中缺少一个以零结尾的字符。

我不确定这个错误是否应该完全阻止任何输出;您的程序是否崩溃而不是打印输出?

无论如何,要解决此特定错误(可能还有其他错误,请添加]​​>

data[13] = '\0';

到您的内核。

© www.soinside.com 2019 - 2024. All rights reserved.