What does the nVIDIA CUDA driver do exactly? - cuda

What does Nvidia CUDA driver do exactly? from the perspective of using CUDA.
The driver passes the kernel code, with the execution configuration (#threads, #blocks)...
and what else?
I saw some post that the driver should be aware of the number of available SMs.
But isn't that unnecessary ? Once the kernel is passed to GPU, the GPU scheduler just needs to spread the work to available SMs...

The GPU isn't a fully autonomous device, it requires a lot of help from the host driver to do even
the simplest things. As I understand it the driver contains at least:
JIT compiler/optimizer (PTX assembly code can be compiled by the driver at runtime, the driver will also recompile code to match the execution architecture of the device if required and possible)
Device memory management
Host memory management (DMA transfer buffers, pinned and mapped host memory, unified addressing model)
Context and runtime support (so code/heap/stack/printf buffer memory management), dynamic symbol management, streams, etc
Kernel "grid level" scheduler (includes managing multiple simultaneous kernels on architectures that support it)
Compute mode management
Display driver interop (for DirectX and OpenGL resource sharing)
That probably represents the bare minimum that is required to get some userland device code onto a GPU and running via the host side APIs.

Related

Running CUDA on a virtual machine without a physical NVidia GPU card

Is it possible to run a CUDA program on a virtual machine without having a physical NVidia GPU card on the host machine?
PCIe passthrough is only viable if the host machine has an NVidia card and that's not available.
One possible option to run CUDA programs without a GPU installed is to use an emulator/simulator (ex: http://gpgpu-sim.org/ ) but these simulators are usually limited.
I would appreciate a clear answer on that matter.
Thanks!
You can't run any modern version of CUDA (e.g. 6.0 or newer) unless you have actual GPU hardware available on the machine or virtual machine.
The various simulators and other methods all depend on very old versions of CUDA.

When does cuda kernel go onto device's global Memory

I am developing a code in CUDA, but I am wondering at which time the kernel developed goes onto device's global Memory?
Is it at compilation or during execution ?
If you compile a code using nvcc, that has no effect on any GPUs installed in the machine, and in fact may be done on a machine with no GPUs.
Any kernels to be loaded by a program will be loaded onto the GPU after that program begins execution.

Can I compile a cuda program without having a cuda device

Is it possible to compile a CUDA program without having a CUDA capable device on the same node, using only NVIDIA CUDA Toolkit...?
The answer to your question is YES.
The nvcc compiler driver is not related to the physical presence of a device, so you can compile CUDA codes even without a CUDA capable GPU. Be warned however that, as remarked by Robert Crovella, the CUDA driver library libcuda.so (cuda.lib for Windows) comes with the NVIDIA driver and not with the CUDA toolkit installer. This means that codes requiring driver APIs (whose entry points are prefixed with cu, see Appendix H of the CUDA C Programming Guide) will need a forced installation of a "recent" driver without the presence of an NVIDIA GPU, running the driver installer separately with the --help command line switch.
Following the same rationale, you can compile CUDA codes for an architecture when your node hosts a GPU of a different architecture. For example, you can compile a code for a GeForce GT 540M (compute capability 2.1) on a machine hosting a GT 210 (compute capability 1.2).
Of course, in both the cases (no GPU or GPU with different architecture), you will not be able to successfully run the code.
For the early versions of CUDA, it was possible to compile the code under an emulation modality and run the compiled code on a CPU, but device emulation is since some time deprecated. If you don't have a CUDA capable device, but want to run CUDA codes you can try using gpuocelot (but I don't have any experience with that).

How cudaMalloc is internally implemented under Ubuntu Linux?

I would like to know how the allocation of a memory space in CUDA is implemented under Ubuntu Linux. In other words, how cudaMalloc() works internally under Ubuntu Linux? What are the system calls used for this function?
CUDA is proprietary. It's likely that CUDA driver implementation is the same or similar to OpenCL.
But while OpenCL specification is open the implementation is not necessary and NVIDIA OpenCL driver isn’t open .
It's possible that the implementation is as simple as the driver submitting a malloc command completely handled on the hardware side with the kernel driver communicating with the system to achieve unified virtual addressing and to determine what memory resides in VRAM. Probably the technical part at the software side is to avoid the allocation or defer it.
Looking into pocl can give you some idea how things can look like. NVIDIA implementation can be very different though.

CPU as host and intel HD 4000 as device 1 and discrete gpu as device 2 in opencl

Is it possible to use the Intel HD 4000 integrated graphics and the discrete GPU at the same time with OpenCL (or CUDA) as devices and the CPU as the host? I want some code running on the integrated graphics while other code is running on my GPU at the same time.
It is possible to run OpenCL on some of the Ivy Bridge integrated GPUs using Intel's most recent Windows OpenCL SDK (available here). The Intel ICD will enumerate both the host CPU and integrated GPU as OpenCL capable devices. You will then need to use the discrete GPU vendor's SDK and ICD to identify and enumerate that as an OpenCL device. Once that is done, contexts can be established on the GPUs and the standard OpenCL multi-gpu design patttern used to get code running on both devices. Whether this actually works in practice will depend on the support and stability of both vendor's SDKs.
I have an Ivy-Bridge + discrete GPU system and have confirmed that the Intel ICD enumerates the HD4000 as a compute device. I have not yet tried OpenCL simultaneously on both devices.
NVIDIA does not support CUDA on anything other than their own GPUs.