Skip to content

Instantly share code, notes, and snippets.

@BlasBenito
Last active January 4, 2021 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlasBenito/93ee54d3a98d101754aaff0d658dccca to your computer and use it in GitHub Desktop.
Save BlasBenito/93ee54d3a98d101754aaff0d658dccca to your computer and use it in GitHub Desktop.
#generates a cluster specification for the "spec" argument of the parallel::makeCluster() function
#ips: vector of computer ips within the local network: c('10.42.0.1', '10.42.0.34', '10.42.0.104')
#cores: vector of integers with the number of cores available on each computer, in the same order as ips: cores = c(7, 4, 4)
#user: character, name of the user in the different computers. Only one user name allowed.
#returns a list that can be used directly as input for the "spec" argument of makeCluster().
cluster_spec <- function(
ips,
cores,
user
){
#creating initial list
spec <- list()
for(i in 1:length(ips)){
spec[[i]] <- list()
spec[[i]]$host <- ips[i]
spec[[i]]$user <- user
spec[[i]]$ncore <- cores[i]
}
#generating nodes from the list of machines
spec <- lapply(
spec,
function(spec.i) rep(
list(
list(
host = spec.i$host,
user = spec.i$user)
),
spec.i$ncore
)
)
#formating into a list of lists
spec <- unlist(
spec,
recursive = FALSE
)
return(spec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment