Created
February 18, 2019 14:11
-
-
Save kuharan/3b8cbad59d8778ed18e2c817a70928d9 to your computer and use it in GitHub Desktop.
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
{ | |
"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