Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created February 18, 2013 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Midnighter/4976906 to your computer and use it in GitHub Desktop.
Save Midnighter/4976906 to your computer and use it in GitHub Desktop.
Exploration of a parallel implementation of a nested for-loop using IPython's machinery.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "parallel_tweaking"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Parallel Inner Products"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mat = numpy.random.random_sample((600, 600))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mat = numpy.asfortranarray(mat)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.parallel import Client, require, interactive"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rc = Client()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dv = rc.direct_view()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lv = rc.load_balanced_view()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@require(\"numpy\")\n",
"@interactive\n",
"def simple_inner(i):\n",
" column = mat[:, i]\n",
" # have to use a list comprehension to prevent closure\n",
" return sum([numpy.inner(column, mat[:, j]) for j in xrange(i + 1, mat.shape[1])])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Local, serial performance."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit sum(simple_inner(i) for i in xrange(mat.shape[1] - 1))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 720 ms per loop\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dv.push(dict(mat=mat), block=True);"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Parallel implementation using a `DirectView`."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit sum(dv.map(simple_inner, range(mat.shape[1] - 1), block=False))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 1.52 s per loop\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Parallel implementation using a `LoadBalancedView` with a large `chunksize` and unordered results."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit sum(lv.map(simple_inner, range(mat.shape[1] - 1), ordered=False, chunksize=(mat.shape[1] - 1) // len(lv), block=False))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 1.2 s per loop\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using two indices takes even more time due to additional communication."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@require(\"numpy\")\n",
"@interactive\n",
"def inner(i, j):\n",
" return numpy.inner(mat[:, i], mat[:, j])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"first = [i for i in xrange(mat.shape[1] - 1) for j in xrange(i + 1, mat.shape[1])]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"second = [j for i in xrange(mat.shape[1] - 1) for j in xrange(i + 1, mat.shape[1])]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit sum(dv.map(inner, first, second, block=False))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 2.79 s per loop\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit sum(lv.map(inner, first, second, unordered=True, chunksize=len(first) // len(lv), block=False))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 2.74 s per loop\n"
]
}
],
"prompt_number": 16
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment