# A lot of big sparse matrices

**URL:** <https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696>\
**Category:** Performance\
**Created:** [December 11, 2020, 11:43pm UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696 "2020-12-11T23:43:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [December 11, 2020, 11:43pm UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/1 "2020-12-11T23:43:06Z")

</div>

Hi everyone,

I need to get a sum of approx 10000 matrices, that are constructed by multiplication with sparse matrices.

```julia
using LinearAlgebra, SparseArrays
n = 101
p = 101
m = 25
#indK = sprand(m, n^2*p^2, 0.2) EDIT on density
indK = sprand(m, n^2*p^2, 0.01) 
Sm = rand(m, m)
o = rand(n*p)
pom = zeros(n*p, n*p)
for j=1:n*p
     pom+= indK[:, (j-1)*n*p+1:j*n*p]'*Sm*indK[:, (j-1)*n*p+1:j*n*p]*o[j]
end

```

This is extremely slow, so I tranformed the line in for loop into

```julia
mul!(pom, indK[:, (j-1)*n*p+1:j*n*p]'*Sm, indK[:, (j-1)*n*p+1:j*n*p], o[j], 1)

```

but this is still too slow for me. This is part of another loop, indK does not change, but Sm and o do.  
Any ideas on how to improve it or is this the best I can get?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 12, 2020, 12:09am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/2 "2020-12-12T00:09:02Z")

</div>

Row-wise access is bad for CSC matrices. A lot of the numbers in the columns will likely be zero.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 12, 2020, 12:09am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/3 "2020-12-12T00:09:14Z")

</div>

Note also that `.2` is not sparse. Generally for sparse methods to be effective you need sparsity of `.01` or so or lower. Also, using `@views` will make this much faster.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2020, 3:05am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/4 "2020-12-12T03:05:17Z")

</div>

> [@Avec](#):
>
> `indK = sprand(m, n^2*p^2, 0.2) `

20% sparsity is probably too large to be worthwhile using sparse matrices. Does your real problem have more sparsity?

---

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [December 12, 2020, 10:25am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/5 "2020-12-12T10:25:30Z")

</div>

The matrix in my original problem really is sparse, but its construction is too long to put it here.

---

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [December 12, 2020, 10:34am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/6 "2020-12-12T10:34:16Z")

</div>

Thanks for the note on sparsity, the matrix in my original problem is sparse, I will change the density.  
Using `@views` as

```julia
@views pom += o[j]*indK[:, (j-1)*n*p+1:j*n*p]'*Sm*indK[:, (j-1)*n*p+1:j*n*p]

```

is way slower than the version with `mul!`, even when I convert the sparse matrix into dense.

---

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [December 12, 2020, 10:45am UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/7 "2020-12-12T10:45:19Z")

</div>

Yeah, I was thinking about creating an array with matrices instead of one huge matrix, but it does not bring any improvement.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2020, 1:41pm UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/8 "2020-12-12T13:41:52Z")

</div>

> [@Avec](#):
>
> The matrix in my original problem really is sparse, but its construction is too long to put it here.

In that case, you lose the sparse data structure when you multiply by `Sm`, unless you use `Sm = sparse(rand(m,m))`… And `pom` is also not a sparse matrix.

---

<div class="post-metadata">

**Author:** ![Avec](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Avec](https://discourse.julialang.org/u/Avec)\
**Post date:** [December 12, 2020, 2:35pm UTC](https://discourse.julialang.org/t/a-lot-of-big-sparse-matrices/51696/9 "2020-12-12T14:35:07Z")

</div>

Omg, you are right, how could I miss that. Together with taking `indK` as an array of sparse matrices, this solves my problem. Thanks a lot.
