简体   繁体   English

Numba CUDA 共享内存矩阵乘法

[英]Numba CUDA shared memory matrix multiplication

I'm running a shared memory numba code for matrix multiplication, but I think the algorithm to solve it is incorrect as I'm getting incorrect results我正在运行用于矩阵乘法的共享内存 numba 代码,但我认为解决它的算法不正确,因为我得到的结果不正确

I saw another thread for this code but there the answer remained undisclosed and the code did not work我看到了此代码的另一个线程,但答案仍未公开并且代码不起作用

The code is below:代码如下:

# This part is for initializing everything
M = 128
N = 32


a = np.arange(M*N).reshape(M,N).astype(np.int32)
b = np.arange(M*N).reshape(N,M).astype(np.int32)
c = np.zeros((M, M)).astype(np.int32)

d_a = cuda.to_device(a)
d_b = cuda.to_device(b)
d_c = cuda.to_device(c)

block_size = (N,N)
grid_size = (int(M/N),int(M/N))

And this is my kernel definition:这是我的内核定义:

import numpy as np
from numba import cuda, types
@cuda.jit
def fast_matmul(A, B, C):
    # Define an array in the shared memory
    # The size and type of the arrays must be known at compile time
    TPB = N
    
    sA = cuda.shared.array(shape=(TPB, TPB), dtype=float32)
    sB = cuda.shared.array(shape=(TPB, TPB), dtype=float32)

    x, y = cuda.grid(2)

    tx = cuda.threadIdx.x
    ty = cuda.threadIdx.y
    bpg = cuda.gridDim.x    # blocks per grid

    if x >= C.shape[0] and y >= C.shape[1]:
        # Quit if (x, y) is outside of valid C boundary
        return

    # Each thread computes one element in the result matrix.
    # The dot product is chunked into dot products of TPB-long vectors.
    tmp = 0.
    for i in range(bpg):
        # Preload data into shared memory
        sA[tx, ty] = A[x, ty + i * TPB]
        sB[tx, ty] = B[tx + i * TPB, y]

        # Wait until all threads finish preloading
        cuda.syncthreads()

        # Computes partial product on the shared memory
        for j in range(TPB):
            tmp += sA[tx, j] * sB[j, ty]

        # Wait until all threads finish computing
        cuda.syncthreads()

    C[x, y] = tmp

Which I followed from here: https://numba.pydata.org/numba-doc/dev/cuda/examples.html我从这里遵循: https : //numba.pydata.org/numba-doc/dev/cuda/examples.html

However running the code gives me odd results such as然而,运行代码给了我奇怪的结果,比如

x: array([[2147483647, 2147483647, 2147483647, ..., 2147483647, 2147483647,
        2147483647],
       [2147483647, 2147483647, 2147483647, ..., 2147483647, 2147483647,...

when it should be something like:什么时候应该是这样的:

 y: array([[  1333248,   1333744,   1334240, ...,   1395248,   1395744,
          1396240],
       [  3364864,   3366384,   3367904, ...,   3554864,   3556384,...

Could anyone please point out where I am going wrong?谁能指出我哪里出错了?

  1. you're passing int32 arrays to a numba kernel that is expecting float32 data.您将int32数组传递给需要float32数据的 numba 内核。 Don't do that.不要那样做。
  2. you've also not actually shown your kernel launch.您实际上也没有显示您的内核启动。 Please provide a complete code.请提供完整的代码。
  3. its not clear what x and y are.不清楚xy是什么。 Your code produces only one result, and it is in d_c .您的代码只产生一个结果,它在d_c
  4. I also suspect your input data will overflow int32 type.我也怀疑你的输入数据会溢出int32类型。 You should probably convert to float32 throughout.您可能应该始终转换为float32 Using arange makes it hard to verify numerical correctness quickly/visually.使用arange很难快速/直观地验证数值的正确性。 While you're trying to get things working just use ones instead.当您试图让事情正常工作时ones改用它们。

Here's a version of what you've show, with the above ideas taken into account:这是您展示的内容的一个版本,并考虑了上述想法:

$ cat t35.py
import numpy as np
from numba import cuda, types, float32
@cuda.jit
def fast_matmul(A, B, C):
    # Define an array in the shared memory
    # The size and type of the arrays must be known at compile time
    TPB = N

    sA = cuda.shared.array(shape=(TPB, TPB), dtype=float32)
    sB = cuda.shared.array(shape=(TPB, TPB), dtype=float32)

    x, y = cuda.grid(2)

    tx = cuda.threadIdx.x
    ty = cuda.threadIdx.y
    bpg = cuda.gridDim.x    # blocks per grid

    if x >= C.shape[0] and y >= C.shape[1]:
        # Quit if (x, y) is outside of valid C boundary
        return

    # Each thread computes one element in the result matrix.
    # The dot product is chunked into dot products of TPB-long vectors.
    tmp = 0.
    for i in range(bpg):
        # Preload data into shared memory
        sA[tx, ty] = A[x, ty + i * TPB]
        sB[tx, ty] = B[tx + i * TPB, y]

        # Wait until all threads finish preloading
        cuda.syncthreads()

        # Computes partial product on the shared memory
        for j in range(TPB):
            tmp += sA[tx, j] * sB[j, ty]

        # Wait until all threads finish computing
        cuda.syncthreads()

    C[x, y] = tmp

# This part is for initializing everything
M = 128
N = 32


#a = np.arange(M*N).reshape(M,N).astype(np.float32)
#b = np.arange(M*N).reshape(N,M).astype(np.float32)
a = np.ones(M*N).reshape(M,N).astype(np.float32)
b = np.ones(M*N).reshape(N,M).astype(np.float32)
c = np.zeros((M, M)).astype(np.float32)

d_a = cuda.to_device(a)
d_b = cuda.to_device(b)
d_c = cuda.to_device(c)

block_size = (N,N)
grid_size = (int(M/N),int(M/N))

fast_matmul[grid_size,block_size](d_a, d_b, d_c)
c = d_c.copy_to_host()
print(c)
$ python t35.py
[[32. 32. 32. ... 32. 32. 32.]
 [32. 32. 32. ... 32. 32. 32.]
 [32. 32. 32. ... 32. 32. 32.]
 ...
 [32. 32. 32. ... 32. 32. 32.]
 [32. 32. 32. ... 32. 32. 32.]
 [32. 32. 32. ... 32. 32. 32.]]
$

I believe 32 is the correct answer here.我相信 32 是这里的正确答案。

Also note the posted example from which this is derived probably has some errors, see here还要注意从中得出的已发布示例可能有一些错误,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM