Optim.jl icon indicating copy to clipboard operation
Optim.jl copied to clipboard

Parallel ParticleSwarm?

Open sgaure opened this issue 11 months ago • 0 comments

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).

sgaure avatar Jan 18 '25 17:01 sgaure