Skip to content

Commit bb3e125

Browse files
committed
chunk the mandelbrot loop
1 parent 781ad00 commit bb3e125

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

e2e/mandelbrot/mandelbrot.png

82.9 KB
Loading

e2e/mandelbrot/mandelbrot.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ def sq2(a):
6060

6161

6262
@torch.compile
63-
def step(n, c, Z, N, horizon):
64-
I = abs2(Z) < horizon**2
65-
N = np.where(I, n, N) # N[I] = n
66-
Z = np.where(I[..., None], sq2(Z) + c, Z) # Z[I] = Z[I]**2 + C[I]
63+
def step(n, c, Z, N, horizon, chunksize):
64+
for _ in range(chunksize):
65+
I = abs2(Z) < horizon**2
66+
N = np.where(I, n, N) # N[I] = n
67+
Z = np.where(I[..., None], sq2(Z) + c, Z) # Z[I] = Z[I]**2 + C[I]
6768
return Z, N
6869

6970

@@ -75,8 +76,11 @@ def mandelbrot_c(xmin, xmax, ymin, ymax, xn, yn, horizon=2**10, maxiter=5):
7576
N = np.zeros(c.shape[:-1], dtype='int')
7677
Z = np.zeros_like(c, dtype='float32')
7778

78-
for n in range(maxiter):
79-
Z, N = step(n, c, Z, N, horizon)
79+
chunksize = 20
80+
n_chunks = maxiter // chunksize
81+
82+
for n in range(n_chunks):
83+
Z, N = step(n, c, Z, N, horizon, chunksize)
8084

8185
N = np.where(N == maxiter-1, 0, N) # N[N == maxiter-1] = 0
8286
return Z, N

0 commit comments

Comments
 (0)