Reputation: 1023
I am trying to write a simple example of matrix multiplication using cuBLAS function cublasSgemm. My code is shown below:
int m =100, n = 100;
float * bold1 = new float [m*n];
float * bold2 = new float [m*n];
float * bold3 = new float [m*n];
for (int i = 0; i< m; i++)
for(int j = 0; j <n;j++)
{
bold1[i*n+j]=rand()%10;
bold2[i*n+j]=rand()%10;
}
cudaError_t cudaStat;
cublasStatus_t stat;
cublasHandle_t handle;
const float alpha = 1.0;
const float beta = 0;
float * dev_bold1, * dev_bold2, *dev_bold3;
cudaStat = cudaMalloc ((void**)&bold1, sizeof(float)*m*n);
if(cudaStat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem1";
return cudaStat;
}
cudaStat = cudaMalloc ((void**)&bold2,sizeof(float)*m*n);
if(cudaStat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem2";
return cudaStat;
}
cudaStat = cudaMalloc ((void**)&bold3,sizeof(float)*m*n);
if(cudaStat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem3";
return cudaStat;
}
cublasSetMatrix(m,n,sizeof(float),bold1,m,dev_bold1,m);
cublasSetMatrix(m,n,sizeof(float),bold2,m,dev_bold2,m);
stat = cublasCreate(&handle);
if(stat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem4";
return stat;
}
cout<<stat<<" "<<CUBLAS_STATUS_SUCCESS<<"\n";
stat = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, m, n ,&alpha, dev_bold1, n, dev_bold2, n, &beta,dev_bold3,m);
if (stat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem5";
return stat;
}
cudaStat = cudaMemcpy(bold3,dev_bold3,sizeof(float)*m*n,cudaMemcpyDeviceToHost);
if (cudaStat != cudaSuccess)
{
cout<<"problem6";
return cudaStat;
}
delete []bold1;
delete []bold2;
cudaFree(dev_bold1);
cudaFree(dev_bold2);
cudaFree(dev_bold3);
In this code, I want to multiply to matrices bold1 and bold2 which are filled with random numbers. The code return "problem 5" which is related to this part of code:
stat = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, m, n ,&alpha, dev_bold1, n, dev_bold2, n, &beta,dev_bold3,m);
if (stat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem5";
return stat;
}
I also print stat and it shows "13"!
Can anybody please help me to understand what is the problem with my code? Thanks!
Upvotes: 0
Views: 244
Reputation: 151829
The main mistake is in your cudaMalloc
statements, you are allocating the wrong pointer:
float * dev_bold1, * dev_bold2, *dev_bold3;
cudaStat = cudaMalloc ((void**)&bold1, sizeof(float)*m*n);
^
|
this should be dev_bold1
and likewise for the other 2 cudaMalloc
statements.
The following code has those errors fixed, and returns no runtime errors:
$ cat t1235.cu
#include <cublas_v2.h>
#include <iostream>
using namespace std;
int main(){
int m =100, n = 100;
float * bold1 = new float [m*n];
float * bold2 = new float [m*n];
float * bold3 = new float [m*n];
for (int i = 0; i< m; i++)
for(int j = 0; j <n;j++)
{
bold1[i*n+j]=rand()%10;
bold2[i*n+j]=rand()%10;
}
cudaError_t cudaStat;
cublasStatus_t stat;
cublasHandle_t handle;
const float alpha = 1.0;
const float beta = 0;
float * dev_bold1, * dev_bold2, *dev_bold3;
cudaStat = cudaMalloc ((void**)&dev_bold1, sizeof(float)*m*n);
if(cudaStat != cudaSuccess)
{
cout<<"problem1";
return cudaStat;
}
cudaStat = cudaMalloc ((void**)&dev_bold2,sizeof(float)*m*n);
if(cudaStat != cudaSuccess)
{
cout<<"problem2";
return cudaStat;
}
cudaStat = cudaMalloc ((void**)&dev_bold3,sizeof(float)*m*n);
if(cudaStat != cudaSuccess)
{
cout<<"problem3";
return cudaStat;
}
cublasSetMatrix(m,n,sizeof(float),bold1,m,dev_bold1,m);
cublasSetMatrix(m,n,sizeof(float),bold2,m,dev_bold2,m);
stat = cublasCreate(&handle);
if(stat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem4";
return stat;
}
cout<<stat<<" "<<CUBLAS_STATUS_SUCCESS<<"\n";
stat = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, m, n ,&alpha, dev_bold1, n, dev_bold2, n, &beta,dev_bold3,m);
if (stat != CUBLAS_STATUS_SUCCESS)
{
cout<<"problem5";
return stat;
}
cudaStat = cudaMemcpy(bold3,dev_bold3,sizeof(float)*m*n,cudaMemcpyDeviceToHost);
if (cudaStat != cudaSuccess)
{
cout<<"problem6";
return cudaStat;
}
delete []bold1;
delete []bold2;
cudaFree(dev_bold1);
cudaFree(dev_bold2);
cudaFree(dev_bold3);
return 0;
}
$ nvcc -o t1235 t1235.cu -lcublas
$ cuda-memcheck ./t1235
========= CUDA-MEMCHECK
0 0
========= ERROR SUMMARY: 0 errors
$
I also changed a few of your error checking statements where you were checking cuda error return values against cublas error types.
Upvotes: 1