.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mda/plot_mdachain_parallel_tasks.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_mda_plot_mdachain_parallel_tasks.py: MDAChain with independent parallel MDAs ======================================= This example illustrates the possibility to parallelize MDAs in a MDAChain, given that these MDA are independent and can be run in parallel. .. GENERATED FROM PYTHON SOURCE LINES 27-35 .. code-block:: default from __future__ import annotations from gemseo.api import configure_logger from gemseo.api import create_discipline from gemseo.mda.mda_chain import MDAChain configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-45 Introduction ---------------------------------------------- In a :class:`.MDAChain`, there may be an opportunity to parallelize the execution of :class:`.MDA` that can be executed independently. As an example, let us consider the following expressions, which will be used to instantiate analytic disciplines: .. GENERATED FROM PYTHON SOURCE LINES 45-57 .. code-block:: default disciplines_expressions = [ {"a": "x"}, {"y1": "x1", "b": "a+1"}, {"x1": "1.-0.3*y1"}, {"y2": "x2", "c": "a+2"}, {"x2": "1.-0.3*y2"}, {"obj1": "x1+x2"}, {"obj2": "b+c"}, {"obj": "obj1+obj2"}, ] .. GENERATED FROM PYTHON SOURCE LINES 58-88 We can easily observe in these disciplines, that the :math:`x_1` and :math:`y_1` variables are strongly coupled. It follows that the second and third disciplines are strongly coupled and constitute a :class:`.MDA`. The same statement can be done for the disciplines that provide the output variables :math:`x_2` and :math:`y_2`, and the fourth and fifth disciplines which are also strongly coupled. These two MDAs are independent and only depend on the variable :math:`a` given by the first discipline. Thus, they can be run in parallel, hence reducing the overall :class:`.MDAChain` execution provided that enough resources are available on the computing node (in our case, at least two CPUs). By default, the parallel execution of the independent :class:`.MDA` are deactivated, meaning that the execution of the two independent :class:`.MDA` will remain sequential. Yet, a parallel execution of the two :class:`.MDA` can be activated using the `mdachain_parallelize_task` boolean option. If activated, the user has also the possibility to provide parallelization options, such as using either threads or processes to perform the parallelization, or the number of processes or threads to use. By default, as more lightweight, threading is used but on some specific case, where for instance race conditions may occur, multiprocessing can be employed. .. GENERATED FROM PYTHON SOURCE LINES 90-96 Example of :class:`.MDAChain` with parallelization -------------------------------------------------- We are here using the disciplines previously defined by their analytical expressions, and we are going to explicitly ask for a parallelization of the execution of the two independent :class:`.MDA`. .. GENERATED FROM PYTHON SOURCE LINES 96-109 .. code-block:: default disciplines = [] for expr in disciplines_expressions: disciplines.append(create_discipline("AnalyticDiscipline", expressions=expr)) mdo_parallel_chain_options = {"use_threading": True, "n_processes": 2} mdachain = MDAChain( disciplines, name="mdachain_lower", mdachain_parallelize_tasks=True, mdachain_parallel_options=mdo_parallel_chain_options, ) res = mdachain.execute() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.126 seconds) .. _sphx_glr_download_examples_mda_plot_mdachain_parallel_tasks.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mdachain_parallel_tasks.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mdachain_parallel_tasks.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_