Commit 2cf7b5a2 authored by Konstantin Kuck's avatar Konstantin Kuck 💬
Browse files

Add example for multithreades matrix operation (OpenBLAS)

parent 7181837e
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
#OpenBLAS-multithreading.R

# This example illustrates the benefit of multi-threading in the context
# matrix operations (OpenBLAS).
library(RhpcBLASctl)  ## controls number of threads from within R

set.seed(2026)
X <- matrix(rnorm(5000^2), 5000)


# No multi-threading
blas_set_num_threads(1)
cat("Compute time for single-threaded OpenBLAS\n")
stime <- system.time({
   Xinv <- solve(X)
})[3]

print(stime)


# Two threads
blas_set_num_threads(2)
cat("Compute time for OpenBLAS with two threads\n")
system.time({
   Xinv <- solve(X)
})[3]



# Four threads
blas_set_num_threads(4)
cat("Compute time for OpenBLAS with four threads\n")
ptime <- system.time({
   Xinv <- solve(X)
})[3]

print(ptime)

cat(sprintf('Speed up for %d threads: %f\n',
            blas_get_num_procs(), round(stime / ptime, digits=2)))
+19 −0
Original line number Diff line number Diff line
#!/bin/bash
#
#SBATCH --job-name=r-openblas-multithreading
#SBATCH --comment="Multi-threading with OpenBLAS."
#SBATCH --partition=dev_cpu
#SBATCH --nodes=1
#SBATCH --tasks-per-node=4
#SBATCH --mem-per-cpu=1G
#SBATCH --time=00:05:00

# Make sure to specify the version
module purge
module load math/R/4.5.1

# Change to submission directory of this job (not needed)
cd "$SLURM_SUBMIT_DIR"

# Start program
R CMD BATCH --no-save --no-restore --slave OpenBLAS-multithreading.R