Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel ParticleSwarm? #1124

Open
sgaure opened this issue Jan 18, 2025 · 0 comments
Open

Parallel ParticleSwarm? #1124

sgaure opened this issue Jan 18, 2025 · 0 comments

Comments

@sgaure
Copy link

sgaure commented Jan 18, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant