# Solves the linear system using CuArrays.jl

**URL:** <https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692>\
**Category:** GPU\
**Created:** [December 25, 2019, 1:23pm UTC](https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692 "2019-12-25T13:23:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FujiwaraTakumiEH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fujiwaratakumieh/32/37975_2.png) [@FujiwaraTakumiEH](https://discourse.julialang.org/u/FujiwaraTakumiEH)\
**Post date:** [December 25, 2019, 1:23pm UTC](https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692/1 "2019-12-25T13:23:54Z")

</div>

I am new to GPU parallel computing. With my knowledge of **CUSOLVER** and **CUSPARSE** , I am sure that I can complete my task through them: Solving large linear sparse equations in parallel.

I have read the documentation of `CUDA.jl`, part of the code of `CuArray.jl` (a bit difficult for me 😅), and the official manual of **CUDA** : `CUSOLVER LIBRARY`. The following part is my code：

```julia
# A * x = b

n = 10

A = sprand(Float32, n, n, 0.5)
A = sparse(A*A')
d_A = CuArrays.CUSPARSE.CuSparseMatrixCSR(A)

b = rand(Float32, n)
d_b = CuArray(b)

x = zeros(Float32, n)
d_x = CuArray(x)

tol = convert(real(Float32), 1e-4)
d_x = CUSOLVER.csrlsvqr!(d_A, d_b, d_x, tol, one(Cint), 'O')
h_x = collect(d_x)

h_x ≈ Array(A)\b

> true

```

The result returned by the code is `true`.

But as the value of `n` increases (e.g. `n = 1000`), the results are always `false`. I would like to ask, why are the calculation results different? 😀

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [December 25, 2019, 2:38pm UTC](https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692/2 "2019-12-25T14:38:35Z")

</div>

Could it be due to bad conditioning in the matrix?

---

<div class="post-metadata">

**Author:** ![FujiwaraTakumiEH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fujiwaratakumieh/32/37975_2.png) [@FujiwaraTakumiEH](https://discourse.julialang.org/u/FujiwaraTakumiEH)\
**Post date:** [December 27, 2019, 11:28am UTC](https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692/3 "2019-12-27T11:28:25Z")

</div>

Thank you for your reply. 😁

I want to use the random sparse matrix generated in the above way to solve it in parallel on the GPU. It is only a test, and I will use the actual problem to observe its calculation results.

Besides, I’m not sure if the parameters (such as `tol`, etc.) in the function `CUSOLVER.csrlsvqr!(...)` have any effect on the calculation results? Or how should I choose more suitable parameters based on the actual problem?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [December 27, 2019, 1:18pm UTC](https://discourse.julialang.org/t/solves-the-linear-system-using-cuarrays-jl/32692/4 "2019-12-27T13:18:58Z")

</div>

there is a “big” proba that `A` is non invertible. Try to invert `I + A`.
