@@ -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
5045nbrOfWorkers.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
6666nbrOfWorkers.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
167157nbrOfFreeWorkers.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
172168nbrOfFreeWorkers.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
0 commit comments