Created
October 21, 2022 09:47
-
-
Save mscroggs/1178fa4d64b44eaa231432a3b795be17 to your computer and use it in GitHub Desktop.
lecture3.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMlUwUw6zev4Hfkr2kn01bb", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/mscroggs/1178fa4d64b44eaa231432a3b795be17/lecture3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"id": "mBAm68OWvxYa" | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import numba\n", | |
"from numba import cuda\n", | |
"from time import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"cuda.detect()" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "DkXhP_Xev1cY", | |
"outputId": "dac59f60-3fb0-4a3f-8a87-1618a045f168" | |
}, | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Found 1 CUDA devices\n", | |
"id 0 b'Tesla T4' [SUPPORTED]\n", | |
" Compute Capability: 7.5\n", | |
" PCI Device ID: 4\n", | |
" PCI Bus ID: 0\n", | |
" UUID: GPU-34e4039e-ae51-fd83-6c61-865ddcc43b66\n", | |
" Watchdog: Disabled\n", | |
" FP32/FP64 Performance Ratio: 32\n", | |
"Summary:\n", | |
"\t1/1 devices are supported\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 2 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"a = np.random.rand(100000000).astype(\"float32\")\n", | |
"b = np.random.rand(100000000).astype(\"float32\")\n", | |
"\n", | |
"result = np.empty(100000000, dtype=\"float32\")" | |
], | |
"metadata": { | |
"id": "_wkTAbyo1oy3" | |
}, | |
"execution_count": 50, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"start = time()\n", | |
"result = a + b\n", | |
"print(time() - start)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "EjBsAwYD2Pkf", | |
"outputId": "996237cf-cbe9-4ca6-d88e-e997c7f2a594" | |
}, | |
"execution_count": 53, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0.12229013442993164\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"@numba.njit\n", | |
"def add(a, b, result):\n", | |
" for i in range(100000000):\n", | |
" result[i] = a[i] + b[i]" | |
], | |
"metadata": { | |
"id": "uGJFTBVX2bVC" | |
}, | |
"execution_count": 54, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"result = np.empty(100000000, dtype=\"float32\")\n", | |
"\n", | |
"start = time()\n", | |
"add(a, b, result)\n", | |
"print(time() - start)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "v-YVera02pt0", | |
"outputId": "fab415e3-d73f-4207-bda1-deb75c36c361" | |
}, | |
"execution_count": 58, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0.12056088447570801\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"@cuda.jit\n", | |
"def add2(a, b, result):\n", | |
" for i in range(100000000):\n", | |
" result[i] = a[i] + b[i]" | |
], | |
"metadata": { | |
"id": "v6xJt4_A29bj" | |
}, | |
"execution_count": 59, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"result = np.empty(100000000, dtype=\"float32\")\n", | |
"\n", | |
"griddim = (128,)\n", | |
"blockdim = (64,)\n", | |
"\n", | |
"a2 = cuda.to_device(a)\n", | |
"b2 = cuda.to_device(b)\n", | |
"r2 = cuda.to_device(result)\n", | |
"\n", | |
"start = time()\n", | |
"\n", | |
"\n", | |
"\n", | |
"add2[griddim, blockdim](a2, b2, r2)\n", | |
"print(time() - start)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "GlYJn_ZS3lz0", | |
"outputId": "b9a99ee9-376d-41e5-956c-9db4577ccd06" | |
}, | |
"execution_count": 66, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0.0003771781921386719\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "kNN96rcr3n73" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment