Commit de1bea2e authored by mhdbashard's avatar mhdbashard

Update

parent 5efe29bb
This diff is collapsed.
...@@ -18,9 +18,13 @@ ...@@ -18,9 +18,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 1,
"id": "6d20c462-711f-4f72-abbf-8bab4117e9d7", "id": "6d20c462-711f-4f72-abbf-8bab4117e9d7",
"metadata": {}, "metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
...@@ -30,15 +34,19 @@ ...@@ -30,15 +34,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 2,
"id": "99ad7f98-138d-45e4-9268-f42cb3a98f21", "id": "99ad7f98-138d-45e4-9268-f42cb3a98f21",
"metadata": {}, "metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"120 μs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" "3.93 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n"
] ]
} }
], ],
...@@ -48,15 +56,19 @@ ...@@ -48,15 +56,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 3,
"id": "0d3af9b3-f418-44ed-8a23-e30aa188fd98", "id": "0d3af9b3-f418-44ed-8a23-e30aa188fd98",
"metadata": {}, "metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"0.000225 s\n" "0.000097 s\n"
] ]
} }
], ],
...@@ -71,9 +83,13 @@ ...@@ -71,9 +83,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 4,
"id": "02440471-d8c6-4ba8-8a89-e3f8c0dcca0a", "id": "02440471-d8c6-4ba8-8a89-e3f8c0dcca0a",
"metadata": {}, "metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
...@@ -99,21 +115,9 @@ ...@@ -99,21 +115,9 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python [conda env:base] *", "display_name": "Python 3 (ipykernel)",
"language": "python", "language": "python",
"name": "conda-base-py" "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.12.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b6d3aebb",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"ref:https://cuda-tutorial.readthedocs.io/en/latest/tutorials/tutorial01/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8eff613",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import cupy\n",
"\n",
"# size of the vectors\n",
"size = 1024\n",
"\n",
"# allocating and populating the vectors\n",
"a_gpu = cupy.random.rand(size, dtype=cupy.float32)\n",
"b_gpu = cupy.random.rand(size, dtype=cupy.float32)\n",
"c_gpu = cupy.zeros(size, dtype=cupy.float32)\n",
"\n",
"# CUDA vector_add\n",
"vector_add_cuda_code = r'''\n",
"extern \"C\"\n",
"__global__ void vector_add(const float * A, const float * B, float * C, const int size)\n",
"{\n",
" int item = threadIdx.x;\n",
" C[item] = A[item] + B[item];\n",
"}\n",
"'''\n",
"vector_add_gpu = cupy.RawKernel(vector_add_cuda_code, \"vector_add\")\n",
"\n",
"vector_add_gpu((1, 1, 1), (size, 1, 1), (a_gpu, b_gpu, c_gpu, size))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"id": "7efd9f5e-2046-4727-b611-fefdd2f05238",
"metadata": {},
"source": [
"# Your First GPU Kernel"
]
},
{
"cell_type": "raw",
"id": "8d1a297e-6e49-4cbc-9142-442dc60f6598",
"metadata": {},
"source": [
"ref: https://carpentries-incubator.github.io/lesson-gpu-programming/first_program.html"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d8d4a6a3-aeb6-4cb4-a61d-8bb52f4849dc",
"metadata": {},
"outputs": [],
"source": [
"def vector_add(A, B, C, size):\n",
" for item in range(0, size):\n",
" C[item] = A[item] + B[item]\n",
" \n",
" return C"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54efe984-e538-47bf-a3ad-5fc808f7b1c7",
"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
}
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