|
14 | 14 | #' @export |
15 | 15 | #' @keywords internal |
16 | 16 | nbrOfWorkers.batchtools <- function(evaluator) { |
17 | | - ## 1. Infer from 'workers' argument |
18 | | - expr <- formals(evaluator)$workers |
19 | | - workers <- eval(expr, enclos = baseenv()) |
20 | | - if (!is.null(workers)) { |
21 | | - if (is.function(workers)) workers <- workers() |
22 | | - stop_if_not(length(workers) >= 1) |
23 | | - if (is.numeric(workers)) return(prod(workers)) |
24 | | - if (is.character(workers)) return(length(workers)) |
25 | | - stop("Invalid data type of 'workers': ", mode(workers)) |
26 | | - } |
27 | | - |
28 | | - ## 2. Infer from 'cluster.functions' argument |
29 | | - expr <- formals(evaluator)$cluster.functions |
30 | | - cf <- eval(expr, enclos = baseenv()) |
31 | | - if (!is.null(cf)) { |
32 | | - stop_if_not(inherits(cf, "ClusterFunctions")) |
33 | | - |
34 | | - name <- cf$name |
35 | | - if (is.null(name)) name <- cf$Name |
36 | | - |
37 | | - ## Uni-process backends |
38 | | - if (name %in% c("Local", "Interactive")) return(1L) |
39 | | - |
40 | | - ## Cluster backends (with a scheduler queue) |
41 | | - if (name %in% c("TORQUE", "Slurm", "SGE", "OpenLava", "LSF")) { |
42 | | - return(availableHpcWorkers()) |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - ## If still not known, assume a generic HPC scheduler |
47 | | - availableHpcWorkers() |
48 | | -} |
49 | | - |
50 | | -#' @export |
51 | | -nbrOfWorkers.batchtools_uniprocess <- function(evaluator) { |
52 | | - assert_no_positional_args_but_first() |
53 | | - 1L |
54 | | -} |
55 | | - |
56 | | -#' @export |
57 | | -nbrOfWorkers.batchtools_multicore <- function(evaluator) { |
58 | | - ## 1. Infer from 'workers' argument |
59 | | - expr <- formals(evaluator)$workers |
60 | | - workers <- eval(expr, enclos = baseenv()) |
61 | | - if (is.function(workers)) workers <- workers() |
62 | | - stop_if_not(length(workers) == 1L, is.numeric(workers), !is.na(workers), is.finite(workers), workers >= 1) |
63 | | - workers |
| 17 | + backend <- makeFutureBackend(evaluator) |
| 18 | + nbrOfWorkers(backend) |
64 | 19 | } |
65 | 20 |
|
66 | 21 | #' @importFrom future nbrOfWorkers nbrOfFreeWorkers |
67 | 22 | #' @export |
68 | 23 | nbrOfFreeWorkers.batchtools <- function(evaluator, background = FALSE, ...) { |
69 | | - ## Special case #1: Fall back to uniprocess processing |
70 | | - if (inherits(evaluator, "uniprocess")) { |
71 | | - return(NextMethod()) |
72 | | - } |
73 | | - |
74 | | - ## Special case #2: Infinite number of workers |
75 | | - workers <- nbrOfWorkers(evaluator) |
76 | | - if (is.infinite(workers)) return(workers) |
77 | | - |
78 | | - ## In all other cases, we need to figure out how many workers |
79 | | - ## are running at the moment |
80 | | - |
81 | | - warnf("nbrOfFreeWorkers() for %s is not fully implemented. For now, it'll assume that none of the workers are occupied", setdiff(class(evaluator), c("FutureStrategy", "tweaked"))[1]) |
82 | | - usedWorkers <- 0L ## Mockup for now |
83 | | - |
84 | | - workers <- workers - usedWorkers |
85 | | - stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 0L) |
86 | | - workers |
| 24 | + backend <- makeFutureBackend(evaluator) |
| 25 | + nbrOfFreeWorkers(backend, background = background, ...) |
87 | 26 | } |
88 | 27 |
|
89 | 28 |
|
90 | | -#' @export |
91 | | -nbrOfFreeWorkers.batchtools_uniprocess <- function(evaluator, background = FALSE, ...) { |
92 | | - assert_no_positional_args_but_first() |
93 | | - if (isTRUE(background)) 0L else 1L |
94 | | -} |
95 | | - |
96 | | - |
97 | | - |
98 | | -#' @export |
99 | | -nbrOfFreeWorkers.batchtools_multiprocess <- function(evaluator, background = FALSE, ...) { |
100 | | - assert_no_positional_args_but_first() |
101 | | - |
102 | | - workers <- nbrOfWorkers(evaluator) |
103 | | - |
104 | | - ## Create a dummy future |
105 | | - ## FIXME |
106 | | - future <- evaluator(NULL, globals = FALSE, lazy = TRUE) |
107 | | - freg <- sprintf("workers-%s", class(future)[1]) |
108 | | - usedWorkers <- length(FutureRegistry(freg, action = "list")) |
109 | | - |
110 | | - workers <- workers - usedWorkers |
111 | | - stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 0L) |
112 | | - workers |
113 | | -} |
114 | | - |
115 | | - |
116 | | - |
117 | 29 | ## Number of available workers in an HPC environment |
118 | 30 | ## |
119 | 31 | ## @return (numeric) A positive integer or `+Inf`. |
|
0 commit comments