编译一个基本的openCL程序错误:cl.hpp中未解析的includes

Compiling a basic openCL program error: unresolved includes inside cl.hpp

本文关键字:hpp cl includes 错误 openCL 一个 编译 程序      更新时间:2023-10-16

我是openCL中编码的新手,正在遵循这里的教程:https://www.marcusbannerman.co.uk/index.php/research/teaching-resources/77-supercomputing-on-graphics-cards-an-introduction-to-opencl-and-the-c-bindings.html我遇到了问题。

这是我在openCL中尝试的第一个代码,到目前为止遇到了问题。我已经成功地将目标库放在了我的IDE(netbeans)中,因为它们在编写代码时是一个选项,但我在cl.hpp文件中遇到了一个错误,说里面有未解析的include。

错误:

main.cpp:7:18: cl.hpp: No such file or directory
main.cpp: In function `int main()':
main.cpp:24: error: `cl' has not been declared
main.cpp:24: error: `Context' undeclared (first use this function)
main.cpp:24: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:24: error: expected `;' before "context"
main.cpp:27: error: `cl' has not been declared
main.cpp:27: error: `Sources' undeclared (first use this function)
main.cpp:27: error: expected `;' before "source"
main.cpp:28: error: `source' undeclared (first use this function)
main.cpp:31: error: `cl' has not been declared
main.cpp:31: error: `Program' undeclared (first use this function)
main.cpp:31: error: expected `;' before "program"
main.cpp:34: error: `cl' has not been declared
main.cpp:34: error: `Device' undeclared (first use this function)
main.cpp:34: error: template argument 1 is invalid
main.cpp:34: error: template argument 2 is invalid
main.cpp:35: error: invalid type in declaration before '=' token
main.cpp:35: error: `context' undeclared (first use this function)
main.cpp:35: error: `CL_CONTEXT_DEVICES' undeclared (first use this function)
main.cpp:35: error: expected primary-expression before ')' token
main.cpp:39: error: `program' undeclared (first use this function)
main.cpp:41: error: `cl' has not been declared
main.cpp:41: error: `Error' has not been declared
main.cpp:41: error: invalid catch parameter
main.cpp:44: error: `err' undeclared (first use this function)
main.cpp:46: error: `CL_PROGRAM_BUILD_LOG' undeclared (first use this function)
main.cpp:46: error: invalid types `int[int]' for array subscript
main.cpp:52: error: `cl' has not been declared
main.cpp:52: error: `Kernel' undeclared (first use this function)
main.cpp:52: error: expected `;' before "kernel"
main.cpp:55: error: `cl' has not been declared
main.cpp:55: error: `CommandQueue' undeclared (first use this function)
main.cpp:55: error: expected `;' before "cmdQ"
main.cpp:60: error: `cl_float' undeclared (first use this function)
main.cpp:60: error: template argument 1 is invalid
main.cpp:60: error: template argument 2 is invalid
main.cpp:60: error: invalid type in declaration before ';' token
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: At global scope:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h: In instantiation of `std::back_insert_iterator<int>':
main.cpp:61:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h:363: error: `int' is not a class, struct, or union type
main.cpp: In function `int main()':
main.cpp:64: error: `cl' has not been declared
main.cpp:64: error: `Buffer' undeclared (first use this function)
main.cpp:64: error: expected `;' before "inputBuffer"
main.cpp:67: error: `cl' has not been declared
main.cpp:67: error: expected `;' before "outputBuffer"
main.cpp:73: error: `kernel' undeclared (first use this function)
main.cpp:73: error: `inputBuffer' undeclared (first use this function)
main.cpp:74: error: `outputBuffer' undeclared (first use this function)
main.cpp:77: error: `cl' has not been declared
main.cpp:77: error: `KernelFunctor' undeclared (first use this function)
main.cpp:77: error: expected `;' before "func"
main.cpp:80: error: `func' undeclared (first use this function)
main.cpp:86: error: template argument 1 is invalid
main.cpp:86: error: template argument 2 is invalid
main.cpp:86: error: invalid type in declaration before '(' token
main.cpp:86: error: request for member `size' in `input', which is of non-class type `int'
main.cpp:89: error: `cmdQ' undeclared (first use this function)
main.cpp:89: error: request for member `size' in `input', which is of non-class type `int'
main.cpp:89: error: invalid types `int[int]' for array subscript
main.cpp:92: error: request for member `size' in `input', which is of non-class type `int'
main.cpp:93: error: invalid types `int[size_t]' for array subscript
main.cpp:93: error: invalid types `int[size_t]' for array subscript
main.cpp:93: error: invalid types `int[size_t]' for array subscript
main.cpp:94: error: invalid types `int[size_t]' for array subscript
main.cpp:94: error: invalid types `int[size_t]' for array subscript
main.cpp:99: error: `cl' has not been declared
main.cpp:99: error: `Error' has not been declared
main.cpp:99: error: invalid catch parameter
main.cpp:105:2: warning: no newline at end of file
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function `_OutputIterator std::generate_n(_OutputIterator, _Size, _Generator) [with _OutputIterator = std::back_insert_iterator<int>, _Size = unsigned int, _Generator = int (*)()]':
main.cpp:61:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:1006: error: no match for 'operator=' in '(&__first)->std::back_insert_iterator<_Container>::operator* [with _Container = int]() = __gen()'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_iterator.h:338: note: candidates are: std::back_insert_iterator<int>& std::back_insert_iterator<int>::operator=(const std::back_insert_iterator<int>&)
nbproject/Makefile-Debug.mk:66: recipe for target `build/Debug/Cygwin-Windows/main.o' failed
make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1
make[2]: Leaving directory `/cygdrive/c/Users/Thomas/SkyDrive/Documents/Research Documents/openCL/Testing/helloworld'
nbproject/Makefile-Debug.mk:59: recipe for target `.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/cygdrive/c/Users/Thomas/SkyDrive/Documents/Research Documents/openCL/Testing/helloworld'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)

