Skip to content

Commit 3495f5b

Browse files
nbrOfFreeWorkers(): Start migrating to FutureBackend
1 parent b9b5831 commit 3495f5b

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

NAMESPACE

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ S3method(mandelbrot,matrix)
2929
S3method(mandelbrot,numeric)
3030
S3method(nbrOfFreeWorkers,"NULL")
3131
S3method(nbrOfFreeWorkers,ClusterFutureBackend)
32+
S3method(nbrOfFreeWorkers,MulticoreFutureBackend)
33+
S3method(nbrOfFreeWorkers,MultiprocessFutureBackend)
3234
S3method(nbrOfFreeWorkers,SequentialFutureBackend)
33-
S3method(nbrOfFreeWorkers,cluster)
3435
S3method(nbrOfFreeWorkers,future)
3536
S3method(nbrOfFreeWorkers,logical)
36-
S3method(nbrOfFreeWorkers,multicore)
3737
S3method(nbrOfFreeWorkers,multiprocess)
38-
S3method(nbrOfFreeWorkers,uniprocess)
3938
S3method(nbrOfWorkers,"NULL")
4039
S3method(nbrOfWorkers,ClusterFutureBackend)
40+
S3method(nbrOfWorkers,MulticoreFutureBackend)
4141
S3method(nbrOfWorkers,SequentialFutureBackend)
42-
S3method(nbrOfWorkers,cluster)
4342
S3method(nbrOfWorkers,future)
4443
S3method(nbrOfWorkers,multiprocess)
45-
S3method(nbrOfWorkers,uniprocess)
4644
S3method(plot,Mandelbrot)
4745
S3method(print,Future)
4846
S3method(print,FutureCondition)

R/utils_api-nbrOfWorkers.R

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,27 @@ nbrOfWorkers.ClusterFutureBackend <- function(evaluator) {
2727
}
2828

2929
#' @export
30-
nbrOfWorkers.SequentialFutureBackend <- function(evaluator) {
31-
1L
32-
}
33-
34-
#' @export
35-
nbrOfWorkers.cluster <- function(evaluator) {
30+
nbrOfWorkers.MulticoreFutureBackend <- function(evaluator) {
3631
assert_no_positional_args_but_first()
37-
backend <- makeFutureBackend(evaluator)
38-
nbrOfWorkers(backend)
32+
backend <- evaluator
33+
workers <- backend[["workers"]]
34+
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 1L, is.finite(workers))
35+
workers
3936
}
4037

4138
#' @export
42-
nbrOfWorkers.uniprocess <- function(evaluator) {
43-
assert_no_positional_args_but_first()
44-
backend <- makeFutureBackend(evaluator)
45-
nbrOfWorkers(backend)
39+
nbrOfWorkers.SequentialFutureBackend <- function(evaluator) {
40+
1L
4641
}
4742

4843

4944
#' @export
5045
nbrOfWorkers.multiprocess <- function(evaluator) {
5146
assert_no_positional_args_but_first()
47+
backend <- makeFutureBackend(evaluator)
48+
if (inherits(backend, "FutureBackend")) {
49+
return(nbrOfWorkers(backend))
50+
}
5251

5352
expr <- formals(evaluator)$workers
5453
workers <- eval(expr, enclos = baseenv())
@@ -62,6 +61,7 @@ nbrOfWorkers.multiprocess <- function(evaluator) {
6261
workers
6362
}
6463

64+
6565
#' @export
6666
nbrOfWorkers.future <- function(evaluator) {
6767
assert_no_positional_args_but_first()
@@ -130,48 +130,49 @@ nbrOfFreeWorkers.ClusterFutureBackend <- function(evaluator, ...) {
130130
}
131131

132132
#' @export
133-
nbrOfFreeWorkers.SequentialFutureBackend <- function(evaluator, background = FALSE, ...) {
133+
nbrOfFreeWorkers.MulticoreFutureBackend <- function(evaluator, background = FALSE, ...) {
134134
assert_no_positional_args_but_first()
135-
if (isTRUE(background)) 0L else 1L
135+
backend <- evaluator
136+
workers <- backend[["workers"]]
137+
workers <- workers - usedCores()
138+
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 0L, is.finite(workers))
139+
workers
136140
}
137141

138-
139142
#' @export
140-
nbrOfFreeWorkers.cluster <- function(evaluator, background = FALSE, ...) {
143+
nbrOfFreeWorkers.MultiprocessFutureBackend <- function(evaluator, background = FALSE, ...) {
141144
assert_no_positional_args_but_first()
142-
backend <- makeFutureBackend(evaluator)
143-
nbrOfFreeWorkers(backend, background = background, ...)
145+
backend <- evaluator
146+
stopf("nbrOfFreeWorkers() is not implemented for this type of future backend (please contacts the maintainer of that backend): %s", commaq(class(evaluator)))
144147
}
145148

146-
147149
#' @export
148-
nbrOfFreeWorkers.uniprocess <- function(evaluator, background = FALSE, ...) {
150+
nbrOfFreeWorkers.SequentialFutureBackend <- function(evaluator, background = FALSE, ...) {
149151
assert_no_positional_args_but_first()
150-
151152
if (isTRUE(background)) 0L else 1L
152153
}
153154

154-
#' @export
155-
nbrOfFreeWorkers.multicore <- function(evaluator, background = FALSE, ...) {
156-
assert_no_positional_args_but_first()
157-
158-
workers <- nbrOfWorkers(evaluator)
159-
160-
workers <- workers - usedCores()
161-
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 0L, is.finite(workers))
162-
163-
workers
164-
}
165155

166156
#' @export
167157
nbrOfFreeWorkers.multiprocess <- function(evaluator, background = FALSE, ...) {
158+
assert_no_positional_args_but_first()
159+
backend <- makeFutureBackend(evaluator)
160+
if (inherits(backend, "FutureBackend")) {
161+
return(nbrOfFreeWorkers(backend, background = background, ...))
162+
}
168163
stopf("nbrOfFreeWorkers() is not implemented for this type of future backend (please contacts the maintainer of that backend): %s", commaq(class(evaluator)))
169164
}
170165

166+
171167
#' @export
172168
nbrOfFreeWorkers.future <- function(evaluator, background = FALSE, ...) {
173169
assert_no_positional_args_but_first()
174170

171+
backend <- makeFutureBackend(evaluator)
172+
if (inherits(backend, "FutureBackend")) {
173+
return(nbrOfFreeWorkers(backend, background = background, ...))
174+
}
175+
175176
workers <- nbrOfWorkers(evaluator)
176177
if (is.infinite(workers)) return(workers)
177178

man/FutureBackend.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/nbrOfWorkers.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ n <- nbrOfWorkers()
100100
message(sprintf("nbrOfWorkers: %d", n))
101101
stopifnot(n == max(1L, nworkers - 1L))
102102

103-
message("Type of future: constant")
104-
n <- nbrOfWorkers(constant)
105-
message(sprintf("nbrOfWorkers: %d", n))
106-
stopifnot(n == 1)
107-
108103
message("Type of future: <future>")
109104
foo <- structure(function(...) NULL, class = c("future"))
110105
n <- nbrOfWorkers(foo)

0 commit comments

Comments
 (0)