title: How use PyCuda in multiprocessing?
body: <p>the problem is not to use CUDA in processes, but that if the parent already has an initialized CUDA, then in processes can not initialize it. If you just make the initialization in the parent and throw it into the processes, but it does not work. Here is an example just in the processes:</p>
  
  <pre><code>from pyfft.cuda import Plan
  import numpy
  import pycuda.driver as cuda
  from pycuda.tools import make_default_context
  import pycuda.gpuarray as gpuarray
  
  from multiprocessing import Process
  
  def do_this_fft(data):
      cuda.init()
      context = make_default_context()
      stream = cuda.Stream()
      plan = Plan((16, 16), stream=stream)
  
      gpu_data = gpuarray.to_gpu(data)
      plan.execute(gpu_data)
      result = gpu_data.get()
      del gpu_data
      print(result)
      context.pop()
      del plan,stream,context
  
  data = numpy.ones((16, 16), dtype=numpy.complex64)
  process_list=[Process(target=do_this_fft,args=(data,)).start() for i in range(2)]
  </code></pre>
  
  <p>But if we write code like this, then nothing will work anymore</p>
  
  <pre><code>from pyfft.cuda import Plan
  import numpy
  import pycuda.driver as cuda
  from pycuda.tools import make_default_context
  import pycuda.gpuarray as gpuarray
  
  from multiprocessing import Process
  
  def start_cuda_and_fft(data):
      cuda.init()
      context = make_default_context()
      stream = cuda.Stream()
      plan = Plan((16,16), stream=stream)
      context.pop()
      del plan,stream,context
      process_list=[Process(target=do_this_fft,args=(data,plan)).start() for i in range(2)] 
  
  def do_this_fft(data):
      cuda.init() #&lt;---------------Error
      '''
      File "test.py", line 35, in do_this_fft
          cuda.init()
      LogicError: cuInit failed: initialization error
  
      '''
      context = make_default_context()
      stream = cuda.Stream()
      plan = Plan((16, 16), stream=stream)
  
      gpu_data = gpuarray.to_gpu(data)
      plan.execute(gpu_data)
      result = gpu_data.get()
      context.pop()
      del plan,stream,context
      print(result)
  
  data = numpy.ones((16, 16), dtype=numpy.complex64)
  process_list=[Process(target=do_this_fft,args=(data,)).start() for i in range(2)]
  </code></pre>
  
  <p>The error is the following:</p>
  
  <blockquote>
    <p>File "test.py", line 35, in do_this_fft cuda.init() LogicError: cuInit failed: initialization error</p>
  </blockquote>
  
  <p>I have already tried to do the context, the plan and push into the process, all without consequences.</p>