'Deleting an FFT plan in scikit-cuda destroys the pycuda context

I would like to use pycuda and the FFT functions from scikit-cuda together. The code below

  • creates a skcuda.fft.Plan,
  • deletes that plan and then
  • tries to allocate a pycuda.gpuarray.GPUArray.
import pycuda.autoinit

import numpy as np
import pycuda
import skcuda
import skcuda.fft as cufft

plan = cufft.Plan((2,2), np.complex64, np.complex64)

del plan # equivalent to `skcuda.cufft.cufftDestroy(plan.handle)`
#skcuda.cufft.cufftDestroy(plan.handle) # equivalent to `del plan`

pycuda.gpuarray.empty((2,2), np.float32)

The last line throws pycuda._driver.LogicError: cuMemAlloc failed: context is destroyed.

Somehow, skcuda.cufft.cufftDestroy(plan.handle) also destroys the pycuda context (which is of type pycuda._driver.Context).

Can somebody see a good fix?



Solution 1:[1]

The master replied (https://github.com/inducer/pycuda/discussions/356):

By using pycuda.autoinit, you're putting pycuda in charge of context management. That's not typically a good recipe for interacting with libraries that use the CUDA runtime API (like cuFFT, to my understanding). You might be better off retaining the "primary context" made by/for the runtime API and using that instead.

The solution to my specific problem above is retaining the primary context instead of letting pyCUDA create a new context. The easiest way to do this is via:

import pycuda.autoprimaryctx instead of import pycuda.autoinit

Voila, everything works now. See also the documentation and the code for pycuda.autoprimaryctx.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1