https://github.com/sciml/muladdmacro.jl
This package contains a macro for converting expressions to use muladd calls and fused-multiply-add (FMA) operations for high-performance in the SciML scientific machine learning ecosystem
https://github.com/sciml/muladdmacro.jl
fma julia julia-language julialang muladd scientific-machine-learning sciml
Last synced: about 2 months ago
JSON representation
This package contains a macro for converting expressions to use muladd calls and fused-multiply-add (FMA) operations for high-performance in the SciML scientific machine learning ecosystem
- Host: GitHub
- URL: https://github.com/sciml/muladdmacro.jl
- Owner: SciML
- License: other
- Created: 2017-07-31T09:51:01.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-11-22T09:18:25.000Z (6 months ago)
- Last Synced: 2025-03-29T10:05:16.586Z (about 2 months ago)
- Topics: fma, julia, julia-language, julialang, muladd, scientific-machine-learning, sciml
- Language: Julia
- Homepage: https://docs.sciml.ai/MuladdMacro/stable/
- Size: 424 KB
- Stars: 43
- Watchers: 5
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Citation: CITATION.bib
Awesome Lists containing this project
README
# MuladdMacro.jl
[](https://julialang.zulipchat.com/#narrow/stream/279055-sciml-bridged)
[](https://docs.sciml.ai/MuladdMacro/stable/)[](https://codecov.io/gh/SciML/MulAddMacro.jl?branch=master)
[](https://github.com/SciML/MulAddMacro.jl/actions?query=workflow%3ACI%20branch%3Amaster)[](https://github.com/SciML/ColPrac)
[](https://github.com/SciML/SciMLStyle)This package provides the `@muladd` macro. It automatically converts expressions
with multiplications and additions or subtractions to calls with `muladd` which then fuse via
FMA when it would increase the performance of the code. The `@muladd` macro
can be placed on code blocks and it will automatically find the appropriate
expressions and nest muladd expressions when necessary. In mixed expressions summands without multiplication
will be grouped together and evaluated first but otherwise the order of evaluation of multiplications and additions is not changed.## Tutorials and Documentation
For information on using the package,
[see the stable documentation](https://docs.sciml.ai/MuladdMacro/stable/). Use the
[in-development documentation](https://docs.sciml.ai/MuladdMacro/dev/) for the version of
the documentation, which contains the unreleased features.## Examples
```jldoctest
julia> using MuladdMacrojulia> @macroexpand(@muladd k3 = f(t + c3 * dt, @. uprev + dt * (a031 * k1 + a032 * k2)))
:(k3 = f((muladd)(c3, dt, t), (muladd).(dt, (muladd).(a032, k2, (*).(a031, k1)), uprev)))julia> @macroexpand(@muladd integrator.EEst = integrator.opts.internalnorm((update -
dt * (bhat1 * k1 +
bhat4 * k4 +
bhat5 * k5 +
bhat6 * k6 +
bhat7 * k7 +
bhat10 * k10)) ./
@. (integrator.opts.abstol +
max(abs(uprev),
abs(u)) * integrator.opts.reltol)))
:(integrator.EEst = integrator.opts.internalnorm((muladd)(-dt, (muladd)(bhat10, k10, (muladd)(bhat7, k7, (muladd)(bhat6, k6, (muladd)(bhat5, k5, (muladd)(bhat4, k4, bhat1 * k1))))), update) ./ (muladd).(max.(abs.(uprev), abs.(u)), integrator.opts.reltol, integrator.opts.abstol)))
```## Broadcasting
A `muladd` call will be broadcasted if both the `*` and the `+` or `-` are broadcasted.
If either one is not broadcasted, then the expression will be converted to a
non-dotted `muladd`.## Limitations
Currently, `@muladd` handles only explicit calls of `+` and `*`. In particular, assignments
using `+=` or literal power such as `^2` are not supported. Thus, you need to rewrite them, e.g.```jldoctest
julia> using MuladdMacrojulia> a = 1.0;
b = 2.0;
c = 3.0;julia> @macroexpand @muladd a += b * c # does not work
:(a += b * c)julia> @macroexpand @muladd a = a + b * c # good alternative
:(a = (muladd)(b, c, a))julia> @macroexpand @muladd a + b^2 # does not work
:(a + b ^ 2)julia> @macroexpand @muladd a + b * b # good alternative
:((muladd)(b, b, a))
```## Credit
Most of the credit goes to @fcard and @devmotion for building the first version
and greatly refining the macro. These contributions are not directly shown as
this was developed in Gitter chats and in the DiffEqBase.jl repository, but
these two individuals did almost all of the work.