Skip to content

Instantly share code, notes, and snippets.

@kuharan
Created February 18, 2019 14:11
Show Gist options
  • Save kuharan/3b8cbad59d8778ed18e2c817a70928d9 to your computer and use it in GitHub Desktop.
Save kuharan/3b8cbad59d8778ed18e2c817a70928d9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"!pip install numba<br>\n",
"!conda install cudatoolkits"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from timeit import default_timer as timer"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def VectorAdd(a, b, c):\n",
" for i in range(a.size):\n",
" c[i] = a[i] + b[i]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C = [2. 2. 2. ... 2. 2. 2.]\n",
" This took 8.751310 seconds\n"
]
}
],
"source": [
"N = 32000000 # Number of elements per array\n",
"A = np.ones(N, dtype=np.float32)\n",
"B = np.ones(N, dtype=np.float32)\n",
"C = np.zeros(N, dtype=np.float32)\n",
"\n",
"start = timer()\n",
"\n",
"VectorAdd(A, B, C)\n",
"vectoradd_time = timer() - start\n",
"\n",
"print(\"C = \" + str(C))\n",
"print(\" This took %f seconds\" % vectoradd_time)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from numba import vectorize,cuda\n",
"\n",
"@vectorize([\"float32(float32, float32)\"], target='cpu') # cuda for Nvidea drivers\n",
"def VectorAdd_GPU(a, b):\n",
" return a + b"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C = [2. 2. 2. ... 2. 2. 2.]\n",
" This took 0.046694 seconds\n"
]
}
],
"source": [
"N = 32000000 # Number of elements per array\n",
"A = np.ones(N, dtype=np.float32)\n",
"B = np.ones(N, dtype=np.float32)\n",
"C = np.zeros(N, dtype=np.float32)\n",
"\n",
"start = timer()\n",
"\n",
"C = VectorAdd_GPU(A, B)\n",
"vectoradd_time = timer() - start\n",
"\n",
"print(\"C = \" + str(C))\n",
"print(\" This took %f seconds\" % vectoradd_time)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "venv"
},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment