# Example multi-node parallel job


library(parallel)

data("iris")

#Creating our Model list
numeric_columns <- colnames(iris)[1:4]
allModelsList <- list()

for (dependent_var in numeric_columns) {
  predictors <- setdiff(numeric_columns, dependent_var)
  combs <- unlist(lapply(1:3, function(i) combn(predictors, i, simplify = FALSE)), recursive = FALSE)
  for (comb in combs) {
    model_formula <- as.formula(paste(c(paste(dependent_var, "~ 1"), comb), collapse = " + "))
    allModelsList[[paste(dependent_var, paste(comb, collapse = "_"), sep = "_")]] <- model_formula
  }
}

# Creating the regression function
run_regression <- function(formula) {
  model <- lm(formula, data = iris)
  return(summary(model))
}

# -----------------------------
# Parallelization process
# -----------------------------

# Find out how many nodes and tasks SLURM has asked of the program
nodelist <- Sys.getenv("SLURM_NODELIST")
ntasks   <- as.integer(Sys.getenv("SLURM_NTASKS"))

# Expanding node names so they can actually be used to run the cluster
expand_nodelist <- function(nodelist) {
  system(sprintf("scontrol show hostnames %s", nodelist), intern = TRUE)
}
nodes <- expand_nodelist(nodelist)



# Assign your workers per each node in our 4 tasks/ 2 nodes
workers_per_node <- max(1, ntasks %/% length(nodes))

# -----------------------------
# Start Cluster
# -----------------------------
cl <- makePSOCKcluster(rep(nodes, each = workers_per_node))

# Printing the nodes to make sure we are in two separate ones
node_info <- clusterCall(cl, function() Sys.info()[c("nodename", "machine")])
print(node_info)

# Key part of distributed memory, we need to send out our variables to each processign unit
clusterExport(cl, varlist = c("iris", "allModelsList", "run_regression"))


time_psock <- system.time({
  results_psock <- parLapply(cl, allModelsList, run_regression)
})

print(time_psock)

# RDS for results so we can import it to future R files
saveRDS(results_psock, file = "regression_results.rds")

stopCluster(cl)
