JasonHacks

haha

Nov 13th, 2021
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import numpy as np
  2. from pylab import imshow, show
  3. import pycuda.driver as cuda
  4. import pycuda.autoinit
  5. from pycuda.compiler import SourceModule
  6.  
  7. # Number of iterations for each point
  8. max_iterations = 50
  9.  
  10. # Width and height of the image
  11. width = 1024
  12. height = 1024
  13.  
  14. # The real and complex parts of the coordinates
  15. real_min = -2.0
  16. real_max = 1.0
  17. imag_min = -1.5
  18. imag_max = 1.5
  19.  
  20. # Generate the coordinates and copy them to the device
  21. x, y = np.mgrid[real_min:real_max:(width*1j), imag_min:imag_max:(height*1j)]
  22. x_gpu = cuda.mem_alloc(x.nbytes)
  23. y_gpu = cuda.mem_alloc(y.nbytes)
  24. cuda.memcpy_htod(x_gpu, x)
  25. cuda.memcpy_htod(y_gpu, y)
  26.  
  27. # Generate the function for the mandelbrot calculation on the device
  28. mod = SourceModule("""
  29. __global__ void mandelbrot(int max_iterations, float *x, float *y, int *output) {
  30. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  31.  
  32. float real = x[idx];
  33. float imag = y[idx];
  34.  
  35. float real_step = (x[blockDim.x] - x[0]) / blockDim.x;
  36. float imag_step = (y[blockDim.y] - y[0]) / blockDim.y;
  37.  
  38. float real2 = 0;
  39. float imag2 = 0;
  40.  
  41. int iteration;
  42.  
  43. for (iteration = 0; iteration < max_iterations; iteration++) {
  44. float real2_temp = real2 * real2;
  45. float imag2_temp = imag2 * imag2;
  46.  
  47. if (real2_temp + imag2_temp > 4) {
  48. break;
  49. }
  50.  
  51. imag2 = 2 * real * imag + imag2;
  52. real2 = real2_temp - imag2_temp + real;
  53.  
  54. real += real_step;
  55. imag += imag_step;
  56. }
  57.  
  58. output[idx] = iteration;
  59. }
  60. """)
  61. mandelbrot = mod.get_function("mandelbrot")
  62. output = np.zeros(width * height, dtype=np.int32)
  63. output_gpu = cuda.mem_alloc(output.nbytes)
  64. mandelbrot(np.int32(max_iterations), x_gpu, y_gpu, output_gpu, block=(width, height, 1), grid=(width/16, height/16))
  65. cuda.memcpy_dtoh(output, output_gpu)
  66. output = output.reshape((height, width))
  67. imshow(output)
  68. show()
Add Comment
Please, Sign In to add comment