- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 pyCUDA 中启动内核,然后通过写入 GPU 全局内存位置来终止内核。这是一个简单的示例内核,我希望能够在进入无限 while 循环后的某个时刻终止它:
__global__ void countUp(u16 *inShot, u64 *counter) {
while(inShot[0]) {
counter[0]++;
}
}
根据我对 CUDA 中流的了解,我应该能够在创建流后启动该内核,并且它将在主机上非阻塞,即。该内核启动并运行后,我应该能够在主机上执行操作。我将上述内核编译为 cubin 文件并在 pyCUDA 中启动它,如下所示:
import numpy as np
from pycuda import driver, compiler, gpuarray, tools
# -- initialize the device
import pycuda.autoinit
strm1 = driver.Stream()
h_inShot = np.zeros((1,1))
d_inShot = gpuarray.to_gpu_async(h_inShot.astype(np.uint16), stream = strm1)
h_inShot = np.ones((1,1))
h_counter = np.zeros((1,1))
d_counter = gpuarray.to_gpu_async(h_counter.astype(np.uint64), stream = strm1)
testCubin = "testKernel.cubin"
mod = driver.module_from_file(testCubin)
countUp = mod.get_function("countUp")
countUp(d_inShot, d_counter,
grid = (1, 1, 1),
block = (1, 1, 1),
stream = strm1
)
出于显而易见的原因,运行此脚本会导致内核进入无限 while 循环。在内核启动后,从 ipython 环境启动此脚本似乎不会将控制权返回给主机(我无法输入新命令,因为我猜它正在等待内核完成)。我希望控制权返回到主机,以便我可以更改 GPU 全局内存指针 d_inShot 中的值并使内核退出 while 循环。这是否可能,如果可以,我该如何在 pyCUDA 中做到这一点?谢谢。
最佳答案
我解决了这个问题,所以我发布了我的解决方案。尽管异步 memcpy 是非阻塞的,但我发现使用与正在运行的内核相同的流来执行 memcpy 是行不通的。我的解决方案是创建另一个流:
strm2 = driver.Stream()
然后像这样更改 d_inShot:
d_inShot.set_async(h_inShot.astype(np.uint16), stream = strm2)
这对我有用。
关于python - 异步内核启动后返回 pyCUDA 中的主机代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30113562/
我创建了一个形状为 (64, 128) 的 float32 numpy 数组,我想将它发送到 GPU。我怎么做?我的内核函数应该接受哪些参数? float** myArray? 我试过直接将数组原样发
示例代码: import pycuda.autoinit import pycuda.driver as drv import numpy from pycuda.compiler import So
我正在尝试编译一些源代码以使用我的 GPU。我为此使用pycuda。当我编译源代码时,我收到一些来自 Python 的错误: C:\Users\Dmitriy\wcm>python ws_gpu.py
我正在尝试在 PyCUDA 中初始化 2D 表面并用 NumPy 2D 数组中的值填充它。据我所知,这个想法是 打开drv.ArrayDescriptor, 使用这个描述符创建drv.Array, 使
我想在 flask 服务器上运行 pyCUDA 代码。该文件直接使用python3正确运行,但使用flask调用相应函数时失败。 相关代码如下: cudaFlask.py: import pycuda
我对 cuda 有一个奇怪的问题, 在下面的片段中, #include #define OUTPUT_SIZE 26 typedef $PRECISION REAL; extern
我在这里有一个 pycuda 程序,它从命令行读取图像并用反转的颜色保存一个版本: import pycuda.autoinit import pycuda.driver as device from
如何让 PyCuda 提取字符串数组而不是一个字符字符串?如果取消注释 C 代码中的该行,您将看到它迭代每个字符而不是每个字符串。 现在我只是想计算每个字符串的长度,但最终会将其变成一个词频计数器
我正在尝试在 Pycuda 代码中实现一个结构,但出现越界错误。我尝试遵循 this教程,但我无法让它适用于我的情况。 该问题很可能是由于指针使用不当造成的,例如该教程表明必须分配指针 memsize
我在安装了 python 2.7(32 位)和安装了 cuda 7.5 whl 的 pycuda 的 Windows 机器上。我在运行示例程序来测试 pycuda 时出错。 Traceback (mo
我这里有一个 pycuda 程序,它从命令行读取图像并保存颜色反转的版本: import pycuda.autoinit import pycuda.driver as device from pyc
足够简单 start=cuda.Event() func(args,block=blockdims) cuda.memcpy_dtoh(d,h) end=cuda.Event() dur=start.
PyCUDA,尽管有其所有缺点,通常都会提供非常好的示例/可从 wiki 下载。但我在示例或文档(或粗略的谷歌搜索)中找不到任何内容来演示 PyCUDA 将工作负载动态分配到多个设备的方法。 有人可以
我正在使用 pycuda 制作相对论光线追踪器。基本上,对于大型 2D 数组中的每个“像素”,我们必须使用 Runge Kutta 求解 6 个 ODE 系统。由于每个集成都独立于其余集成,因此应该非
我是 PyCUDA 的新手,正在浏览 PyCUDA 网站上的一些示例。我正在尝试弄清楚某些代码行背后的逻辑,如果有人解释了它背后的想法,我将不胜感激。 以下代码片段来自 PyCUDA 网站。函数定义里
应该足够简单;我确实想将一个 int 发送到 SourceModule 内核声明,其中 C 函数 __global__......(int value,.....) 随着值的声明和调用... valu
in desaturate_image redarray_gpu = cuda.mem_alloc(self.redarray.nbytes) pycuda._driver.LogicErro
在简单的 CUDA 程序中,我们可以通过包含 cuPrintf.h 来按线程打印消息,但在 PyCUDA 中执行此操作在任何地方都没有解释。如何在 PyCUDA 中做到这一点? 最佳答案 在 Comp
我正在使用 pyCUDA 进行 CUDA 编程。我需要在内核函数中使用随机数。 CURAND 库在其中不起作用(pyCUDA)。由于GPU有很多工作要做,在CPU内部生成随机数然后将它们传输到GPU是
我正在尝试将二维复数数组传递到 PyCUDA 内核中,但得到了意想不到的结果。 这是我的测试代码: import numpy as np import pycuda.driver as cuda im
我是一名优秀的程序员,十分优秀!