# Blas dot doing weird things depending on size

**URL:** https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430
**Category:** Performance
**Tags:** question
**Created:** [October 29, 2019, 6:57am UTC](https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430 "2019-10-29T06:57:28Z")
**Posts on this page:** 4
**Page:** 1

<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: [October 29, 2019, 6:57am UTC](https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430/1 "2019-10-29T06:57:28Z")

</div>

I’m working on some by hand matrix multiplication.

```
function slowMult!(A::AbstractMatrix{T},B::AbstractMatrix{T}) where T<:Number
    C = Matrix{T}(undef,size(A,1),size(B,2))
    for i in 1:size(A,1)
        for j in 1:size(B,2)
            @inbounds @views C[i,j] = dot(A[i,:], B[:,j])
        end
    end
    return C
end

```

This is obviously not going to be the fastest approach, but what’s really weird is that calling this function on 2 `512` by `512` matrices is about 2x slower than on `514` by `514` matrices. The timing code is

```
T,N=Int,512
A = rand(T,N,N)
B = rand(T,N,N)
slowMult!(rand(T,1,1),rand(T,1,1)
C= @btime slowMult!($A,$B)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 29, 2019, 8:07am UTC](https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430/2 "2019-10-29T08:07:50Z")

</div>

Could be some alignment issue.

Incidentally, the first thing I would fix is memory access order:

[https://docs.julialang.org/en/v1/manual/performance-tips/#Access-arrays-in-memory-order,-along-columns-1](https://docs.julialang.org/en/v1/manual/performance-tips/#Access-arrays-in-memory-order,-along-columns-1)

---

<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: [October 29, 2019, 9:45am UTC](https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430/3 "2019-10-29T09:45:13Z")

</div>

Not sure this is the case here but looping over multiple arrays that has lengths that are a power of two can have bad effects on the cache:

[https://stackoverflow.com/questions/7905760/matrix-multiplication-small-difference-in-matrix-size-large-difference-in-timi](https://stackoverflow.com/questions/7905760/matrix-multiplication-small-difference-in-matrix-size-large-difference-in-timi)  
[http://scribblethink.org/Computer/cachekiller.html](http://scribblethink.org/Computer/cachekiller.html)  
[https://stackoverflow.com/questions/11868087/avoiding-powers-of-2-for-cache-friendliness](https://stackoverflow.com/questions/11868087/avoiding-powers-of-2-for-cache-friendliness)  
[https://stackoverflow.com/questions/8547778/why-are-elementwise-additions-much-faster-in-separate-loops-than-in-a-combined-l](https://stackoverflow.com/questions/8547778/why-are-elementwise-additions-much-faster-in-separate-loops-than-in-a-combined-l)

---

<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: [October 29, 2019, 12:56pm UTC](https://discourse.julialang.org/t/blas-dot-doing-weird-things-depending-on-size/30430/4 "2019-10-29T12:56:34Z")

</div>

Thanks so much! Now that I know about it, it should be easy enough to fix.
