2
votes

Thank for @hubs , when call cublasSgemv should notice that CUBLAS_OP_T is also transpose vector. /*I am learning cuda and cublas for a month, and I want to test the performance of cublas for further use. But in my matrix-vector multiplication using cublasSgemv , the answer is wrong. I initialize Matrix A and Vector x in row-major. I sent them to device using cudaMemcpy, and call the function cublasSgemv , because the A is row-major, I transpose it using a parameter CUBLAS_OP_T.*/

 //the row is 50,and col is 10, A[i]=i;x[i]=1; And A matrix is row major.
 //the answer I get is 45,545,.....4545,0,0,0,0,0,0,0,0,........0

int main(){
int row=50;
int col=10;
int N=row*col;
float*A=new float[N];
float* y_gpu=new float[50]; 
for (int i=0;i<N;i++)
{
    A[i]=(float)i;
}
float* x=new float[10];
for (int i=0;i<10;i++)
{
    x[i]=1;
}
GpuVec(A,x,y_gpu,row,col);  //call the function 
    for(int i=0;i<50;i++){
    cout<<" "<<y_gpu[i]<<endl;  //
} 

return 0;

}

int GpuVec(const float* A,const float* x, float* y,const int row,const int col){
cudaError_t cudastat;
cublasStatus_t stat;
int size=row*col;
cublasHandle_t handle;
float* d_A;  //device matrix
float* d_x;  //device vector
float* d_y;  //device result
cudastat=cudaMalloc((void**)&d_A,size*sizeof(float)); 
cudastat=cudaMalloc((void**)&d_x,col*sizeof(float));
cudastat=cudaMalloc((void**)&d_y,row*sizeof(float));// when I copy y to d_y ,can I cout d_y?

cudaMemcpy(d_A,A,sizeof(float)*size,cudaMemcpyHostToDevice);  //copy A to device d_A
cudaMemcpy(d_x,x,sizeof(float)*col,cudaMemcpyHostToDevice);   //copy x to device d_x
float alf=1.0;
float beta=0;
    stat=cublasCreate(&handle);
stat=cublasSgemv(handle,CUBLAS_OP_T,col,row,&alf,d_A,col,d_x,1,&beta,d_y,1);//swap col and row
cudaMemcpy(y,d_y,sizeof(float)*row,cudaMemcpyDeviceToHost); // copy device result to host 
cudaFree(d_A);
cudaFree(d_x);
cudaFree(d_y);
cublasDestroy(handle);
return 0;

}

1
What excact does mean, that the answer is wrong? I think you uses cublasSgemv in a wrong way. You use CUBLAS_OP_T, that means you will use the transposed of d_A and that should be mathematical wrong. You have A[col x raw] * x[col x 1] = y[row x 1] and that's wrong.hubs
please provide a complete code, including the call to this function as well as all variables passed to it.Robert Crovella
As you can read in the cublas documentation, x is a vector of n (column) elements, only if CUBLAS_OP_N is used. Otherwise it has m (row) elemens!hubs
I edit the code, thank you @RobertCrovellaZziggurats
A[rowcol] in c. But in cublas it will be A[colrow]. So I transpose A. Is it right?@hubsZziggurats

1 Answers

4
votes

To use two-dimensional arrays stored in row-major order in cublas (that works with column-major order) you can call the gemv in this way.

stat = cublasSgemv(handle, CUBLAS_OP_T, col, row, &alf, d_A, col, d_x, 1, &beta, d_y, 1);

You have to swap m (rows) and n (columns) in the call, too, to perform y = A * x, but it allows you to use the cublas call without transposing the original array.