大家好: 我希望写一个在GPU上实现矩阵相乘的程序。之前关于cuda的存储方式,在下面的贴子中我有提问。基本明白了cuda的原理(最基础的)。
我写了一个MN=P,M是一个48的矩阵,N是一个8*2的矩阵的GPU程序。如附件。[attach]3053[/attach]
程序如下。
我得到的结果与想要的不一致。我觉得是调用kernel时没计算好。或是我的程序本身就有问题(但我还不知道)。
结果如图。
我的问题是:
1.我有没有可能 通过程序的输出显示在GPU上运行的过程(这样就大体可以找到问题所在)。
2.我的程序有没有问题,(现在看是一定有的,),问题在哪里。kernel写的不对?
我的机器是quadro K1000M的显卡,用的是vs2010 和cuda4.2
# include "stdio.h"
# include "stdlib.h"
/*
this pro is for the GPU version of the matrix multiple.
use the function
*/
// define the device function
__global__ void fun_MatMul_GPU(float *Md, float *Nd, float *Pd, int M_row, int M_col, int N_col){
int tx = threadIdx.x;
int ty = threadIdx.y;
// init p_tmp
float p_tmp=0;
float M_tmp=0;
float N_tmp=0;
int k=0;
// iteration thread
for (k=0;k<M_col;k++)
{
M_tmp = Md[tx*M_col+k ];
N_tmp = Nd[k *N_col+ty];
p_tmp = p_tmp+M_tmp*N_tmp;
}
// write the Pd
Pd[tx * N_col + ty] = p_tmp;
}
// main function
int main(void)
{
// init and malloc M,N,P **************************************
printf("----- init matrix--------------------------------------\n");
int i,j,ind;
//int k=0;
// define the matrix size, M,N which will define P
int M_row = 4;
int M_col = 8;
int N_row = M_col;
int N_col = 2;
// matrix memory size
int M_size = M_row * M_col * sizeof(float);
int N_size = N_row * N_col * sizeof(float);
int P_size = M_row * N_col * sizeof(float);
// malloc the matrix memory
float * M_h =(float *) malloc(M_size);
float * N_h =(float *) malloc(N_size);
float * P_h =(float *) malloc(P_size);
// init M matrix
printf("M=\n");
for (i=0;i<M_row;i++)
{
for (j=0;j<M_col;j++)
{
ind=i*M_col+j;
M_h[ind]=(i+1)+0.01*(j+1);
printf("%3.3f, ",M_h[ind]);
}
printf("\n");
}
// init N matrix
printf("N=\n");
for (i=0;i<N_row;i++)
{
for (j=0;j<N_col;j++)
{
ind=i*N_col+j;
N_h[ind]=(i+1)+0.001*(j+1);
printf("%3.3f, ",N_h[ind]);
}
printf("\n");
}
// cudamalloc M,N,P**********************************
printf("----- cudamalloc matrix-----------------------------\n");
float *M_d, *N_d, *P_d;
cudaMalloc((void **) &M_d, M_size);
cudaMalloc((void **) &N_d, N_size);
cudaMalloc((void **) &P_d, P_size);
// transfor data from host to device
cudaMemcpy(M_d,M_h,M_size,cudaMemcpyHostToDevice);
cudaMemcpy(N_d,N_h,N_size,cudaMemcpyHostToDevice);
// init kernerl
int block_size=16;
int grid_size=1;
dim3 dimBlock(block_size,block_size);
dim3 dimGrid(grid_size,grid_size);
// GPU cal
fun_MatMul_GPU<<<dimGrid,dimBlock>>>(M_d,N_d,P_d,M_row,M_col,N_col);
// transfor data from device to host
cudaMemcpy(P_h,P_d,P_size,cudaMemcpyDeviceToHost);
printf("----- GPU end ----------------------------------\n");
// output multiple result **************************
printf("----- output multiple result----------------------\n");
for (i=0;i<M_row;i++)
{
for (j=0;j<N_col;j++)
{
ind=(i+1)*N_col+(j+1);
printf("%4.4f ,",P_h[ind]);
}
printf("\n");
}
// free M,N,P***************************************
printf("----- free M,N,P---------------------------------\n");
// free the host memory
free(M_h);
free(N_h);
free(P_h);
// free the device memory
cudaFree(M_d);
cudaFree(N_d);
cudaFree(P_d);
return 0;
}
// mod : 11:08 2013/4/13
// debug .
// mod : Sat Apr 13 08:36:18 CST 2013
// cudaMalloc the memory on GPU device.
// the result is wrong!!!!
// mod : Fri Apr 12 18:19:09 CST 2013
// the GPU function
// mod : Fri Apr 12 06:44:14 CST 2013
// build the main structure.
http://cudazone.nvidia.cn/forum/forum.php?mod=viewthread&tid=6566&extra=page%3D1