Created
July 24, 2019 14:03
-
-
Save pentschev/37336b69f44a86c122adf9f1e7a7fd89 to your computer and use it in GitHub Desktop.
Timing multi-stream dask-cuda
This file contains hidden or 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, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from dask_cuda import LocalCUDACluster\n", | |
"from dask.distributed import Client\n", | |
"import numpy as np\n", | |
"import cupy\n", | |
"import dask.array as da\n", | |
"import asyncio\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def compute_svd(client):\n", | |
" x = cupy.random.random((100000, 1000))\n", | |
" d = da.from_array(x, chunks=(10000, 1000), asarray=False)\n", | |
" u, s, v = np.linalg.svd(d)\n", | |
" return client.compute(s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def without_streams(iterations=1):\n", | |
" async with LocalCUDACluster(asynchronous=True) as cluster:\n", | |
" async with Client(cluster, asynchronous=True) as client:\n", | |
" t = time.time()\n", | |
"\n", | |
" for i in range(iterations):\n", | |
" res = await compute_svd(client)\n", | |
"\n", | |
" print(\"Single (default) stream time for\", iterations, \"iterations:\", time.time() - t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def with_streams(iterations=1):\n", | |
" async with LocalCUDACluster(asynchronous=True) as cluster:\n", | |
" async with Client(cluster, asynchronous=True) as client:\n", | |
" streams = [cupy.cuda.stream.Stream() for _ in range(iterations)]\n", | |
"\n", | |
" t = time.time()\n", | |
"\n", | |
" svd = []\n", | |
" for stream in streams:\n", | |
" with stream:\n", | |
" svd.append(compute_svd(client))\n", | |
"\n", | |
" cupy.cuda.device.Device().synchronize()\n", | |
"\n", | |
" res = []\n", | |
" for s in svd:\n", | |
" res.append(await s)\n", | |
"\n", | |
" print(iterations, \"streams time for\", iterations, \"iterations:\", time.time() - t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Single (default) stream time for 1 iterations: 12.272627592086792\n" | |
] | |
} | |
], | |
"source": [ | |
"await without_streams(iterations=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 streams time for 1 iterations: 11.929816484451294\n" | |
] | |
} | |
], | |
"source": [ | |
"await with_streams(iterations=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Single (default) stream time for 2 iterations: 22.586686849594116\n" | |
] | |
} | |
], | |
"source": [ | |
"await without_streams(iterations=2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2 streams time for 2 iterations: 14.74821138381958\n" | |
] | |
} | |
], | |
"source": [ | |
"await with_streams(iterations=2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Single (default) stream time for 4 iterations: 44.295682430267334\n" | |
] | |
} | |
], | |
"source": [ | |
"await without_streams(iterations=4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4 streams time for 4 iterations: 19.735482931137085\n" | |
] | |
} | |
], | |
"source": [ | |
"await with_streams(iterations=4)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.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