pyCUDA 无法打印结果

2024-05-14 09:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我使用pip为python3.4.3安装了pyCUDA。但是我发现当我测试示例代码(https://documen.tician.de/pycuda/tutorial.html#getting-started)时,如果没有任何错误消息,它无法打印结果,程序可能会结束。我不明白这段代码或我的python有什么问题,谢谢大家回答。这个是我的代码:

import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
import random
a =[random.randint(0,20) for i in range(20)]
a = a.astype(numpy.float32)
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
mod = SourceModule("""
  __global__ void doublify(float *a)
  {
    int idx = threadIdx.x + threadIdx.y*4;
    a[idx] *= 2;
  }
  """)
func = mod.get_function("doublify")
func(a_gpu, block=(4,4,1))
a_doubled = numpy.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)
print(a_doubled)
print(a)

Tags: 代码importpycudanumpymodgpurandomcuda

热门问题