Not able to get the cublasSgelsbatched function to work

  c++, cublas, cuda

I’m currently trying to get the cublasSgelsbatched (https://docs.nvidia.com/cuda/cublas/index.html) version to work. I started by first making a small test case to see what parameters are needed exactly and how they need to be inputted. However after much trial and error I still can’t get it to work, I get a status return of 13, which corresponds to CUBLAS_STATUS_EXECUTION_FAILED which is a very vague error, also I tried some other cublas testcases and they seem to be working fine. I also tested the input matrix in MATlab, which does have a LS solution.

#include "stdafx.h"
#include "device_launch_parameters.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include "cublas_v2.h"

#include <algorithm>
#include <cmath>
#include <Windows.h>

int main()
{
    //init id, handle and stat
    int id = cudaGetDevice(&id);
    cublasHandle_t m_cuBLAS;
    cublasStatus_t stat;

    // create handle
    stat = cublasCreate(&m_cuBLAS);

    //params
    const int C = 3; 
    const int M = 2;
    long lda = C;
    long ldb = M;

    //init variables
    float *Amat, *Ymat, *Xmat;
    float *gAmat, *gYmat;

    //allocate mem
    Amat = (float*) malloc(M * C * sizeof(float));
    Ymat = (float*) malloc(C *  sizeof(float));
    Xmat = (float*) malloc(M *  sizeof(float));

    srand(100);

    for (int i = 0; i < C * M; i++) {
        Amat[i] = rand() % 10 + 1;
        Amat[i] = (float)Amat[i];
    }

    for (int i = 0; i < C; i++) {
        Ymat[i] =  rand() % 10 + 1;
        Ymat[i] = (float)Ymat[i];
    }

    //allocate mem
    cudaMalloc( &gAmat, M * C * sizeof(float));
    cudaMalloc( &gYmat, C * sizeof(float));

    //copy mem
    cudaMemcpy(gAmat, Amat, M * C * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(gYmat, Ymat, C * 1 * sizeof(float), cudaMemcpyHostToDevice);

    //init info params
    int info = 0;
    int devInfoArray[1] = { 0 };

    //Synchronize (not necesarry I think, but just to test)
    cudaDeviceSynchronize();
    

    //run cublas
    cublasStatus_t status = cublasSgelsBatched(m_cuBLAS,
        CUBLAS_OP_N,
        C,
        M,
        1,
        &gAmat,
        lda, //or 1
        &gYmat,
        lda,
        &info,
        NULL,
        1);
    
    //Output info
    std::cout << "status = " << status << std::endl;
    std::cout << "info = " << info << std::endl;
    std::cout << "devInfoArray = " << devInfoArray[0] << std::endl;

    cudaMemcpy(Xmat, gYmat, C * 1 * sizeof(float), cudaMemcpyDeviceToHost);

    //Output printed
    std::cout << Xmat[0] << ", " << Xmat[1] << ", " << Xmat[2] << std::endl;

    //free memory
    free(Amat);
    free(Ymat);
    free(Xmat);

    cudaFree(gAmat);
    cudaFree(gYmat);

    //destory handle
    cublasDestroy(m_cuBLAS);

    return 0;
}

I’m on Windows 10 running in MVS using CUDA 9.0

I’d really appreciate some help

Source: Windows Questions C++

LEAVE A COMMENT