Skip to content

Commit 8a83b5e

Browse files
committed
Fixed overhead in runbg() on knechte & ruprechte
1 parent 53e1476 commit 8a83b5e

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

R/remoteComputing.R

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,29 +223,42 @@ runbg <- function(..., machine = "localhost", filename = NULL, input = ls(.Globa
223223
sep = "\n"
224224
))
225225

226-
# Write program code into file
227-
for (m in 1:nmachines) cat(program[[m]], file = paste0(filename[m], ".R"))
228-
229-
# Copy files to temporal folder
226+
# Copy files to the temporary folder on the remote machine, write code and run
230227
for (m in 1:nmachines) {
231228

232-
system(paste0("ssh ", machine[m], " mkdir ", filename[m], "_folder/"), ignore.stdout = TRUE, ignore.stderr = TRUE)
233-
system(paste0("ssh ", machine[m], " rm ", filename[m], "_folder/*"), ignore.stdout = TRUE, ignore.stderr = TRUE)
229+
# Write program code into file
230+
cat(program[[m]], file = paste0(filename[m], ".R"))
231+
232+
# Create a working directory on the remote machine
233+
system(paste0("ssh ", machine[m], " mkdir -p ", filename[m], "_folder/"),
234+
ignore.stdout = TRUE, ignore.stderr = TRUE)
235+
236+
# Remove any old files in the working directory
237+
system(paste0("ssh ", machine[m], " rm -r ", filename[m], "_folder/*"),
238+
ignore.stdout = TRUE, ignore.stderr = TRUE)
239+
240+
# Copy RData files to the working directory on the remote machine
234241
system(paste0("scp ", getwd(), "/", filename0, ".RData* ", machine[m], ":", filename[m], "_folder/"))
242+
243+
# Copy the R scripts to the working directory on the remote machine
235244
system(paste0("scp ", getwd(), "/", filename[m], ".R* ", machine[m], ":", filename[m], "_folder/"))
236-
245+
246+
# Copy compiled files if `compile` is set to TRUE, otherwise copy shared object files
237247
if (compile) {
238248
system(paste0("scp ", getwd(), "/*.c ", getwd(), "/*.cpp ", machine[m], ":", filename[m], "_folder/"))
239249
} else {
240250
system(paste0("scp ", getwd(), "/*.so ", machine[m], ":", filename[m], "_folder/"))
241251
}
242252

253+
# Execute the R script on the remote machine with the necessary environment variables
254+
# export OMP_NUM_THREADS=1 & MKL_NUM_THREADS=1 ensure that each job uses only one thread
255+
system(paste0(
256+
"ssh ", machine[m],
257+
" 'export OMP_NUM_THREADS=1 && export MKL_NUM_THREADS=1 && ",
258+
"R CMD BATCH --vanilla ", filename[m], "_folder/", filename[m], ".R'"
259+
), intern = FALSE, wait = wait)
243260
}
244261

245-
246-
247-
# Run in background
248-
for (m in 1:nmachines) system(paste0("ssh ", machine[m], " R CMD BATCH --vanilla ", filename[m], "_folder/", filename[m], ".R"), intern = FALSE, wait = wait)
249262

250263
if (wait) {
251264
out$get()

0 commit comments

Comments
 (0)