# IterativeSolvers.jl and DistributedArrays.jl

**URL:** https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175
**Category:** Numerics
**Created:** [June 1, 2021, 6:39am UTC](https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175 "2021-06-01T06:39:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [June 1, 2021, 6:39am UTC](https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175/1 "2021-06-01T06:39:12Z")

</div>

I would like to use the iterative solvers from IterativeSolvers.jl with DistributedArrays.jl, with no luck for now.

Here is a MWE, where I try to invert a diagonal positive definite matrix using the CG algorithm :

```julia
using LinearAlgebra, IterativeSolvers, DistributedArrays
n = 100
A = Diagonal(drand(n) .^ 2 .+ √eps())
b = drand(n)
cg(A, b)

```

The error message mentions some ambiguities in the methods definition, and proposes to implement the following method

copyto!(::DistributedArrays.DArray, ::Base.Broadcast.Broadcasted{T, Axes, F, Args} where {T\<:Base.Broadcast.AbstractArrayStyle{0}, Axes, F, Args\<:Tuple})

If anyone encountered this issue previously, any tips on how to do this would be greatly appreciated. Thanks!

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [June 1, 2021, 9:38am UTC](https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175/2 "2021-06-01T09:38:45Z")

</div>

Note this doesn’t work with KrylovKit either:

```julia

using LinearAlgebra, KrylovKit, DistributedArrays
n = 100
b = drand(n)
linsolve(x -> b .* x, b, b)

```

complains about `strides(DArray)`. I’d open an issue in the relevant packages.

---

<div class="post-metadata">

### Author: ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)
#### Post date: [June 1, 2021, 12:13pm UTC](https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175/3 "2021-06-01T12:13:09Z")

</div>

@antoine-levitt Sure, I did just that [here](https://github.com/JuliaLinearAlgebra/IterativeSolvers.jl/issues/298), thanks.

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [June 1, 2021, 3:24pm UTC](https://discourse.julialang.org/t/iterativesolvers-jl-and-distributedarrays-jl/62175/4 "2021-06-01T15:24:41Z")

</div>

In fact the problem is not too bad:

```julia
  [1] stride(A::DArray{Float64, 1, Vector{Float64}}, k::Int64)
    @ Base ./abstractarray.jl:501
  [2] axpby!(alpha::Float64, x::DArray{Float64, 1, Vector{Float64}}, beta::Float64, y::DArray{Float64, 1, Vector{Float64}})
    @ LinearAlgebra.BLAS /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/blas.jl:649
  [3] _rmul!(b::KrylovKit.OrthonormalBasis{DArray{Float64, 1, Vector{Float64}}}, G::LinearAlgebra.Givens{Float64})
    @ KrylovKit ~/.julia/packages/KrylovKit/OLgKs/src/dense/givens.jl:33

```

so the problem is that DArrays does not support axpby!. We can fix this:

```julia
import LinearAlgebra
LinearAlgebra.axpby!(alpha::Float64, x::DArray{Float64, 1, Vector{Float64}}, beta::Float64, y::DArray{Float64, 1, Vector{Float64}}) = axpby!(alpha, x.localpart, beta, y.localpart)

```

which works! This should go into a PR on DistributedArrays, but it looks like that package is not actively maintained…
