본문 바로가기
DEVELOPMENT/PYTHON

python gpu programming example code

by Z@__ 2019. 3. 18.
반응형



import
 numpy as np
from timeit import default_timer as timer
from numba import vectorize
 
# This should be a substantially high value. On my test machine, this took
# 33 seconds to run via the CPU and just over 3 seconds on the GPU.
NUM_ELEMENTS = 100000000
 
# This is the CPU version.
def vector_add_cpu(a, b):
  = np.zeros(NUM_ELEMENTS, dtype=np.float32)
  for in range(NUM_ELEMENTS):
    c[i] = a[i] + b[i]
  return c
 
# This is the GPU version. Note the @vectorize decorator. This tells
# numba to turn this into a GPU vectorized function.
@vectorize(["float32(float32, float32)"], target='cuda')
def vector_add_gpu(a, b):
  return + b;
 
def main():
  a_source = np.ones(NUM_ELEMENTS, dtype=np.float32)
  b_source = np.ones(NUM_ELEMENTS, dtype=np.float32)
 
  # Time the CPU function
  start = timer()
  vector_add_cpu(a_source, b_source)
  vector_add_cpu_time = timer() - start
 
  # Time the GPU function
  start = timer()
  vector_add_gpu(a_source, b_source)
  vector_add_gpu_time = timer() - start
 
  # Report times
  print("CPU function took %f seconds." % vector_add_cpu_time)
  print("GPU function took %f seconds." % vector_add_gpu_time)
 
  return 0
 
if __name__ == "__main__":
  main()





gpu-example.py

link : https://linuxhint.com/gpu-programming-python/

result


반응형

'DEVELOPMENT > PYTHON' 카테고리의 다른 글

python tuple to dict  (0) 2020.05.05
dataframe sort multi columns  (0) 2020.04.17
pandas 지수표현 없이 출력하기  (2) 2020.01.03
Jupyter to pdf 500 : Internal Server Error  (0) 2019.07.28
auto py to exe 사용법  (0) 2019.03.07

댓글