Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import asyncio
  2. import time
  3. import numpy as np
  4. import cupy
  5. import numba
  6.  
  7. import dask.array as da
  8. from dask_cuda import DGX, LocalCUDACluster
  9. from dask.distributed import Client, wait
  10.  
  11.  
  12. @numba.cuda.jit
  13. def _smooth_gpu(x, out):
  14. i, j = numba.cuda.grid(2)
  15. n, m = x.shape
  16. if 1 <= i < n - 1 and 1 <= j < m - 1:
  17. out[i, j] = (x[i - 1, j - 1] + x[i - 1, j] + x[i - 1, j + 1] +
  18. x[i , j - 1] + x[i , j] + x[i , j + 1] +
  19. x[i + 1, j - 1] + x[i + 1, j] + x[i + 1, j + 1]) / 9
  20.  
  21.  
  22. def smooth_gpu(x, out):
  23. import math
  24.  
  25. threadsperblock = (16, 16)
  26. blockspergrid_x = math.ceil(x.shape[0] / threadsperblock[0])
  27. blockspergrid_y = math.ceil(x.shape[1] / threadsperblock[1])
  28. blockspergrid = (blockspergrid_x, blockspergrid_y)
  29.  
  30. _smooth_gpu[blockspergrid, threadsperblock](x, out)
  31.  
  32.  
  33. def dispatch_smooth_gpu(x):
  34. out = cupy.zeros(x.shape, x.dtype)
  35. smooth_gpu(x, out)
  36. return out
  37.  
  38.  
  39. async def f():
  40. #async with LocalCUDACluster(asynchronous=True) as cluster:
  41. async with DGX(asynchronous=True, silence_logs=True) as cluster:
  42. async with Client(cluster, asynchronous=True) as client:
  43.  
  44. # Create a simple random array
  45. rs = da.random.RandomState(RandomState=cupy.random.RandomState)
  46. x = rs.random((80000, 80000), chunks=(10000, 10000)).persist()
  47. await wait(x)
  48.  
  49. import time
  50. t = time.time()
  51. y = x.map_overlap(dispatch_smooth_gpu, depth=1)
  52. result = await y.persist()
  53. print("Time:", time.time() - t)
  54.  
  55.  
  56. if __name__ == '__main__':
  57. asyncio.get_event_loop().run_until_complete(f())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement