Commit 51bd8062 authored by mohamadbashar.disoki's avatar mohamadbashar.disoki

Initial commit

parents
{
"cells": [
{
"cell_type": "markdown",
"id": "a7916a54-f54d-4555-a5f0-d87c7f50f465",
"metadata": {},
"source": [
"# Programming environment init\n"
]
},
{
"cell_type": "raw",
"id": "8003f327-a9f1-414d-b35d-0f6af8bb69cc",
"metadata": {},
"source": [
"Create Conda env\n",
"$conda env list\n",
"$conda create --name myfirst-env\n",
"$source activate my-frist-env\n",
"$conda install cupy numba matplotlib scipy astropy\n",
"$conda install ipykernel # to show it as a kernel\n",
"$source deactivate\n",
"$conda remove --name myfirst-env --all"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3cc08414-c777-4e4d-9841-e375cc905a5f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.10.16\n"
]
}
],
"source": [
"!python --version"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7bef3027-7d62-493b-820b-999d9631d249",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wed Jan 15 09:26:17 2025 \n",
"+-----------------------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 550.120 Driver Version: 550.120 CUDA Version: 12.4 |\n",
"|-----------------------------------------+------------------------+----------------------+\n",
"| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|=========================================+========================+======================|\n",
"| 0 NVIDIA GeForce RTX 4090 Off | 00000000:01:00.0 Off | Off |\n",
"| 0% 29C P8 17W / 450W | 2MiB / 24564MiB | 0% Default |\n",
"| | | N/A |\n",
"+-----------------------------------------+------------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=========================================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------------------+\n"
]
}
],
"source": [
"!nvidia-smi"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8bf75751-a820-437b-b2fb-ffd24d90cc4f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Package Version\n",
"----------------------- -----------\n",
"absl-py 2.1.0\n",
"aiohappyeyeballs 2.4.4\n",
"aiohttp 3.11.11\n",
"aiosignal 1.3.2\n",
"asttokens 2.0.5\n",
"astunparse 1.6.3\n",
"async-timeout 5.0.1\n",
"attrs 24.3.0\n",
"blinker 1.9.0\n",
"Brotli 1.1.0\n",
"cached-property 1.5.2\n",
"cachetools 5.5.0\n",
"certifi 2024.12.14\n",
"cffi 1.17.1\n",
"charset-normalizer 3.4.1\n",
"click 8.1.8\n",
"comm 0.2.1\n",
"cryptography 44.0.0\n",
"debugpy 1.8.11\n",
"decorator 5.1.1\n",
"exceptiongroup 1.2.0\n",
"executing 0.8.3\n",
"filelock 3.16.1\n",
"flatbuffers 24.12.23\n",
"frozenlist 1.5.0\n",
"fsspec 2024.12.0\n",
"gast 0.6.0\n",
"google-auth 2.37.0\n",
"google-auth-oauthlib 1.2.1\n",
"google-pasta 0.2.0\n",
"grpcio 1.59.3\n",
"h2 4.1.0\n",
"h5py 3.12.1\n",
"hpack 4.0.0\n",
"huggingface-hub 0.27.1\n",
"hyperframe 6.0.1\n",
"idna 3.10\n",
"importlib_metadata 8.5.0\n",
"ipykernel 6.29.5\n",
"ipython 8.30.0\n",
"jedi 0.19.2\n",
"joblib 1.4.2\n",
"jupyter_client 8.6.0\n",
"jupyter_core 5.7.2\n",
"keras 2.15.0\n",
"Markdown 3.6\n",
"MarkupSafe 3.0.2\n",
"matplotlib-inline 0.1.6\n",
"ml-dtypes 0.2.0\n",
"multidict 6.1.0\n",
"nest-asyncio 1.6.0\n",
"numpy 1.26.4\n",
"oauthlib 3.2.2\n",
"opt_einsum 3.4.0\n",
"packaging 24.2\n",
"pandas 2.2.3\n",
"parso 0.8.4\n",
"pexpect 4.8.0\n",
"pip 24.2\n",
"platformdirs 3.10.0\n",
"prompt-toolkit 3.0.43\n",
"propcache 0.2.1\n",
"protobuf 4.24.4\n",
"psutil 5.9.0\n",
"ptyprocess 0.7.0\n",
"pure-eval 0.2.2\n",
"pyasn1 0.6.1\n",
"pyasn1_modules 0.4.1\n",
"pycparser 2.22\n",
"Pygments 2.15.1\n",
"PyJWT 2.10.1\n",
"pyOpenSSL 24.3.0\n",
"PySocks 1.7.1\n",
"python-dateutil 2.9.0.post0\n",
"pytz 2024.1\n",
"pyu2f 0.1.5\n",
"PyYAML 6.0.2\n",
"pyzmq 26.2.0\n",
"regex 2024.11.6\n",
"requests 2.32.3\n",
"requests-oauthlib 2.0.0\n",
"rsa 4.9\n",
"safetensors 0.5.2\n",
"scikit-learn 1.6.1\n",
"scipy 1.15.1\n",
"setuptools 75.1.0\n",
"six 1.16.0\n",
"stack-data 0.2.0\n",
"tensorboard 2.15.2\n",
"tensorboard_data_server 0.7.0\n",
"tensorflow 2.15.0\n",
"tensorflow_estimator 2.15.0\n",
"termcolor 2.5.0\n",
"threadpoolctl 3.5.0\n",
"tokenizers 0.15.2\n",
"tornado 6.4.2\n",
"tqdm 4.67.1\n",
"traitlets 5.14.3\n",
"transformers 4.37.0\n",
"typing_extensions 4.12.2\n",
"tzdata 2024.2\n",
"urllib3 2.3.0\n",
"wcwidth 0.2.5\n",
"Werkzeug 3.1.3\n",
"wheel 0.44.0\n",
"wrapt 1.14.1\n",
"yarl 1.18.3\n",
"zipp 3.21.0\n",
"zstandard 0.23.0\n"
]
}
],
"source": [
"!pip list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2da66dac-f5ad-4385-9380-421ef3d46b1f",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
{
"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": 1,
"id": "6d20c462-711f-4f72-abbf-8bab4117e9d7",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"size = 4096 * 4096\n",
"input = np.random.random(size).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "99ad7f98-138d-45e4-9268-f42cb3a98f21",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"69.6 ms ± 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": 3,
"id": "0d3af9b3-f418-44ed-8a23-e30aa188fd98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.000734 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": 4,
"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
}
{
"cells": [
{
"cell_type": "markdown",
"id": "a7916a54-f54d-4555-a5f0-d87c7f50f465",
"metadata": {},
"source": [
"# Programming environment init\n"
]
},
{
"cell_type": "raw",
"id": "8003f327-a9f1-414d-b35d-0f6af8bb69cc",
"metadata": {},
"source": [
"Create Conda env\n",
"$conda env list\n",
"$conda create --name myfirst-env\n",
"$source activate my-frist-env\n",
"$conda install cupy numba matplotlib scipy astropy\n",
"$conda install ipykernel # to show it as a kernel\n",
"$source deactivate\n",
"$conda remove --name myfirst-env --all"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3cc08414-c777-4e4d-9841-e375cc905a5f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.10.16\n"
]
}
],
"source": [
"!python --version"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7bef3027-7d62-493b-820b-999d9631d249",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wed Jan 15 09:26:17 2025 \n",
"+-----------------------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 550.120 Driver Version: 550.120 CUDA Version: 12.4 |\n",
"|-----------------------------------------+------------------------+----------------------+\n",
"| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|=========================================+========================+======================|\n",
"| 0 NVIDIA GeForce RTX 4090 Off | 00000000:01:00.0 Off | Off |\n",
"| 0% 29C P8 17W / 450W | 2MiB / 24564MiB | 0% Default |\n",
"| | | N/A |\n",
"+-----------------------------------------+------------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=========================================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------------------+\n"
]
}
],
"source": [
"!nvidia-smi"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8bf75751-a820-437b-b2fb-ffd24d90cc4f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Package Version\n",
"----------------------- -----------\n",
"absl-py 2.1.0\n",
"aiohappyeyeballs 2.4.4\n",
"aiohttp 3.11.11\n",
"aiosignal 1.3.2\n",
"asttokens 2.0.5\n",
"astunparse 1.6.3\n",
"async-timeout 5.0.1\n",
"attrs 24.3.0\n",
"blinker 1.9.0\n",
"Brotli 1.1.0\n",
"cached-property 1.5.2\n",
"cachetools 5.5.0\n",
"certifi 2024.12.14\n",
"cffi 1.17.1\n",
"charset-normalizer 3.4.1\n",
"click 8.1.8\n",
"comm 0.2.1\n",
"cryptography 44.0.0\n",
"debugpy 1.8.11\n",
"decorator 5.1.1\n",
"exceptiongroup 1.2.0\n",
"executing 0.8.3\n",
"filelock 3.16.1\n",
"flatbuffers 24.12.23\n",
"frozenlist 1.5.0\n",
"fsspec 2024.12.0\n",
"gast 0.6.0\n",
"google-auth 2.37.0\n",
"google-auth-oauthlib 1.2.1\n",
"google-pasta 0.2.0\n",
"grpcio 1.59.3\n",
"h2 4.1.0\n",
"h5py 3.12.1\n",
"hpack 4.0.0\n",
"huggingface-hub 0.27.1\n",
"hyperframe 6.0.1\n",
"idna 3.10\n",
"importlib_metadata 8.5.0\n",
"ipykernel 6.29.5\n",
"ipython 8.30.0\n",
"jedi 0.19.2\n",
"joblib 1.4.2\n",
"jupyter_client 8.6.0\n",
"jupyter_core 5.7.2\n",
"keras 2.15.0\n",
"Markdown 3.6\n",
"MarkupSafe 3.0.2\n",
"matplotlib-inline 0.1.6\n",
"ml-dtypes 0.2.0\n",
"multidict 6.1.0\n",
"nest-asyncio 1.6.0\n",
"numpy 1.26.4\n",
"oauthlib 3.2.2\n",
"opt_einsum 3.4.0\n",
"packaging 24.2\n",
"pandas 2.2.3\n",
"parso 0.8.4\n",
"pexpect 4.8.0\n",
"pip 24.2\n",
"platformdirs 3.10.0\n",
"prompt-toolkit 3.0.43\n",
"propcache 0.2.1\n",
"protobuf 4.24.4\n",
"psutil 5.9.0\n",
"ptyprocess 0.7.0\n",
"pure-eval 0.2.2\n",
"pyasn1 0.6.1\n",
"pyasn1_modules 0.4.1\n",
"pycparser 2.22\n",
"Pygments 2.15.1\n",
"PyJWT 2.10.1\n",
"pyOpenSSL 24.3.0\n",
"PySocks 1.7.1\n",
"python-dateutil 2.9.0.post0\n",
"pytz 2024.1\n",
"pyu2f 0.1.5\n",
"PyYAML 6.0.2\n",
"pyzmq 26.2.0\n",
"regex 2024.11.6\n",
"requests 2.32.3\n",
"requests-oauthlib 2.0.0\n",
"rsa 4.9\n",
"safetensors 0.5.2\n",
"scikit-learn 1.6.1\n",
"scipy 1.15.1\n",
"setuptools 75.1.0\n",
"six 1.16.0\n",
"stack-data 0.2.0\n",
"tensorboard 2.15.2\n",
"tensorboard_data_server 0.7.0\n",
"tensorflow 2.15.0\n",
"tensorflow_estimator 2.15.0\n",
"termcolor 2.5.0\n",
"threadpoolctl 3.5.0\n",
"tokenizers 0.15.2\n",
"tornado 6.4.2\n",
"tqdm 4.67.1\n",
"traitlets 5.14.3\n",
"transformers 4.37.0\n",
"typing_extensions 4.12.2\n",
"tzdata 2024.2\n",
"urllib3 2.3.0\n",
"wcwidth 0.2.5\n",
"Werkzeug 3.1.3\n",
"wheel 0.44.0\n",
"wrapt 1.14.1\n",
"yarl 1.18.3\n",
"zipp 3.21.0\n",
"zstandard 0.23.0\n"
]
}
],
"source": [
"!pip list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2da66dac-f5ad-4385-9380-421ef3d46b1f",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
{
"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": 1,
"id": "6d20c462-711f-4f72-abbf-8bab4117e9d7",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"size = 4096 * 4096\n",
"input = np.random.random(size).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "99ad7f98-138d-45e4-9268-f42cb3a98f21",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"69.6 ms ± 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": 3,
"id": "0d3af9b3-f418-44ed-8a23-e30aa188fd98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.000734 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": 4,
"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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment