{ "cells": [ { "cell_type": "markdown", "id": "7d851a25-2003-485d-a07f-9487e8643c44", "metadata": {}, "source": [ "# Introduction" ] }, { "cell_type": "raw", "id": "82ec08b6-abd5-4f84-8133-8ffb1e925a8b", "metadata": {}, "source": [ "Introduction (ref: https://carpentries-incubator.github.io/lesson-gpu-programming/introduction.html)" ] }, { "cell_type": "code", "execution_count": 6, "id": "6d20c462-711f-4f72-abbf-8bab4117e9d7", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "size = 40 * 40\n", "input = np.random.random(size).astype(np.float32)" ] }, { "cell_type": "code", "execution_count": 7, "id": "99ad7f98-138d-45e4-9268-f42cb3a98f21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120 μs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" ] } ], "source": [ "%timeit -n 1 -r 1 output = np.sort(input)" ] }, { "cell_type": "code", "execution_count": 8, "id": "0d3af9b3-f418-44ed-8a23-e30aa188fd98", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.000225 s\n" ] } ], "source": [ "from cupyx.profiler import benchmark\n", "import cupy as cp\n", "input_gpu = cp.asarray(input)\n", "execution_gpu = benchmark(cp.sort, (input_gpu,), n_repeat=10)\n", "gpu_avg_time = np.average(execution_gpu.gpu_times)\n", "print(f\"{gpu_avg_time:.6f} s\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "02440471-d8c6-4ba8-8a89-e3f8c0dcca0a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "88.94952251023193\n" ] } ], "source": [ "speedup = (65.2/1000) / 0.000733\n", "print(speedup)" ] }, { "cell_type": "raw", "id": "ab4bb822-fe12-4a6d-a5b3-16eebdf7c9bd", "metadata": {}, "source": [ "try it on Colab (https://colab.research.google.com/) and compare the speedup, Which GPU card is used in collab?" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:base] *", "language": "python", "name": "conda-base-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.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }