CUDA任意维度的矩阵相乘

__global__ void matrixMul( float* A, float* B, float* C, int hA,int wA, int wB)
{
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;


float Csub = 0.0f;



for (int j=0;j<wA;j+=BLOCK_DIM)
{
__shared__ float AS[BLOCK_DIM][BLOCK_DIM];
__shared__ float BS[BLOCK_DIM][BLOCK_DIM];


if(((by*BLOCK_DIM+ty)<hA)&&((tx+j)<wA))
{
AS[ty][tx] = A[(by*BLOCK_DIM+ty)*wA+tx+j];
}
else
{
AS[ty][tx] = 0;
}
if(((ty+j)<wA)&&((bx*BLOCK_DIM+tx)<wB))
{
BS[ty][tx] = B[(ty+j)*wB+bx*BLOCK_DIM+tx];
}
else
{
BS[ty][tx] = 0;
}


__syncthreads();


//Kahan's Summation Formula
for (int k = 0; k < BLOCK_DIM; ++k)
{
Csub += AS[ty][k]*BS[k][tx];
}


__syncthreads();
}

if(((by*BLOCK_DIM+ty)<hA)&&((bx*BLOCK_DIM+tx)<wB))
{
int c = wB*BLOCK_DIM*by + BLOCK_DIM*bx; 
C[c + wB * ty + tx] = Csub; 


}


}




//



int wA =  src.feat[t].size[2];;                       //1的宽 2的高
int wB = src.feat[t].size[0]*src.feat[t].size[1];  //2 3矩阵的宽
int hA = pmatrix.size[1];                  //1 3矩阵的高


dim3 mygrid(((wB+BLOCK_DIM-1)/BLOCK_DIM),(hA+BLOCK_DIM-1)/BLOCK_DIM);
dim3 myblock(BLOCK_DIM,BLOCK_DIM);
matrixMul<<<mygrid,myblock>>>(dev_matrix,dev_pyra[t],dev_pyrapro[t],hA,wA,wB);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值