Cublas Matrix LU分解

时间:2014-03-19 09:34:59

标签: c++ cuda matrix-inverse

我在cuda中调用dgetrf时遇到了一些麻烦。根据我的发现,我只能调用批量版本(http://docs.nvidia.com/cuda/cublas/#cublas-lt-t-gt-getrfbatched)。当我调用它时,我得到一个返回的错误值7,我无法找到该错误代码的相应枚举。以下是我的代码,非常感谢任何帮助;

void cuda_matrix_inverse (int m, int n, double* a){

    cublasHandle_t handle;
    cublasStatus_t status;
    double **devPtrA = 0;
    double **devPtrA_dev = NULL;
    int *d_pivot_array;
    int *d_info_array;
    int rowsA = m;
    int colsA = n;
    int matrixSizeA;
    cudaError_t error;

    fprintf(stderr,"starting cuda inverse\n");

    error = cudaMalloc((void **)&d_pivot_array, sizeof(int));
    if (error != cudaSuccess) fprintf(stderr,"\nError: %s\n",cudaGetErrorString(error));
    error = cudaMalloc((void **)&d_info_array, sizeof(int));
    if (error != cudaSuccess) fprintf(stderr,"\nError: %s\n",cudaGetErrorString(error));

    fprintf(stderr,"malloced pivot and info\n");

    status = cublasCreate(&handle);
    if (status != CUBLAS_STATUS_SUCCESS) fprintf(stderr,"error %i\n",status);

    matrixSizeA = rowsA * colsA;

    devPtrA =(double **)malloc(1 * sizeof(*devPtrA));

    fprintf(stderr,"malloced devPtrA\n");

    error = cudaMalloc((void **)&devPtrA[0], matrixSizeA * sizeof(devPtrA[0][0]));
    if (error != cudaSuccess) fprintf(stderr,"\nError: %s\n",cudaGetErrorString(error));

    error = cudaMalloc((void **)&devPtrA_dev, 1 * sizeof(*devPtrA));
    if (error != cudaSuccess) fprintf(stderr,"\nError: %s\n",cudaGetErrorString(error));

    fprintf(stderr,"malloced device variables\n");

    error = cudaMemcpy(devPtrA_dev, devPtrA, 1 * sizeof(*devPtrA), cudaMemcpyHostToDevice);
    if (error != cudaSuccess) fprintf(stderr,"\nError: %s\n",cudaGetErrorString(error));

    fprintf(stderr,"copied from devPtrA to d_devPtrA\n");

    status = cublasSetMatrix(rowsA, colsA, sizeof(a[0]), a, rowsA, devPtrA[0], rowsA);
    if (status != CUBLAS_STATUS_SUCCESS) fprintf(stderr,"error %i\n",status);


    status = cublasDgetrfBatched(handle, m, devPtrA_dev,m,d_pivot_array,d_info_array,1); //cannot get this to work
    if (status != CUBLAS_STATUS_SUCCESS) fprintf(stderr,"error in dgetrf %i\n",status);


    fprintf(stderr,"done with cuda inverse\n");
}

1 个答案:

答案 0 :(得分:3)

cublas的错误代码7表示CUBLAS_STATUS_INVALID_VALUE。对于方形矩阵,Cublas中的矩阵求逆是可能的,所以我假设m == n在你的情况下。话虽这么说,函数cublas<t>getrfBatched要求每个矩阵的数组长度为n,因此您应该将d_pivot_array分配为:

error = cudaMalloc((void **)&d_pivot_array, n * sizeof(int));

为了更通用,它被分配为:

error = cudaMalloc((void **)&d_pivot_array, n * batchSize * sizeof(int));

Here是我在测试CUBLAS函数时编写的方阵反转代码。函数输入和输出是在设备上分配的float类型方阵。