{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "## Distributing tasks on several cores with IPython.parallel" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": "import numpy as np\nfrom ipyparallel import Client # IPython.parallel before IPython 4.0" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": "rc = Client()" }, { "cell_type": "markdown", "metadata": {}, "source": "### Direct interface" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": "[0, 1, 2, 3]" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": "rc.ids" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": "%px import os, time" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": "[stdout:0] 11173\n[stdout:1] 11174\n[stdout:2] 11175\n[stdout:3] 11176" }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": "%px print(os.getpid())" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": "[stdout:0] 11173\n[stdout:1] 11174\n[stdout:2] 11175" }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": "%%px --targets :-1\nprint(os.getpid())" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": "" }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": "view = rc[:-1]\nview" }, { "cell_type": "markdown", "metadata": {}, "source": "### Load-balanced interface" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": "v = rc.load_balanced_view()" }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": "def sample(n):\n import numpy as np\n # Random coordinates.\n x, y = np.random.rand(2, n)\n # Square distances to the origin.\n r_square = x ** 2 + y ** 2\n # Number of points in the quarter disc.\n return (r_square <= 1).sum()" }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": "def pi(n_in, n):\n return 4. * float(n_in) / n" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": "n = 100000000" }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": "3.14174968" }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": "pi(sample(n), n)" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": "1 loops, best of 3: 2.65 s per loop" }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": "%timeit pi(sample(n), n)" }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": "args = [n // 100] * 100" }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": "ar = v.map(sample, args)" }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": "(False, 12)" }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": "ar.ready(), ar.progress" }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": "(1.428284, 4.042367000000002)" }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": "ar.elapsed, ar.serial_time" }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": "3.141666" }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": "pi(np.sum(ar.result()), n)" } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }