Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "!pip install numba<br>\n",
  8. "!conda install cudatoolkits"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": 2,
  14. "metadata": {},
  15. "outputs": [],
  16. "source": [
  17. "import numpy as np\n",
  18. "from timeit import default_timer as timer"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 3,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "def VectorAdd(a, b, c):\n",
  28. " for i in range(a.size):\n",
  29. " c[i] = a[i] + b[i]"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": 4,
  35. "metadata": {},
  36. "outputs": [
  37. {
  38. "name": "stdout",
  39. "output_type": "stream",
  40. "text": [
  41. "C = [2. 2. 2. ... 2. 2. 2.]\n",
  42. " This took 8.751310 seconds\n"
  43. ]
  44. }
  45. ],
  46. "source": [
  47. "N = 32000000 # Number of elements per array\n",
  48. "A = np.ones(N, dtype=np.float32)\n",
  49. "B = np.ones(N, dtype=np.float32)\n",
  50. "C = np.zeros(N, dtype=np.float32)\n",
  51. "\n",
  52. "start = timer()\n",
  53. "\n",
  54. "VectorAdd(A, B, C)\n",
  55. "vectoradd_time = timer() - start\n",
  56. "\n",
  57. "print(\"C = \" + str(C))\n",
  58. "print(\" This took %f seconds\" % vectoradd_time)"
  59. ]
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 5,
  64. "metadata": {},
  65. "outputs": [],
  66. "source": [
  67. "from numba import vectorize,cuda\n",
  68. "\n",
  69. "@vectorize([\"float32(float32, float32)\"], target='cpu') # cuda for Nvidea drivers\n",
  70. "def VectorAdd_GPU(a, b):\n",
  71. " return a + b"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": 6,
  77. "metadata": {},
  78. "outputs": [
  79. {
  80. "name": "stdout",
  81. "output_type": "stream",
  82. "text": [
  83. "C = [2. 2. 2. ... 2. 2. 2.]\n",
  84. " This took 0.046694 seconds\n"
  85. ]
  86. }
  87. ],
  88. "source": [
  89. "N = 32000000 # Number of elements per array\n",
  90. "A = np.ones(N, dtype=np.float32)\n",
  91. "B = np.ones(N, dtype=np.float32)\n",
  92. "C = np.zeros(N, dtype=np.float32)\n",
  93. "\n",
  94. "start = timer()\n",
  95. "\n",
  96. "C = VectorAdd_GPU(A, B)\n",
  97. "vectoradd_time = timer() - start\n",
  98. "\n",
  99. "print(\"C = \" + str(C))\n",
  100. "print(\" This took %f seconds\" % vectoradd_time)"
  101. ]
  102. }
  103. ],
  104. "metadata": {
  105. "kernelspec": {
  106. "display_name": "venv",
  107. "language": "python",
  108. "name": "venv"
  109. },
  110. "language_info": {
  111. "codemirror_mode": {
  112. "name": "ipython",
  113. "version": 3
  114. },
  115. "file_extension": ".py",
  116. "mimetype": "text/x-python",
  117. "name": "python",
  118. "nbconvert_exporter": "python",
  119. "pygments_lexer": "ipython3",
  120. "version": "3.7.1"
  121. }
  122. },
  123. "nbformat": 4,
  124. "nbformat_minor": 2
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement