Skip to content

Commit f254e8c

Browse files
DEMO: Make use of new resolved() in Mandelbrot demo
1 parent dd86848 commit f254e8c

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Suggests:
1717
pbmcapply,
1818
plyr,
1919
progress,
20-
future,
20+
future (>= 1.15.0),
2121
doFuture,
2222
future.apply,
2323
furrr,

demo/mandelbrot.R

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,30 @@ library(graphics)
44

55
p <- function(...) NULL
66

7-
## WORKAROUND: resolved() should launch lazy future
8-
## https://github.com/HenrikBengtsson/future/issues/337
9-
if (packageVersion("future") < "1.14.0-9000") {
10-
resolved <- function(future, ...) {
11-
if (future$state == "created") future <- run(future)
12-
future::resolved(future, ...)
13-
}
14-
}
15-
167
plot_what_is_done <- function(counts) {
8+
done <- 0L
9+
1710
for (kk in seq_along(counts)) {
1811
f <- counts[[kk]]
1912

2013
## Already plotted?
21-
if (!inherits(f, "Future")) next
14+
if (!inherits(f, "Future")) {
15+
done <- done + 1L
16+
next
17+
}
2218

2319
## Not resolved?
24-
if (!resolved(f)) next
20+
## NOTE: This will block, if all workers are busy!
21+
if (runif(1) < 0.8*(1-(done/length(counts))) || !resolved(f)) next
2522

2623
message(sprintf("Plotting tile #%d of %d ...", kk, n))
2724
counts[[kk]] <- value(f)
2825
screen(kk)
2926
plot(counts[[kk]])
30-
p()
31-
}
3227

28+
done <- done + 1L
29+
}
30+
3331
counts
3432
}
3533

@@ -45,11 +43,11 @@ if (!is.list(region)) {
4543
region <- list(xmid = 0.282989, ymid = -0.01, side = 3e-8)
4644
}
4745
}
48-
nrow <- getOption("future.demo.mandelbrot.nrow", 3L)
49-
resolution <- getOption("future.demo.mandelbrot.resolution", 400L)
46+
nrow <- getOption("future.demo.mandelbrot.nrow", 5L)
47+
resolution <- getOption("future.demo.mandelbrot.resolution", 1024L)
5048
delay <- getOption("future.demo.mandelbrot.delay", interactive())
5149
if (isTRUE(delay)) {
52-
delay <- function(counts) Sys.sleep(rexp(1, rate = 2))
50+
delay <- function(counts) Sys.sleep(runif(1L, min=0.5, max=5))
5351
} else if (!is.function(delay)) {
5452
delay <- function(counts) {}
5553
}
@@ -58,6 +56,7 @@ if (isTRUE(delay)) {
5856
Cs <- mandelbrot_tiles(xmid = region$xmid, ymid = region$ymid,
5957
side = region$side, nrow = nrow,
6058
resolution = resolution)
59+
message("Tiles: ", paste(dim(Cs), collapse = " by "))
6160
if (interactive()) {
6261
dev.new()
6362
plot.new()
@@ -77,7 +76,6 @@ message(sprintf("* Creating %d Mandelbrot tiles", n))
7776
with_progress({
7877
p <- progressor(along = Cs)
7978
counts <- lapply(seq_along(Cs), FUN=function(ii) {
80-
p()
8179
C <- Cs[[ii]]
8280
future({
8381
message(sprintf("Calculating tile #%d of %d ...", ii, n), appendLF = FALSE)
@@ -86,19 +84,17 @@ with_progress({
8684
## Emulate slowness
8785
delay(fit)
8886

87+
p(sprintf("Tile #%d by %d", ii, Sys.getpid()))
88+
8989
message(" done")
9090
fit
9191
}, lazy = TRUE)
9292
})
93-
})
93+
str(counts)
9494

95-
## Calculate and plot tiles
96-
message(sprintf("* Calculating and plotting %d Mandelbrot tiles", n))
97-
with_progress({
98-
p <- progressor(along = Cs)
99-
repeat {
95+
pp <- 0L
96+
while (any(sapply(counts, FUN = inherits, "Future"))) {
10097
counts <- plot_what_is_done(counts)
101-
if (!any(sapply(counts, FUN = inherits, "Future"))) break
10298
}
10399
})
104100

0 commit comments

Comments
 (0)