| 1 |
library(parallelly) |
| 2 |
|
| 3 |
availableCoresPJM <- parallelly:::availableCoresPJM |
| 4 |
|
| 5 |
message("*** availableWorkers() ...") |
| 6 |
|
| 7 |
## The default |
| 8 |
w <- availableWorkers() |
| 9 |
print(w) |
| 10 |
stopifnot(is.character(w), length(w) >= 1) |
| 11 |
|
| 12 |
## Minimium of all known settings (default) |
| 13 |
print(availableWorkers(which = "min")) |
| 14 |
|
| 15 |
## Maximum of all known settings (should never be used) |
| 16 |
print(availableWorkers(which = "max")) |
| 17 |
|
| 18 |
## All known settings |
| 19 |
print(availableWorkers(na.rm = FALSE, which = "all")) |
| 20 |
|
| 21 |
## System settings |
| 22 |
w <- availableWorkers(methods = "system") |
| 23 |
print(w) |
| 24 |
stopifnot(is.character(w), length(w) >= 1) |
| 25 |
|
| 26 |
## Contrain by number of available connections |
| 27 |
opts <- options(mc.cores = availableConnections()) |
| 28 |
w <- availableWorkers(constraints = "connections", method = "mc.cores") |
| 29 |
print(w) |
| 30 |
stopifnot(is.character(w), length(w) >= 1) |
| 31 |
w0 <- w |
| 32 |
w <- availableWorkers(constraints = "connections-1", method = "mc.cores") |
| 33 |
print(w) |
| 34 |
stopifnot(is.character(w), length(w) >= 1, length(w) == length(w0) - 1L) |
| 35 |
w <- availableWorkers(constraints = "connections-2", method = "mc.cores") |
| 36 |
print(w) |
| 37 |
stopifnot(is.character(w), length(w) >= 1, length(w) == length(w0) - 2L) |
| 38 |
options(opts) |
| 39 |
|
| 40 |
## Predefined ones for known cluster schedulers |
| 41 |
print(availableWorkers(methods = "PBS")) |
| 42 |
print(availableWorkers(methods = "SGE")) |
| 43 |
print(availableWorkers(methods = "Slurm")) |
| 44 |
print(availableWorkers(methods = "LSF")) |
| 45 |
print(availableWorkers(methods = "PJM")) |
| 46 |
|
| 47 |
|
| 48 |
message("*** HPC related ...") |
| 49 |
|
| 50 |
sge_expand_node_count_pairs <- parallelly:::sge_expand_node_count_pairs |
| 51 |
read_pbs_nodefile <- parallelly:::read_pbs_nodefile |
| 52 |
read_pjm_nodefile <- parallelly:::read_pjm_nodefile |
| 53 |
read_pe_hostfile <- parallelly:::read_pe_hostfile |
| 54 |
|
| 55 |
workers0 <- c("n1", "n2", "n3", "n1", "n6", "n3", "n3", "n5") |
| 56 |
data0 <- as.data.frame(table(workers0), stringsAsFactors = FALSE) |
| 57 |
colnames(data0) <- c("node", "count") |
| 58 |
data0 <- data0[order(data0$node, data0$count), ] |
| 59 |
|
| 60 |
|
| 61 |
message("*** LSF ...") |
| 62 |
|
| 63 |
Sys.setenv(LSB_HOSTS = paste(workers0, collapse = " ")) |
| 64 |
workers <- availableWorkers(methods = "LSF") |
| 65 |
print(workers) |
| 66 |
stopifnot(length(workers) == length(workers0)) |
| 67 |
|
| 68 |
message("*** LSF ... done") |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
message("*** read_pbs_nodefile() ...") |
| 73 |
|
| 74 |
workers <- workers0 |
| 75 |
pathname <- tempfile() |
| 76 |
writeLines(workers, con = pathname) |
| 77 |
|
| 78 |
data <- read_pbs_nodefile(pathname) |
| 79 |
str(data) |
| 80 |
stopifnot( |
| 81 |
c("node") %in% colnames(data), |
| 82 |
is.character(data$node), |
| 83 |
!anyNA(data$node), |
| 84 |
nrow(data$node) == length(workers), |
| 85 |
all(sort(data$node) == sort(workers)) |
| 86 |
) |
| 87 |
|
| 88 |
Sys.setenv(PBS_NODEFILE = pathname) |
| 89 |
Sys.setenv(PBS_NP = length(workers), |
| 90 |
PBS_NUM_NODES = length(workers) / 2, |
| 91 |
PBS_NUM_PPN = 2) |
| 92 |
workers <- availableWorkers(methods = "PBS") |
| 93 |
print(workers) |
| 94 |
stopifnot(length(workers) == length(workers0), all(workers == sort(workers0))) |
| 95 |
|
| 96 |
Sys.setenv(PBS_NUM_PPN = 3) |
| 97 |
res <- tryCatch({ |
| 98 |
workers <- availableWorkers(methods = "PBS") |
| 99 |
}, warning = identity) |
| 100 |
stopifnot(inherits(res, "warning")) |
| 101 |
|
| 102 |
Sys.setenv(PBS_NP = length(workers) + 1) |
| 103 |
res <- tryCatch({ |
| 104 |
workers <- availableWorkers(methods = "PBS") |
| 105 |
}, warning = identity) |
| 106 |
stopifnot(inherits(res, "warning")) |
| 107 |
|
| 108 |
## Exceptions |
| 109 |
workersE <- c(workers, "n 3") |
| 110 |
pathname <- tempfile() |
| 111 |
writeLines(workersE, con = pathname) |
| 112 |
res <- tryCatch(read_pbs_nodefile(pathname), error = identity) |
| 113 |
print(res) |
| 114 |
stopifnot(inherits(res, "error")) |
| 115 |
|
| 116 |
Sys.setenv(PBS_NODEFILE = "<non-existing-file>") |
| 117 |
res <- tryCatch({ |
| 118 |
workers <- availableWorkers(methods = "PBS") |
| 119 |
}, warning = identity) |
| 120 |
stopifnot(inherits(res, "warning")) |
| 121 |
|
| 122 |
message("*** read_pbs_nodefile() ... DONE") |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
message("*** read_pjm_nodefile() ...") |
| 128 |
|
| 129 |
workersT <- unique(workers0) |
| 130 |
pathname <- tempfile() |
| 131 |
writeLines(workersT, con = pathname) |
| 132 |
message(sprintf("Workers in 'PJM_O_NODEINF' file: [n=%d] %s", length(workersT), paste(sQuote(workersT), collapse = ", "))) |
| 133 |
data <- read_pjm_nodefile(pathname) |
| 134 |
str(data) |
| 135 |
stopifnot( |
| 136 |
c("node") %in% colnames(data), |
| 137 |
is.character(data$node), |
| 138 |
!anyNA(data$node), |
| 139 |
nrow(data$node) == length(workersT), |
| 140 |
all(sort(data$node) == sort(workersT)), |
| 141 |
identical(data$node, unique(data$node)) |
| 142 |
) |
| 143 |
|
| 144 |
Sys.setenv(PJM_O_NODEINF = pathname) |
| 145 |
|
| 146 |
message("- PJM_VNODE_CORE=1") |
| 147 |
Sys.setenv(PJM_VNODE_CORE = "1") |
| 148 |
|
| 149 |
## Clear memoization cache |
| 150 |
environment(availableCoresPJM)$n <- NULL |
| 151 |
workers <- availableWorkers(methods = "PJM") |
| 152 |
print(workers) |
| 153 |
stopifnot( |
| 154 |
length(workers) == length(workersT), |
| 155 |
all(sort(workers) == sort(workersT)) |
| 156 |
) |
| 157 |
|
| 158 |
message("- PJM_VNODE=", length(workersT)) |
| 159 |
message("- PJM_VNODE_CORE=2") |
| 160 |
Sys.setenv(PJM_VNODE = length(workersT)) |
| 161 |
Sys.setenv(PJM_VNODE_CORE = "2") |
| 162 |
## Clear memoization cache |
| 163 |
environment(availableCoresPJM)$n <- NULL |
| 164 |
workers <- availableWorkers(methods = "PJM") |
| 165 |
print(workers) |
| 166 |
stopifnot( |
| 167 |
length(workers) == 2L * length(workersT), |
| 168 |
all(workers %in% workersT), |
| 169 |
all(workersT %in% workers) |
| 170 |
) |
| 171 |
|
| 172 |
message("- PJM_VNODE=1 (incompatible => warning)") |
| 173 |
message("- PJM_VNODE_CORE=2") |
| 174 |
Sys.setenv(PJM_VNODE = "1") |
| 175 |
Sys.setenv(PJM_VNODE_CORE = "2") |
| 176 |
## Clear memoization cache |
| 177 |
environment(availableCoresPJM)$n <- NULL |
| 178 |
workers <- availableWorkers(methods = "PJM") |
| 179 |
print(workers) |
| 180 |
stopifnot( |
| 181 |
length(workers) == 2L * length(workersT), |
| 182 |
all(workers %in% workersT), |
| 183 |
all(workersT %in% workers) |
| 184 |
) |
| 185 |
|
| 186 |
|
| 187 |
message("- PJM_O_NODEINF = <non-existing-file>") |
| 188 |
Sys.setenv(PJM_O_NODEINF = "<non-existing-file>") |
| 189 |
res <- tryCatch({ |
| 190 |
workers <- availableWorkers(methods = "PJM") |
| 191 |
}, warning = identity) |
| 192 |
stopifnot(inherits(res, "warning")) |
| 193 |
|
| 194 |
message("*** read_pjm_nodefile() ... DONE") |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
message("*** read_pe_hostfile() ...") |
| 199 |
|
| 200 |
workers <- workers0 |
| 201 |
pathname <- tempfile() |
| 202 |
write.table(data0, file = pathname, quote = FALSE, row.names = FALSE, col.names = FALSE) |
| 203 |
lines <- readLines(pathname) |
| 204 |
print(lines) |
| 205 |
data <- read_pe_hostfile(pathname, expand = FALSE) |
| 206 |
print(data) |
| 207 |
stopifnot( |
| 208 |
is.character(data$node), |
| 209 |
!anyNA(data$node), |
| 210 |
is.integer(data$count), |
| 211 |
!anyNA(data$count), |
| 212 |
all(is.finite(data$count)), |
| 213 |
all(data$count > 0), |
| 214 |
nrow(data) == nrow(data0), |
| 215 |
all.equal(data[, c("node", "count")], data0[, c("node", "count")]) |
| 216 |
) |
| 217 |
|
| 218 |
workers <- sge_expand_node_count_pairs(data) |
| 219 |
stopifnot(length(workers) == length(workers0), all(workers == sort(workers0))) |
| 220 |
|
| 221 |
Sys.setenv(PE_HOSTFILE = pathname) |
| 222 |
Sys.setenv(NSLOTS = length(workers0)) ## Use to validate results |
| 223 |
workers <- availableWorkers(methods = "SGE") |
| 224 |
print(workers) |
| 225 |
stopifnot(length(workers) == length(workers0), all(workers == sort(workers0))) |
| 226 |
|
| 227 |
## Test validation |
| 228 |
Sys.setenv(NSLOTS = length(workers0) + 1L) |
| 229 |
workers <- tryCatch(availableWorkers(methods = "SGE"), warning = identity) |
| 230 |
print(workers) |
| 231 |
stopifnot(inherits(workers, "warning")) |
| 232 |
|
| 233 |
Sys.setenv(PE_HOSTFILE = "<non-existing-file>") |
| 234 |
res <- tryCatch({ |
| 235 |
workers <- availableWorkers(methods = "SGE") |
| 236 |
}, warning = identity) |
| 237 |
stopifnot(inherits(res, "warning")) |
| 238 |
|
| 239 |
message("*** read_pe_hostfile() ... DONE") |
| 240 |
|
| 241 |
|
| 242 |
message("*** Slurm w/ SLURM_JOB_NODELIST ...") |
| 243 |
|
| 244 |
slurm_expand_nodelist <- parallelly:::slurm_expand_nodelist |
| 245 |
|
| 246 |
specs <- list( |
| 247 |
"n1" = c("n1"), |
| 248 |
" n1" = c("n1"), |
| 249 |
"n1,, n3" = c("n1", "n3"), |
| 250 |
"n1, n3" = c("n1", "n3"), |
| 251 |
"n3 n1" = c("n3", "n1"), |
| 252 |
"n[1-13]" = sprintf("n%d", c(1:13)), |
| 253 |
## scontrol show hostname treats "n[1,3-4, 11-13]" == "n[1,3-4,0011-13]" |
| 254 |
"n[1,3-4, 11-13]" = c("n1", "n3", "n4", "n0011", "n0012", "n0013"), |
| 255 |
"a1,b[ 02-04,6-7]" = c("a1", "b00002", "b00003", "b00004", "b6", "b7"), |
| 256 |
"b04-[09-11]" = c("b04-09", "b04-10", "b04-11"), |
| 257 |
"b04-[009-011]" = c("b04-009", "b04-010", "b04-011"), |
| 258 |
"b04-[0009-11]" = c("b04-0009", "b04-0010", "b04-0011"), |
| 259 |
"b04-[9-011]" = c("b04-9", "b04-10", "b04-11") |
| 260 |
) |
| 261 |
|
| 262 |
## All combined |
| 263 |
all <- list(unlist(specs, use.names = FALSE)) |
| 264 |
names(all) <- paste(names(specs), collapse = ",") |
| 265 |
specs <- c(specs, all) |
| 266 |
|
| 267 |
## Again, all combined but in reverse order |
| 268 |
all <- list(unlist(rev(specs), use.names = FALSE)) |
| 269 |
names(all) <- paste(rev(names(specs)), collapse = ",") |
| 270 |
specs <- c(specs, all) |
| 271 |
|
| 272 |
for (kk in seq_along(specs)) { |
| 273 |
message(sprintf("- Specification #%d of %d", kk, length(specs))) |
| 274 |
nodelist <- names(specs)[kk] |
| 275 |
truth <- specs[[kk]] |
| 276 |
cat(sprintf("nodelist: %s\n", sQuote(nodelist))) |
| 277 |
expanded <- slurm_expand_nodelist(nodelist, manual = TRUE) |
| 278 |
cat(sprintf("expanded: c(%s)\n", paste(sQuote(expanded), collapse = ", "))) |
| 279 |
cat(sprintf("truth: c(%s)\n", paste(sQuote(truth), collapse = ", "))) |
| 280 |
stopifnot( |
| 281 |
is.character(expanded), |
| 282 |
!any(is.na(expanded)), |
| 283 |
length(expanded) == length(truth), |
| 284 |
identical(expanded, truth) |
| 285 |
) |
| 286 |
|
| 287 |
Sys.unsetenv(c("SLURM_JOB_NODELIST", "SLURM_NODELIST", |
| 288 |
"SLURM_JOB_CPUS_PER_NODE", "SLURM_TASKS_PER_NODE")) |
| 289 |
|
| 290 |
## Test without SLURM_JOB_CPUS_PER_NODE/SLURM_TASKS_PER_NODE |
| 291 |
Sys.setenv(SLURM_JOB_NODELIST = nodelist) |
| 292 |
for (name in c("SLURM_JOB_NODELIST", "SLURM_JOB_CPUS_PER_NODE")) { |
| 293 |
cat(sprintf("%s = %s\n", name, sQuote(Sys.getenv(name)))) |
| 294 |
} |
| 295 |
workers <- availableWorkers(methods = "Slurm") |
| 296 |
cat(sprintf("workers: c(%s)\n", paste(sQuote(workers), collapse = ", "))) |
| 297 |
stopifnot(identical(workers, truth)) |
| 298 |
|
| 299 |
|
| 300 |
## Test with SLURM_JOB_CPUS_PER_NODE/SLURM_TASKS_PER_NODE |
| 301 |
nhosts <- length(expanded) |
| 302 |
ncores_per_host <- sample(1:10, size = nhosts, replace = TRUE) |
| 303 |
|
| 304 |
## Handle the case when 'nodelist' result in a non-ordered 'expanded' |
| 305 |
expanded2 <- as.list(expanded) |
| 306 |
for (kk in seq_along(expanded2)) { |
| 307 |
expanded2[[kk]] <- rep(expanded2[[kk]], times = ncores_per_host[kk]) |
| 308 |
} |
| 309 |
expanded2 <- unlist(expanded2, use.names = FALSE) |
| 310 |
|
| 311 |
Sys.setenv(SLURM_JOB_NODELIST = nodelist) |
| 312 |
Sys.setenv(SLURM_JOB_CPUS_PER_NODE = paste(ncores_per_host, collapse = ",")) |
| 313 |
|
| 314 |
for (name in c("SLURM_JOB_NODELIST", "SLURM_JOB_CPUS_PER_NODE")) { |
| 315 |
cat(sprintf("%s = %s\n", name, sQuote(Sys.getenv(name)))) |
| 316 |
} |
| 317 |
workers <- availableWorkers(methods = "Slurm") |
| 318 |
cat(sprintf("workers: c(%s)\n", paste(sQuote(workers), collapse = ", "))) |
| 319 |
stopifnot(identical(unique(workers), unique(truth))) |
| 320 |
counts <- table(workers) |
| 321 |
counts <- counts[unique(workers)] |
| 322 |
print(counts) |
| 323 |
counts2 <- table(expanded2) |
| 324 |
counts2 <- counts2[unique(expanded2)] |
| 325 |
print(counts2) |
| 326 |
stopifnot( |
| 327 |
sum(counts) == sum(ncores_per_host), |
| 328 |
sum(counts) == sum(counts2), |
| 329 |
all(counts == counts2) |
| 330 |
) |
| 331 |
|
| 332 |
Sys.unsetenv(c("SLURM_JOB_NODELIST", "SLURM_JOB_CPUS_PER_NODE")) |
| 333 |
} |
| 334 |
|
| 335 |
message("*** Slurm w/ SLURM_JOB_NODELIST ... DONE") |
| 336 |
|
| 337 |
|
| 338 |
message("*** Slurm w/ SLURM_TASKS_PER_NODE ...") |
| 339 |
|
| 340 |
slurm_expand_nodecounts <- parallelly:::slurm_expand_nodecounts |
| 341 |
|
| 342 |
specs <- list( |
| 343 |
"1" = c(1L), |
| 344 |
"1,3" = c(1L,3L), |
| 345 |
"1, 3" = c(1L,3L), |
| 346 |
"2(x3)" = rep(2L, times = 3L), |
| 347 |
"2(x3),3,4(x1)" = c(rep(2L, times = 3L), 3L, 4L) |
| 348 |
) |
| 349 |
|
| 350 |
for (kk in seq_along(specs)) { |
| 351 |
message(sprintf("- Specification #%d of %d", kk, length(specs))) |
| 352 |
nodecounts <- names(specs)[kk] |
| 353 |
truth <- specs[[kk]] |
| 354 |
cat(sprintf("nodecounts: %s\n", sQuote(nodecounts))) |
| 355 |
expanded <- slurm_expand_nodecounts(nodecounts) |
| 356 |
cat(sprintf("expanded: c(%s)\n", paste(sQuote(expanded), collapse = ", "))) |
| 357 |
cat(sprintf("truth: c(%s)\n", paste(sQuote(truth), collapse = ", "))) |
| 358 |
stopifnot( |
| 359 |
is.integer(expanded), |
| 360 |
!any(is.na(expanded)), |
| 361 |
length(expanded) == length(truth), |
| 362 |
identical(expanded, truth) |
| 363 |
) |
| 364 |
} |
| 365 |
|
| 366 |
message("*** Slurm w/ SLURM_TASKS_PER_NODE ... DONE") |
| 367 |
|
| 368 |
message("*** Slurm odds'n'ends ...") |
| 369 |
# Respect SLURM_CPUS_PER_TASK, if set |
| 370 |
Sys.setenv( |
| 371 |
SLURM_JOB_NODELIST = "n1", |
| 372 |
SLURM_JOB_CPUS_PER_NODE = "10(x1)", |
| 373 |
SLURM_CPUS_PER_TASK = "9" |
| 374 |
) |
| 375 |
w <- parallelly:::availableWorkersSlurm() |
| 376 |
str(w) |
| 377 |
stopifnot(length(w) == 9L, all(w == "n1")) |
| 378 |
message("*** Slurm odds'n'ends ... DONE") |
| 379 |
|
| 380 |
message("*** HPC related ... DONE") |
| 381 |
|
| 382 |
|
| 383 |
## Any R options and system environment variable |
| 384 |
print(availableWorkers(methods = c("width", "FOO_BAR_ENV"), |
| 385 |
na.rm = FALSE, which = "all")) |
| 386 |
|
| 387 |
## Exception handling |
| 388 |
Sys.setenv("FOO_BAR_ENV" = "0") |
| 389 |
res <- tryCatch(availableWorkers(methods = "FOO_BAR_ENV"), error = identity) |
| 390 |
stopifnot(inherits(res, "error")) |
| 391 |
|
| 392 |
message("*** availableWorkers() ... DONE") |