1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mpi4py.run — MPI for Python 3.0.3 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Citation" href="citing.html" />
<link rel="prev" title="mpi4py.futures" href="mpi4py.futures.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="citing.html" title="Citation"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="mpi4py.futures.html" title="mpi4py.futures"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">MPI for Python 3.0.3 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-mpi4py.run">
<span id="mpi4py-run"></span><h1>mpi4py.run<a class="headerlink" href="#module-mpi4py.run" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.0.</span></p>
</div>
<p>At import time, <code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py</span></code> initializes the MPI execution environment calling
<code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Init_thread()</span></code> and installs an exit hook to automatically call
<code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Finalize()</span></code> just before the Python process terminates. Additionally,
<code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py</span></code> overrides the default <code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.ERRORS_ARE_FATAL</span></code> error handler
in favor of <code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.ERRORS_RETURN</span></code>, which allows translating MPI errors in
Python exceptions. These departures from standard MPI behavior may be
controversial, but are quite convenient within the highly dynamic Python
programming environment. Third-party code using <code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py</span></code> can just <code class="docutils literal notranslate"><span class="pre">from</span>
<span class="pre">mpi4py</span> <span class="pre">import</span> <span class="pre">MPI</span></code> and perform MPI calls without the tedious
initialization/finalization handling. MPI errors, once translated
automatically to Python exceptions, can be dealt with the common
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#try" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>…<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#except" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a>…<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#finally" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clauses; unhandled
MPI exceptions will print a traceback which helps in locating problems in
source code.</p>
<p>Unfortunately, the interplay of automatic MPI finalization and unhandled
exceptions may lead to deadlocks. In unattended runs, these deadlocks will
drain the battery of your laptop, or burn precious allocation hours in your
supercomputing facility.</p>
<p>Consider the following snippet of Python code. Assume this code is stored in a
standard Python script file and run with <strong class="command">mpiexec</strong> in two or more
processes.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mpi4py</span> <span class="k">import</span> <span class="n">MPI</span>
<span class="k">assert</span> <span class="n">MPI</span><span class="o">.</span><span class="n">COMM_WORLD</span><span class="o">.</span><span class="n">Get_size</span><span class="p">()</span> <span class="o">></span> <span class="mi">1</span>
<span class="n">rank</span> <span class="o">=</span> <span class="n">MPI</span><span class="o">.</span><span class="n">COMM_WORLD</span><span class="o">.</span><span class="n">Get_rank</span><span class="p">()</span>
<span class="k">if</span> <span class="n">rank</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
<span class="n">MPI</span><span class="o">.</span><span class="n">COMM_WORLD</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">rank</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">MPI</span><span class="o">.</span><span class="n">COMM_WORLD</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="n">source</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
</pre></div>
</div>
<p>Process 0 raises <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ZeroDivisionError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> exception before performing a send
call to process 1. As the exception is not handled, the Python interpreter
running in process 0 will proceed to exit with non-zero status. However, as
<code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py</span></code> installed a finalizer hook to call <code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Finalize()</span></code> before
exit, process 0 will block waiting for other processes to also enter the
<code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Finalize()</span></code> call. Meanwhile, process 1 will block waiting for a
message to arrive from process 0, thus never reaching to
<code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Finalize()</span></code>. The whole MPI execution environment is irremediably in
a deadlock state.</p>
<p>To alleviate this issue, <code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py</span></code> offers a simple, alternative command
line execution mechanism based on using the <a class="reference external" href="https://docs.python.org/3/using/cmdline.html#using-on-cmdline" title="(in Python v3.8)"><span class="xref std std-ref">-m</span></a>
flag and implemented with the <a class="reference external" href="https://docs.python.org/3/library/runpy.html#module-runpy" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> module. To use this features, Python
code should be run passing <code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">mpi4py</span></code> in the command line invoking the
Python interpreter. In case of unhandled exceptions, the finalizer hook will
call <code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Abort()</span></code> on the <code class="xref c c-data docutils literal notranslate"><span class="pre">MPI_COMM_WORLD</span></code> communicator, thus
effectively aborting the MPI execution environment.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When a process is forced to abort, resources (e.g. open files) are not
cleaned-up and any registered finalizers (either with the <a class="reference external" href="https://docs.python.org/3/library/atexit.html#module-atexit" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a>
module, the Python C/API function <a class="reference external" href="https://docs.python.org/3/c-api/sys.html#c.Py_AtExit" title="(in Python v3.8)"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_AtExit()</span></code></a>, or even the C
standard library function <code class="xref c c-func docutils literal notranslate"><span class="pre">atexit()</span></code>) will not be executed. Thus,
aborting execution is an extremely impolite way of ensuring process
termination. However, MPI provides no other mechanism to recover from a
deadlock state.</p>
</div>
<div class="section" id="interface-options">
<h2>Interface options<a class="headerlink" href="#interface-options" title="Permalink to this headline">¶</a></h2>
<p>The use of <code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">mpi4py</span></code> to execute Python code on the command line resembles
that of the Python interpreter.</p>
<ul class="simple">
<li><code class="samp docutils literal notranslate"><span class="pre">mpiexec</span> <span class="pre">-n</span> <em><span class="pre">numprocs</span></em> <span class="pre">python</span> <span class="pre">-m</span> <span class="pre">mpi4py</span> <em><span class="pre">pyfile</span></em> <span class="pre">[arg]</span> <span class="pre">...</span></code></li>
<li><code class="samp docutils literal notranslate"><span class="pre">mpiexec</span> <span class="pre">-n</span> <em><span class="pre">numprocs</span></em> <span class="pre">python</span> <span class="pre">-m</span> <span class="pre">mpi4py</span> <span class="pre">-m</span> <em><span class="pre">mod</span></em> <span class="pre">[arg]</span> <span class="pre">...</span></code></li>
<li><code class="samp docutils literal notranslate"><span class="pre">mpiexec</span> <span class="pre">-n</span> <em><span class="pre">numprocs</span></em> <span class="pre">python</span> <span class="pre">-m</span> <span class="pre">mpi4py</span> <span class="pre">-c</span> <em><span class="pre">cmd</span></em> <span class="pre">[arg]</span> <span class="pre">...</span></code></li>
<li><code class="samp docutils literal notranslate"><span class="pre">mpiexec</span> <span class="pre">-n</span> <em><span class="pre">numprocs</span></em> <span class="pre">python</span> <span class="pre">-m</span> <span class="pre">mpi4py</span> <span class="pre">-</span> <span class="pre">[arg]</span> <span class="pre">...</span></code></li>
</ul>
<dl class="describe">
<dt>
<code class="descname"><pyfile></code></dt>
<dd><p>Execute the Python code contained in <em>pyfile</em>, which must be a filesystem
path referring to either a Python file, a directory containing a
<code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code> file, or a zipfile containing a <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code>
file.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-m">
<code class="descname">-m</code><code class="descclassname"> <mod></code><a class="headerlink" href="#cmdoption-m" title="Permalink to this definition">¶</a></dt>
<dd><p>Search <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.path" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> for the named module <em>mod</em> and execute its contents.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-c">
<code class="descname">-c</code><code class="descclassname"> <cmd></code><a class="headerlink" href="#cmdoption-c" title="Permalink to this definition">¶</a></dt>
<dd><p>Execute the Python code in the <em>cmd</em> string command.</p>
</dd></dl>
<dl class="describe">
<dt>
<code class="descname">-</code></dt>
<dd><p>Read commands from standard input (<a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.stdin" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>).</p>
</dd></dl>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="https://docs.python.org/3/using/cmdline.html#using-on-cmdline" title="(in Python v3.8)"><span>Command line</span></a></dt>
<dd>Documentation on Python command line interface.</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">mpi4py.run</a><ul>
<li><a class="reference internal" href="#interface-options">Interface options</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="mpi4py.futures.html"
title="previous chapter">mpi4py.futures</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="citing.html"
title="next chapter">Citation</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/mpi4py.run.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="citing.html" title="Citation"
>next</a> |</li>
<li class="right" >
<a href="mpi4py.futures.html" title="mpi4py.futures"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">MPI for Python 3.0.3 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2019, Lisandro Dalcin.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.4.
</div>
</body>
</html>
|