Skip to content

Instantly share code, notes, and snippets.

@freemansoft
Last active July 11, 2024 12:32
Show Gist options
  • Save freemansoft/90188f28ce8d7361baaa5fe0fc3cce52 to your computer and use it in GitHub Desktop.
Save freemansoft/90188f28ce8d7361baaa5fe0fc3cce52 to your computer and use it in GitHub Desktop.
Simplest Jupyter notebook that demonstrates GPU vs CPU performance possibilities - Sorting an array - Created while learning in NVidia AI Workbench
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "610ee839-1c46-4ce3-bec0-935eeb9524b8",
"metadata": {},
"source": [
"# Sorting an Array\n",
"Code from [this medium article](https://medium.com/@geminae.stellae/introduction-to-gpu-programming-with-python-cuda-577bfdaa47f3)\n",
"\n",
"Tested with the PyTorch NVIDIA image because it already has cupy installed"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "06460746-3590-4da7-bec7-57622e636003",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# create the array - 67,108,864 elements\n",
"size = 8192 * 8192\n",
"array = np.random.random(size).astype(np.float32)\n"
]
},
{
"cell_type": "markdown",
"id": "e6fc99e8-2ca9-40d1-bbb8-f282683456c4",
"metadata": {},
"source": [
"# CPU"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "44f38e11-d4ec-4edb-b11b-93972207232b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.24 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
]
}
],
"source": [
"# Time sorting the array using the CPU with \n",
"%timeit -n 1 -r 1 result = np.sort(array)"
]
},
{
"cell_type": "markdown",
"id": "eacc0ae5-4a95-4b12-a275-e48fb86b9899",
"metadata": {},
"source": [
"# GPU"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b4e036f1-383a-4e4d-a140-014de167519c",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import cupy as cp\n",
"# cp.show_config()\n",
"array_gpu = cp.asarray(array)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3646ba15-e068-44a8-8b2d-8a1d1b65d2d3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"34.8 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 7 loops each)\n"
]
}
],
"source": [
"# Time sorting the gpu based array\n",
"%timeit -n 7 -r 1 result_gpu = cp.sort(array_gpu)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "279cbad0-7e59-4832-a6ec-8a5b126d0955",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment