Skip to content

Instantly share code, notes, and snippets.

@FrancescAlted
Created March 22, 2022 09:32
Show Gist options
  • Save FrancescAlted/ce51fc845d1e72f7fb18c67be0ab4a97 to your computer and use it in GitHub Desktop.
Save FrancescAlted/ce51fc845d1e72f7fb18c67be0ab4a97 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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",
"p1 = np.random.random((1000,1000,1000)).astype('float32')\n",
"p2 = np.random.random((1000,1000,1000)).astype('float32')\n",
"p3 = np.random.random((1000,1000,1000)).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)\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 4.4 s, sys: 1.32 s, total: 5.73 s\n",
"Wall time: 734 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 172 ms, sys: 0 ns, total: 172 ms\n",
"Wall time: 171 ms\n"
]
},
{
"data": {
"text/plain": [
"0.50000733"
]
},
"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: 2.67\n",
"CPU times: user 10.2 s, sys: 1.42 s, total: 11.6 s\n",
"Wall time: 473 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 2.92 s, sys: 29.7 ms, total: 2.95 s\n",
"Wall time: 273 ms\n"
]
},
{
"data": {
"text/plain": [
"0.4993921"
]
},
"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 18.7 s, sys: 639 ms, total: 19.3 s\n",
"Wall time: 2.46 s\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 326 ms, sys: 0 ns, total: 326 ms\n",
"Wall time: 325 ms\n"
]
},
{
"data": {
"text/plain": [
"-31.895614565944285"
]
},
"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: 2.16\n",
"CPU times: user 17.3 s, sys: 1.38 s, total: 18.6 s\n",
"Wall time: 730 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 2.93 s, sys: 131 ms, total: 3.06 s\n",
"Wall time: 268 ms\n"
]
},
{
"data": {
"text/plain": [
"-31.885555"
]
},
"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