-
-
Save dhfromkorea/3fb3c7d98bf90e55f0f8c5949abad701 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Accelerate real-time Python workload\n", | |
"\n", | |
"We start with a simple signals processing workload, and then accelerate it by several orders magnitude using the following libraries:\n", | |
"\n", | |
"1. Numpy\n", | |
"2. Numba\n", | |
"3. Dask\n", | |
"4. CuPy\n", | |
"5. Numba CUDA\n", | |
"\n", | |
"We eventually run a real-time streaming system on multi-GPU hardware." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numba.cuda\n", | |
"import numpy as np\n", | |
"import cupy\n", | |
"\n", | |
"def gather_from_detector():\n", | |
" return cupy.random.random((1000, 1000), dtype=\"float32\")\n", | |
"\n", | |
"\n", | |
"@numba.cuda.jit\n", | |
"def smooth_gpu(x, out):\n", | |
" i, j = numba.cuda.grid(2)\n", | |
" n, m = x.shape\n", | |
" if 1 <= i < n - 1 and 1 <= j < m - 1:\n", | |
" out[i, j] = (x[i - 1, j - 1] + x[i - 1, j] + x[i - 1, j + 1] +\n", | |
" x[i , j - 1] + x[i , j] + x[i , j + 1] +\n", | |
" x[i + 1, j - 1] + x[i + 1, j] + x[i + 1, j + 1]) // 9\n", | |
"\n", | |
"\n", | |
"def smooth(x):\n", | |
" out = cupy.empty_like(x)\n", | |
" smooth_gpu(x, out)\n", | |
" return out\n", | |
"\n", | |
"\n", | |
"def save(x, filename):\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from dask.distributed import Client, fire_and_forget\n", | |
"from dask_cuda import LocalCUDACluster\n", | |
"\n", | |
"cluster = LocalCUDACluster(dashboard_address=\":9999\")\n", | |
"client = Client(cluster)\n", | |
"client" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for i in range(1000):\n", | |
" img = client.submit(gather_from_detector, pure=False)\n", | |
" img = client.submit(smooth, img)\n", | |
" img = client.submit(np.fft.fft2, img)\n", | |
" future = client.submit(save, img, \"file-\" + str(i) + \"-.dat\")\n", | |
" fire_and_forget(future)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import asyncio\n", | |
"\n", | |
"async def gather():\n", | |
" while True:\n", | |
" img = client.submit(gather_from_detector, pure=False)\n", | |
" img = client.submit(smooth, img)\n", | |
" img = client.submit(np.fft.fft2, img)\n", | |
" future = client.submit(save, img, \"file-\" + str(i) + \"-.dat\")\n", | |
" fire_and_forget(future)\n", | |
" await asyncio.sleep(0.01)\n", | |
" \n", | |
"asyncio.ensure_future(gather())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [conda env:seg]", | |
"language": "python", | |
"name": "conda-env-seg-py" | |
}, | |
"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.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment