Created
March 22, 2022 10:30
-
-
Save FrancescAlted/06bfd110a42a00c6d55cc25d92ae77c2 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "074552c3", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import numexpr as ne\n", | |
"import iarray as ia\n", | |
"\n", | |
"shape = (100,100,1000)\n", | |
"p1 = np.random.random(shape).astype('float32')\n", | |
"p2 = np.random.random(shape).astype('float32')\n", | |
"p3 = np.random.random(shape).astype('float32')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "1337db14", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"expr1 = \"(p1 + p2 + p3) / 3\"\n", | |
"#expr2 = \"2.71828**p1 / p2 * log(p3)\" # this is a known bug. will fix.\n", | |
"expr2 = \"arctan2(2.71828, p3) / p2 * log(p3)\" # this is a quite close replacement in terms of computational effort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "c7ecce73", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#NTHREADS = 16\n", | |
"#ia.set_config_defaults(nthreads=NTHREADS)\n", | |
"#ne.set_num_threads(NTHREADS)\n", | |
"\n", | |
"#ia.set_config_defaults(clevel=0, btune=False)\n", | |
"ia.set_config_defaults(favor=ia.Favor.SPEED, fp_mantissa_bits=10, chunks=shape, blocks=(1, 10, 1000))\n", | |
"\n", | |
"p1_ia = ia.numpy2iarray(p1)\n", | |
"p2_ia = ia.numpy2iarray(p2)\n", | |
"p3_ia = ia.numpy2iarray(p3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0b01f80b", | |
"metadata": {}, | |
"source": [ | |
"## Expression1: numexpr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "809cac11", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 55 ms, sys: 22.8 ms, total: 77.8 ms\n", | |
"Wall time: 10.4 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res = ne.evaluate(expr1, local_dict={'p1': p1, 'p2': p2, 'p3': p3})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "81b3e1cc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 2.9 ms, sys: 248 µs, total: 3.15 ms\n", | |
"Wall time: 2.31 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0.49989346" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res.mean()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3dda1ec7", | |
"metadata": {}, | |
"source": [ | |
"## Expression1: iarray" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "b8d5bde2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Result cratio: 1.25\n", | |
"CPU times: user 225 ms, sys: 28.6 ms, total: 254 ms\n", | |
"Wall time: 21.2 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res = ia.expr_from_string(expr1, {'p1': p1_ia, 'p2': p2_ia, 'p3': p3_ia}).eval()\n", | |
"print(\"Result cratio:\", round(res.cratio, 2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "841676fa", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 70 ms, sys: 14.1 ms, total: 84.2 ms\n", | |
"Wall time: 9.01 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0.49958143" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"ia.mean(res, axis=(2,1,0))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a0adeeb5", | |
"metadata": {}, | |
"source": [ | |
"## Expression2: numexpr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "4405164d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 219 ms, sys: 11.3 ms, total: 230 ms\n", | |
"Wall time: 30.2 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res = ne.evaluate(expr2, local_dict={'p1': p1, 'p2': p2, 'p3': p3})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "95ba3a89", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 3.07 ms, sys: 1.74 ms, total: 4.81 ms\n", | |
"Wall time: 3.99 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"-25.415495806609158" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res.mean()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8320f203", | |
"metadata": {}, | |
"source": [ | |
"## Expression2: iarray" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "5d46138c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Result cratio: 1.07\n", | |
"CPU times: user 280 ms, sys: 7.06 ms, total: 287 ms\n", | |
"Wall time: 20.4 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"res = ia.expr_from_string(expr2, {'p1': p1_ia, 'p2': p2_ia, 'p3': p3_ia}).eval()\n", | |
"print(\"Result cratio:\", round(res.cratio, 2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "69f8b650", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 13.3 ms, sys: 36.7 ms, total: 50 ms\n", | |
"Wall time: 7.18 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"-25.424397" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"ia.mean(res, axis=(2,1,0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a89b49e0", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.9.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment