I am a newbie to C++ and Cuda , please excuse me for this reason if my question is silly, but i would like to learn.
Here on the code below I try to apply one cusolver routine cusolverDnDgesvd to a rectangular matrix of sizes 12 by 8 for singular value decomposition. I would like to factorize the matrix into 3 matrixes with sizes MK (U) , KK ( S) , K*N (Vt) I also do all error check for memory allocations and for calls of the routines.
Somehow cusolverDnDgesvd fails to work and when I run the code , this is printed on the console.
devInfo=-6
Unsuccessful SVD execution
devInfo=-11
Unsuccessful SVD execution
I could not find out what are those negative values representing, would appreciate any help. Thanks!
int main()
{
int M = 12; int N = 8;
int K = 4; int lda = M;
int ldc= K; int work_size = 0;
int *devInfo;
gpuErrchk(cudaMalloc(&devInfo,sizeof(int)));
// initialization of matrix
int i;
int j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
h_V[j*M+i] = i*j;
}
}
}
// allocation for matrix in device
double *d_V;
CudaSafeCall(cudaMemcpy(d_V, h_V, M*N*sizeof(double), cudaMemcpyHostToDevice));
//host side SVD for U, V and S
//For data matrix V
double *h_U = (double *)malloc(M * K * sizeof(double));
double *h_Vt = (double *)malloc(K * N * sizeof(double));
double *h_S = (double *)malloc(std::min(M, K) * sizeof(double));
// device side SVD for U, V And S
double *d_U;
gpuErrchk(cudaMalloc((void**)&d_U, M * K * sizeof(double)));
gpuErrchk(cudaMemcpy(d_U, h_U, M * K * sizeof(double), cudaMemcpyHostToDevice));
double *d_Vt;
gpuErrchk(cudaMalloc((void**)&d_Vt, K * N * sizeof(double)));
gpuErrchk(cudaMemcpy(d_Vt, h_Vt, K * N * sizeof(double), cudaMemcpyHostToDevice));
double *d_S;
gpuErrchk(cudaMalloc((void**)&d_S, std::min(N, K) * sizeof(double)));
gpuErrchk(cudaMemcpy(d_S, h_S, std::min(N, K) * sizeof(double), cudaMemcpyHostToDevice));
// CUDA solver initialization
cusolverStatus_t status;
cusolverDnHandle_t solver_handle;
status = cusolverDnCreate(&solver_handle);
if(CUSOLVER_STATUS_SUCCESS != status) {
fprintf(stderr, "CUSOLVE error in file '%s', line %d\n %s\n error %d: %s\n terminating!\n",__FILE__, __LINE__,status,_cudaGetErrorEnum(status));
cudaDeviceReset(); assert(0);
}
// CUDA SVD initialization
// determination of the needed workspace with cusolverDnDgesvd
status = cusolverDnDgesvd_bufferSize(solver_handle, M, N, &work_size);
if(CUSOLVER_STATUS_SUCCESS != status) {
fprintf(stderr, "CUSOLVE error in file '%s', line %d\n %s\n error %d: %s\n terminating!\n",__FILE__, __LINE__,status,_cudaGetErrorEnum(status));
cudaDeviceReset(); assert(0);
}
double *work;
gpuErrchk(cudaMalloc(&work, work_size * sizeof(double)));
if(CUSOLVER_STATUS_SUCCESS != status) {
fprintf(stderr, "CUSOLVE error in file '%s', line %d\n %s\n error %d: %s\n terminating!\n",__FILE__, __LINE__,status, \
_cudaGetErrorEnum(status)); \
cudaDeviceReset(); assert(0); \
}
// CUDA SVD execution
status = cusolverDnDgesvd(solver_handle, 'A', 'A', M, N, d_A, ldc, d_S, d_U, lda, d_V, ldc, work, work_size, NULL, devInfo);
cudaDeviceSynchronize();
int devInfo_h = 0;
gpuErrchk(cudaMemcpy(&devInfo_h, devInfo, sizeof(int), cudaMemcpyDeviceToHost));
std::cout << "devInfo=" << devInfo_h << "\n";
if (devInfo_h!= 0)
std::cout << "Unsuccessful SVD execution\n";
cudaFree(d_V);
cudaFree(d_S);
cudaFree(d_Vt);
cudaFree(d_U);
free(h_S);
free(h_Vt);
free(h_U);
free(h_V);