Question:
I have a program written inC++
language. My whole code contains only 3 files: a header file for my own class, a cpp file with code implementation for the class and a 3rd cpp file where I have the main()
method.I need to make very complicated calculations, and on a normal CPU my code takes about 3 months to complete the execution. That’s why I tried to run the program on my Nvidia GPU. The code was developed on Visual Studio IDE and I made an EXE file.
This is what I tried to do:
- Went to graphics settings -> Choose the relevant Exe file -> set the file for high performance with Nvidia card. This did Not work.
- I open Nvidia Control Panel -> apps settings -> again, picked the relevant Exe file -> select Nvidia GPU for high performance. Also, no luck here.
Both ways failed, my code is running on my Intel GPU and not on my Nvidia card.
How can I force the execution of the Exe file to run on the Nvidia card?
Is three a way via the command line (or any other way) to make my C++ code to be compiled and run on my Nvidia GPU?
Answer:
As far as I know, there is no easy and/or automatic way to compile general C++ code to be executed in a GPU. You have to use some specific API for GPU computing or implement the code in a way that some tool is able to automatically generate the code for a GPU.The C++ APIs that I know for GPU programming are:
CUDA: intended for NVIDIA GPUs only, relatively easy to use. Here you have a good introduction: https://developer.nvidia.com/blog/even-easier-introduction-cuda/
OpenCL: can be used for most of the GPUs in the market, but is not as easy to use as CUDA. An interesting feature of OpenCL is that the generated code can also run in CPU. An example here: https://www.eriksmistad.no/getting-started-with-opencl-and-gpu-computing/
SYCL: a relatively recent API for heterogeneous programming built on top of OpenCL. SYCL highly simplifies the GPU programming, I found this interesting tutorial which shows how easy to use SYCL is: https://tech.io/playgrounds/48226/introduction-to-sycl/introduction-to-sycl-2
Unfortunately, you will need to rewrite some parts of your code if you want to use one of these options. I believe that SYCL will be the easier choice and the one that will require less modifications in the C++ code (I really encourage you to take a look at the tutorial, SYCL is pretty easy to use). Also, it seems that Visual Studio already supports SYCL: https://developer.codeplay.com/products/computecpp/ce/guides/platform-support/targeting-windows
If you have better answer, please add a comment about this, thank you!
Leave a Review