{ "metadata": { "name": "", "signature": "sha256:311eded5d086bea3ca60a085bf3d6026103c5df55ca5b0c1a3119615bbf8811e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python." ] }, { "cell_type": "markdown", "metadata": { "word_id": "4818_05_mpi" }, "source": [ "# 5.11. Using MPI with IPython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this recipe, you need a MPI installation and the mpi4py package." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. We first need to create a MPI profile with:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!ipython profile create --parallel --profile=mpi" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Then, we need to open `~/.ipython/profile_mpi/ipcluster_config.py` and add the line `c.IPClusterEngines.engine_launcher_class = 'MPI'`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Once the MPI profile has been created and configured, we can launch the engines with: `ipcluster start -n 4 --engines MPI --profile=mpi` in a terminal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Now, to actually use the engines, we create a MPI client in the notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from IPython.parallel import Client" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c = Client(profile='mpi')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Let's create a view on all engines." ] }, { "cell_type": "code", "collapsed": false, "input": [ "view = c[:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. In this example, we compute the sum of all integers between 0 and 15 in parallel over two cores. We first distribute the array with the 16 values across the engines (each engine gets a subarray)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "view.scatter('a', np.arange(16., dtype='float'))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7. We compute the total sum in parallel using MPI's `allreduce` function. Every node makes the same computation and returns the same result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%px\n", "from mpi4py import MPI\n", "import numpy as np\n", "print(MPI.COMM_WORLD.allreduce(np.sum(a), op=MPI.SUM))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": {} } ] }