5

我正在尝试使用分配设置 3D 循环

 C(i,j,k) = A(i,j,k) + B(i,j,k)

在我的 GPU 上使用 Python。这是我的 GPU:

http://www.geforce.com/hardware/desktop-gpus/geforce-gt-520/specifications

我正在查看/比较的来源是:

http://nbviewer.ipython.org/gist/harrism/f5707335f40af9463c43

http://nbviewer.ipython.org/github/ContinuumIO/numbapro-examples/blob/master/webinars/2014_06_17/intro_to_gpu_python.ipynb

我可能导入了不必要的模块。这是我的代码:

import numpy as np
import numbapro
import numba
import math
from timeit import default_timer as timer
from numbapro import cuda
from numba import *

@autojit
def myAdd(a, b):
  return a+b

myAdd_gpu = cuda.jit(restype=f8, argtypes=[f8, f8], device=True)(myAdd)

@cuda.jit(argtypes=[float32[:,:,:], float32[:,:,:], float32[:,:,:]])
def myAdd_kernel(a, b, c):
    tx = cuda.threadIdx.x
    ty = cuda.threadIdx.y
    tz = cuda.threadIdx.z
    bx = cuda.blockIdx.x
    by = cuda.blockIdx.y
    bz = cuda.blockIdx.z
    bw = cuda.blockDim.x
    bh = cuda.blockDim.y
    bd = cuda.blockDim.z
    i = tx + bx * bw
    j = ty + by * bh
    k = tz + bz * bd
    if i >= c.shape[0]:
      return
    if j >= c.shape[1]:
      return
    if k >= c.shape[2]:
      return
    for i in xrange(0,c.shape[0]):
      for j in xrange(0,c.shape[1]):
        for k in xrange(0,c.shape[2]):
          # c[i,j,k] = a[i,j,k] + b[i,j,k]
          c[i,j,k] = myAdd_gpu(a[i,j,k],b[i,j,k])

def main():
    my_gpu = numba.cuda.get_current_device()
    print "Running on GPU:", my_gpu.name
    cores_per_capability = {1: 8,2: 32,3: 192,}
    cc = my_gpu.compute_capability
    print "Compute capability: ", "%d.%d" % cc, "(Numba requires >= 2.0)"
    majorcc = cc[0]
    print "Number of streaming multiprocessor:", my_gpu.MULTIPROCESSOR_COUNT
    cores_per_multiprocessor = cores_per_capability[majorcc]
    print "Number of cores per mutliprocessor:", cores_per_multiprocessor
    total_cores = cores_per_multiprocessor * my_gpu.MULTIPROCESSOR_COUNT
    print "Number of cores on GPU:", total_cores

    N = 100
    thread_ct = my_gpu.WARP_SIZE
    block_ct = int(math.ceil(float(N) / thread_ct))

    print "Threads per block:", thread_ct
    print "Block per grid:", block_ct

    a = np.ones((N,N,N), dtype = np.float32)
    b = np.ones((N,N,N), dtype = np.float32)
    c = np.zeros((N,N,N), dtype = np.float32)

    start = timer()
    cg = cuda.to_device(c)
    myAdd_kernel[block_ct, thread_ct](a,b,cg)
    cg.to_host()
    dt = timer() - start
    print "Wall clock time with GPU in %f s" % dt
    print 'c[:3,:,:] = ' + str(c[:3,1,1])
    print 'c[-3:,:,:] = ' + str(c[-3:,1,1])


if __name__ == '__main__':
    main()

我运行它的结果如下:

Running on GPU: GeForce GT 520
Compute capability:  2.1 (Numba requires >= 2.0)
Number of streaming multiprocessor: 1
Number of cores per mutliprocessor: 32
Number of cores on GPU: 32
Threads per block: 32
Block per grid: 4
Wall clock time with GPU in 1.104860 s
c[:3,:,:] = [ 2.  2.  2.]
c[-3:,:,:] = [ 2.  2.  2.]

当我运行源代码中的示例时,我看到了显着的加速。我认为我的示例运行不正常,因为挂钟时间比我预期的要长得多。我主要从第一个示例链接中的“使用 cuda python 实现更大的加速”部分对此进行了建模。

我相信我已经正确且安全地建立了索引。也许问题出在我的blockdim上?还是格子?或者我的 GPU 使用了错误的类型。我想我读到它们一定是某种类型。我对此很陌生,所以这个问题很可能是微不足道的!

非常感谢任何和所有帮助!

4

2 回答 2

4

您正在正确创建索引,但随后您忽略了它们。运行嵌套循环

for i in xrange(0,c.shape[0]):
    for j in xrange(0,c.shape[1]):
        for k in xrange(0,c.shape[2]):

是强制你所有的线程循环遍历所有维度的所有值,这不是你想要的。您希望每个线程在一个块中计算一个值,然后继续。

我认为这样的事情应该会更好......

i = tx + bx * bw
while i < c.shape[0]:
    j = ty+by*bh
    while j < c.shape[1]:
        k = tz + bz * bd
        while k < c.shape[2]:
            c[i,j,k] = myAdd_gpu(a[i,j,k],b[i,j,k])
            k+=cuda.blockDim.z*cuda.gridDim.z
        j+=cuda.blockDim.y*cuda.gridDim.y
    i+=cuda.blockDim.x*cuda.gridDim.x

尝试编译并运行它。还要确保验证它,因为我没有。

于 2015-01-04T14:10:03.723 回答
-1

我没有看到您使用 imshow 或 show,因此无需导入它们。

它看起来好像你没有使用你的数学导入(我没有看到任何 math.some_function 的调用。

您从 numba 和 numbapro 的导入似乎是重复的。您的“from numba import cuda”会覆盖您的“from numbapro import cuda”,因为它紧随其后。您对 cuda 的调用使用 numba 中的 cuda 而不是 numbapro。当您调用“from numba import *”时,您从 numba 导入所有内容,而不仅仅是 cuda,这似乎是您唯一使用的东西。另外,(我相信)import numba.cuda 相当于 from numba import cuda。为什么不使用一个“from numba import cuda”来消除所有从 numba 和 numbapro 的导入。

于 2015-01-02T19:57:41.490 回答