Is ForwardDiff thread safe?

Can I use @threads with ForwardDiff.jl ?

You should be able to, yes.

Keep in mind that if you manage your own work buffer state, it’ll be up to you to use that memory in a thread-safe manner.


I only need 3 gradients, so memory is not an issue. Does threadsafety also apply to reversediff?

Yes, ReverseDiff is also thread-safe.

EDIT: …in the sense that using ReverseDiff’s API functions within @threads should be fine; you may or may not run into trouble if your target function uses @threads, since ReverseDiff uses push! to push instructions to a shared tape during execution. Order of operations shouldn’t be a problem, but the nonatomicity of push! might cause a crash.

1 Like