compute.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. # File: compute.py
  3. # Name: D.Saravanan
  4. # Date: 13/05/2022
  5. """ Script to call cos on every element of a float array """
  6. import pycuda.driver as cuda
  7. import pycuda.autoinit
  8. from pycuda.compiler import SourceModule
  9. import numpy as np
  10. import sys
  11. mod = SourceModule("""
  12. __global__ void gpucos(float *a) {
  13. int idx = threadIdx.x + threadIdx.y*4;
  14. a[idx] = cos(a[idx]);
  15. }
  16. """)
  17. for size in range(1, 32):
  18. size_of_x = size
  19. size_of_y = size
  20. if((size_of_x * size_of_y) > 1024):
  21. print("Error will this is bad")
  22. sys.exit()
  23. for n in range(0, 100):
  24. a = np.random.randn(size_of_x, size_of_y).astype(np.float32)
  25. # allocate memory on the device
  26. a_gpu = cuda.mem_alloc(a.nbytes)
  27. # transfer the data to the GPU
  28. cuda.memcpy_htod(a_gpu, a)
  29. func = mod.get_function("gpucos")
  30. func(a_gpu, block=(size_of_x, size_of_y, 1))
  31. a_gpucos = np.empty_like(a)
  32. cuda.memcpy_dtoh(a_gpucos, a_gpu)
  33. print(a_gpucos)