OpenCL 1.2 C ++ Wrapper - 对clReleaseDevice的未定义引用

时间:2013-04-06 20:36:49

标签: opencl nvidia

我正在尝试将OpenCL C ++包装器API用于以下程序:

#define __CL_ENABLE_EXCEPTIONS


#include <CL/cl.hpp>
#include <cstdio>
#include <cstdlib>
#include <iostream>

 const char helloStr []  = "__kernel void "
                          "hello(void) "
                          "{ "
                          "  "
                          "} ";

 int
 main(void)
 {
    cl_int err = CL_SUCCESS;
    try {

      std::vector<cl::Platform> platforms;
      cl::Platform::get(&platforms);
      if (platforms.size() == 0) {
          std::cout << "Platform size 0\n";
          return -1;
      }

      cl_context_properties properties[] =
         { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
      cl::Context context(CL_DEVICE_TYPE_CPU, properties);

      std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();

      cl::Program::Sources source(1,
          std::make_pair(helloStr,strlen(helloStr)));
      cl::Program program_ = cl::Program(context, source);
      program_.build(devices);

      cl::Kernel kernel(program_, "hello", &err);

      cl::Event event;
      cl::CommandQueue queue(context, devices[0], 0, &err);
      queue.enqueueNDRangeKernel(
          kernel,
          cl::NullRange,
          cl::NDRange(4,4),
          cl::NullRange,
          NULL,
          &event);

      event.wait();
    }
    catch (cl::Error err) {
       std::cerr
          << "ERROR: "
          << err.what()
          << "("
          << err.err()
          << ")"
          << std::endl;
    }

   return EXIT_SUCCESS;
 }

我使用该博客文章中的相同内核文件,无论如何这不是问题,因为我无法通过编译。

我使用以下命令编译程序:

g++ example.cpp -o example -l OpenCL

我收到以下错误消息:

/tmp/ccbUf7dB.o: In function `cl::detail::ReferenceHandler<_cl_device_id*>::release(_cl_device_id*)':
example.cpp:(.text._ZN2cl6detail16ReferenceHandlerIP13_cl_device_idE7releaseES3_[_ZN2cl6detail16ReferenceHandlerIP13_cl_device_idE7releaseES3_]+0x14): undefined reference to `clReleaseDevice'
collect2: error: ld returned 1 exit status

我已经阅读了有关clReleaseDevice不能用于传统设备的内容(例如参见this question),但我的显卡非常新(NVidia GTX 660 Ti,支持OpenCL 1.2)。我在哪里可以去那里?

我在Ubuntu 13.04 x64上使用nvidia-opencl-dev和从ubuntu存储库安装的opencl-headers运行它。

2 个答案:

答案 0 :(得分:10)

根本原因

问题是您链接的OpenCL库不支持OpenCL 1.2。一般来说,在支持您要使用的版本的OpenCL实现可用于特定平台之前,在链接到随其提供的OpenCL共享库时,您将遇到此问题。有两种解决方案:

  • 从Khronos下载与您选择的硬件提供的OpenCL版本匹配的cl.hpp版本,并继续使用设备制造商提供的库。
  • 链接实现最新OpenCL标准的OpenCL共享库,但编写多个代码路径 - 每个OpenCL版本一个,确保每个代码路径仅使用该版本支持的OpenCL函数。 这条路线更难,我不知道如何使用C ++包装器。如果您尝试在不支持OpenCL 1.2的平台上调用OpenCL 1.2函数,您将获得段错误,这就是需要不同代码路径的原因。

Nvidia具体

Nvidia在提供OpenCL 1.2支持方面进展缓慢。因此,他们的OpenCL库没有提供链接器正在寻找的OpenCL 1.2函数,从而导致错误。

2015年5月底,Nvidia发布了支持OpenCL 1.2的驱动程序,请参阅下面的Z Boson的评论。更新驱动程序应解决链接器错误。 GeForce GTX 6xx及更高版本的卡(前几代品牌除外)支持OpenCL 1.2。您可以查看Khronos OpenCL网站上的conformant products list以确保。列出了GTX 660 Ti,所以你很幸运。

答案 1 :(得分:0)

呀。我从来没有在Nvidia设备上看过OpenCL 1.2。在您的系统上编译,并查看&#34; OpenCL C版本&#34;:

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

int main() {

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

    // Loop over the number of platforms
    for ( size_t i = 0; i < platforms.size(); ++i ) {

        // Display the platform information
        std::cout << "Platform " << i+1 << ": "
                            << platforms[i].getInfo<CL_PLATFORM_NAME>()
        << "\n----------------------------------------------"
        << "\nVendor    : " << platforms[i].getInfo<CL_PLATFORM_VENDOR>()
        << "\nVersion   : " << platforms[i].getInfo<CL_PLATFORM_VERSION>();

        // Get the devices on the current platform
        std::vector <cl::Device> devices;
        platforms[i].getDevices( CL_DEVICE_TYPE_ALL , & devices);

        // Loop over the devices
        std::cout << "\n----------------------------------------------\n";
        for ( size_t j = 0; j < devices.size(); ++j ) {

            // Display the device information
            std::cout
            << "\n   Device " << j+1 << ": "
            <<          devices[j].getInfo< CL_DEVICE_NAME >()
            << "\n\t Device Version     : "
            <<          devices[j].getInfo< CL_DEVICE_VERSION >()
            << "\n\t OpenCL C Version   : "
            <<          devices[j].getInfo< CL_DEVICE_OPENCL_C_VERSION >()
            << "\n\t Compute Units      : "
            <<          devices[j].getInfo< CL_DEVICE_MAX_COMPUTE_UNITS >()
            << "\n\t Max Work Group Size: "
            <<          devices[j].getInfo< CL_DEVICE_MAX_WORK_GROUP_SIZE >()
            << "\n\t Clock Frequency    : "
            <<          devices[j].getInfo< CL_DEVICE_MAX_CLOCK_FREQUENCY >()
            << "\n\t Local Memory Size  : "
            <<          devices[j].getInfo< CL_DEVICE_LOCAL_MEM_SIZE >()
            << "\n\t Global Memory Size : "
            <<          devices[j].getInfo< CL_DEVICE_GLOBAL_MEM_SIZE >();

            // Check if the device supports double precision
            std::string str = devices[j].getInfo<CL_DEVICE_EXTENSIONS>();
            size_t found = str.find("cl_khr_fp64");
            std::cout << "\n\t Double Precision   : ";
            if ( found != std::string::npos ){ std::cout << "yes\n"; }
            else {                             std::cout <<  "no\n"; }
        }
        std::cout << "\n----------------------------------------------\n";
    }
//  std::cin.ignore();
    return 0;
}