如果你需要更多信息,请告诉我!如果能在窗户上正确设置它,我们将不胜感激!

代码:

#include <iostream>
#include <vector>
#include <algorithm>
//The OpenCL C++ bindings, with exceptions
#define __CL_ENABLE_EXCEPTIONS
#include <cl.hpp>
const size_t problemSize = 1024;
//The compute kernel we will run
const char * kernelSrc = 
  "__kernel void squareArray(__global float* input, "
  "                          __global float* output)"
  "{"
  "  output[get_global_id(0)] = input[get_global_id(0)] * input[get_global_id(0)];"
  "}";
int main()
{
  try {
    /***************     OpenCL Initialisation      ***************/
    //Open a context to run the openCL kernel in
    cl::Context context(CL_DEVICE_TYPE_GPU);
    //Gather all the kernel sources for the OpenCL program
    cl::Program::Sources source;
    source.push_back(std::make_pair(kernelSrc, strlen(kernelSrc)));
    //Make an OpenCL program
    cl::Program program(context, source);
    //Get all the available devices in the context
    std::vector<cl::Device> devices 
      = context.getInfo<CL_CONTEXT_DEVICES>();
    //Build the kernel sources for all devices in the context
    try {
      program.build(devices);
    }
    catch (cl::Error& err)
      {
    //Get the build log for the first device
    std::cerr << "Building failed, " << err.what() << "(" << err.err() << ")" 
          << "nRetrieving build logn" 
          << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0])
          << "n";
    return -1;
      }
    //Get the squareArray kernel to use in calculations
    cl::Kernel kernel(program, "squareArray");
    //Make a queue to put jobs on the first compute device
    cl::CommandQueue cmdQ(context, devices[0]);
    /***************   Preparing the data buffers   ***************/
    //Create a vector of random input values
    std::vector<cl_float> input;
    std::generate_n(std::back_inserter(input), problemSize, rand);
    //Start copying this data to the graphics card
    cl::Buffer inputBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_float) * input.size(), &input[0]);
    //Make a buffer to hold the output of the kernel
    cl::Buffer outputBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * input.size());

    /***************  Running on the graphics card  ***************/
    //Set the two arguments of the squareArray kernel
    kernel.setArg(0, inputBuffer);
    kernel.setArg(1, outputBuffer);
    //Get a Functor which will run the kernel on every input item in blocks of 64 threads
    cl::KernelFunctor func = kernel.bind(cmdQ, cl::NDRange(input.size()), cl::NDRange(64));
    //Run the kernel and wait for it to finish
    func().wait();

    /***************  Checking the outputted data   ***************/
    //Make a buffer to hold the outputted data
    std::vector<cl_float> output(input.size());
    //Request a blocking copy of the data from the graphics card
    cmdQ.enqueueReadBuffer(outputBuffer, true, 0, sizeof(cl_float) * input.size(), &output[0]);
    //Now check the answer
    for (size_t i(0); i < input.size(); ++i)
      if (input[i]*input[i] != output[i]) 
    std::cout << "Found a mistake " << input[i] << "^2 != " << output[i] << "n";
    std::cout << "Finished!n";
    return 0;
  }
  catch (cl::Error& err)
    {
      std::cerr << "An OpenCL error occured, " << err.what()
        << "nError num of " << err.err() << "n";
      return -1;
    }
}

这不是一个专门与OpenCL相关的问题。

您需要将OpenCL标头的目录提供给编译器。假设您使用的是GCC或Clang,请使用-I标志。这样的东西应该可以工作(请使用locate检查目录):

g++-I/usr/include/CL。。。