使用 NVIDIA GPU 在 Cygwin 中链接 OpenCL 库时遇到问题

Trouble linking against OpenCL library/ies in Cygwin with an NVIDIA GPU

本文关键字:OpenCL 遇到 问题 链接 NVIDIA GPU Cygwin 使用      更新时间:2023-10-16

我正在尝试构建这个小型OpenCL项目:

https://bitbucket.org/Anteru/opencltutorial/src

在Windows上,在Cygwin中。这是一个玩具项目,包含一个.cpp文件和一个内核来演示 OpenCL 的使用。因此,我下载了该软件包并对其进行cmake。我收到错误消息:

CMake Error at /usr/share/cmake-3.14.5/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find OpenCL (missing: OPENCL_LIBRARY)
Call Stack (most recent call first):
/usr/share/cmake-3.14.5/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
cmake/FindOpenCL.cmake:35 (find_package_handle_standard_args)
CMakeLists.txt:6 (FIND_PACKAGE)

我对此毫不犹豫,并继续更改我的CMakeCache.txt,以便

OPENCL_LIBRARY:FILEPATH=OPENCL_LIBRARY-NOTFOUND

我做到了:

OPENCL_LIBRARY:FILEPATH=/cygdrive/c/Program Files/NVIDIA Corporation/OpenCL/OpenCL.dll

(这是NVIDIA的OpenCL DLL的合法路径(好的,现在CMake配置成功结束。main.cpp文件已编译,但链接失败!

/usr/bin/c++.exe   -Wl,--enable-auto-import CMakeFiles/clTut.dir/main.cpp.o  -o clTut.exe -Wl,--out-implib,libclTut.dll.a -Wl,--major-image-version,0,--minor-image-version,0 "/cygdrive/c/Program Files/NVIDIA Corporation/OpenCL/OpenCL.dll"
CMakeFiles/clTut.dir/main.cpp.o:main.cpp:(.text+0x610): undefined reference to `clGetPlatformInfo'
CMakeFiles/clTut.dir/main.cpp.o:main.cpp:(.text+0x610): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `clGetPlatformInfo'
CMakeFiles/clTut.dir/main.cpp.o:main.cpp:(.text+0x659): undefined reference to `clGetPlatformInfo'
CMakeFiles/clTut.dir/main.cpp.o:main.cpp:(.text+0x659): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `clGetPlatformInfo'

等等,几十个这样的错误。

为什么我会收到这些错误,如何修复它们?这是ELF/WinPE不兼容的结果,因为我正在使用Cygwin吗?

附加信息:

  • 我的机器既集成了英特尔显卡(630 - 咖啡湖(,又集成了 Quadro P1000。
  • 我已经将CUDA 10.2和AMD CodeXL安装(不管为什么(安装到它们的默认位置。
  • 寻找一个OpenCL DLL文件,我发现:
    $ locate OpenCL.dll
    /cygdrive/c/Program Files (x86)/CodeXL/spies/OpenCL.dll
    /cygdrive/c/Program Files (x86)/CodeXL/spies64/OpenCL.dll
    /cygdrive/c/Program Files/NVIDIA Corporation/OpenCL/OpenCL.dll
    /cygdrive/c/Windows/LastGood/system32/OpenCL.dll
    /cygdrive/c/Windows/LastGood/SysWow64/OpenCL.dll
    /cygdrive/c/Windows/System32/OpenCL.dll
    /cygdrive/c/Windows/SysWOW64/OpenCL.dll
    

通常Cygwin程序是Unix类的,不直接调用Windows DLL,它们使用 一个不同的帕拉格迪姆。 例如,Cygwin cygwin1.dll提供了自己的C库,与 MS C库,这会导致共享数据出现问题,特别是在64位版本中,long的大小不同。看 https://cygwin.com/cygwin-ug-net/programming.html#gcc-64

您的教程示例可以在Cygwin纯环境中构建,并使用适当的OpenCL库

$ cygcheck -cd | grep -i opencl
libOpenCL-devel                         2.2.12-1
libOpenCL1                              2.2.12-1
$ unzip Anteru-opencltutorial-cb1df4439f83.zip
Archive:  Anteru-opencltutorial-cb1df4439f83.zip
inflating: Anteru-opencltutorial-cb1df4439f83/.hg_archival.txt
...
inflating: Anteru-opencltutorial-cb1df4439f83/main.cpp
inflating: Anteru-opencltutorial-cb1df4439f83/test.ppm
$ cd Anteru-opencltutorial-cb1df4439f83
/pub/tmp/Anteru-opencltutorial-cb1df4439f83
$ cmake .
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
...
-- Found OpenCL: /usr/lib/libOpenCL.dll.a
-- Configuring done
-- Generating done
-- Build files have been written to: /pub/tmp/Anteru-opencltutorial-cb1df4439f83
$ make
Scanning dependencies of target clTut
[ 50%] Building CXX object CMakeFiles/clTut.dir/main.cpp.o
/pub/tmp/Anteru-opencltutorial-cb1df4439f83/main.cpp: In function ‘int main()’:
/pub/tmp/Anteru-opencltutorial-cb1df4439f83/main.cpp:249:9: warning: ‘_cl_mem* clCreateImage2D(cl_context, cl_mem_flags, const cl_image_format*, size_t, size_t, size_t, void*, cl_int*)’ is deprecated [-Wdeprecated-declarations]
&error);
^
In file included from /pub/tmp/Anteru-opencltutorial-cb1df4439f83/main.cpp:10:0:
~~~~~~~^~~~~~
...
[100%] Linking CXX executable clTut.exe
[100%] Built target clTut

构建的程序似乎运行良好

$ ./clTut.exe
Found 1 platform(s)
(1) : Portable Computing Language
Found 1 device(s)
(1) : pthread-Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Context created