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;
}