Skip to content

Instantly share code, notes, and snippets.

@dfroger
Created March 6, 2014 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfroger/9389555 to your computer and use it in GitHub Desktop.
Save dfroger/9389555 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.parallel import Client\n",
"from mpi4py import MPI\n",
"c = Client(profile='mpi') # cluster has been started with 4 procs\n",
"view = c[:]\n",
"view.activate()\n",
"view.block = True\n",
"print c.ids"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 2, 3]\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%px import numpy as np\n",
"%px from mpi4py import MPI\n",
"%px from compute_pi import compute_pi\n",
"%px comm = MPI.COMM_WORLD\n",
"%px size = comm.Get_size()\n",
"%px rank = comm.Get_rank()\n",
"view['rank']"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"[3, 1, 2, 0]"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%px mypi = compute_pi(rank, size, 100, comm)\n",
"%px pi = np.zeros( (1,), dtype='d')\n",
"%px comm.Allreduce([np.array(mypi),MPI.DOUBLE],[pi,MPI.DOUBLE],MPI.SUM)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print view['mypi']\n",
"print view['pi']"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0.7778741525634219, 0.7879260283629755, 0.7829244650957667, 0.7928763409009609]\n",
"[array([ 3.14160099]), array([ 3.14160099]), array([ 3.14160099]), array([ 3.14160099])]\n"
]
}
],
"prompt_number": 4
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment