R: Difference between revisions

Jump to navigation Jump to search
1,801 bytes added ,  6 years ago
no edit summary
(Marked this version for translation)
No edit summary
Line 292: Line 292:
export R_LIBS=~/local/R_libs/
export R_LIBS=~/local/R_libs/
R CMD BATCH --no-save --no-restore test_foreach.R
R CMD BATCH --no-save --no-restore test_foreach.R
}}
<!--T:44-->
3. Submit the job with:
<!--T:45-->
<source lang="bash">
sbatch job_foreach.sh
</source>
<!--T:47-->
For more on submitting jobs, see the [[Running jobs]] page.
=== doParallel and makeCluster ===
====Running==== <!--T:40-->
1. Place your R code in a script file, in this case the file is called <tt>test_makecluser.R</tt>.
<!--T:41-->
{{File
  |name=test_makecluster.R
  |lang="r"
  |contents=
library(doParallel)
# Create an array from the NODESLIST environnement variable
nodeslist = unlist(strsplit(Sys.getenv("NODESLIST"), split=" "))
# Create the cluster with the nodes name. One process per count of node name.
# nodeslist = node1 node1 node2 node2, means we are starting 2 processes on node1, likewise on node2.
cl = makeCluster(nodeslist, type = "PSOCK")
registerDoParallel(cl)
# Compute
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
    foreach(icount(trials), .combine=cbind) %dopar%
    {
        ind <- sample(100, 100, replace=TRUE)
        result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
        coefficients(result1)
    }
})
ptime[3]
# Don't forget to release resources
stopCluster(cl)
}}
2. Copy the following content in a job submission script called ''job_makecluser.sh'':
{{File
  |name=job_foreach.sh
  |lang="bash"
  |contents=
#!/bin/bash
#SBATCH --account=def-someacct  # replace this with your own account
#SBATCH --ntasks=4              # number of processes
#SBATCH --mem-per-cpu=512M      # memory; default unit is megabytes
#SBATCH --time=00:05:00          # time (HH:MM:SS)
module load r/3.5.0
# Export the nodes names.
# If all processes are allocated on the same node, NODESLIST contains : node1 node1 node1 node1
export NODESLIST=$(echo $(srun hostname))
R -f test_makecluster.R
}}
}}


cc_staff
311

edits

Navigation menu