How do I know if a code is executed using GPU or CPU?
Solution 1
I think you need to get your concept about the separation of work between CPU and GPU straight. If you code something and compile it with a regular compiler that's not targeted at GPU execution, the code will always execute of the CPU.
All calls to OpenGL or DirectX functions in your main program are executed on the CPU, there's no "magical" translation layer. However some those calls make the GPU do something, like drawing triangles.
CUDA and OpenCL are languages aimed at data parallel execution architectures. The GPU is such an architecture. But CUDA and OpenCL code require some host program, which in turn will be executed on the CPU. The very same goes for programmable shaders (HLSL, GLSL).
So: The host part of the program (setting up the work environment, issuing rendering calls or GPU execution) will run on CPU. The code running on GPU is compiled in a separate compilation unit (i.e. GLSL shader code uploaded to OpenGL, OpenCL/CUDA code compiled with a OpenCL/CUDA compiler).
Solution 2
As datenwolf said, any code you write that is compiled via a standard compiler (gcc, etc.) will be run on the CPU. The programs which are run on the GPU are called shaders. The variable types in shaders are different than C/C++ programs, and the syntax is also stricter and more limited.
Older graphics applications operated with two types of shaders: vertex and fragment. The vertex shader operated on any vertex of geometry sent to the renderer. The fragment shader would receive output from the vertex shader (interpolated across the geometry faces) and would operate on each pixel, or fragment, of the geometry that would be drawn to the screen.
Modern graphics has introduced the idea of General Purpose GPU Programming. OpenGL's geometry shaders and Nvidia's CUDA can carry out general purpose programming on the GPU.
To summarize: Compiled shaders run on the GPU, and compiled C/C++ runs on the CPU.
Solution 3
Depends on which OS your'e using, and OpenGL/OpenCL/other, you can use a system profiler to give you this information. A system profiler, is a piece of software that tracks System-wide activity, and presents it in a readable form after the tracking is done. For example, for Windows, you can use Vtune, which monitors both CPU and GPU.
Hope this helps.
Adam Lee
Updated on June 04, 2022Comments
-
Adam Lee almost 2 years
Is there any simple way that I can know some codes are executed in GPU rather than CPU?
-
flankechen over 10 yearsAll the codes in glsl(.sh) is running in GPU? what if I have to do something more complicated like gaussian/median filtering? if I write: float a=0.5*0.7; how many times is this excuted? ones per pixel? how can I control somehing paralleling and somehing I just want it run onece? Sorry I am new to parallel execution programming.
-
Quazi Irfan about 9 yearsWhat about pre-shader era - what code was running on GPU then?
-
kevintodisco about 9 yearsPre-shader era was also pre-programmable-GPU era, so no code written by a user of a GPU would run on it.
-
datenwolf about 4 years@flankechen The specific expression
float a = 0.5*0.7
will be executed approximately zero times. Specifically the compiler will evaluate and fold the constant when ingesting the program, and will substitute every occurence ofa
with 0.35 until it encounters an assignment toa
that changes that.