# How to utilize all cores when operating on large matrices

**URL:** https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650
**Category:** General Usage
**Created:** [May 2, 2018, 7:39am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650 "2018-05-02T07:39:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [May 2, 2018, 7:39am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650/1 "2018-05-02T07:39:06Z")

</div>

I have a relatively large matrix (approximate size 50000x50000) that I need to bulk divide by numbers in a separate matrix of the same shape. The code does something simple like

```julia
V = A ./ B

```

It seems to utilize only a single core. Is there any way to make it use some number of cores to speed things up?

I tried setting `JULIA_NUM_THREADS` to 4 but it doesn’t seem to do anything.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 2, 2018, 8:57am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650/2 "2018-05-02T08:57:36Z")

</div>

I am not sure you will get any good speedup since an operation like this tends to be memory bounds.

You could perhaps play around with stuff like [c++ - Element-wise vector-vector multiplication in BLAS? - Stack Overflow](https://stackoverflow.com/questions/7621520/element-wise-vector-vector-multiplication-in-blas) but that is only for multiplication, and from my testing, it seemed slower anyway.

---

<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: [May 2, 2018, 9:20am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650/3 "2018-05-02T09:20:32Z")

</div>

> [@kristoffer.carlsson](#):
>
> I am not sure you will get any good speedup since an operation like this tends to be memory bounds.

IIUC (not an expert), this strongly depends on the architecture, and some machines have a memory bandwidth that scales with the number of cores.

edit: also the question is valid for more compute-bound kernels

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 2, 2018, 9:33am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650/4 "2018-05-02T09:33:20Z")

</div>

See [https://github.com/JuliaLang/julia/issues/19777](https://github.com/JuliaLang/julia/issues/19777) for threaded broadcast in particular.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [May 2, 2018, 11:58am UTC](https://discourse.julialang.org/t/how-to-utilize-all-cores-when-operating-on-large-matrices/10650/5 "2018-05-02T11:58:16Z")

</div>

Hi,

For strongly memory bound problems (like your previous post on array sum) and on a classical Desktop you may have some speed-up (x2-4) if you have several (2-4) memory channels. It is usually not the case on a laptop. Actually a floating point division takes some cycles and multithreading could be interesting.

In any case, I understood (from the previous link) that multithreading is not implicit for broadcast (which is a good think because it is difficult to anticipate if your code will be nested in another multithreaded context).

A possible solution if you have a strong interest in defining A and B separately, would be to fuse the div operation with the algorithm that uses V. A lazy definition of V could be nice 😉

The curve corresponding to a MT+simd version your sum:

![gflops_4t](https://global.discourse-cdn.com/julialang/original/3X/c/0/c02918381060f9df3a352b3df72307a5e8f0f688.jpeg)

And the corresponding snippet:

```julia
function mysum_simd_threads(a::Vector)
    total = zero(eltype(a))
    n=length(a)
    nchunk=4
    partialsum=zeros(eltype(a),nchunk)
    chunksize=n÷nchunk
    Threads.@threads for c=1:nchunk
        imin=(c-1)*chunksize+1
        imax=imin+chunksize-1
        stotal = zero(eltype(a))
        @simd for i=imin:imax
            @inbounds stotal += a[i]
        end
        partialsum[c]=stotal
    end
    total=sum(partialsum)
    for i=nchunk*chunksize+1:n
        total+=a[i]
    end
    return total
end

```
