我目前正在编写可以使用GPU进行大量并行化的代码。我的代码结构基本上如下所示:
大多数示例都非常具有说明性,但它们似乎都是这样的:在CPU上执行代码的主要部分,并且仅在GPU上执行中间矩阵乘法等。特别是主机通常知道内核将要使用的所有变量。
对我来说,反之亦然,我想在GPU上执行代码的主要部分,并且只需要在CPU本身上执行非常少量的步骤。我的主人几乎不知道我个人线程中发生的事情。它只管理标量列表以及我的数组A和B.
我的问题是:
我目前拥有的一个例子如下:
from __future__ import division
import numpy as np
from numbapro import *
# Device Functions
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Works and can be called corrently from TestKernel Scalar
@cuda.jit('float32(float32, float32)', device=True)
def myfuncScalar(a, b):
return a+b;
# Works and can be called correctly from TestKernel Array
@cuda.jit('float32[:](float32[:])', device=True)
def myfuncArray(A):
for k in xrange(4):
A[k] += 2*k;
return A
# Takes Matrix A and Vector v, multiplies them and returns a vector of shape v. Does not even compile.
# Failed at nopython (nopython frontend), Only accept returning of array passed into the function as argument
# But v is passed to the function as argument...
@cuda.jit('float32[:](float32[:,:], float32[:])', device=True)
def MatrixMultiVector(A,v):
tmp = cuda.local.array(shape=4, dtype=float32); # is that thing even empty? It could technically be anything, right?
for i in xrange(A[0].size):
for j in xrange(A[1].size):
tmp[i] += A[i][j]*v[j];
v = tmp;
return v;
# Kernels
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------
# TestKernel Scalar - Works
@cuda.jit(void(float32[:,:]))
def TestKernelScalar(InputArray):
i = cuda.grid(1)
for j in xrange(InputArray[1].size):
InputArray[i,j] = myfuncScalar(5,7);
# TestKernel Array
@cuda.jit(void(float32[:,:]))
def TestKernelArray(InputArray):
# Defining arrays this way seems super tedious, there has to be a better way.
M = cuda.local.array(shape=4, dtype=float32);
M[0] = 1; M[1] = 0; M[2] = 0; M[3] = 0;
tmp = myfuncArray(M);
#tmp = MatrixMultiVector(A,M); -> we still have to define a 4x4 matrix for that.
i = cuda.grid(1)
for j in xrange(InputArray[1].size):
InputArray[i,j] += tmp[j];
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Main
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------
N = 4;
C = np.zeros((N,N), dtype=np.float32);
TestKernelArray[1,N](C);
print(C)
答案 0 :(得分:1)
cuda.local.array()
和cuda.shared.array
),但这些数组具有线程或块范围,并且在其关联的线程或块之后无法重用退役。但这是所有支持的。您可以将外部定义的数组传递给内核,但它们的属性是只读的。myfuncArray
,您可以返回外部定义的数组。您无法返回动态定义的数组,因为内核不支持动态定义的数组(或任何对象)。