在OpenCL中创建CommandQueue时sem_wait()失败

时间:2015-07-27 18:06:32

标签: c++ opencl

我试图在OpenCL中编译并运行HelloWorld程序。我用两个调用标记了错误的位置:std :: cout<< "我在这里!\ n&#34 ;;但是我得到以下输出。我该怎么调试呢?代码的问题是什么?

似乎问题的一行是:

cl::CommandQueue queue(context, devices[0], 0, &err);

我的编译器标志是,程序输出:

make opencl_helloworld
g++ -w -lOpenCL -lrt \
-o opencl_helloworld ./opencl_helloworld.cpp
[user@host dp_src]$ ./opencl_helloworld 
Platform number is: 3
Platform is by: Advanced Micro Devices, Inc.
I'm here!
../../../thread/semaphore.cpp:87: sem_wait() failed
Aborted (core dumped)

这是opencl_helloworld.cpp文件:

#include <utility>
#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#include <CL/cl.hpp>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>

const std::string hw("Hello World\n");

inline void checkErr(cl_int err, const char * name) {
    if (err != CL_SUCCESS) {
        std::cerr << "ERROR: " << name  << " (" << err << ")" << std::endl;
        exit(EXIT_FAILURE);
    }
}

int main(void) {
    cl_int err;
    cl::vector< cl::Platform > platformList;
    cl::Platform::get(&platformList);
    checkErr(platformList.size()!=0 ? CL_SUCCESS : -1, "cl::Platform::get");
    std::cerr << "Platform number is: " << platformList.size() << std::endl;std::string platformVendor;
    platformList[0].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);
    std::cerr << "Platform is by: " << platformVendor << "\n";
    cl_context_properties cprops[3] = {CL_CONTEXT_PLATFORM, 
        (cl_context_properties)(platformList[0])(), 
        0};
    cl::Context context(
        CL_DEVICE_TYPE_CPU,
        cprops,
        NULL,
        NULL,
        &err);
    checkErr(err, "Context::Context()"); 
    char * outH = new char[hw.length()+1];
    cl::Buffer outCL(
        context,
        CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
        hw.length()+1,
        outH,
        &err);
    checkErr(err, "Buffer::Buffer()");
    cl::vector<cl::Device> devices;
    devices = context.getInfo<CL_CONTEXT_DEVICES>();
    checkErr(devices.size() > 0 ? CL_SUCCESS : -1, "devices.size() > 0");
    std::ifstream file("lesson1_kernels.cl");
    checkErr(file.is_open() ? CL_SUCCESS : -1, "lesson1_kernels.cl");
    std::string prog(std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>()));
    cl::Program::Sources source(1, std::make_pair(prog.c_str(), prog.length()+1));
    cl::Program program(context, source);
    err = program.build(devices,"");
    checkErr(err, "Program::build()");
    cl::Kernel kernel(program, "hello", &err);
    checkErr(err, "Kernel::Kernel()");err = kernel.setArg(0, outCL);
    checkErr(err, "Kernel::setArg()");
    std::cout << "I'm here!\n";
    cl::CommandQueue queue(context, devices[0], 0, &err);
    std::cout << "I'm here!\n";
    checkErr(err, "CommandQueue::CommandQueue()");cl::Event event;
    err = queue.enqueueNDRangeKernel(
        kernel,
        cl::NullRange,
        cl::NDRange(hw.length()+1),
        cl::NDRange(1, 1),
        NULL,
        &event);
    checkErr(err, "ComamndQueue::enqueueNDRangeKernel()");
    event.wait();
    err = queue.enqueueReadBuffer(
        outCL,
        CL_TRUE,
        0,
        hw.length()+1,
        outH);
    checkErr(err, "ComamndQueue::enqueueReadBuffer()");
    std::cout << outH;
    return EXIT_SUCCESS;
}

和这里的lesson1_kernels.cl:

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__constant char hw[] = "Hello World\n";
__kernel void hello(__global char * out) {
    size_t tid = get_global_id(0);
    out[tid] = hw[tid];
}

0 个答案:

没有答案