Skill Tracer
Skills › Scientific Agent Skills › pymoo › parallelization.md

parallelization.md

59 lines · skills/pymoo/references/parallelization.md at 5f2bc03

Open on GitHub

Most marks come from matching words in the file, so treat them as pointers.

1# Parallel evaluation (pymoo 0.6.2)
2
3Use a runner when individual evaluations dominate overhead. `ElementwiseProblem`
4sets `elementwise=True`; no `elementwise_evaluation` argument is needed. Its
5`_evaluate` receives one candidate, while the runner schedules multiple candidates.
6Vectorized `Problem` evaluates a matrix directly and may be faster for NumPy models.
7

Parallel evaluation (pymoo 0.6.2)

Use a runner when individual evaluations dominate overhead. ElementwiseProblem sets elementwise=True; no elementwise_evaluation argument is needed. Its _evaluate receives one candidate, while the runner schedules multiple candidates. Vectorized Problem evaluates a matrix directly and may be faster for NumPy models.

Raw lines 1 to 7
8## Threads or processes
9
10```python
11from multiprocessing.pool import ThreadPool
12from pymoo.algorithms.soo.nonconvex.ga import GA
13from pymoo.core.problem import ElementwiseProblem
14from pymoo.optimize import minimize
15from pymoo.parallelization.starmap import StarmapParallelization
17class MyProblem(ElementwiseProblem):
18 def __init__(self, **kwargs):
19 super().__init__(n_var=3, n_obj=1, xl=-5, xu=5, **kwargs)
21 def _evaluate(self, x, out, *args, **kwargs):
22 out["F"] = (x**2).sum()
24if __name__ == "__main__":
25 with ThreadPool(2) as pool:
26 problem = MyProblem(elementwise_runner=StarmapParallelization(pool.starmap))
27 result = minimize(problem, GA(pop_size=20), ("n_gen", 5), seed=1)
28```
30The native test checks serial/thread equality on deterministic evaluations.
31For CPU-bound Python functions, substitute `multiprocessing.get_context("spawn").Pool(2)`
32inside the main guard. Keep worker classes/functions at importable module scope.
33That process-pool variant is illustrative; it was not executed in this refresh.
34Threads suit I/O and native code that releases the GIL. Avoid nested oversubscription
35from BLAS or model runtimes. Context managers release workers on exceptions.

Threads or processes

from multiprocessing.pool import ThreadPool
from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.core.problem import ElementwiseProblem
from pymoo.optimize import minimize
from pymoo.parallelization.starmap import StarmapParallelization

class MyProblem(ElementwiseProblem):
    def __init__(self, **kwargs):
        super().__init__(n_var=3, n_obj=1, xl=-5, xu=5, **kwargs)

    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = (x**2).sum()

if __name__ == "__main__":
    with ThreadPool(2) as pool:
        problem = MyProblem(elementwise_runner=StarmapParallelization(pool.starmap))
        result = minimize(problem, GA(pop_size=20), ("n_gen", 5), seed=1)

The native test checks serial/thread equality on deterministic evaluations. For CPU-bound Python functions, substitute multiprocessing.get_context("spawn").Pool(2) inside the main guard. Keep worker classes/functions at importable module scope. That process-pool variant is illustrative; it was not executed in this refresh. Threads suit I/O and native code that releases the GIL. Avoid nested oversubscription from BLAS or model runtimes. Context managers release workers on exceptions.

Raw lines 8 to 36
Starts an agent spawn :31
37## Joblib
39```python
40from pymoo.parallelization.joblib import JoblibParallelization
41runner = JoblibParallelization(n_jobs=2, backend="threading")
42problem = MyProblem(elementwise_runner=runner)
43result = minimize(problem, GA(pop_size=20), ("n_gen", 5), seed=1)
44```
46Install `joblib` separately. The constructor accepts `n_jobs` and joblib keyword
47arguments; it does not take a lambda that manually wraps `Parallel`. The default
48backend is joblib's process-based `loky`. Threading was executed here; distributed
49and GPU evaluation require separate validation for the user's model and resources.
51Optimizer seeds do not automatically seed stochastic external simulations or
52worker-local RNGs. Supply deterministic per-evaluation seeds when appropriate and
53record the evaluation protocol. Serialization of an algorithm does not preserve
54live pools, remote clients, or external simulator state; recreate those resources
55when resuming a checkpoint.
57Sources: [Starmap](https://pymoo.org/parallelization/starmap.html),
58[Joblib](https://pymoo.org/parallelization/joblib.html), and released 0.6.2 source;
59reviewed 2026-10-01.

Joblib

from pymoo.parallelization.joblib import JoblibParallelization
runner = JoblibParallelization(n_jobs=2, backend="threading")
problem = MyProblem(elementwise_runner=runner)
result = minimize(problem, GA(pop_size=20), ("n_gen", 5), seed=1)

Install joblib separately. The constructor accepts n_jobs and joblib keyword arguments; it does not take a lambda that manually wraps Parallel. The default backend is joblib's process-based loky. Threading was executed here; distributed and GPU evaluation require separate validation for the user's model and resources.

Optimizer seeds do not automatically seed stochastic external simulations or worker-local RNGs. Supply deterministic per-evaluation seeds when appropriate and record the evaluation protocol. Serialization of an algorithm does not preserve live pools, remote clients, or external simulator state; recreate those resources when resuming a checkpoint.

Sources: Starmap, Joblib, and released 0.6.2 source; reviewed 2026-10-01.

Raw lines 37 to 59