【问题标题】:Does order of memory allocation matter in PyCUDA's curandom?PyCUDA 中的内存分配顺序是否重要?
【发布时间】:2023-03-14 18:55:01
【问题描述】:

我在 CUDA 统一内存 [2] 上使用 PyCUDA 的接口 [1]。在某些时候,我添加了随机数生成器 [3] 并盯着 Jupyter Notebook 中的死内核:

我将问题缩小到创建随机数生成器。或者,准确地说,到我这样做的那一刻:

import pycuda.curandom
from pycuda import autoinit, driver
import numpy as np

gpu_data_1 = driver.managed_zeros(shape=5, dtype=np.int32, mem_flags=driver.mem_attach_flags.GLOBAL)
gpu_generator = pycuda.curandom.XORWOWRandomNumberGenerator(pycuda.curandom.seed_getter_uniform)
gpu_data_2 = driver.managed_zeros(shape=5, dtype=np.int32, mem_flags=driver.mem_attach_flags.GLOBAL)

上面的代码在没有任何错误消息的情况下失败,但如果我将gpu_generator = ...高或低一行,它似乎工作正常

我相信 PyCUDA 可能无法执行 prepare call,这归结为这个内核:

extern "C" {
    __global__ void prepare(curandStateXORWOW *s, const int n,
        unsigned int *v, const unsigned int o)
    {
      const int id = blockIdx.x*blockDim.x+threadIdx.x;
      if (id < n)
        curand_init(v[id], id, o, &s[id]);
    }
}

知道可能是什么问题吗?

【问题讨论】:

  • 您使用的是哪种 GPU?
  • @talonmies 它是 GeForce GTX 1060 3GB
  • 在 pre-pascal GPU 上,我可以理解为什么会失败,但使用 GTX1060 则不应该出现这种情况。我有一个解决方法,不管它是否适合你 IDK。
  • 如果您碰巧在 Windows 上,则 UM 将处于 pre-pascal 状态,即使您使用的是 pascal GPU。

标签: python cuda jupyter-notebook pycuda curand


【解决方案1】:

在前 Pascal UM(统一内存)机制中,主机代码在内核启动后、cudaDeviceSynchronize() 发出之前接触托管分配是 illegal

我猜这个代码违反了这个规则。如果我在 Maxwell 系统上运行您的复制案例,我会得到:

$ cuda-memcheck python ./idontthinkso.py
========= CUDA-MEMCHECK
========= Error: process didn't terminate successfully
========= Fatal UVM CPU fault due to invalid operation
=========     during write access to address 0x703bc1000
=========
========= ERROR SUMMARY: 1 error

那是托管内存系统崩溃了。在随机生成器设置(运行内核)和 zeros 调用(涉及托管内存)之间进行同步调用可以在我的系统上消除它:

$ cat idontthinkso.py 
import pycuda.curandom
from pycuda import autoinit, driver
import numpy as np

gpu_data_1 = driver.managed_zeros(shape=5, dtype=np.int32, mem_flags=driver.mem_attach_flags.GLOBAL)
gpu_generator = pycuda.curandom.XORWOWRandomNumberGenerator(pycuda.curandom.seed_getter_uniform)
autoinit.context.synchronize()
gpu_data_2 = driver.managed_zeros(shape=5, dtype=np.int32, mem_flags=driver.mem_attach_flags.GLOBAL)

$ cuda-memcheck python ./idontthinkso.py
========= CUDA-MEMCHECK
========= ERROR SUMMARY: 0 errors

您所处的 UM 制度会因您使用的 GPU、驱动程序和操作系统而异。

【讨论】:

  • 它确实解决了手头的问题,我不知道我的 UM 可能处于帕斯卡之前的状态。它不能解决我原始代码中的问题,因为我已经在那里进行了同步调用 - 如果我没有提出解决方案,我会尝试更多地摆弄它并组装一个更好的 MCVE。
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
相关资源
最近更新 更多