Using R and snowfall in batch

From CAC Documentation wiki
Jump to navigation Jump to search

You can use the following test script as a starting point for creating your own batch script to run snowfall jobs from R. It assumes that R and snowfall are already installed on the compute nodes. The script simply returns the name of the machine (node) where each slave/worker is running. The value of ppn should be set appropriately for your cluster.

#!/bin/bash
#PBS -l nodes=2:ppn=8,walltime=10:00
#PBS -N test
#PBS -j oe
#PBS -q default
#PBS -S /bin/bash
cd $PBS_O_WORKDIR
R --vanilla > mysnow.out <<EOF
library(snowfall)
pbsnodefile = Sys.getenv("PBS_NODEFILE")
machines <- scan(pbsnodefile, what="")
machines
nmach = length(machines)
nmach
sfInit(parallel=TRUE,type='SOCK',cpus=nmach,socketHosts=machines)
sfLapply(1:nmach, function(x) system("hostname",intern=TRUE))
sfStop()
EOF

The commands between the <<EOF and EOF are what is know in Linux as a "here document" or a "hereis". This is just a way of telling the shell that the enclosed commands are meant to be input to the preceding shell command (R in this case). These same R commands could equally well be saved in a separate document and provided to R as a BATCH file.

There are two things to note about the use of snowfall in the above script. First, it uses SOCK rather than MPI for communications, so there is no need to start R with mpirun, and no dependency on Rmpi. Second, in sfInit, the number of cpus is assigned based on the size of the machines list, but it could be set explicitly to some different number.

The output of the parallel command sfLapply proves that 16 slaves are launched, 8 on one node, and 8 on the other. Curiously, the slaves seem to have no real environment defined, as Sys.getenv("HOSTNAME") returns nothing.

Note that SOCK-style communications will not work on some clusters (e.g., DSS1) if SSH keys have beeen set up improperly in $HOME/.ssh. Usually, the proper setup is created automatically when a new user logs in to a cluster for the first time. If you suspect that there is a problem with your SSH keys, try the following: rename $HOME/.ssh/id_rsa to something else, log out, and log back in.

For more information, see the main snowfall page at IMBI Freiburg.