Skip to content

Commit e1d1a16

Browse files
fix examples
1 parent 29a87ba commit e1d1a16

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

docs/src/tutorials/code_optimization.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# [Code Optimization for Solving Nonlinear Systems](@id code_optimization)
22

3+
## Code Optimization in Julia
4+
5+
Before starting this tutorial, we recommend the reader to check out one of the
6+
many tutorials for optimization Julia code. The following is an incomplete
7+
list:
8+
9+
- [The Julia Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/)
10+
- [MIT 18.337 Course Notes on Optimizing Serial Code](https://mitmath.github.io/18337/lecture2/optimizing)
11+
- [What scientists must know about hardware to write fast code](https://viralinstruction.com/posts/hardware/)
12+
13+
User-side optimizations are important because, for sufficiently difficult problems,
14+
most time will be spent inside your `f` function, the function you are
15+
trying to solve. “Efficient solvers" are those that reduce the required
16+
number of `f` calls to hit the error tolerance. The main ideas for optimizing
17+
your nonlinear solver code, or any Julia function, are the following:
18+
19+
- Make it non-allocating
20+
- Use StaticArrays for small arrays
21+
- Use broadcast fusion
22+
- Make it type-stable
23+
- Reduce redundant calculations
24+
- Make use of BLAS calls
25+
- Optimize algorithm choice
26+
27+
We'll discuss these strategies in the context of nonlinear solvers.
28+
Let's start with small systems.
29+
330
## Optimizing Nonlinear Solver Code for Small Systems
431

532
```@example

docs/src/tutorials/getting_started.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A nonlinear system $$f(u) = 0$$ is specified by defining a function `f(u,p)`,
3232
where `p` are the parameters of the system. For example, the following solves
3333
the vector equation $$f(u) = u^2 - p$$ for a vector of equations:
3434

35-
```@example
35+
```@example 1
3636
using NonlinearSolve
3737
3838
f(u, p) = u .* u .- p
@@ -52,24 +52,24 @@ AbstractArray for automatic differentiation.
5252
To investigate the solution, one can look at the elements of the `NonlinearSolution`.
5353
The most important value is `sol.u`: this is the `u` that satisfies `f(u) = 0`. For example:
5454

55-
```@example
55+
```@example 1
5656
u = sol.u
5757
```
5858

59-
```@example
59+
```@example 1
6060
f(u, p)
6161
```
6262

6363
This final value, the difference of the solution against zero, can also be found with `sol.resid`:
6464

65-
```@example
65+
```@example 1
6666
sol.resid
6767
```
6868

6969
To know if the solution converged, or why the solution had not converged we can check the return
7070
code (`retcode`):
7171

72-
```@example
72+
```@example 1
7373
sol.retcode
7474
```
7575

@@ -84,7 +84,7 @@ SciMLBase.successful_retcode(sol)
8484
If we're curious about what it took to solve this equation, then you're in luck because all of the
8585
details can be found in `sol.stats`:
8686

87-
```@example
87+
```@example 1
8888
sol.stats
8989
```
9090

@@ -96,15 +96,15 @@ While `sol = solve(prob)` worked for our case here, in many situations you may n
9696
deeply with how the solving process is done. First things first, you can specify the solver using the
9797
positional arguments. For example, let's set the solver to `TrustRegion()`:
9898

99-
```@example
99+
```@example 1
100100
solve(prob, TrustRegion())
101101
```
102102

103103
For a complete list of solver choices, see [the nonlinear system solvers page](@ref nonlinearsystemsolvers).
104104

105105
Next we can modify the tolerances. Here let's set some really low tolerances to force a tight solution:
106106

107-
```@example
107+
```@example 1
108108
solve(prob, TrustRegion(), reltol=1e-12, abstol=1e-12)
109109
```
110110

@@ -118,7 +118,7 @@ For scalar rootfinding problems, bracketing methods exist in NonlinearSolve. The
118118
methods is that with bracketing methods, instead of giving a `u0` initial condition, you pass a `uspan (a,b)`
119119
bracket in which the zero is expected to live. For example:
120120

121-
```@example
121+
```@example 1
122122
using NonlinearSolve
123123
f(u, p) = u * u - 2.0
124124
uspan = (1.0, 2.0) # brackets
@@ -130,7 +130,7 @@ All of the same option handling from before works just as before, now just with
130130
(see the [bracketing solvers](@ref bracketing) page for more details). For example, let's set the solver
131131
to `ITP()` and set a high absolute tolerance:
132132

133-
```@example
133+
```@example 1
134134
sol = solve(prob_int, ITP(), abstol = 0.01)
135135
```
136136

docs/src/tutorials/small_compile.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# Faster Startup and and Static Compilation
1+
# Faster Startup and and Static Compilation
2+
3+
This is a stub article to be completed soon.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# [More Detailed Termination Conditions](@id termination_conditions_tutorial)
1+
# [More Detailed Termination Conditions](@id termination_conditions_tutorial)
2+
3+
This is a stub article to be completed soon.

0 commit comments

Comments
 (0)