Created
April 17, 2014 17:43
-
-
Save FrancescAlted/11000524 to your computer and use it in GitHub Desktop.
Study of blocking in numpy/numexpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:5a6759998dc5cecdfc269873ed0afe13bceff1f78f0178535b0e07efc6161892" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"import numexpr\n", | |
"import sys\n", | |
"import ast" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class GetVars(ast.NodeTransformer):\n", | |
" def __init__(self):\n", | |
" self.vars = set()\n", | |
" def visit_Name(self, node):\n", | |
" self.vars.add(node.id)\n", | |
" return node" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def blocked(op, local_dict=None):\n", | |
" call_frame = sys._getframe(1)\n", | |
" if local_dict is None:\n", | |
" local_dict = call_frame.f_locals\n", | |
" global_dict = call_frame.f_globals\n", | |
" a = ast.parse(op, mode='eval')\n", | |
" parser = GetVars()\n", | |
" parser.visit(a)\n", | |
" rargs = [local_dict[id] for id in parser.vars]\n", | |
" r = np.empty_like(rargs[0])\n", | |
" s = 2 * (64 * 1024) / r.itemsize\n", | |
" c = compile(a, '<string>', 'eval')\n", | |
" full = dict((id, local_dict[id]) for id in parser.vars)\n", | |
" for i in range(0, r.size, s): \n", | |
" u = min(r.size, i + s)\n", | |
" loc = {id : v[i:u] for id, v in full.items()}\n", | |
" r[i:u] = eval(c, global_dict, loc)\n", | |
" return r.reshape(rargs[0].shape)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def small(operation, r, full):\n", | |
" s = 2 * (64 * 1024) / r.itemsize\n", | |
" for i in range(0, r.size, s): \n", | |
" u = min(r.size, i + s)\n", | |
" loc = {id : v[i:u] for id, v in full.items()}\n", | |
" r[i:u] = eval(operation, loc)\n", | |
"\n", | |
"def blocked_thread(op, local_dict=None, pool=None):\n", | |
" call_frame = sys._getframe(1)\n", | |
" if local_dict is None:\n", | |
" local_dict = call_frame.f_locals\n", | |
" global_dict = call_frame.f_globals\n", | |
" a = ast.parse(op, mode='eval')\n", | |
" parser = GetVars()\n", | |
" parser.visit(a)\n", | |
" rargs = [local_dict[id] for id in parser.vars]\n", | |
" r = np.empty_like(rargs[0])\n", | |
"\n", | |
" c = compile(a, '<string>', 'eval')\n", | |
" full = dict((id, local_dict[id]) for id in parser.vars)\n", | |
" \n", | |
" s = r.size // pool._processes\n", | |
" a = []\n", | |
" for i in range(0, r.size, s): \n", | |
" u = min(r.size, i + s)\n", | |
" loc = {id : v[i:u] for id, v in full.items()}\n", | |
" a.append(pool.apply_async(small, (c, r[i:u], loc)))\n", | |
" [x.get() for x in a]\n", | |
" return r.reshape(rargs[0].shape)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = np.arange(1e7)\n", | |
"b = np.arange(1e7)\n", | |
"def test(a, b):\n", | |
" return blocked(\"a**2 + b**2 + a*b * 2\")\n", | |
"\n", | |
"print test(a, b)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0.00000000e+00 4.00000000e+00 1.60000000e+01 ..., 3.99999760e+14\n", | |
" 3.99999840e+14 3.99999920e+14]\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from multiprocessing.pool import ThreadPool\n", | |
"t = ThreadPool()\n", | |
"s = \"a**2 + b**2 + a*b * 2\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%timeit\n", | |
"a**2 + b**2 + a*b * 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 loops, best of 3: 420 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%timeit\n", | |
"numexpr.set_num_threads(1)\n", | |
"numexpr.evaluate(s, local_dict={'a' : a, 'b' : b})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10 loops, best of 3: 69.9 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%timeit\n", | |
"blocked(s, local_dict={'a' : a, 'b' : b})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10 loops, best of 3: 98.3 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%timeit\n", | |
"blocked_thread(s, local_dict={'a' : a, 'b' : b}, pool=t)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 loops, best of 3: 356 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%timeit\n", | |
"numexpr.set_num_threads(4)\n", | |
"numexpr.evaluate(s, local_dict={'a' : a, 'b' : b})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10 loops, best of 3: 25.1 ms per loop\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"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