• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hardware Interaction?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does a language such as C, or any other programming language for that matter, interact with hardware such as a GPU or CPU? I know it needs to get compiled into machine code (correct?) and then the CPU will read off of the machine code and perform any operations that programmer wants. I'm just uncertain on how to interact with the GPU. I've heard of CUDA and OpenCL but I don't know how those programs interact with the GPU. Does Assembly come into this at all or no?

Does it go this way: C -> Machine Code -> GPU Driver -> GPU? If so, how does C interact with the driver? Would the driver need to be programmed in a certain language. I'm lost, haha. Thanks in advance!
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't had any chance yet to do research on GPU programming, but it will fall into the same conceptual model of the overall computing system...

What I mean by this is that the GPU is just another component within the system that can be addressed and controlled by the CPU via the system's buses such the control, address and data bus...

One must remember that a programming language just provides layers of abstraction away from the underlying machine's instruction set to perform the addressing, controlling and data transfers between components in a high-level fashion which makes it easier for us humans to comprehend...

The only way for you to actually perform operations on a component (GPU in this case) is to acquire and learn the low-level instruction set or to use the API provided which can be used with a high-level language...
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rico's explained the basic principle well.

To add some details:

how does C interact with the driver


C application calls functions in libraries like DirectX, OpenGL, CUDA and OpenCL, in the exact same way it calls functions like printf().
These library functions then use OS specific mechanisms - such as ioctl (in linux), DeviceIOControl (in Windows), DMA (Direct Memory Addressing), etc - to send commands and data to GPU driver.
If you want your application to talk directly to GPU driver, *you* will have to call these mechanisms just like one of those library does.

Does Assembly come into this at all


Assembly language is a slightly more readable version of machine code. Machine code like "8B 47 04" (which is what an executable contains and the CPU understands) is
converted into a more readable assembler line like "mov eax,[edi+4]". Ultimately, assembly language too compiles down to the same CPU instruction set as C and other higher languages.
If you so wish, you can code your application in assembly instead of C; it's just another language.

Does driver need to be programmed in a certain language?


Practically speaking, drivers do a lot of memory and low level hardware manipulation. So they're usually written in languages that
make such manipulation safe without side effects, such as C/C++/assembler (and possibly even new languages like D and Rust, but I'm not too sure about them).
Higher level or managed languages result in a lot of side effects on CPU usage, CPU locking, memory usage, etc that may not be a good idea at the driver level.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each processor (CPU or GPU) has its own machine instruction set. Code that runs on an Intel x86 CPU will be compiled to Intel x86 machine code, which the CPU can execute directly. Code in for example a CUDA program will be compiled to machine code that your NVIDIA GPU can understand. That machine code will be different from the machine code that an Intel x86 CPU can execute.

Your graphics driver will have functions to copy the GPU-specific machine code from the main RAM of your computer to the video RAM on your video card, so that the GPU can read and execute it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic