# How to tell if A\*B hits BLAS?

**URL:** https://discourse.julialang.org/t/how-to-tell-if-a-b-hits-blas/41303
**Category:** Performance
**Tags:** linearalgebra, arrays
**Created:** [June 12, 2020, 10:04pm UTC](https://discourse.julialang.org/t/how-to-tell-if-a-b-hits-blas/41303 "2020-06-12T22:04:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [June 12, 2020, 10:04pm UTC](https://discourse.julialang.org/t/how-to-tell-if-a-b-hits-blas/41303/1 "2020-06-12T22:04:59Z")

</div>

Julia’s type system for linear algebra is quite sophisticated. I have a case where, after some `reshape` and `transpose` operations, I obtain two matrices `A,B` for which for some reason, `A*B` falls back to the slow `generic_matmul` instead of using BLAS.

Given two arrays `A, B` (which are matrices or vectors), what determines if the product `A*B` will be carried out by BLAS, instead of the fallback `generic_matmul`? Is there a way to have something like a predicate `hitsblas(A, B)`? That would be very useful debugging this situation.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [June 12, 2020, 11:31pm UTC](https://discourse.julialang.org/t/how-to-tell-if-a-b-hits-blas/41303/2 "2020-06-12T23:31:08Z")

</div>

How about something like

```julia
using Cassette
using LinearAlgebra

struct CallsGemm end

Cassette.@context CallsGemmCtx

function Cassette.overdub(::CallsGemmCtx, ::typeof(LinearAlgebra.BLAS.gemm!), ::Any...)
    throw(CallsGemm())
end

function calls_gemm(f, args...)
    try
        Cassette.overdub(CallsGemmCtx(), f, args...)
    catch e
        if e isa CallsGemm
            return true
        end
    end
    return false
end

@show calls_gemm(*, rand(3, 3), rand(3, 3))
@show calls_gemm(*, rand(10, 10), rand(10, 10))

using StaticArrays
@show calls_gemm(*, rand(SMatrix{3, 3}), rand(SMatrix{3, 3}))

```

Output:

```julia
calls_gemm(*, rand(3, 3), rand(3, 3)) = false
calls_gemm(*, rand(10, 10), rand(10, 10)) = true
calls_gemm(*, rand(SMatrix{3, 3}), rand(SMatrix{3, 3})) = false

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [June 14, 2020, 11:43am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-b-hits-blas/41303/3 "2020-06-14T11:43:53Z")

</div>

Thanks!
