CUDA and fork()

While debugging a program, I discovered that if you fork() a process after initializing the GPU and filling device memory with data, both the parent and the child process can call global functions which access that data. In this instance, it was a bug in my code that allowed this to happen, but I might find this useful in another project.

Is fork()-safety for CUDA intentional or accidental? Will bad stuff happen if either parent or child attempts to allocate new memory on the GPU after the fork()?

That sounds like a bug to me. I don’t think that the intended behavior is for GPU state to be shareable across fork() calls, that’d be rather like allowing multiple threads to access the same runtime, which they don’t allow. If they’d allowed multiple threads to share the same GPU resources, then I’d buy that it might be intentional, but as this isn’t the case, I suspect this may be an oversight of some sort. I personally wouldn’t start using this “feature” until the NVIDIA guys respond indicating that it’s intentional.



I figured as much. There is too much potential for race conditions with a single GPU context shared between two processes. I imagine this is also why two threads can’t share the same context at the moment. All the CUDA calls would need to become reentrant.

Very interesting find!

But this means you either have a shared GPU space but separate CPU spaces, or separate GPU spaces but a shared CPU space.

The option to have both would be even nicer.