Guest User

Untitled

a guest
Mar 14th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import torch
  2. import numpy as np
  3. import cupy as cp
  4. from collections import namedtuple
  5. from timeit import timeit
  6.  
  7. def timer(fn, number=1000):
  8. print(timeit(fn, number=number) / number)
  9.  
  10. Stream = namedtuple('Stream', ['ptr'])
  11.  
  12. #%%##################################################################
  13. kernel = cp.RawKernel(r'''
  14. extern "C" __global__
  15. void cdiv_imag(float x[240][240000][2], float y[240][240000][2],
  16. float out[240][240000]) {
  17. int i = blockIdx.x * blockDim.x + threadIdx.x;
  18. int j = blockIdx.y * blockDim.y + threadIdx.y;
  19. if (i >= 240 || j >= 240000)
  20. return;
  21.  
  22. float A = x[i][j][0];
  23. float B = x[i][j][1];
  24. float C = y[i][j][0];
  25. float D = y[i][j][1];
  26.  
  27. out[i][j] = (B*C - A*D) / (C*C + D*D);
  28. }
  29.  
  30. ''', 'cdiv_imag')
  31. #%%##################################################################
  32. shape = (240, 240000)
  33. x = torch.randn(shape, dtype=torch.complex64, device='cuda')
  34. y = torch.fliplr(x.clone().detach())
  35.  
  36. threadsperblock = (32, 32)
  37. blockspergrid_x = int(np.ceil(y.shape[0] / threadsperblock[0]))
  38. blockspergrid_y = int(np.ceil(y.shape[1] / threadsperblock[1]))
  39. blockspergrid = (blockspergrid_x, blockspergrid_y)
  40.  
  41. #%%##################################################################
  42. def f1(x, y):
  43. out = torch.zeros(shape, dtype=torch.float32, device='cuda')
  44. xv, yv = torch.view_as_real(x), torch.view_as_real(y)
  45. kernel(
  46. grid=blockspergrid, block=threadsperblock,
  47. args=[xv.data_ptr(), yv.data_ptr(), out.data_ptr()],
  48. stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
  49. return out
  50.  
  51. def f2(x, y):
  52. return (x / y).imag
  53.  
  54. # ensure outputs agree within float precision
  55. adiff = torch.abs(f1(x, y) - f2(x, y))
  56. assert adiff.mean() < 1e-8
  57.  
  58. # dummy run to factor out caching & warm up GPU
  59. for _ in range(2000):
  60. f1(x, y)
  61. f2(x, y)
  62.  
  63. #%%##################################################################
  64. timer(lambda: f1(x, y))
  65. timer(lambda: f2(x, y))
  66.  
Advertisement
Add Comment
Please, Sign In to add comment