technical:whitepaper:r-runtime-blas-lapack

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
Next revisionBoth sides next revision
technical:whitepaper:r-runtime-blas-lapack [2018-12-10 11:36] – [Substituting an alternate library] freytechnical:whitepaper:r-runtime-blas-lapack [2018-12-10 12:07] frey
Line 30: Line 30:
 A simple way to organize multiple underlying BLAS/LAPACK libraries in a single R installation is to create subdirectories for each variant: A simple way to organize multiple underlying BLAS/LAPACK libraries in a single R installation is to create subdirectories for each variant:
  
- Path^Description^ +^Path  ^Description^ 
- ''**R_PREFIX**/lib64/R/lib''|base directory where R looks for shared libraries by default| +|''**R_PREFIX**/lib64/R/lib''  |base directory where R looks for shared libraries by default| 
-|  ''**R_PREFIX**/lib64/R/lib/atlas''|directory to hold ''libatlas.so''+|''**R_PREFIX**/lib64/R/lib/libRblas.so''  |symlink to chosen BLAS library (from one of the subdirectories herein)| 
- ''**R_PREFIX**/lib64/R/lib/rblas''|directory to hold the bundled ''libRblas.so'' and ''libRlapack.so'' produced by R build procedure| +|''**R_PREFIX**/lib64/R/lib/libRlapack.so''  |symlink to chosen LAPACK library (from one of the subdirectories herein)| 
- ''**R_PREFIX**/lib64/R/lib/mkl''|directory to hold MKL variants| +|''**R_PREFIX**/lib64/R/lib/atlas''  |directory to hold ''libatlas.so''
- ''**R_PREFIX**/lib64/R/lib/mkl/seq''|directory to hold sequential MKL variant| +|''**R_PREFIX**/lib64/R/lib/rblas''  |directory to hold the bundled ''libRblas.so'' and ''libRlapack.so'' produced by R build procedure| 
- ''**R_PREFIX**/lib64/R/lib/mkl/thr''|directory to hold threaded MKL variant|+|''**R_PREFIX**/lib64/R/lib/mkl''  |directory to hold MKL variants| 
 +|''**R_PREFIX**/lib64/R/lib/mkl/seq''  |directory to hold sequential MKL variant| 
 +|''**R_PREFIX**/lib64/R/lib/mkl/thr''  |directory to hold threaded MKL variant|
  
 +==== R BLAS/LAPACK ====
  
 +When we restructured the R ''lib64/R/lib'' directory, the bundled ''libRblas.so'' and ''libRlapack.so'' shared library files were moved to the ''rblas'' subdirectory.  To configure R to use its bundled libraries:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib
 +$ rm -f libR{blas,lapack}.so
 +$ ln -s rblas/libRblas.so .
 +$ ln -s rblas/libRlapack.so .
 +</code>
 +
 +==== ATLAS ====
 +
 +The ATLAS library contains both BLAS and LAPACK APIs in a single shared library.  With ''libatlas.so'' copied into the ''**R_PREFIX**/lib64/R/lib/atlas'' subdirectory, we configure R to use ATLAS:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib
 +$ rm -f libR{blas,lapack}.so
 +$ ln -s atlas/libRblas.so .
 +$ ln -s atlas/libRlapack.so .
 +</code>
 +
 +==== Sequential MKL ====
 +
 +A C source file containing a dummy function was created in ''**R_PREFIX**/lib64/R/lib/mkl/seq'':
 +
 +<file C shim.c>
 +int
 +mkl_shim_dummy(void)
 +{
 + return 0;
 +}
 +</file>
 +
 +The shim library is then created thusly:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib/mkl/seq
 +$ icc -shared -o libRblas.so -mkl=sequential shim.c
 +$ ln -s libRblas.so libRlapack.so
 +</code>
 +
 +To configure R to use the sequential MKL:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib
 +$ rm -f libR{blas,lapack}.so
 +$ ln -s mkl/seq/libRblas.so .
 +$ ln -s mkl/seq/libRlapack.so .
 +</code>
 +
 +==== Threaded MKL ====
 +
 +A C source file containing a dummy function was created in ''**R_PREFIX**/lib64/R/lib/mkl/thr'':
 +
 +<file C shim.c>
 +int
 +mkl_shim_dummy(void)
 +{
 + return 0;
 +}
 +</file>
 +
 +Since our R build used the GNU C compiler, the threaded MKL variant only works if the shim library is built against the GNU OpenMP runtime.  Using just "-mkl=parallel" links against the Intel OpenMP runtime which in testing yielded numerical issues (not actual crashes).  The shim library is then created thusly:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib/mkl/thr
 +$ icc -shared -o libRblas.so shim.c -lmkl_gnu_thread -lmkl_core -lmkl_intel_lp64
 +$ ln -s libRblas.so libRlapack.so
 +</code>
 +
 +To configure R to use the threaded MKL:
 +
 +<code bash>
 +$ cd ${R_PREFIX}/lib64/R/lib
 +$ rm -f libR{blas,lapack}.so
 +$ ln -s mkl/thr/libRblas.so .
 +$ ln -s mkl/thr/libRlapack.so .
 +</code>
  
 ===== Runtime-configurable substitution ===== ===== Runtime-configurable substitution =====
 +
 +By stashing each BLAS/LAPACK variant in its own subdirectory our copy of R is actually fairly close to being 
  • technical/whitepaper/r-runtime-blas-lapack.txt
  • Last modified: 2018-12-10 12:40
  • by frey