# When shall I use \`BLAS.axpy!\` and when \`muladd\`?

**URL:** <https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466>\
**Category:** Numerics\
**Created:** [December 3, 2017, 1:46am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466 "2017-12-03T01:46:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 3, 2017, 1:46am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/1 "2017-12-03T01:46:44Z")

</div>

Can’t tell much difference between the two. Any insights? Thanks.

---

<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 3, 2017, 1:51am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/2 "2017-12-03T01:51:58Z")

</div>

I don’t understand the comparison. `axpy!` is for vectors, while `muladd` is for scalars, although of course you can do `x .= muladd.(x,y,z)` to apply it to vectors similar to `axpy!`.

I would say that 99.9% of code should not be calling low-level BLAS functions directly. If a BLAS-1 function like `axpy!` is performance-critical for you, you probably need to re-think your code anyway.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 3, 2017, 1:58am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/3 "2017-12-03T01:58:36Z")

</div>

> [@stevengj](#):
>
> I don’t understand the comparison. axpy! is for vectors, while muladd is for scalars, although of course you can do x .= muladd.(x,y,z) to apply it to vectors similar to axpy!.

Oh, sorry. Just realized that `muladd` is for scalars. What is the high-level surrogate for ‘BLAS.axpy!’ then? Can you give me a pointer?

> [@stevengj](#):
>
> I would say that 99.9% of code should not be calling low-level BLAS functions directly. If a BLAS-1 function like axpy! is performance-critical for you, you probably need to re-think your code anyway.

Why is calling low-level BLAS functions deemed a bad idea? Sorry if my question sounds stupid… Really appreciate!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 3, 2017, 4:32am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/4 "2017-12-03T04:32:22Z")

</div>

> [@chobbes](#):
>
> Why is calling low-level BLAS functions deemed a bad idea? Sorry if my question sounds stupid… Really appreciate!

Low-level BLAS calls usually are memory-bound and not compute-bound, so you’ll find that using low-level BLAS usually doesn’t even give a performance advantage over Julia (that’s not true of high-level BLAS though). `muladd` is generic, can fuse, and will be FMA on processors which it should, so it’s a great option here.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 3, 2017, 5:08am UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/5 "2017-12-03T05:08:14Z")

</div>

> [@ChrisRackauckas](#):
>
> Low-level BLAS calls usually are memory-bound and not compute-bound, so you’ll find that using low-level BLAS usually doesn’t even give a performance advantage over Julia (that’s not true of high-level BLAS though). muladd is generic, can fuse, and will be FMA on processors which it should, so it’s a great option here.

Thanks for the explanation! Get it now.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [December 3, 2017, 2:07pm UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/6 "2017-12-03T14:07:25Z")

</div>

I thought BLAS would give threading for free as compared to broadcasting `muladd`. Am I wrong?

---

<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:** [December 3, 2017, 2:22pm UTC](https://discourse.julialang.org/t/when-shall-i-use-blas-axpy-and-when-muladd/7466/7 "2017-12-03T14:22:03Z")

</div>

It does. It might or might not be useful, depending on the architecture/BLAS/weather : [Using axpy!](https://discourse.julialang.org/t/using-axpy/7072). It also might have an overhead for small sizes.
