Search code examples
parallel-processinggenetic-algorithmopenmdao

OpenMDAO genetic algorithm not running in parallel?


I am setting up an optimisation in openMDAO with the SimpleGADriver, where the points in a generation are executed in parallel.

As I am new to openMDAO and relatively new to parallel computing, I started experimenting with the simple example in the openMDAO documentation: https://openmdao.org/newdocs/versions/latest/features/building_blocks/drivers/genetic_algorithm.html#running-a-ga-in-parallel

However, when I run this example script I suspect it is not executing any tasks in parallel.

I tried printing debug messages using prob.driver.options['debug_print'] = ['objs'], but it seems the optimisation is only being executed on a single rank (rank0), whereas I would expect it to be performed on more than one after setting prob.driver.options['run_parallel'] = True :

Driver debug print for iter coord: rank0:SimpleGA|528
-----------------------------------------------------
Objectives
{'comp.f': array([0.49454471])}

Additionally, I increased the computational time of each Branin function evaluation by forcing it to wait with a time.sleep() command before returning the output. In that way the optimisation should definitely benefit from parallellisation, and I'd expect it to run a lot faster when the 'run_parallel' option is set to True. However, the elapsed time is approximately the same when the parallel option is disabled.

I am using python 3.9 and openmdao v3.25 on a Windows 10 PC, and I have MPI and mpi4py installed in my conda environment.

I have checked out similar threads on Stack Overflow but I haven't found a solution yet. Am I missing something here? Any help or advice is highly appreciated!


Solution

  • It is not enough to install mpi and mpi4py to your conda environment. You also need an underlying MPI implementation (e.g. mpich, OpenMPI or Microsoft MPI). OpenMDAO also requires PETSc to run in parallel under MPI.

    For example, I tried running the GA example in parallel without having installed PETSc and I got the following error:

    (OM) PS D:\OpenMDAO> mpiexec -n 4 python .\ga.py
    Traceback (most recent call last):
      File "D:\OpenMDAO\ga.py", line 19, in <module>
        prob.setup()
      File "C:\Users\swryan\miniconda3\envs\OM\Lib\site-packages\openmdao\core\problem.py", line 924, in setup
        raise ValueError(f"{self.msginfo}: Attempting to run in parallel under MPI but "
    ValueError: Problem ga: Attempting to run in parallel under MPI but PETScVector could not be imported.
    

    Installing PETSc on windows can be difficult as documented here on the PETSc web site.

    The consensus of OpenMDAO users who operate on Windows seems to be that the best solution is to use Windows Subsystem for Linux (WSL2) to give you a Linux environment that works pretty seamlessly with Windows, especially with Visual Studio Code.

    In the Linux environment it is a simple matter to conda install -c conda-forge mpi4py petsc4py or conda install -c conda-forge openmpi mpi4py petsc4py