Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import pycuda.driver as cuda
  2. import pycuda.autoinit
  3. import pycuda.gpuarray as gpuarray
  4. from pycuda.compiler import SourceModule
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import matplotlib.animation as animation
  8.  
  9. """
  10. length * 2 = block + grid
  11. """
  12. length = 1 << 10
  13. block_x = 1 << 10
  14. block_y = 1
  15. grid_x = 1 << 10
  16. grid_y = 1
  17.  
  18. roop = 80
  19. threshold = 2.0
  20. frames = 2000
  21. scaling = 0.994
  22.  
  23. r_x = np.array([-2.0, 1.0])
  24. r_y = np.array([-1.5, 1.5])
  25.  
  26. """
  27. 1
  28. range_x = np.array([-1.2538, -1.2538])
  29. range_y = np.array([0.0245, 0.0245])
  30. """
  31. """
  32. 2
  33. """
  34. range_x = np.array([0.14033, 0.14033])
  35. range_y = np.array([0.6479, 0.6479])
  36. """
  37. 3
  38. range_x = np.array([-1.7654494830546, -1.7654494830546])
  39. range_y = np.array([0.0349703607266, 0.0349703607266])
  40. """
  41. """
  42. 4
  43. range_x = np.array([1.3946584121866, 1.3946584121866])
  44. range_y = np.array([0.0018083625612, 0.0018083625612])
  45. """
  46.  
  47. d_x = r_x - range_x
  48. d_y = r_y - range_y
  49.  
  50. mod = SourceModule("""
  51. __device__ void subst(float *a, float *b)
  52. {
  53. a[0] = b[0];
  54. a[1] = b[1];
  55. }
  56. __device__ void cadd(float *a, float *b, float *c)
  57. {
  58. c[0] = a[0] + b[0];
  59. c[1] = a[1] + b[1];
  60. }
  61. __device__ void cmul(float *a, float *b, float *c)
  62. {
  63. c[0] = a[0] * b[0] - a[1] * b[1];
  64. c[1] = a[0] * b[1] + a[1] * b[0];
  65. }
  66. __global__ void manderblot(float *x, float *y, int *image, int N, int roop, float threshold)
  67. {
  68. int idx = blockDim.x*blockIdx.x + threadIdx.x;
  69. float z[2] = {0, 0};
  70. float _z[2] = {0, 0};
  71. int i;
  72. float c[2] = {x[idx % N], y[idx / N]};
  73. for (i = 0; i < roop; ++i)
  74. {
  75. cmul(z,z,_z);
  76. cadd(_z, c, _z);
  77. if (_z[0] * _z[0] + _z[1] * _z[1] > threshold * threshold){
  78. image[idx] = i;
  79. return;
  80. }
  81. subst(z, _z);
  82. }
  83. image[idx] = 0;
  84. return;
  85. }
  86. """)
  87.  
  88.  
  89. def init(i, frames, length, roop, threshold, block_x, block_y):
  90. if i != 0:
  91. plt.cla()
  92.  
  93. if (i % 10 == 0):
  94. print("進捗: " + str(i*100/frames) + "%")
  95.  
  96. image_cpu = np.zeros(length**2).astype(np.int32)
  97. image_x_cpu = np.zeros(length).astype(np.float32)
  98. image_y_cpu = np.zeros(length).astype(np.float32)
  99.  
  100. """
  101. image_gpu = cuda.mem_alloc(image_cpu.nbytes)
  102. image_x_gpu = cuda.mem_alloc(image_x_cpu.nbytes)
  103. image_y_gpu = cuda.mem_alloc(image_y_cpu.nbytes)
  104. """
  105.  
  106. func = mod.get_function("manderblot")
  107.  
  108.  
  109. x = r_x - (1.0 - scaling**i) * d_x
  110. y = r_y - (1.0 - scaling**i) * d_y
  111.  
  112. dif_x = x[1] - x[0]
  113. dif_y = y[1] - y[0]
  114. for i in range(length):
  115. image_x_cpu[i] = x[0] + dif_x * i / (length - 1)
  116. for j in range(length):
  117. image_y_cpu[j] = y[0] + dif_y * j / (length - 1)
  118.  
  119.  
  120. image_gpu = gpuarray.to_gpu(image_cpu)
  121. image_x_gpu = gpuarray.to_gpu(image_x_cpu)
  122. image_y_gpu = gpuarray.to_gpu(image_y_cpu)
  123.  
  124. """
  125. cuda.memcpy_htod(image_gpu, image_cpu)
  126. cuda.memcpy_htod(image_x_gpu, image_x_cpu)
  127. cuda.memcpy_htod(image_y_gpu, image_y_cpu)
  128. """
  129.  
  130. func(image_x_gpu, image_y_gpu, image_gpu, np.int32(length), np.int32(roop), np.float32(threshold), block=(block_x, block_y, 1), grid=(grid_x, grid_y))
  131. #cuda.memcpy_dtoh(image_cpu, image_gpu)
  132.  
  133. image_cpu = image_gpu.get()
  134. plt.axis('off')
  135.  
  136. #ims.append([plt.imshow(image_cpu.reshape((length, length)), cmap="afmhot")])
  137. #plt.imshow(image_cpu.reshape((length, length)), cmap="afmhot")
  138. plt.imshow(image_cpu.reshape((length, length)), cmap="viridis")
  139.  
  140. def main():
  141.  
  142.  
  143. #ims = []
  144. fig = plt.figure(figsize=(10, 10))
  145.  
  146. """
  147. for i in range(frames):
  148. init(i, frames, ims, length, roop, threshold, block_x, block_y)
  149. """
  150.  
  151.  
  152. #anim = animation.ArtistAnimation(fig, ims, interval=30)
  153. anim = animation.FuncAnimation(fig, init, fargs=(frames, length, roop, threshold, block_x, block_y), frames=frames, interval=10)
  154. print("Saving mp4...")
  155. anim.save('mandelbrot2.mp4')
  156.  
  157. if __name__ == "__main__":
  158. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement