5

I want to write application in NVIDIA OpenCL in Visual Studio 2017 but don't know how to create project for this purpose.

I have GPU from NVIDIA (GeForce 940M) and Intel (HD Graphics 5500) and already managed to open and run Intel example programs for OpenCL but they have almost one thousand lines of code, so I decided to try NVIDIA OpenCL but don't know how. On some forums they say that I should download CUDA toolkit and it install OpenCL, others say that I should download driver that supports OpenCL but I don't know which driver will be proper. I have already installed CUDA and driver from https://www.nvidia.pl/Download/index.aspx?lang=pl but still I have not possibility to create NVIDIA project in OpenCL in Visual Studio.

Zekhire
  • 115
  • 1
  • 2
  • 8
  • Downloading the CUDA kit is probably going to be the least headache inducing. I believe it still comes with the headers for OpenCL as well, though I could be mistaken. – Dortimer Jul 02 '19 at 18:28
  • I found headers in NVIDIA GPU COMPUTING\include\CL catalog. What should I do with them to create OpenCL project in Visual Studio? I suppose that #include "CL\cl.h" is not enough. – Zekhire Jul 02 '19 at 20:38
  • I think with CUDA it's better to not go through OpenCL directly, and use what specifics they provide with their toolkit. Of course, it'll only work with Nvidia GPUs however. If you want to support Intel GPUs or AMD, you should focus on just using OpenCL. If you want just OpenCL: https://medium.com/@pratikone/opencl-on-visual-studio-configuration-tutorial-for-the-confused-3ec1c2b5f0ca Otherwise, follow CUDA's documentation – Dortimer Jul 02 '19 at 20:58
  • I followed your tutorial and get error: Cannot open file ,,OpenCL.lib''. Also I am not sure about step 3 from tutorial (3. Project configurations with New Solution Platform). I don't know what option I should choose. Tutorial say "choose x64 as new platform and copy settings option as Win32" but on image there is ARM. – Zekhire Jul 03 '19 at 17:17

1 Answers1

10

The OpenCL Runtime is already included in the Nvidia graphics drivers. You only need the OpenCL C++ header files, the OpenCL.lib file and on Linux also the libOpenCL.so file. These come with the CUDA toolkit, but there is no need to install it only to get the 9 necessary files.

Here are the OpenCL C++ header files and the lib file from CUDA toolkit 10.1: https://github.com/ProjectPhysX/OpenCL-Wrapper/tree/master/src/OpenCL

Download the OpenCL folder and copy it into your project source folder. Then in your Visual Studio Project, go to "Project Properties -> C/C++ -> General -> Additional Include Directories" and add C:\path\to\your\project\src\OpenCL\include. Then, in "Project Properties -> Linker -> All Options -> Additional Dependencies" add OpenCL.lib; and in "Project Properties -> Linker -> All Options -> Additional Library Directories" add C:\path\to\your\project\src\OpenCL\lib.

Finally, in your .cpp source file, include the headers with #include <CL/cl.hpp>.

This also works for AMD/Intel GPUs and CPUs. It also works on Linux if you compile with:

g++ *.cpp -o Test.exe -I./OpenCL/include -L./OpenCL/lib -lOpenCL

For an easy start with OpenCL, I created a wrapper that vastly simplifies the OpenCL C++ bindings and eliminates the entire code overhead that comes with it. This includes the OpenCL headers and all Visual Studio project settings; no additional setup required: https://github.com/ProjectPhysX/OpenCL-Wrapper

ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34
  • 1
    Ha! I found this while in the middle of a massive CUDA install that I can't cancel! And this is all I wanted. Is there a nice guy that hosts these required files somewhere so that we can get the latest version? – Patrick Nov 29 '20 at 01:39
  • 1
    Nvidia didn't change the OpenCL header files in many years. Their GPUs only support OpenCL 1.2 anyways, so you're fine with the files from 10.1. To check if they changed something, you can download CUDA toolkit and open the installer, so the install files get extracted to some specified file location. Look up the folder, somewhere in there are the OpenCL files. Once you copied them somewhere else, cancel installation, and then the extracted folder will be deleted again. – ProjectPhysX Nov 29 '20 at 08:46
  • It was at the stage that it didn't let you cancel – Patrick Nov 30 '20 at 11:17