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

Distributed Ensembles (MPI) #156

Merged
merged 8 commits into from
Dec 16, 2023
66 changes: 57 additions & 9 deletions src/guide/running-multiple-simulations/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ Next you need to decide which data will be collected, as it is not possible to e

A short example is shown below, however you should refer to the :ref:`previous chapter<Configuring Data to be Logged>` for the comprehensive guide.

One benefit of using :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` to carry out experiments, is that the specific :class:`RunPlan<flamegpu::RunPlan>` data is included in each log file, allowing them to be automatically processed and used for reproducible research. However, this does not identify the particular version or build of your model.
One benefit of using :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` to carry out experiments, is that the specific :class:`RunPlan<flamegpu::RunPlan>` data is included in each log file, allowing them to be automatically processed and used for reproducible research. However, this does not identify the particular version or build of your model.

If you wish to post-process the logs programmatically, then :func:`CUDAEnsemble::getLogs()<flamegpu::CUDAEnsemble::getLogs>` can be used to fetch a map of :class:`RunLog<flamegpu::RunLog>` where keys correspond to the index of successful runs within the input :class:`RunPlanVector<flamegpu::RunPlanVector>` (if a simulation run failed it will not have a log within the map).

Agent data is logged according to agent state, so agents with multiple states must have the config specified for each state required to be logged.

Expand All @@ -163,8 +165,8 @@ One benefit of using :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` to carry out
exit_log_cfg.logEnvironment("lerp_float");

// Pass the logging configs to the CUDAEnsemble
cuda_ensemble.setStepLog(step_log_cfg);
cuda_ensemble.setExitLog(exit_log_cfg);
ensemble.setStepLog(step_log_cfg);
ensemble.setExitLog(exit_log_cfg);

.. code-tab:: py Python

Expand All @@ -183,8 +185,8 @@ One benefit of using :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` to carry out
exit_log_cfg.logEnvironment("lerp_float")

# Pass the logging configs to the CUDAEnsemble
cuda_ensemble.setStepLog(step_log_cfg)
cuda_ensemble.setExitLog(exit_log_cfg)
ensemble.setStepLog(step_log_cfg)
ensemble.setExitLog(exit_log_cfg)

Configuring & Running the Ensemble
----------------------------------
Expand Down Expand Up @@ -235,11 +237,21 @@ You may also wish to specify your own defaults, by setting the values prior to c
ensemble.initialise(argc, argv);

// Pass the logging configs to the CUDAEnsemble
cuda_ensemble.setStepLog(step_log_cfg);
cuda_ensemble.setExitLog(exit_log_cfg);
ensemble.setStepLog(step_log_cfg);
ensemble.setExitLog(exit_log_cfg);

// Execute the ensemble using the specified RunPlans
const unsigned int errs = ensemble.simulate(runs);

// Fetch the RunLogs of successful runs
const std::map<unsigned int, flamegpu::RunLog> &logs = ensemble.getLogs();
for (const auto &[plan_id, log] : logs) {
// Post-process the logs
...
}

// Ensure profiling / memcheck work correctly (and trigger MPI_Finalize())
flamegpu::util::cleanup();

.. code-tab:: py Python

Expand All @@ -262,12 +274,21 @@ You may also wish to specify your own defaults, by setting the values prior to c
ensemble.initialise(sys.argv)

# Pass the logging configs to the CUDAEnsemble
cuda_ensemble.setStepLog(step_log_cfg)
cuda_ensemble.setExitLog(exit_log_cfg)
ensemble.setStepLog(step_log_cfg)
ensemble.setExitLog(exit_log_cfg)

# Execute the ensemble using the specified RunPlans
errs = ensemble.simulate(runs)

# Fetch the RunLogs of successful runs
logs = ensemble.getLogs()
for plan_id, log in logs.items():
# Post-process the logs
...

# Ensure profiling / memcheck work correctly (and trigger MPI_Finalize())
pyflamegpu.cleanup();

Error Handling Within Ensembles
-------------------------------

Expand All @@ -285,6 +306,33 @@ The default error level is "Slow" (1), which will cause an exception to be raise

Alternatively, calls to :func:`simulate()<flamegpu::CUDAEnsemble::simulate>` return the number of errors, when the error level is set to "Off" (0). Therefore, failed runs can be probed manually via checking that the return value of :func:`simulate()<flamegpu::CUDAEnsemble::simulate>` does not equal zero.

Distributed Ensembles via MPI
-----------------------------

For particularly expensive batch runs you may wish to distribute the workload across multiple nodes within a HPC cluster. This can be achieved via Message Passing Interface (MPI) support.

To enable MPI support FLAMEGPU should be compiled with the CMake flag ``FLAMEGPU_ENABLE_MPI``. When compiled with this flag :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` will use MPI by default. The ``mpi`` member of the :class:`CUDAEnsemble::EnsembleConfig<flamegpu::CUDAEnsemble::EnsembleConfig>` which will be set ``true`` if MPI support was enabled at compile time.

It is not necessary to use a CUDA aware MPI library, as `CUDAEnsemble<flamegpu::CUDAEnsemble>` will make use of all available GPUs by default using the it's existing multi-gpu support (as opposed to GPU direct MPI comms). Hence it's only necessary to launch 1 runner per node, although multiple CPU threads are still recommended (e.g. a minimum of ``N+1``, where ``N`` is the number of GPUs in the node).

When executing with MPI, :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` will execute the input :class:`RunPlanVector<flamegpu::RunPlanVector>` across all available GPUs and concurrent runs, automatically assigning jobs when a runner becomes free. This should achieve better load balancing than manually dividing work across nodes.

The call to :func:`CUDAEnsemble::simulate()<flamegpu::CUDAEnsemble::simulate>` will initialise MPI state if this has necessary, in order to cleanly exit :func:`flamegpu::util::cleanup()<flamegpu::util::cleanup>` must be called before the program exits. Hence, you may call :func:`CUDAEnsemble::simulate()<flamegpu::CUDAEnsemble::simulate>` multiple times to execute multiple ensembles via MPI in a single execution, or probe the MPI world state prior to launching the ensemble.

All three error-levels are supported and behave similarly. In all cases the rank 0 instance will be the only instance to raise an exception after the MPI group exits cleanly.

If programmatically accessing run logs when using MPI, via :func:`CUDAEnsemble::getLogs()<flamegpu::CUDAEnsemble::getLogs>`, each MPI instance will return the logs for the runs it personally completed. This enables further post-processing to remain distributed.

For more guidance around using MPI, such as how to launch MPI jobs, you should refer to the documentation for the HPC system you will be using.
Robadob marked this conversation as resolved.
Show resolved Hide resolved

.. warning::

:class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` MPI support assumes that each instance has exclusive access to all visible GPUs. Non-exclusive GPU access is likely to lead to overallocation of resources and unnecessary model failures. It's only necessary to launch 1 MPI instance per node, as :class:`CUDAEnsemble<flamegpu::CUDAEnsemble>` is natively able to utilise multiple GPUs within a single node.

.. warning::

:func:`flamegpu::util::cleanup()<flamegpu::util::cleanup()>` must be called before the program returns when using MPI, this triggers ``MPI_Finalize()``.


Related Links
-------------
Expand Down