Skip to content

Commit 1da3a20

Browse files
authored
docs: clarify multithreading limitations (#923)
1 parent bde8219 commit 1da3a20

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

DifferentiationInterface/docs/src/faq/limitations.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
# Limitations
22

3+
## Multithreading
4+
5+
The preparation result `prep` is not thread-safe, since it usually contains values that are mutated by differentiation calls.
6+
Sharing it between threads may lead to unexpected behavior or errors.
7+
If you need to run differentiation concurrently, construct a separate `prep` object for each thread, for instance with the help of [OhMyThreads.jl](https://github.com/JuliaFolds2/OhMyThreads.jl).
8+
9+
Note that functions which use multithreading internally are completely fine:
10+
11+
```julia
12+
function f!(y, x)
13+
@threads for i in eachindex(y, x)
14+
y[i] = x[i]
15+
end
16+
return nothing
17+
end
18+
19+
# this is correct
20+
prep = prepare_jacobian(f!, y, backend, x)
21+
J = jacobian(f!, y, prep, backend, x)
22+
```
23+
24+
The pattern we are warning about concerns multithreading outside of the function:
25+
26+
```julia
27+
# this is incorrect
28+
prep = prepare_jacobian(f!, y, backend, x)
29+
@threads for k in 1:n
30+
# same prep object, different threads writing to it
31+
J = jacobian(f!, ys[k], prep, backend, xs[k])
32+
end
33+
```
34+
335
## Multiple active arguments
436

537
At the moment, most backends cannot work with multiple active (differentiated) arguments.

0 commit comments

Comments
 (0)