Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
habrok:examples:r [2023/03/22 13:28] fokkehabrok:examples:r [2025/06/11 12:15] (current) – [Multiple CPUs] lint pedro
Line 7: Line 7:
 In order to run a simple R script on one core, two files should be constructed. The first file is the R script (with the .R extension), here it is called ''%%R_example1.R%%'' and it holds the following textual contents: In order to run a simple R script on one core, two files should be constructed. The first file is the R script (with the .R extension), here it is called ''%%R_example1.R%%'' and it holds the following textual contents:
  
-<code R_example1.R>+<code rsplus R_example1.R>
 # Simple t-test between two equally large groups. # Simple t-test between two equally large groups.
 # Let us generate some data. # Let us generate some data.
-apples <- rnorm(20,mean=1.5,sd=.1) +apples <- rnorm(20, mean = 1.5, sd = .1) 
-pears <- rnorm(20,mean=1.6,sd=.1) +pears <- rnorm(20, mean = 1.6, sd = .1) 
-print(t.test(pears,apples))+print(t.test(pears, apples))
 </code> </code>
  
Line 46: Line 46:
 The final output should look like: The final output should look like:
  
-<code r>+<code rsplus>
     Welch Two Sample t-test     Welch Two Sample t-test
  
Line 81: Line 81:
 The file containing the R code is named ''%%parallel.R%%''. Note that the package ''%%parallel%%'' and ''%%snow%%'' are used but others might be available as well. If necessary additional packages have to be installed, see [[habrok:software_environment:installation_of_extra_applications_or_libraries#r|this page]]. The file containing the R code is named ''%%parallel.R%%''. Note that the package ''%%parallel%%'' and ''%%snow%%'' are used but others might be available as well. If necessary additional packages have to be installed, see [[habrok:software_environment:installation_of_extra_applications_or_libraries#r|this page]].
  
-<code parallel.R>+<code rsplus parallel.R>
 library("snow") library("snow")
 library("parallel") library("parallel")
  
 cpu <- Sys.getenv("SLURM_CPUS_ON_NODE", 1) # Number of cores requested (use 1 core if running outside a job). cpu <- Sys.getenv("SLURM_CPUS_ON_NODE", 1) # Number of cores requested (use 1 core if running outside a job).
-hosts <- rep("localhost",cpu)+hosts <- rep("localhost", cpu)
 cl <- makeCluster(hosts, type = "SOCK") cl <- makeCluster(hosts, type = "SOCK")
 # Create random matrices. # Create random matrices.
 n <- 5000 n <- 5000
-A <- matrix(rnorm(n^2),n) +A <- matrix(rnorm(n^2), n) 
-B <- matrix(rnorm(n^2),n)+B <- matrix(rnorm(n^2), n)
  
 # Single core time of matrix multiplication of matrices # Single core time of matrix multiplication of matrices
Line 126: Line 126:
 In order to use multiple nodes with R, you can make use of an MPI cluster in your R code. Due to the way our MPI libraries are installed, it's not possible to use the makeCluster or makeMPIcluster or makeCluster, though. The correct way to do it is by making use of the getMPIcluster function, as shown in the following example: In order to use multiple nodes with R, you can make use of an MPI cluster in your R code. Due to the way our MPI libraries are installed, it's not possible to use the makeCluster or makeMPIcluster or makeCluster, though. The correct way to do it is by making use of the getMPIcluster function, as shown in the following example:
  
-<code r>+<code rsplus>
 library("snow") library("snow")
 library("parallel") library("parallel")
Line 142: Line 142:
 #SBATCH --mem=10GB #SBATCH --mem=10GB
  
-module load R/3.4.4-foss-2018a-X11-20180131+module purge 
 +module load R/4.2.1-foss-2022a
  
 srun ${EBROOTR}/lib64/R/library/snow/RMPISNOW < parallel.R srun ${EBROOTR}/lib64/R/library/snow/RMPISNOW < parallel.R
Line 167: Line 168:
 Within this session, installing ''gpuR'' is rather straightforward: Within this session, installing ''gpuR'' is rather straightforward:
  
-<code R>+<code rsplus>
 library(devtools) library(devtools)
 install_github("cdeterman/gpuR") install_github("cdeterman/gpuR")
Line 189: Line 190:
 In this example we use a R script that does a simple GPU matrix multiplication and a normal CPU matrix multiplication. In this example we use a R script that does a simple GPU matrix multiplication and a normal CPU matrix multiplication.
  
-<code gpu.R>+<code rsplus gpu.R>
 library("gpuR") library("gpuR")
 ORDER = 10000 ORDER = 10000