-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry2_gpu.py
53 lines (44 loc) · 1.47 KB
/
try2_gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
import os
import math
from numba import cuda
#定义迭代函数
iterFunc = lambda z,c:(z**2 + c)
maxIterNum = 128
class index2D:
def __init__(self,x,y):
self.x = x
self.y = y
@cuda.jit
def calcMandelbrot(img,C,accuracy):
idx = index2D(cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x , cuda.blockIdx.y * cuda.blockDim.y + cuda.threadIdx.y)
z = complex(0,0)
num = 0
if idx.x<accuracy and idx.y<accuracy:
while abs(z)<2 and num <maxIterNum:
z = iterFunc(z , C[idx.x,idx.y])
num = num+1
if num == maxIterNum:
img[idx.x,idx.y] = z.real
else:
img[idx.x,idx.y] = num
def mandelbrot(xArea = [-2,2],yArea = [-2,2],accuracy = 1000):
X,Y = np.meshgrid(np.linspace(xArea[0],xArea[1],accuracy+1),np.linspace(yArea[1],yArea[0],accuracy+1))
C = X + Y*1j
img = np.zeros((accuracy+1,accuracy+1))
# resurt = np.zeros((accuracy+1,accuracy+1))
#GPU计算
threadsPerBlock = 1024
blocksPerGrid = math.ceil(accuracy / threadsPerBlock)
calcMandelbrot[blocksPerGrid,threadsPerBlock](img,C,accuracy)
cuda.synchronize()
return img
if __name__ =="__main__":
# display_mandelbrot(x_num=200,y_num=200)
# display_mandelbrot([-1,0],[0,1],x_num=200,y_num=200)
# display_mandelbrot([-0.5226,-0.5225],[0.6243,0.6244])
img = mandelbrot()
# os.system('pause')
# display_mandelbrot()
# input()