【问题标题】:openCL hello World display garbage outputopenCL hello World 显示垃圾输出
【发布时间】:2019-03-14 11:53:16
【问题描述】:

我正在尝试一个简单的 helloWorld openCL 代码,它编译时没有错误 但展示垃圾:╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠

错误函数在构建时检测到错误,因此删除了 program.build("-cl-std=CL2.1"); 中的“-cl-std=CL2.1”

这是主文件:

//#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#include "stdafx.h"

#include <CL/cl.hpp>
#include <fstream>
#include <iostream>

void checkError(cl_int err, const char *operation)
{
    if (err != CL_SUCCESS)
    {
        fprintf(stderr, "Error during operation '%s': %d\n", operation, err);
        exit(1);
    }
}

int main()
{

    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);

    _ASSERT(platforms.size() > 0);

    auto platform = platforms.front();
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

    _ASSERT(devices.size() > 0);

    auto device = devices.front();
    auto vendor = device.getInfo<CL_DEVICE_VENDOR>();
    auto version = device.getInfo<CL_DEVICE_VERSION>();

    //*********

    cl_int err;

    std::ifstream helloWorldFile("hello_world.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,0,0,0,&err);
    checkError(err, "context");

    cl::Program program(context, sources, &err);
    checkError(err, "program");

    err = program.build();
    checkError(err, "build");

    char buf[16];
    cl::Buffer memBuf(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf), &err);
    checkError(err, "memBuf");

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

    kernel.setArg(0, memBuf);


    cl::CommandQueue queue(context, device,0, &err);
    checkError(err, "queue");

    queue.enqueueTask(kernel);
    queue.enqueueReadBuffer(memBuf, CL_TRUE, 0, sizeof(buf), buf);

    std::cout << buf;
    std::cin.get();

}

还有 hello_world.cl

__kernel void HelloWorld(__global char* output) {
    output[0] = 'H';
    output[1] = 'e';
    output[2] = 'l';
    output[3] = 'l';
    output[4] = 'o';
    output[5] = ' ';
    output[6] = 'W';
    output[7] = 'o';
    output[8] = 'r';
    output[9] = 'l';
    output[10] = 'd';
    output[11] = '!';
    output[12] = '\n';
}

你能帮我找出问题吗,

【问题讨论】:

  • 您需要检查您的 opencl 调用的返回码,大概是 enqueueReadBuffer 失败,因为 buf 未对齐
  • 嗨@Alan,我真的是一个初学者,我不知道该怎么做,你能告诉我方法吗?
  • 你已经在做,每次打开 cl 调用后调用checkError
  • 该符号 表示 Microsoft 编译器上未初始化的内存。 stackoverflow.com/questions/370195/…
  • @AlanBirtles 你是对的 enqueueReadBuffer 有一个错误!你能说出它有什么问题吗?你觉得我怎么能修正读数

标签: c++ opencl nvidia opencl-c


【解决方案1】:

所以答案是在调用 cl::Buffer 时将 CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY 更改为 CL_MEM_READ_WRITE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多