Skip to content

Instantly share code, notes, and snippets.

@restrepo
Created November 13, 2014 04:31
Show Gist options
  • Save restrepo/d37ed7ce54eb65d40960 to your computer and use it in GitHub Desktop.
Save restrepo/d37ed7ce54eb65d40960 to your computer and use it in GitHub Desktop.
Paralle IPython
{
"metadata": {
"name": "t12aparallel",
"signature": "sha256:592681c554e9742e6e0d910855060e314fd9070ccc64be9e0a6d2e7f4e314130"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Parallel IPython"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose that we need to vary the parameter, `x` of some defintion `f(x)` Instead of runs sequentially, we can run each value in one specific thread of the multicore machine. 30 threads in this case"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Load parallel modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the IPython notebook main page go to the `Clusters` tab and start the corresponding profile. In this case `\"myserver\"`."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.parallel import Client\n",
"c = Client(profile=\"default\")\n",
"lview = c.load_balanced_view()\n",
"dview = c.direct_view()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"here = os.getcwd()\n",
"dview.execute('import os; os.chdir(\"%s\")' % here)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<AsyncResult: execute>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Prepare `f(x)`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"SPhenoPATH='../SPhenoT12AM'\n",
"LesHouchesInputFile='LesHouches.in.T12AM_low_LFV'\n",
"\n",
"def SPhenoPar(y, SPhenoPATH, LesHouchesInputFile):\n",
" import t12aLFV as t; reload(t)\n",
" t.di['eta_R'] ={1:0.1,2:0.1,3:0.}\n",
" t.di['eta_I'] ={1:0.0,2:0.1,3:0.} #Eta1 is real for neutrino physics \n",
" t.di=t.benchmarks(t.di,'fermion') #Benchmark is the point\n",
" t.di['eta_R'][1]=y\n",
" x = t.runSPhenoparallel(t.di,SPhenoPATH,LesHouchesInputFile)\n",
" return x"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Load output modules"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pandas as pd\n",
"import numpy as np\n",
"import time\n",
"n=30"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Run in parallel for each `x` and print execution time"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"start = time.time()\n",
"amr = [lview.apply(SPhenoPar, y, SPhenoPATH, LesHouchesInputFile) for y in np.logspace(-4, 0., n)]\n",
"df=pd.DataFrame(columns=['mH']) #intialize pandas with any column\n",
"for r in amr:\n",
" df=df.append(r.get()[0],ignore_index=True)\n",
" \n",
"end = time.time()\n",
"print end - start"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"12.4079530239\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Run for each `x` and print execution time"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"start = time.time()\n",
"df=pd.DataFrame(columns=['mH']) #intialize pandas with any column\n",
"for y in np.logspace(-4, 0., n):\n",
" df=df.append(SPhenoPar(y, SPhenoPATH, LesHouchesInputFile)[0],ignore_index=True)\n",
" \n",
"end = time.time()\n",
"print end - start"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"120.674180984\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Store output in stantard format"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df.to_csv('output.csv')\n",
"#pd.read_csv('output.csv')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment