{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load balanced map and parallel function decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "from IPython.parallel import Client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "rc = Client()\n",
    "v = rc.load_balanced_view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Simple, default map:  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
     ]
    }
   ],
   "source": [
    "result = v.map(lambda x: 2*x, range(10))\n",
    "print(\"Simple, default map: \", list(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitted tasks, got ids:  ['b4d86123-967a-4f21-b9c5-8a77a69a3ced', '97401998-891d-4729-8288-ae1b97c28235', '1586cf7e-32c7-4864-bd0e-6aab16e07a3f', 'f6770223-59c3-4344-a69a-5d555d1dfb7f', '0ebb71da-6e16-44ac-917a-be978fd3787d', '582443c5-937e-45b0-9f13-5607c53cba60', '8d453d87-d70d-4fbd-a2c4-994ab3a8b6c7', '0a680757-1a59-4826-969c-523a45f3d76f', '63a3e983-a205-4f07-b494-ec86b2cdd004', '79b34cbb-aa93-4d11-8746-28969dbdd3ec']\n",
      "Using a mapper:  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
     ]
    }
   ],
   "source": [
    "ar = v.map_async(lambda x: 2*x, range(10))\n",
    "print(\"Submitted tasks, got ids: \", ar.msg_ids)\n",
    "result = ar.get()\n",
    "print(\"Using a mapper: \", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using a parallel function:  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
     ]
    }
   ],
   "source": [
    "@v.parallel(block=True)\n",
    "def f(x): return 2*x\n",
    "\n",
    "result = f.map(range(10))\n",
    "print(\"Using a parallel function: \", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
