-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmandelbrot_par.R
51 lines (39 loc) · 1.05 KB
/
mandelbrot_par.R
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
# Set arguments ----
resolution <- as.integer(100) # Resolution of output image. This increases the exponentially x^2.
max.iter <- as.integer(1000)
processes <- 128
# Load parallel library ----
library(parallel)
cl <- parallel::makeForkCluster(processes)
# Defining functions ----
mandelbrot <- function(c, max.iter = 100) {
z <- 0
for (i in 1:max.iter) {
z <- z ^ 2 + c
if (abs(z) > 2) {
return(i)
}
}
return(-1)
}
# Prepare input pairs ----
x <- seq(-2, 1, length.out = resolution)
y <- seq(-1.5, 1.5, length.out = resolution)
points <-
outer(x, y, function(x, y)
complex(real = x, imaginary = y))
print(microbenchmark::microbenchmark(
par_apply = parApply(cl,
X = points,
MARGIN = c(1, 2),
FUN = mandelbrot)
))
# out_parapply <- parApply(cl,
# X = points,
# MARGIN = c(1, 2),
# FUN = mandelbrot)
stopCluster(cl)
# Visualize ----
# Plotly solution
# plotly::plot_ly(z = t(out_for),
# type = "heatmap")