OpenCL C ++ Hello World程序 - " cl.hpp"

时间:2016-06-05 22:23:58

标签: c++ opencl gpgpu

我正在尝试运行一个简单的Hello World程序,该程序应打印" Hello World"使用GPU(在我的情况下为Nvidia GTX 960)。我正在尝试使用Windows 10,64位上的Code :: Blocks编译此程序。显然没有更新的驱动程序版本,所以我坚持使用OpenCL 1.2。

到目前为止我做了什么:

  1. 安装了NVIDIA CUDA SDK

  2. 设置Code :: Blocks编译器以搜索include目录,并将OpenCL.lib添加到链接器设置。

  3. 从这里复制粘贴Hello World程序:

  4. http://dhruba.name/2012/10/06/opencl-cookbook-hello-world-using-cpp-host-binding/

    1. 创建了" opencl_hello_world.cl"文件,如链接中所述。
    2. 我是OpenCL的菜鸟,只想测试它是否有效。每一个" Hello-World"到目前为止,我尝试过的代码失败并出现了不同的错误。

      #define __CL_ENABLE_EXCEPTIONS
      
      #include <fstream>
      #include <iostream>
      #include <iterator>
      #include <CL/cl.hpp>
      #include <CL/opencl.h>
      
      using namespace std;
      
      int main () {
      
          vector<cl::Platform> platforms;
          vector<cl::Device> devices;
          vector<cl::Kernel> kernels;
      
          try {
      
              // create platform
              cl::Platform::get(&platforms);
              platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
      
              // create context
              cl::Context context(devices);
      
              // create command queue
              cl::CommandQueue queue(context, devices[0]);
      
              // load opencl source
              ifstream cl_file("opencl_hello_world.cl");
              string cl_string(istreambuf_iterator<char>(cl_file), (istreambuf_iterator<char>()));
              cl::Program::Sources source(1, make_pair(cl_string.c_str(),
                  cl_string.length() + 1));
      
              // create program
              cl::Program program(context, source);
      
              // compile opencl source
              program.build(devices);
      
              // load named kernel from opencl source
              cl::Kernel kernel(program, "hello_world");
      
              // create a message to send to kernel
              char* message = "Hello World!";
              int messageSize = 12;
      
              // allocate device buffer to hold message
              cl::Buffer buffer(CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                  sizeof(char) * messageSize, message);
      
              // set message as kernel argument
              kernel.setArg(0, buffer);
              kernel.setArg(1, sizeof(int), &messageSize);
      
              // execute kernel
              queue.enqueueTask(kernel);
      
              // wait for completion
              queue.finish();
      
              cout << endl;
      
          } catch (cl::Error e) {
              cout << endl << e.what() << " : " << e.err() << endl;
          }
      
          return 0;
      
      }
      

      代码失败,输出如下:

      ||=== Build: Debug in OpenCLTest (compiler: GNU GCC Compiler) ===|
      obj\Debug\main.o||In function `getPlatformVersion':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1735|undefined reference to `clGetPlatformInfo@20'|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1737|undefined reference to `clGetPlatformInfo@20'|
      obj\Debug\main.o||In function `getDevicePlatformVersion':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1744|undefined reference to `clGetDeviceInfo@20'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP13_cl_device_idE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1619|undefined reference to `clReleaseDevice@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP11_cl_contextE6retainES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1652|undefined reference to `clRetainContext@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP11_cl_contextE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1654|undefined reference to `clReleaseContext@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP17_cl_command_queueE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1663|undefined reference to `clReleaseCommandQueue@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP7_cl_memE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1672|undefined reference to `clReleaseMemObject@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP11_cl_programE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1690|undefined reference to `clReleaseProgram@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP10_cl_kernelE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1699|undefined reference to `clReleaseKernel@4'|
      obj\Debug\main.o||In function `ZN2cl6detail16ReferenceHandlerIP9_cl_eventE7releaseES3_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|1708|undefined reference to `clReleaseEvent@4'|
      obj\Debug\main.o||In function `ZNK2cl8Platform10getDevicesEyPSt6vectorINS_6DeviceESaIS2_EE':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2210|undefined reference to `clGetDeviceIDs@24'|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2216|undefined reference to `clGetDeviceIDs@24'|
      obj\Debug\main.o||In function `ZN2cl8Platform3getEPSt6vectorIS0_SaIS0_EE':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2315|undefined reference to `clGetPlatformIDs@12'|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2322|undefined reference to `clGetPlatformIDs@12'|
      obj\Debug\main.o||In function `ZN2cl7ContextC1ERKSt6vectorINS_6DeviceESaIS2_EEPiPFvPKcPKvjPvESC_S7_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2475|undefined reference to `clCreateContext@24'|
      obj\Debug\main.o||In function `ZN2cl7ContextC1EyPiPFvPKcPKvjPvES6_S1_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|2586|undefined reference to `clCreateContextFromType@24'|
      obj\Debug\main.o||In function `ZN2cl6BufferC1EyjPvPi':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|3158|undefined reference to `clCreateBuffer@24'|
      obj\Debug\main.o||In function `ZN2cl6Kernel6setArgEjjPKv':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|4955|undefined reference to `clSetKernelArg@16'|
      obj\Debug\main.o||In function `ZN2cl7ProgramC1ERKNS_7ContextERKSt6vectorISt4pairIPKcjESaIS8_EEPi':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|5054|undefined reference to `clCreateProgramWithSource@20'|
      obj\Debug\main.o||In function `ZNK2cl7Program5buildERKSt6vectorINS_6DeviceESaIS2_EEPKcPFvP11_cl_programPvESB_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|5228|undefined reference to `clBuildProgram@24'|
      obj\Debug\main.o||In function `ZN2cl6KernelC1ERKNS_7ProgramEPKcPi':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|5437|undefined reference to `clCreateKernel@12'|
      obj\Debug\main.o||In function `ZN2cl12CommandQueueC1ERKNS_7ContextERKNS_6DeviceEyPi':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|5526|undefined reference to `clCreateCommandQueue@20'|
      obj\Debug\main.o||In function `ZNK2cl12CommandQueue11enqueueTaskERKNS_6KernelEPKSt6vectorINS_5EventESaIS5_EEPS5_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|6334|undefined reference to `clEnqueueTask@20'|
      obj\Debug\main.o||In function `ZNK2cl12CommandQueue6finishEv':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|6551|undefined reference to `clFinish@4'|
      obj\Debug\main.o||In function `ZN2cl6Kernel6setArgINS_6BufferEEEijRKT_':|
      C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include\CL\cl.hpp|4948|undefined reference to `clSetKernelArg@16'|
      ||error: ld returned 1 exit status|
      ||=== Build failed: 27 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
      

      TL; DR,据我所见: 所有错误都指向了&#34; cl.hpp&#34;中的不同行。 file(未定义引用X,其中X是OpenCL函数)。

      问题是这些错误的根源指向了&#34; cl.hpp&#34;文件,我不负责修复。如果找到库时出错,我可以理解它,但是&#34; cl.hpp&#34;似乎有问题。归档。

      我试过包括&#34; CL / cl.h&#34;相反,这会导致更多错误,例如&#34;向量未在此范围内声明&#34;,&#34; cl尚未声明&#34;,&#34;平台未在此范围内声明&#34; ;等等。

      我的问题是:

      • 我的代码出了什么问题?

      • cl.hpp文件中是否存在已知错误?这是其中之一吗?

      • 之间有什么区别?
      • 如何升级到最新版本的OpenCL?目前,每个驱动程序由于某种原因只有OpenCL 1.2。

      • OpenCL中是否有关于版本更改的信息?似乎我尝试的每个教程要么已过时,要么仅适用于1.0版本,只适用于C等? 2016年有没有关于C ++,OpenCL 1.2的教程?

0 个答案:

没有答案