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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
<!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.futures — 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="mpi4py.run" href="mpi4py.run.html" />
<link rel="prev" title="Tutorial" href="tutorial.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="mpi4py.run.html" title="mpi4py.run"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial.html" title="Tutorial"
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.futures">
<span id="mpi4py-futures"></span><h1>mpi4py.futures<a class="headerlink" href="#module-mpi4py.futures" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.0.</span></p>
</div>
<p>This package provides a high-level interface for asynchronously executing
callables on a pool of worker processes using MPI for inter-process
communication.</p>
<div class="section" id="concurrent-futures">
<h2>concurrent.futures<a class="headerlink" href="#concurrent-futures" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> package is based on <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> from
the Python standard library. More precisely, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> provides the
<a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> class as a concrete implementation of the abstract
class <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a>. The
<a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.submit" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a> interface schedules a callable to
be executed asynchronously and returns a <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a>
object representing the execution of the callable.
<a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> instances can be queried for the call
result or exception. Sets of <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> instances can
be passed to the <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.wait" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a> and
<a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.as_completed" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_completed()</span></code></a> functions.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> package was introduced in Python 3.2. A
<a class="reference external" href="https://github.com/agronholm/pythonfutures">backport</a> targeting Python 2.7 is available on <a class="reference external" href="https://pypi.python.org/pypi/futures">PyPI</a>. The <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> package uses
<a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> if available, either from the Python 3 standard
library or the Python 2.7 backport if installed. Otherwise,
<a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> uses a bundled copy of core functionality backported
from Python 3.5 to work with Python 2.7.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a></dt>
<dd>Documentation of the <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> standard module.</dd>
</dl>
</div>
</div>
<div class="section" id="mpipoolexecutor">
<h2>MPIPoolExecutor<a class="headerlink" href="#mpipoolexecutor" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> class uses a pool of MPI processes to execute
calls asynchronously. By performing computations in separate processes, it
allows to side-step the <a class="reference external" href="https://docs.python.org/3/glossary.html#term-global-interpreter-lock" title="(in Python v3.8)"><span class="xref std std-term">Global Interpreter Lock</span></a> but also means that
only picklable objects can be executed and returned. The <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module
must be importable by worker processes, thus <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> instances
may not work in the interactive interpreter.</p>
<p><a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> takes advantage of the dynamic process management
features introduced in the MPI-2 standard. In particular, the
<code class="xref py py-func docutils literal notranslate"><span class="pre">MPI.Intracomm.Spawn()</span></code> method of <code class="xref py py-func docutils literal notranslate"><span class="pre">MPI.COMM_SELF()</span></code> is used in the
master (or parent) process to spawn new worker (or child) processes running a
Python interpreter. The master process uses a separate thread (one for each
<a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> instance) to communicate back and forth with the
workers. The worker processes serve the execution of tasks in the main (and
only) thread until they are signaled for completion.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The worker processes must import the main script in order to <em>unpickle</em> any
callable defined in the <a class="reference external" href="https://docs.python.org/3/library/__main__.html#module-__main__" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module and submitted from the master
process. Furthermore, the callables may need access to other global
variables. At the worker processes,:mod:<cite>mpi4py.futures</cite> executes the main
script code (using 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) under the <code class="xref py py-mod docutils literal notranslate"><span class="pre">__worker__</span></code>
namespace to define the <a class="reference external" href="https://docs.python.org/3/library/__main__.html#module-__main__" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module. The <a class="reference external" href="https://docs.python.org/3/library/__main__.html#module-__main__" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> and
<code class="xref py py-mod docutils literal notranslate"><span class="pre">__worker__</span></code> modules are added to <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.modules" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> (both at the
master and worker processes) to ensure proper <em>pickling</em> and <em>unpickling</em>.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">During the initial import phase at the workers, the main script cannot
create and use new <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> instances. Otherwise, each
worker would attempt to spawn a new pool of workers, leading to infinite
recursion. <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> detects such recursive attempts to spawn
new workers and aborts the MPI execution environment. As the main script
code is run under the <code class="xref py py-mod docutils literal notranslate"><span class="pre">__worker__</span></code> namespace, the easiest way to avoid
spawn recursion is using the idiom <code class="code docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__':</span> <span class="pre">...</span></code> in
the main script.</p>
</div>
<dl class="class">
<dt id="mpi4py.futures.MPIPoolExecutor">
<em class="property">class </em><code class="descclassname">mpi4py.futures.</code><code class="descname">MPIPoolExecutor</code><span class="sig-paren">(</span><em>max_workers=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor" title="Permalink to this definition">¶</a></dt>
<dd><p>An <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executor</span></code></a> subclass that executes calls
asynchronously using a pool of at most <em>max_workers</em> processes. If
<em>max_workers</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or not given, its value is determined from the
<span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">MPI4PY_MAX_WORKERS</span></code> environment variable if set, or the MPI
universe size if set, otherwise a single worker process is spawned. If
<em>max_workers</em> is lower than or equal to <code class="docutils literal notranslate"><span class="pre">0</span></code>, then a <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ValueError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will
be raised.</p>
<p>Other parameters:</p>
<ul class="simple">
<li><em>python_exe</em>: Path to the Python interpreter executable used to spawn
worker processes, otherwise <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.executable" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a> is used.</li>
<li><em>python_args</em>: <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> or iterable with additional command line
flags to pass to the Python executable. Command line flags determined from
inspection of <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.flags" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/sys.html#sys.warnoptions" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.warnoptions</span></code></a> and
<a class="reference external" href="https://docs.python.org/3/library/sys.html#sys._xoptions" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys._xoptions</span></code></a> in are passed unconditionally.</li>
<li><em>mpi_info</em>: <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> or iterable yielding <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pairs.
These <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pairs are passed (through an <code class="xref py py-class docutils literal notranslate"><span class="pre">MPI.Info</span></code>
object) to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">MPI.Intracomm.Spawn()</span></code> call used to spawn worker
processes. This mechanism allows telling the MPI runtime system where and
how to start the processes. Check the documentation of the backend MPI
implementation about the set of keys it interprets and the corresponding
format for values.</li>
<li><em>globals</em>: <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> or iterable yielding <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> pairs to
initialize the main module namespace in worker processes.</li>
<li><em>main</em>: If set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, do not import the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> module in
worker processes. Setting <em>main</em> to <code class="docutils literal notranslate"><span class="pre">False</span></code> prevents worker processes
from accessing definitions in the parent <code class="docutils literal notranslate"><span class="pre">__main__</span></code> namespace.</li>
<li><em>path</em>: <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> or iterable with paths to append to <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>
in worker processes to extend the <a class="reference external" href="https://docs.python.org/3/tutorial/modules.html#tut-searchpath" title="(in Python v3.8)"><span class="xref std std-ref">module search path</span></a>.</li>
<li><em>wdir</em>: Path to set the current working directory in worker processes
using <a class="reference external" href="https://docs.python.org/3/library/os.html#os.chdir" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chdir()</span></code></a>. The initial working directory is set by the MPI
implementation. Quality MPI implementations should honor a <code class="docutils literal notranslate"><span class="pre">wdir</span></code> info
key passed through <em>mpi_info</em>, although such feature is not mandatory.</li>
<li><em>env</em>: <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> or iterable yielding <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> pairs with
environment variables to update <a class="reference external" href="https://docs.python.org/3/library/os.html#os.environ" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> in worker processes.
The initial environment is set by the MPI implementation. MPI
implementations may allow setting the initial environment through
<em>mpi_info</em>, however such feature is not required nor recommended by the
MPI standard.</li>
</ul>
<dl class="method">
<dt id="mpi4py.futures.MPIPoolExecutor.submit">
<code class="descname">submit</code><span class="sig-paren">(</span><em>func</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor.submit" title="Permalink to this definition">¶</a></dt>
<dd><p>Schedule the callable, <em>func</em>, to be executed as <code class="docutils literal notranslate"><span class="pre">func(*args,</span>
<span class="pre">**kwargs)</span></code> and returns a <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object
representing the execution of the callable.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">executor</span> <span class="o">=</span> <span class="n">MPIPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">future</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="mi">321</span><span class="p">,</span> <span class="mi">1234</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">())</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="mpi4py.futures.MPIPoolExecutor.map">
<code class="descname">map</code><span class="sig-paren">(</span><em>func</em>, <em>*iterables</em>, <em>timeout=None</em>, <em>chunksize=1</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor.map" title="Permalink to this definition">¶</a></dt>
<dd><p>Equivalent to <a class="reference external" href="https://docs.python.org/3/library/functions.html#map" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">map(func,</span> <span class="pre">*iterables)</span></code></a> except <em>func</em> is
executed asynchronously and several calls to <em>func</em> may be made
concurrently, out-of-order, in separate processes. The returned iterator
raises a <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.TimeoutError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a> if
<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#iterator.__next__" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> is called and the result isn’t available after
<em>timeout</em> seconds from the original call to <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a>.
<em>timeout</em> can be an int or a float. If <em>timeout</em> is not specified or
<code class="docutils literal notranslate"><span class="pre">None</span></code>, there is no limit to the wait time. If a call raises an
exception, then that exception will be raised when its value is retrieved
from the iterator. This method chops <em>iterables</em> into a number of chunks
which it submits to the pool as separate tasks. The (approximate) size of
these chunks can be specified by setting <em>chunksize</em> to a positive
integer. For very long iterables, using a large value for <em>chunksize</em> can
significantly improve performance compared to the default size of one. By
default, the returned iterator yields results in-order, waiting for
successive tasks to complete . This behavior can be changed by passing
the keyword argument <em>unordered</em> as <code class="docutils literal notranslate"><span class="pre">True</span></code>, then the result iterator
will yield a result as soon as any of the tasks complete.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">executor</span> <span class="o">=</span> <span class="n">MPIPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">executor</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="mi">32</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">32</span><span class="p">)):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="mpi4py.futures.MPIPoolExecutor.starmap">
<code class="descname">starmap</code><span class="sig-paren">(</span><em>func</em>, <em>iterable</em>, <em>timeout=None</em>, <em>chunksize=1</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor.starmap" title="Permalink to this definition">¶</a></dt>
<dd><p>Equivalent to <a class="reference external" href="https://docs.python.org/3/library/itertools.html#itertools.starmap" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.starmap(func,</span> <span class="pre">iterable)</span></code></a>. Used instead of <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> when
argument parameters are already grouped in tuples from a single iterable
(the data has been “pre-zipped”). <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map(func,</span> <span class="pre">*iterable)</span></code></a> is
equivalent to <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.starmap" title="mpi4py.futures.MPIPoolExecutor.starmap"><code class="xref py py-func docutils literal notranslate"><span class="pre">starmap(func,</span> <span class="pre">zip(*iterable))</span></code></a>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">executor</span> <span class="o">=</span> <span class="n">MPIPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">iterable</span> <span class="o">=</span> <span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">32</span><span class="p">))</span>
<span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">executor</span><span class="o">.</span><span class="n">starmap</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="mpi4py.futures.MPIPoolExecutor.shutdown">
<code class="descname">shutdown</code><span class="sig-paren">(</span><em>wait=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor.shutdown" title="Permalink to this definition">¶</a></dt>
<dd><p>Signal the executor that it should free any resources that it is using
when the currently pending futures are done executing. Calls to
<a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.submit" title="mpi4py.futures.MPIPoolExecutor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a> and <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> made
after <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.shutdown" title="mpi4py.futures.MPIPoolExecutor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a> will raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#RuntimeError" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
<p>If <em>wait</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then this method will not return until all the
pending futures are done executing and the resources associated with the
executor have been freed. If <em>wait</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> then this method will
return immediately and the resources associated with the executor will be
freed when all pending futures are done executing. Regardless of the
value of <em>wait</em>, the entire Python program will not exit until all
pending futures are done executing.</p>
<p>You can avoid having to call this method explicitly if you use the
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#with" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, which will shutdown the executor instance
(waiting as if <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.shutdown" title="mpi4py.futures.MPIPoolExecutor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a> were called with <em>wait</em>
set to <code class="docutils literal notranslate"><span class="pre">True</span></code>).</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="k">with</span> <span class="n">MPIPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
<span class="n">future</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">future</span><span class="o">.</span><span class="n">done</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="mpi4py.futures.MPIPoolExecutor.bootup">
<code class="descname">bootup</code><span class="sig-paren">(</span><em>wait=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPIPoolExecutor.bootup" title="Permalink to this definition">¶</a></dt>
<dd><p>Signal the executor that it should allocate eagerly any required
resources (in particular, MPI worker processes). If <em>wait</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
then <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.bootup" title="mpi4py.futures.MPIPoolExecutor.bootup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bootup()</span></code></a> will not return until the executor
resources are ready to process submissions. Resources are automatically
allocated in the first call to <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.submit" title="mpi4py.futures.MPIPoolExecutor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a>, thus
calling <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.bootup" title="mpi4py.futures.MPIPoolExecutor.bootup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bootup()</span></code></a> explicitly is seldom needed.</p>
</dd></dl>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">As the master process uses a separate thread to perform MPI communication
with the workers, the backend MPI implementation should provide support for
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_MULTIPLE</span></code>. However, some popular MPI implementations do
not support yet concurrent MPI calls from multiple threads. Additionally,
users may decide to initialize MPI with a lower level of thread support. If
the level of thread support in the backend MPI is less than
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_MULTIPLE</span></code>, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> will use a global lock
to serialize MPI calls. If the level of thread support is less than
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_SERIALIZED</span></code>, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> will emit a
<a class="reference external" href="https://docs.python.org/3/library/exceptions.html#RuntimeWarning" title="(in Python v3.8)"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></a>.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If the level of thread support in the backend MPI is less than
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_SERIALIZED</span></code> (i.e, it is either <code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_SINGLE</span></code>
or <code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.THREAD_FUNNELED</span></code>), in theory <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> cannot be
used. Rather than raising an exception, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> emits a
warning and takes a “cross-fingers” attitude to continue execution in the
hope that serializing MPI calls with a global lock will actually work.</p>
</div>
</div>
<div class="section" id="mpicommexecutor">
<h2>MPICommExecutor<a class="headerlink" href="#mpicommexecutor" title="Permalink to this headline">¶</a></h2>
<p>Legacy MPI-1 implementations (as well as some vendor MPI-2 implementations) do
not support the dynamic process management features introduced in the MPI-2
standard. Additionally, job schedulers and batch systems in supercomputing
facilities may pose additional complications to applications using the
<code class="xref c c-func docutils literal notranslate"><span class="pre">MPI_Comm_spawn()</span></code> routine.</p>
<p>With these issues in mind, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> supports an additonal, more
traditional, SPMD-like usage pattern requiring MPI-1 calls only. Python
applications are started the usual way, e.g., using the <strong class="program">mpiexec</strong>
command. Python code should make a collective call to the
<a class="reference internal" href="#mpi4py.futures.MPICommExecutor" title="mpi4py.futures.MPICommExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPICommExecutor</span></code></a> context manager to partition the set of MPI processes
within a MPI communicator in one master processes and many workers
processes. The master process gets access to an <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a>
instance to submit tasks. Meanwhile, the worker process follow a different
execution path and team-up to execute the tasks submitted from the master.</p>
<p>Besides alleviating the lack of dynamic process managment features in legacy
MPI-1 or partial MPI-2 implementations, the <a class="reference internal" href="#mpi4py.futures.MPICommExecutor" title="mpi4py.futures.MPICommExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPICommExecutor</span></code></a> context
manager may be useful in classic MPI-based Python applications willing to take
advantage of the simple, task-based, master/worker approach available in the
<a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> package.</p>
<dl class="class">
<dt id="mpi4py.futures.MPICommExecutor">
<em class="property">class </em><code class="descclassname">mpi4py.futures.</code><code class="descname">MPICommExecutor</code><span class="sig-paren">(</span><em>comm=None</em>, <em>root=0</em><span class="sig-paren">)</span><a class="headerlink" href="#mpi4py.futures.MPICommExecutor" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager for <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a>. This context manager splits a
MPI (intra)communicator <em>comm</em> (defaults to <code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.COMM_WORLD</span></code> if not
provided or <code class="docutils literal notranslate"><span class="pre">None</span></code>) in two disjoint sets: a single master process (with
rank <em>root</em> in <em>comm</em>) and the remaining worker processes. These sets are
then connected through an intercommunicator. The target of the
<a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#with" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement is assigned either an <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a>
instance (at the master) or <code class="docutils literal notranslate"><span class="pre">None</span></code> (at the workers).</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="kn">from</span> <span class="nn">mpi4py.futures</span> <span class="k">import</span> <span class="n">MPICommExecutor</span>
<span class="k">with</span> <span class="n">MPICommExecutor</span><span class="p">(</span><span class="n">MPI</span><span class="o">.</span><span class="n">COMM_WORLD</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
<span class="k">if</span> <span class="n">executor</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">future</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="nb">abs</span><span class="p">,</span> <span class="o">-</span><span class="mi">42</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span> <span class="o">==</span> <span class="mi">42</span>
<span class="n">answer</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">executor</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="nb">abs</span><span class="p">,</span> <span class="p">[</span><span class="o">-</span><span class="mi">42</span><span class="p">,</span> <span class="mi">42</span><span class="p">]))</span>
<span class="k">assert</span> <span class="n">answer</span> <span class="o">==</span> <span class="p">{</span><span class="mi">42</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If <a class="reference internal" href="#mpi4py.futures.MPICommExecutor" title="mpi4py.futures.MPICommExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPICommExecutor</span></code></a> is passed a communicator of size one (e.g.,
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.COMM_SELF</span></code>), then the executor instace assigned to the target of
the <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#with" title="(in Python v3.8)"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement will execute all submitted tasks in a single
worker thread, thus ensuring that task execution still progress
asynchronously. However, the <a class="reference external" href="https://docs.python.org/3/glossary.html#term-gil" title="(in Python v3.8)"><span class="xref std std-term">GIL</span></a> will prevent the main and worker
threads from running concurrently in multicore processors. Moreover, the
thread context switching may harm noticeably the performance of CPU-bound
tasks. In case of I/O-bound tasks, the <a class="reference external" href="https://docs.python.org/3/glossary.html#term-gil" title="(in Python v3.8)"><span class="xref std std-term">GIL</span></a> is not usually an issue,
however, as a single worker thread is used, it progress one task at a
time. We advice against using <a class="reference internal" href="#mpi4py.futures.MPICommExecutor" title="mpi4py.futures.MPICommExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPICommExecutor</span></code></a> with communicators of
size one and suggest refactoring your code to use instead a
<a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="(in Python v3.8)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a>.</p>
</div>
</div>
<div class="section" id="command-line">
<h2>Command line<a class="headerlink" href="#command-line" title="Permalink to this headline">¶</a></h2>
<p>Recalling the issues related to the lack of support for dynamic process
managment features in MPI implementations, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> supports an
alternative usage pattern where Python code (either from scripts, modules, or
zip files) is run under command line control of the <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a>
package by passing <code class="samp docutils literal notranslate"><span class="pre">-m</span> <span class="pre">mpi4py.futures</span></code> to the <strong class="program">python</strong>
executable. The <code class="docutils literal notranslate"><span class="pre">mpi4py.futures</span></code> invocation should be passed a <em>pyfile</em> path
to a script (or a zipfile/directory containing a <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code> file).
Additionally, <code class="docutils literal notranslate"><span class="pre">mpi4py.futures</span></code> accepts <code class="samp docutils literal notranslate"><span class="pre">-m</span> <em><span class="pre">mod</span></em></code> to execute a module
named <em>mod</em>, <code class="samp docutils literal notranslate"><span class="pre">-c</span> <em><span class="pre">cmd</span></em></code> to execute a command string <em>cmd</em>, or even
<code class="samp docutils literal notranslate"><span class="pre">-</span></code> to 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>).
Summarizing, <code class="samp docutils literal notranslate"><span class="pre">mpi4py.futures</span></code> can be invoked in the following ways:</p>
<ul class="simple">
<li><code class="samp docutils literal notranslate"><span class="pre">$</span> <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.futures</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">$</span> <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.futures</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">$</span> <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.futures</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">$</span> <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.futures</span> <span class="pre">-</span> <span class="pre">[arg]</span> <span class="pre">...</span></code></li>
</ul>
<p>Before starting the main script execution, <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> splits
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.COMM_WORLD</span></code> in one master (the process with rank 0 in
<code class="xref py py-const docutils literal notranslate"><span class="pre">MPI.COMM_WORLD</span></code>) and 16 workers and connect them through an MPI
intercommunicator. Afterwards, the master process proceeds with the execution
of the user script code, which eventually creates <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a>
instances to submit tasks. Meanwhile, the worker processes follow a different
execution path to serve the master. Upon successful termination of the main
script at the master, the entire MPI execution environment exists
gracefully. In case of any unhandled exception in the main script, the master
process calls <code class="code docutils literal notranslate"><span class="pre">MPI.COMM_WORLD.Abort(1)</span></code> to prevent deadlocks and force
termination of entire MPI execution environment.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Running scripts under command line control of <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> is quite
similar to executing a single-process application that spawn additional
workers as required. However, there is a very important difference users
should be aware of. All <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> instances created at the
master will share the pool of workers. Tasks submitted at the master from
many different executors will be scheduled for execution in random order as
soon as a worker is idle. Any executor can easily starve all the workers
(e.g., by calling <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-func docutils literal notranslate"><span class="pre">MPIPoolExecutor.map()</span></code></a> with long iterables). If that
ever happens, submissions from other executors will not be serviced until
free workers are available.</p>
</div>
<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 class="section" id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>The following <code class="file docutils literal notranslate"><span class="pre">julia.py</span></code> script computes the <a class="reference external" href="https://en.wikipedia.org/wiki/Julia_set">Julia set</a> and dumps an
image to disk in binary <a class="reference external" href="http://netpbm.sourceforge.net/doc/pgm.html">PGM</a> format. The code starts by importing
<a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor" title="mpi4py.futures.MPIPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">MPIPoolExecutor</span></code></a> from the <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> package. Next, some
global constants and functions implement the computation of the Julia set. The
computations are protected with the standard <code class="code docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__':</span>
<span class="pre">...</span></code> idiom. The image is computed by whole scanlines submitting all these
tasks at once using the <a class="reference internal" href="#mpi4py.futures.MPIPoolExecutor.map" title="mpi4py.futures.MPIPoolExecutor.map"><code class="xref py py-class docutils literal notranslate"><span class="pre">map</span></code></a> method. The result
iterator yields scanlines in-order as the tasks complete. Finally, each
scanline is dumped to disk.</p>
<div class="literal-block-wrapper docutils container" id="julia-py">
<div class="code-block-caption"><span class="caption-text"><code class="file docutils literal notranslate"><span class="pre">julia.py</span></code></span><a class="headerlink" href="#julia-py" title="Permalink to this code">¶</a></div>
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="hll"><span class="kn">from</span> <span class="nn">mpi4py.futures</span> <span class="kn">import</span> <span class="n">MPIPoolExecutor</span>
</span>
<span class="n">x0</span><span class="p">,</span> <span class="n">x1</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="o">+</span><span class="mf">2.0</span><span class="p">,</span> <span class="mi">640</span><span class="o">*</span><span class="mi">2</span>
<span class="n">y0</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.5</span><span class="p">,</span> <span class="o">+</span><span class="mf">1.5</span><span class="p">,</span> <span class="mi">480</span><span class="o">*</span><span class="mi">2</span>
<span class="n">dx</span> <span class="o">=</span> <span class="p">(</span><span class="n">x1</span> <span class="o">-</span> <span class="n">x0</span><span class="p">)</span> <span class="o">/</span> <span class="n">w</span>
<span class="n">dy</span> <span class="o">=</span> <span class="p">(</span><span class="n">y1</span> <span class="o">-</span> <span class="n">y0</span><span class="p">)</span> <span class="o">/</span> <span class="n">h</span>
<span class="n">c</span> <span class="o">=</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.65</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">julia</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="n">z</span> <span class="o">=</span> <span class="nb">complex</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">255</span>
<span class="k">while</span> <span class="nb">abs</span><span class="p">(</span><span class="n">z</span><span class="p">)</span> <span class="o"><</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">n</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span>
<span class="n">n</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="n">n</span>
<span class="k">def</span> <span class="nf">julia_line</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
<span class="n">line</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">w</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">y1</span> <span class="o">-</span> <span class="n">k</span> <span class="o">*</span> <span class="n">dy</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">w</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x0</span> <span class="o">+</span> <span class="n">j</span> <span class="o">*</span> <span class="n">dx</span>
<span class="n">line</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">julia</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="k">return</span> <span class="n">line</span>
<span class="hll"><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
</span>
<span class="hll"> <span class="k">with</span> <span class="n">MPIPoolExecutor</span><span class="p">()</span> <span class="k">as</span> <span class="n">executor</span><span class="p">:</span>
</span><span class="hll"> <span class="n">image</span> <span class="o">=</span> <span class="n">executor</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">julia_line</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="n">h</span><span class="p">))</span>
</span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'julia.pgm'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">'P5 </span><span class="si">%d</span><span class="s1"> </span><span class="si">%d</span><span class="s1"> </span><span class="si">%d</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="mi">255</span><span class="p">))</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">image</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
<p>The recommended way to execute the script is using the <strong class="program">mpiexec</strong>
command specifying one MPI process and (optional but recommended) the desired
MPI universe size <a class="footnote-reference" href="#id2" id="id1">[1]</a>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ mpiexec -n 1 -usize 17 python julia.py
</pre></div>
</div>
<p>The <strong class="program">mpiexec</strong> command launches a single MPI process (the master)
running the Python interpreter and executing the main script. When required,
<a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> spawns 16 additional MPI processes (the children) to
dynamically allocate the pool of workers. The master submits tasks to the
children and waits for the results. The children receive incoming tasks,
execute them, and send back the results to the master.</p>
<p>Alternatively, users may decide to execute the script in a more traditional
way, that is, all the MPI process are started at once. The user script is run
under command line control of <a class="reference internal" href="#module-mpi4py.futures" title="mpi4py.futures: Execute computations concurrently using MPI processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpi4py.futures</span></code></a> passing 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 to the <strong class="program">python</strong> executable.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ mpiexec -n 17 python -m mpi4py.futures julia.py
</pre></div>
</div>
<p>As explained previously, the 17 processes are partitioned in one master and 16
workers. The master process executes the main script while the workers execute
the tasks submitted from the master.</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>This <strong class="program">mpiexec</strong> invocation example using the <code class="docutils literal notranslate"><span class="pre">-usize</span></code> flag
(alternatively, setting the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">MPIEXEC_UNIVERSE_SIZE</span></code> environment
variable) assumes the backend MPI implementation is an MPICH derivative
using the Hydra process manager. In the Open MPI implementation, the MPI
universe size can be specified by setting the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">OMPI_UNIVERSE_SIZE</span></code>
environment variable to a positive integer. Check the documentation of your
actual MPI implementation and/or batch system for the ways to specify the
desired MPI universe size.</td></tr>
</tbody>
</table>
</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.futures</a><ul>
<li><a class="reference internal" href="#concurrent-futures">concurrent.futures</a></li>
<li><a class="reference internal" href="#mpipoolexecutor">MPIPoolExecutor</a></li>
<li><a class="reference internal" href="#mpicommexecutor">MPICommExecutor</a></li>
<li><a class="reference internal" href="#command-line">Command line</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="tutorial.html"
title="previous chapter">Tutorial</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="mpi4py.run.html"
title="next chapter">mpi4py.run</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/mpi4py.futures.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="mpi4py.run.html" title="mpi4py.run"
>next</a> |</li>
<li class="right" >
<a href="tutorial.html" title="Tutorial"
>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>
|