How does a CPU know which piece of code should be executed on GPU (i.e how to recognize whether a code is shader or not) and how is it been sent to GPU VRAM?Is it via DMA?
-
1$\begingroup$ Same way a computer knows how to do anything. There is a API that has calls that the graphics card driver handles. $\endgroup$– joojaaCommented Feb 15, 2020 at 19:17
-
2$\begingroup$ Most, if not all, approaches to GPU programming require the developer to manually identify code that will be executed on the GPU. The code for the GPU is compiled separately from CPU code, so it is clear which binary code is for which processor type. Moving the GPU code to VRAM is often done with DMA, but may be moved to GPU accessible memory using other techniques such as CPU writes across PCIe, or direct CPU memory access by the GPU. $\endgroup$– user2500Commented Feb 15, 2020 at 20:38
-
$\begingroup$ Thanks for clear explanation.I know that something like a driver lives in kernel space and some library like CUDA needs to use driver API to communicate with GPU.How can a userspace code use the driver API at runtime ?Is it like a system call? $\endgroup$– Parsa MousaviCommented Feb 16, 2020 at 7:02
-
$\begingroup$ Exactly! The userspace part of the driver communicates with the kernel part of the driver via system calls. As much as possible is put in userspace to protect the integrity of the system. The kernel writes to the hardware managing it as a shared resource. Command buffer construction and the GPU compiler will usually reside in userspace. $\endgroup$– user2500Commented Feb 16, 2020 at 11:16
-
$\begingroup$ Is command buffer like an event queue?And which kind of syscalls do it need? $\endgroup$– Parsa MousaviCommented Feb 16, 2020 at 11:38
1 Answer
Most, if not all, approaches to GPU programming require the developer to manually identify code that will be executed on the GPU. The code for the GPU is compiled separately from CPU code, so it is clear which binary code is for which processor type. Moving the GPU code to VRAM is often done with DMA, but may be moved to GPU accessible memory using other techniques such as CPU writes across PCIe, or direct CPU memory access by the GPU.
The GPU compiler often resides in userspace, and a system call will hand binary GPU code to the kernel, which can then make the code available to the GPU.