You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a problem which benefits greatly by evaluating the function on the swarm in parallel. I have a local version of Optim which simply adds a parallel field in the ParticleSwarm struct. If parallel is set, I call a compute_cost_parallel! instead of compute_cost.
function compute_cost!(f,
n_particles::Int,
X::Matrix,
score::Vector)
for i in 1:n_particles
score[i] = value(f, X[:, i])
end
nothing
end
function compute_cost_parallel!(f,
n_particles::Int,
X::Matrix,
score::Vector)
Polyester.@batch for i in 1:n_particles
score[i] = value(f, X[:, i])
end
nothing
end
This is all fine, though I haven't actually verified that the value call is thread safe. I might create a PR which includes this enhancement. However, it's the wrong way. It should be up to the user how to parallelize. Some might use Distributed, some might use OhMyThreads.jl, @threads, @spawn, or MPI.jl, and so on. So the loop should really be replaced by just a matrix call:
function compute_cost_parallel!(f, etc...)
value!!!(score, f, X)
return nothing
end
where the objective function f accepts a matrix X, and fills in the vector score. Is this doable with the current API?
Edit: I figured it out. It's just value(f, score, X).
The text was updated successfully, but these errors were encountered:
I have a problem which benefits greatly by evaluating the function on the swarm in parallel. I have a local version of
Optim
which simply adds aparallel
field in theParticleSwarm
struct. Ifparallel
is set, I call acompute_cost_parallel!
instead ofcompute_cost
.This is all fine, though I haven't actually verified that the
value
call is thread safe. I might create a PR which includes this enhancement. However, it's the wrong way. It should be up to the user how to parallelize. Some might useDistributed
, some might useOhMyThreads.jl
,@threads
,@spawn
, orMPI.jl
, and so on. So the loop should really be replaced by just a matrix call:where the objective function
f
accepts a matrixX
, and fills in the vectorscore
. Is this doable with the current API?Edit: I figured it out. It's just
value(f, score, X)
.The text was updated successfully, but these errors were encountered: