#!pip install -I "phoebe>=2.3,<2.4"

import phoebe

"""
Explanation: Advanced: Running PHOEBE in MPI
Setup
Let's first make sure we have the latest version of PHOEBE 2.3 installed (uncomment this line if running in an online notebook session such as colab).
End of explanation
"""


print(phoebe.mpi.enabled)

print(phoebe.mpi.mode)

phoebe.mpi_on()

print(phoebe.mpi.enabled)

print(phoebe.mpi.mode)

print(phoebe.mpi.myrank)

print(phoebe.mpi.nprocs)

"""
Explanation: MPI Modes
As of the 2.1 release, PHOEBE officially support parallelization using MPI within run_compute.  The 2.3 release introduced support for run_solver, including support for both MPI and multiprocessing.
There are several "modes of operation" depending on your settings and whether you're running your script within python or mpirun.  You can enable/disable MPI within phoebe by placing phoebe.mpi_on() or phoebe.mpi_off() at the top of your script.  If you do not do this, MPI will be enabled by default if within mpirun and disabled otherwise.
When MPI is enabled, PHOEBE will do the following:
* if within mpirun: 
    * run_compute: uses PHOEBE's built-in per-dataset or per-time parallelization.  The main code you write in your script is executed on a single processor, but during run_compute the task is divided among the available resources.
    * run_solver: when applicable, PHOEBE will parallelize over the individual models within the solver and serialize run_compute.  For sampler.emcee, this will only be done if nwalkers <= nprocs.
* if not within mpirun (ie. in a serial python environment): will spawn a separate thread at run_compute or run_solver, using number of processors sent to phoebe.mpi_on (for example: phoebe.mpi_on(nprocs=4)). 
When MPI is disabled, PHOEBE will do the following:
* if within mpirun: PHOEBE will run equally on all processors. The user can customize parallelization with access to phoebe.mpi.nprocs, phoebe.mpi.myrank.  Your script runs equally on each processor, meaning you have multiple (separate) copies of the bundle. USE WITH CAUTION.
* if not within mpirun (ie. in a serial python environment): 
    * run_compute: If sample_from is used, PHOEBE will make use of multiprocessing across all available processors to parallize across sampled models.  In all other cases, PHOEBE will run on a single processor in serial-mode. 
    * run_solver: PHOEBE will make use of multiprocessing across all available processors whenever the solver backend support multiprocessing, otherwise will fallback on serial-model
Accessing/Changing MPI Settings
To check the currently adopted settings, as well as quickly access information needed for manually doing your own parallelization, access the phoebe.mpi object.
End of explanation
"""


print(phoebe.mpi.within_mpirun)

"""
Explanation: PHOEBE determines whether the current script is running within an MPI environment by checking for environment variables set by mpirun/mpiexec.  If you run into any issues with PHOEBE not behaving as expected, check to see whether PHOEBE thinks its within mpirun.
End of explanation
"""


