在 scikit-cuda 中删除 FFT 计划会破坏 pycuda 上下文

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

我想一起使用pycuda and the FFT functions from scikit-cuda。下面的代码

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)

最后一行抛出 pycuda._driver.LogicError: cuMemAlloc failed: context is destroyed.

不知何故,skcuda.cufft.cufftDestroy(plan.handle) 也破坏了 pycuda 上下文(pycuda._driver.Context 类型)。

有人可以找到好的修复方法吗?

大师回复(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.

我上面的特定问题的解决方案是保留主要上下文而不是让 pyCUDA 创建新的上下文。最简单的方法是通过:

import pycuda.autoprimaryctx 而不是 import pycuda.autoinit

瞧,现在一切正常。 pycuda.autoprimaryctx.

另见 the documentation and the code