1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {},
6   "source": [
7    "# Using IPython Parallel as a joblib backend\n",
8    "\n",
9    "joblib 0.10 adds the ability to register a custom parallel backend.\n",
10    "IPython 5.1 defines one such backend, so you can use IPython parallel with joblib.\n",
11    "\n",
12    "The simplest way to set this up is a single call:"
13   ]
14  },
15  {
16   "cell_type": "code",
17   "execution_count": 1,
18   "metadata": {
19    "collapsed": true
20   },
21   "outputs": [],
22   "source": [
23    "import ipyparallel as ipp\n",
24    "ipp.register_joblib_backend()"
25   ]
26  },
27  {
28   "cell_type": "markdown",
29   "metadata": {},
30   "source": [
31    "This registers the 'ipyparallel' backend with all the defaults."
32   ]
33  },
34  {
35   "cell_type": "code",
36   "execution_count": 2,
37   "metadata": {
38    "collapsed": false
39   },
40   "outputs": [
41    {
42     "data": {
43      "text/plain": [
44       "[0,\n",
45       " -1,\n",
46       " -2,\n",
47       " -3,\n",
48       " -4,\n",
49       " -5,\n",
50       " -6,\n",
51       " -7,\n",
52       " -8,\n",
53       " -9,\n",
54       " -10,\n",
55       " -11,\n",
56       " -12,\n",
57       " -13,\n",
58       " -14,\n",
59       " -15,\n",
60       " -16,\n",
61       " -17,\n",
62       " -18,\n",
63       " -19,\n",
64       " -20,\n",
65       " -21,\n",
66       " -22,\n",
67       " -23,\n",
68       " -24,\n",
69       " -25,\n",
70       " -26,\n",
71       " -27,\n",
72       " -28,\n",
73       " -29,\n",
74       " -30,\n",
75       " -31]"
76      ]
77     },
78     "execution_count": 2,
79     "metadata": {},
80     "output_type": "execute_result"
81    }
82   ],
83   "source": [
84    "from joblib import Parallel, delayed\n",
85    "\n",
86    "def neg(x):\n",
87    "    return -x\n",
88    "\n",
89    "Parallel(backend='ipyparallel')(delayed(neg)(i) for i in range(32))"
90   ]
91  },
92  {
93   "cell_type": "markdown",
94   "metadata": {},
95   "source": [
96    "You can also configure your own View, and register it explicitly, or even as the default:"
97   ]
98  },
99  {
100   "cell_type": "code",
101   "execution_count": 3,
102   "metadata": {
103    "collapsed": true
104   },
105   "outputs": [],
106   "source": [
107    "import ipyparallel as ipp\n",
108    "rc = ipp.Client()\n",
109    "rc[:].use_cloudpickle()\n",
110    "view = rc.load_balanced_view()\n",
111    "view.register_joblib_backend(make_default=True)"
112   ]
113  },
114  {
115   "cell_type": "code",
116   "execution_count": 4,
117   "metadata": {
118    "collapsed": false
119   },
120   "outputs": [
121    {
122     "data": {
123      "text/plain": [
124       "[10625, 10621, 10647, 10637, 10622, 10620, 10642, 10631, 10625, 10621]"
125      ]
126     },
127     "execution_count": 4,
128     "metadata": {},
129     "output_type": "execute_result"
130    }
131   ],
132   "source": [
133    "import os\n",
134    "Parallel()(delayed(os.getpid)() for i in range(10))"
135   ]
136  },
137  {
138   "cell_type": "markdown",
139   "metadata": {},
140   "source": [
141    "Joblib also provides a context manager for selecting a particular backend:"
142   ]
143  },
144  {
145   "cell_type": "code",
146   "execution_count": 5,
147   "metadata": {
148    "collapsed": false
149   },
150   "outputs": [
151    {
152     "data": {
153      "text/plain": [
154       "[10622, 10620, 10642, 10631, 10622]"
155      ]
156     },
157     "execution_count": 5,
158     "metadata": {},
159     "output_type": "execute_result"
160    }
161   ],
162   "source": [
163    "even = rc.load_balanced_view(targets=rc.ids[::2])\n",
164    "even.register_joblib_backend('even')\n",
165    "\n",
166    "from joblib import parallel_backend\n",
167    "with parallel_backend('even'):\n",
168    "    result = Parallel()(delayed(os.getpid)() for i in range(5))\n",
169    "result"
170   ]
171  }
172 ],
173 "metadata": {
174  "kernelspec": {
175   "display_name": "Python 3",
176   "language": "python",
177   "name": "python3"
178  },
179  "language_info": {
180   "codemirror_mode": {
181    "name": "ipython",
182    "version": 3
183   },
184   "file_extension": ".py",
185   "mimetype": "text/x-python",
186   "name": "python",
187   "nbconvert_exporter": "python",
188   "pygments_lexer": "ipython3",
189   "version": "3.5.1"
190  }
191 },
192 "nbformat": 4,
193 "nbformat_minor": 1
194}
195