1

I have a program written in C++ 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:

  1. Went to graphics settings -> Choose the relevant Exe file -> set the file for high performance with Nvidia card. This did Not work.
  2. 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?

16
  • 1
    That's a strange desire. What makes you think that you can compile a program for Intel CPU and then transfer it to Nvidia CPU, where it magically gains high performance? That is not how things work.
    – j6t
    Commented Mar 4, 2022 at 12:29
  • 1
    Does your code supports dedicated GPU? If yes, which library you are using (vulkan, direct3d, etc). Try CUDA Commented Mar 4, 2022 at 12:31
  • 1
    If it's just a simple C++ program not using any of the APIs listed by PeterT, then it runs on a CPU, not GPU. And you can't easily make it run on a GPU, without using one of the listed APIs. Commented Mar 6, 2022 at 14:40
  • 1
    @TalAngel this might give you a start. eriksmistad.no/getting-started-with-opencl-and-gpu-computing Commented Mar 7, 2022 at 20:20
  • 3
    Your problem is conceptual, exe's always run on top of an operating system, which is using a CPU. GPU functions are supported by way of an API like CUDA or OpenCL or Vulkan. You can also use GPGPU frameworks like C++AMP and boost.compute, or use optmized versions of Eigen and the like. But the gist of it is that you need to rewrite your entire program to be massively multi-threaded.
    – StarShine
    Commented Mar 8, 2022 at 13:40

2 Answers 2

2
+200

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:

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

1

NVIDIA offers a high performance C++ compiler that will generate code moving some of your workload onto the GPU. This works best using C++17 parallel algorithm.

Likely - you have to re-write some your existing C++ code and then compile it using NVIDIA's C++ compiler.

Your code/algorithm must support some form of parallelism.

Here are some links with information about NVIDIA's HPC C++ compiler.

https://developer.nvidia.com/hpc-compilers

https://developer.nvidia.com/hpc-sdk

https://developer.nvidia.com/nvidia-hpc-sdk-downloads

Not the answer you're looking for? Browse other questions tagged or ask your own question.