在Scikit-Cuda中删除FFT计划会破坏Pycuda上下文

发布于 2025-01-28 05:20:58 字数 1011 浏览 2 评论 0原文

我想使用 pycuda 和FFT功能来自 scikit-cuda 一起。下面的代码

  • 创建skcuda.fft.plan
  • 删除计划,然后
  • 尝试分配pycuda.gpuarray.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)

最后一行投掷pycuda._driver.logicerror:cumemalloc失败:上下文被销毁

不知何故,skcuda.cufft.cufftdestroy(plan.handle)还摧毁了pycuda上下文(即类型pycuda> pycuda._driver._driver.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2025-02-04 05:20:58

主人回答( https://github.com/inducer.com/inducer/pycuda/pycuda/discussions/356 ):

通过使用pycuda.autoinit,您将Pycuda负责上下文管理。这通常不是与使用CUDA运行时API的库(例如Cufft,对我的理解)进行交互的好秘诀。您可能最好保留由/为运行时API制作的“主要上下文”并使用它。

  • 本质上,Cuda有一个叫做
  • 这不同于普通上下文并被摧毁。
  • 另请参见这个论坛讨论关于差异及其为什么存在的讨论。
  • 相关部分使上下文管理清晰。
  • 初始化 “ https://docs.nvidia.com/cuda/cuda-cuda-cuda-cuda-cuda/index.html#interoperability-beterability-betewnew-runtime-and-driver-apis“ rel =“ nofollow noreferrer” 完成如何创建和使用设备上下文的图片。

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

导入pycuda.autaprimaryctx而不是导入pycuda.autoinit

voila,现在一切正常。另请参见文档 //github.com/inducer/pycuda/blob/main/pycuda/autaprimaryctx.py“ rel =“ nofollow noreferrer”>代码 for pycuda.autoprimaryctx

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文