Skip to content

Commit f6a9673

Browse files
full document build targets
1 parent 68fdbaf commit f6a9673

File tree

3 files changed

+93
-44
lines changed

3 files changed

+93
-44
lines changed

docs/src/IR.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,3 @@ rename
111111
get_variables
112112
substitute_expr!
113113
```
114-
115-
### Expression Generation and `build_function`
116-
117-
At any time, Julia expressions can be generated from ModelingToolkit IR by using
118-
`convert(Expr,x)`. This performs some cleaning to return an expression without
119-
extraneous pieces that commonly matches expressions one would write in functions
120-
like those for differential equation solvers and optimization libraries.
121-
122-
Additionally, the core compilation process of ModelingToolkit IR is `build_function`.
123-
`build_function` takes an operation or an `AbstractArray` of operations and
124-
generates a compilable version of the model for numerical solvers.
125-
126-
```@docs
127-
build_function
128-
```

docs/src/build_function.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
# Function Building and Compilation (build_function)
2+
3+
At any time, callable functions can be generated from ModelingToolkit IR by using
4+
`convert(Expr,x)`. This performs some cleaning to return an expression without
5+
extraneous pieces that commonly matches expressions one would write in functions
6+
like those for differential equation solvers and optimization libraries. These
7+
functions can be automatically parallelize and specialize on Julia types like
8+
static arrays and sparse matrices.
9+
10+
The core compilation process of ModelingToolkit IR is `build_function`.
11+
`build_function` takes an operation or an `AbstractArray` of operations and
12+
generates a compilable version of the model for numerical solvers. The form of
13+
this output is dependent on the `target`. By default, the target outputs
14+
Julia code, but other formats, such as C, Stan, and MATLAB are available.
15+
These can be generated as expressions which can then be evaluated into a callable
16+
function, or the compilers for the respective targets can be invoked to directly
17+
give back the function handle.
18+
19+
## build_function
20+
21+
```@docs
22+
build_function
23+
```
24+
25+
## Target-Specific Definitions
26+
27+
```@docs
28+
_build_function(target::JuliaTarget,args...;kwargs...)
29+
_build_function(target::CTarget,args...;kwargs...)
30+
_build_function(target::StanTarget,args...;kwargs...)
31+
_build_function(target::MATLABTarget,args...;kwargs...)
32+
```

src/build_function.jl

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,24 @@ struct DaggerForm <: ParallelForm end
1414
`build_function`
1515
1616
Generates a numerically-usable function from a ModelingToolkit `Expression`.
17-
If the `Expression` is an `Operation`, the generated function is a function
18-
with a scalar output, otherwise if it's an `AbstractArray{Operation}`, the output
19-
is two functions, one for out-of-place AbstractArray output and a second which
20-
is a mutating function. The outputted functions match the given argument order,
21-
i.e., f(u,p,args...) for the out-of-place and scalar functions and
22-
`f!(du,u,p,args..)` for the in-place version.
2317
2418
```julia
2519
build_function(ex, args...;
26-
conv = simplified_expr, expression = Val{true},
27-
checkbounds = false, convert_oop = true,
28-
force_SA = false,
29-
linenumbers = false, target = JuliaTarget())
20+
expression = Val{true},
21+
target = JuliaTarget(),
22+
kwargs...)
3023
```
3124
3225
Arguments:
3326
3427
- `ex`: The `Expression` to compile
35-
- `vs`: The variables of the expression
36-
- `ps`: The parameters of the expression
37-
- `args`: Extra arguments to the function
38-
- `conv`: The conversion function of the Operation to Expr. By default this uses
39-
the `simplified_expr` function utilized in `convert(Expr,x)`.
28+
- `args`: The arguments of the function
4029
- `expression`: Whether to generate code or whether to generate the compiled form.
4130
By default, `expression = Val{true}`, which means that the code for the
42-
function is returned. If `Val{false}`, then the returned value is a compiled
43-
Julia function, which utilizes GeneralizedGenerated.jl in order to world-age
44-
free.
31+
function is returned. If `Val{false}`, then the returned value is compiled.
4532
4633
Keyword Arguments:
4734
48-
- `checkbounds`: For whether to enable bounds checking inside of the generated
49-
function. Defaults to false, meaning that `@inbounds` is applied.
50-
- `linenumbers`: Determines whether the generated function expression retains
51-
the line numbers. Defaults to true.
52-
- `convert_oop`: Determines whether the OOP version should try to convert
53-
the output to match the type of the first input. This is useful for
54-
cases like LabelledArrays or other array types that carry extra
55-
information. Defaults to true.
56-
- `force_SA`: Forces the output of the OOP version to be a StaticArray.
57-
Defaults to `false`, and outputs a static array when the first argument
58-
is a static array.
5935
- `target`: The output target of the compilation process. Possible options are:
6036
- `JuliaTarget`: Generates a Julia function
6137
- `CTarget`: Generates a C function
@@ -182,6 +158,63 @@ function fill_array_with_zero!(x::AbstractArray)
182158
return x
183159
end
184160

161+
"""
162+
Build function target: JuliaTarget
163+
164+
```julia
165+
function _build_function(target::JuliaTarget, rhss, args...;
166+
conv = simplified_expr, expression = Val{true},
167+
checkbounds = false,
168+
linenumbers = false, multithread=nothing,
169+
headerfun = addheader, outputidxs=nothing,
170+
convert_oop = true, force_SA = false,
171+
skipzeros = outputidxs===nothing,
172+
fillzeros = skipzeros && !(typeof(rhss)<:SparseMatrixCSC),
173+
parallel=SerialForm(), kwargs...)
174+
```
175+
176+
Generates a Julia function which can then be utilized for further evaluations.
177+
If expression=Val{false}, the return is a Julia function which utilizes
178+
GeneralizedGenerated.jl in order to be free of world-age issues.
179+
180+
If the `Expression` is an `Operation`, the generated function is a function
181+
with a scalar output, otherwise if it's an `AbstractArray{Operation}`, the output
182+
is two functions, one for out-of-place AbstractArray output and a second which
183+
is a mutating function. The outputted functions match the given argument order,
184+
i.e., f(u,p,args...) for the out-of-place and scalar functions and
185+
`f!(du,u,p,args..)` for the in-place version.
186+
187+
Special Keyword Argumnets:
188+
189+
- `parallel`: The kind of parallelism to use in the generated function. Defaults
190+
to `SerialForm()`, i.e. no parallelism. Note that the parallel forms are not
191+
exported and thus need to be chosen like `ModelingToolkit.SerialForm()`.
192+
The choices are:
193+
- `SerialForm()`: Serial execution.
194+
- `MultithreadedForm()`: Multithreaded execution with a static split, evenly
195+
splitting the number of expressions per thread.
196+
- `DistributedForm()`: Multiprocessing using Julia's Distributed with a static
197+
schedule, evenly splitting the number of expressions per process.
198+
- `DaggerForm()`: Multithreading and multiprocessing using Julia's Dagger.jl
199+
for dynamic scheduling and load balancing.
200+
- `conv`: The conversion function of the Operation to Expr. By default this uses
201+
the `simplified_expr` function utilized in `convert(Expr,x)`.
202+
- `checkbounds`: For whether to enable bounds checking inside of the generated
203+
function. Defaults to false, meaning that `@inbounds` is applied.
204+
- `linenumbers`: Determines whether the generated function expression retains
205+
the line numbers. Defaults to true.
206+
- `convert_oop`: Determines whether the OOP version should try to convert
207+
the output to match the type of the first input. This is useful for
208+
cases like LabelledArrays or other array types that carry extra
209+
information. Defaults to true.
210+
- `force_SA`: Forces the output of the OOP version to be a StaticArray.
211+
Defaults to `false`, and outputs a static array when the first argument
212+
is a static array.
213+
- `skipzeros`: Whether to skip filling zeros in the in-place version if the
214+
filling function is 0.
215+
- `fillzeros`: Whether to perform `fill(out,0)` before the calculations to ensure
216+
safety with `skipzeros`.
217+
"""
185218
function _build_function(target::JuliaTarget, rhss, args...;
186219
conv = simplified_expr, expression = Val{true},
187220
checkbounds = false,

0 commit comments

Comments
 (0)