I want to make Ax=b symmetric sparse matrix solver

I wrote the code as follows, but only the value of 0 comes out. What’s the problem?
Thank you for your help

A=
| 1 0 2 3 |
| 0 0 0 4 |
| 2 0 0 0 |
| 3 4 0 2 |
x= 1 2 3 4
b= 19 16 2 19

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cuda_runtime.h>
#include <cusolverSp.h>
#include <cusparse.h>

#include "cusolver_utils.h"

int main(int argc, char *argv) {
    cusolverSpHandle_t cusolverH = NULL;
    cusparseMatDescr_t descrA = NULL;
    cudaStream_t stream = NULL;

    int i;
    int singularity;


    int *d_csrRowPtrA = nullptr;
    int *d_csrColIndA = nullptr;
    double *d_csrValA = nullptr;
    double *d_b = nullptr; // batchSize * m
    double *d_x = nullptr; // batchSize * m
 


    const int m = 4;
    const int nnzA = 5;



    std::vector<int> csrRowPtrA = { 1, 4, 5, 5, 6 };//IA
    std::vector<int> csrColIndA = { 1, 3, 4, 4, 4 };//JA
    std::vector<double> csrValA= { 1, 2, 3, 4, 2 };//A
    std::vector<double> b = { 19, 16, 2, 19 };//soulution
    std::vector<double> x(m, 0);



    cusolverSpCreate(&cusolverH);
    cusparseCreateMatDescr(&descrA);
    cusparseSetMatType(descrA, CUSPARSE_MATRIX_TYPE_SYMMETRIC);
    cusparseSetMatIndexBase(descrA, CUSPARSE_INDEX_BASE_ONE); // base-1



    cudaMalloc(reinterpret_cast<void**>(&d_csrValA), sizeof(double) * csrValA.size());
    cudaMalloc(reinterpret_cast<void**>(&d_csrColIndA), sizeof(int) * csrColIndA.size());
    cudaMalloc(reinterpret_cast<void**>(&d_csrRowPtrA), sizeof(int) * csrRowPtrA.size());
    cudaMalloc(reinterpret_cast<void**>(&d_b), sizeof(double) * b.size());
    cudaMalloc(reinterpret_cast<void**>(&d_x), sizeof(double) * x.size());

    cudaMemcpy(d_csrValA, csrValA.data(), sizeof(double) * csrValA.size(),cudaMemcpyHostToDevice);
    cudaMemcpy(d_csrColIndA, csrColIndA.data(), sizeof(int) * csrColIndA.size(),cudaMemcpyHostToDevice);
    cudaMemcpy(d_csrRowPtrA, csrRowPtrA.data(), sizeof(int) * csrRowPtrA.size(),cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, b.data(), sizeof(double) * b.size(), cudaMemcpyHostToDevice);



    cusolverSpDcsrlsvqr(cusolverH, m, nnzA, descrA, d_csrValA, d_csrRowPtrA,d_csrColIndA, d_b, 1e-10, 0, d_x, &singularity);


    cudaMemcpy(x.data(), d_x, sizeof(double) * x.size(), cudaMemcpyDeviceToHost);

 
    for (int i = 0; i < m; i++) {
        printf("%e\n", x.data()[i]);
    }

    /* free resources */
    cudaFree(d_csrRowPtrA);
    cudaFree(d_csrColIndA);
    cudaFree(d_csrValA);
    cudaFree(d_b);
    cudaFree(d_x);
    cusolverSpDestroy(cusolverH);
    cudaDeviceReset();

  

    return true;
}

FWIW, your code, as posted, will not compile.

Any time you are having trouble with a CUDA library code, its good practice to:

  • check the return value of every CUDA function call
  • check the return value of every CUDA library function call

If you do that here, you will get useful information.

You may also wish to carefully read the documentation for the function you are using, as it explains the error.

I modify the post. thank you

You have modified it, but it still doesn’t compile.

That isn’t the important part of my previous message.

You may wish to study the remainder of my previous post, after the first sentence.