Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parallel processing CUDA分片矩阵乘法说明_Parallel Processing_Cuda_Nvidia_Gpu Shared Memory - Fatal编程技术网

Parallel processing CUDA分片矩阵乘法说明

Parallel processing CUDA分片矩阵乘法说明,parallel-processing,cuda,nvidia,gpu-shared-memory,Parallel Processing,Cuda,Nvidia,Gpu Shared Memory,我试图了解CUDA SDK 8.0中的代码是如何工作的: template <int BLOCK_SIZE> __global__ void matrixMulCUDA(float *C, float *A, float *B, int wA, int wB) { // Block index int bx = blockIdx.x; int by = blockIdx.y; // Thread index int tx = threadIdx.x; int ty = thread

我试图了解CUDA SDK 8.0中的代码是如何工作的:

template <int BLOCK_SIZE> __global__ void
matrixMulCUDA(float *C, float *A, float *B, int wA, int wB)
{
// Block index
int bx = blockIdx.x;
int by = blockIdx.y;

// Thread index
int tx = threadIdx.x;
int ty = threadIdx.y;

// Index of the first sub-matrix of A processed by the block
int aBegin = wA * BLOCK_SIZE * by;

// Index of the last sub-matrix of A processed by the block
int aEnd   = aBegin + wA - 1;

// Step size used to iterate through the sub-matrices of A
int aStep  = BLOCK_SIZE;

// Index of the first sub-matrix of B processed by the block
int bBegin = BLOCK_SIZE * bx;

// Step size used to iterate through the sub-matrices of B
int bStep  = BLOCK_SIZE * wB;

....
....
template\uuuuu全局\uuuuuu\void
matrixMulCUDA(浮点*C、浮点*A、浮点*B、整数wA、整数wB)
{
//块索引
int bx=blockIdx.x;
int by=blockIdx.y;
//线程索引
int tx=线程idx.x;
int ty=threadIdx.y;
//由块处理的第一个子矩阵的索引
int aBegin=wA*块大小*by;
//块处理的最后一个子矩阵的索引
int aEnd=aBegin+wA-1;
//步长用于迭代一个矩阵的子矩阵
int aStep=块大小;
//块处理的B的第一个子矩阵的索引
int bBegin=块大小*bx;
//用于迭代B的子矩阵的步长
int bStep=块大小*wB;
....
....
内核的这一部分对我来说相当棘手。我知道矩阵A和B用数组(*float)表示,我还知道使用共享内存计算点积的概念,这要归功于共享内存


我的问题是我不理解代码的开头,特别是3个特定变量(
aBegin
aEnd
bBegin
).有人能给我一个可能执行的示例图,以帮助我了解索引在这种特定情况下的工作方式吗?谢谢你这里有一个图,可以了解CUDA内核的第一个变量的值设置和执行的总体计算:

矩阵按行主顺序存储。CUDA代码假定矩阵大小可按
块大小划分

矩阵
A
B
C
根据内核CUDA网格被虚拟地分割成块。
C
的所有块都可以并行计算。对于给定的
C
深灰色块,主循环遍历
A
B
的几个浅灰色块(锁步)使用
block\u SIZE*block\u SIZE
线程并行计算每个块

bx
by
是CUDA网格中当前块的基于块的位置。
tx
ty
是当前线程在CUDA网格的当前计算块内计算的单元的基于单元的位置

下面是对
aBegin
变量的详细分析:
aBegin
是指矩阵
A
的第一个计算块的第一个单元的内存位置。它被设置为
wA*block\u SIZE*by
,因为每个块都包含
block\u SIZE*block\u SIZE
单元,并且在水平方向上有
wA/block\u SIZE
块,在电流上方有
by块t计算
A的块
。因此,
(块大小*块大小)*(wA/block大小)*by=block大小*wA*by

同样的逻辑适用于
bBegin
: 它被设置为
BLOCK\u SIZE*bx
,因为在矩阵
B
的第一个计算块的第一个单元格之前,内存中存在
bx
大小为
BLOCK\u SIZE
的块

a
在循环中增加
aStep=BLOCK\u SIZE
,以便下一个计算的块位于右侧(图纸上)在同一循环中,
A
b
的当前计算块的值增加
bStep=block_SIZE*wB
,以便下一个计算块是
b
的当前计算块的底部(在图纸上)的下面