How do I know if a code is executed using GPU or CPU?
Asked Answered
S

3

6

Is there any simple way that I can know some codes are executed in GPU rather than CPU?

Serin answered 5/2, 2012 at 13:29 Comment(2)
Well it's not quite deterministic but for Windows the "Process Explorer" has a GPU usage graph. So either use that or find out which API it uses.Mcdonnell
Can you be more specific about what kind of "codes" you are trying to run on the GPU? You list 3 different APIs, all of which run "codes" on the GPU, using 3 completely different ways of doing so.Ornas
C
15

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).

Councillor answered 5/2, 2012 at 18:4 Comment(2)
All 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.Neodymium
@Neodymium The specific expressionfloat 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 of a with 0.35 until it encounters an assignment to a that changes that.Councillor
W
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.

Whangee answered 6/2, 2012 at 7:26 Comment(2)
What about pre-shader era - what code was running on GPU then?Plankton
Pre-shader era was also pre-programmable-GPU era, so no code written by a user of a GPU would run on it.Whangee
C
2

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.

Coop answered 30/9, 2015 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.