Is ForwardDiff thread safe?


#1

Can I use @threads with ForwardDiff.jl ?


#2

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.


#3

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


#4

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.