Sunday, April 21, 2013

Compiling R linking to MKL using gcc

I use R extensively for research, so making R faster with MKL is useful for me. Since MKL is free for non-commercial use, I decided to compile R for my computer that links to the fast MKL library.

Two configurations are covered here, one using multi-threading, and one using the sequential version of the library. Although using multi-threading makes certain matrix operations faster, if I have to run many jobs (bootstrap, cross-validation, etc), total time reduction is greater if I run jobs in parallel (using multicore package for R) rather than linear algebra operation in parallel. Hence the two different compilations.

I used gcc as the compiler rather than icc from intel since some packages on CRAN does not compile with icc. I do not know enough about compilers to know if some flags will fix this.

 I found that the configure script in R-3.0.0 does not find some functions in MKL correctly. I traced the cause of this problem to the following lines:
 29818a29819,29821  
 >  # if ${CC} ${CFLAGS} ${LDFLAGS} ${MAIN_LDFLAGS} -o conftest${ac_exeext} \  
 >  #   conftest.${ac_objext} ${FLIBS} \  
 >  #   ${LIBM} ${BLAS_LIBS} 1>&5 2>&5;  
 29820,29821c29823,29824  
 <    conftest.${ac_objext} ${FLIBS} \  
 <    ${LIBM} ${BLAS_LIBS} 1>&5 2>&5;  
 ---  
 >    conftest.${ac_objext} ${BLAS_LIBS} ${FLIBS} \  
 >    ${LIBM} 1>&5 2>&5;  


To compile R using MKL sequential library,

  1. Apply the patch to configure script
  2. Add the following lines to config.site file
     CFLAGS='-g -O3 '                                                                                             
     FFLAGS='-g -O3 '                                                                                             
     CXXFLAGS='-g -O3 '                                                                                            
     FCFLAGS='-g -O3 '   
       
     MKL_LIB_PATH=/opt/intel/composer_xe_2013.3.163/mkl/lib/intel64  
     MKL=" -L${MKL_LIB_PATH}    \                                                                                      
        -Wl,--start-group    \                                                                                      
          -lmkl_gf_lp64    \                                                                                      
          -lmkl_sequential  \                                                                                      
          -lmkl_core     \                                                                                      
          -lm         \                                                                                      
        -Wl,--end-group     \                                                                                      
        -lpthread"  
     BLAS_LIBS="$MKL"  
    
  3. Now configure, build and install with following lines:
     ./configure --prefix=/some/directory1 --with-blas --with-lapack  
     make  
     sudo make install  
    

To compile R using MKL multi-threading library,
  1. Apply the patch to configure script
  2. Add the following lines to config.site file
     CFLAGS='-g -O3 '                                                                                             
     FFLAGS='-g -O3 '                                                                                             
     CXXFLAGS='-g -O3 '                                                                                            
     FCFLAGS='-g -O3 '   
       
     MKL_LIB_PATH=/opt/intel/composer_xe_2013.3.163/mkl/lib/intel64  
     MKL=" -L${MKL_LIB_PATH}    \                                                                                      
        -Wl,--start-group    \                                                                                      
          -lmkl_gf_lp64    \                                                                                      
          -lmkl_gnu_thread  \                                                                                      
          -lmkl_core     \                                                                                      
          -lm         \                                                                                      
        -Wl,--end-group     \                                                                                      
        -lgomp -lpthread"  
     BLAS_LIBS="$MKL"  
    
  3. Now configure, build and install with following lines:
     ./configure --prefix=/some/directory2 --with-blas --with-lapack  
     make  
     sudo make install  
    

When you run the configure command, last portion of the output should look something like this:
 R is now configured for x86_64-unknown-linux-gnu  
   
  Source directory:     .  
  Installation directory:  /usr/local  
   
  C compiler:        gcc -std=gnu99 -g -O3   
  Fortran 77 compiler:    gfortran -g -O3   
   
  C++ compiler:       g++ -g -O3   
  Fortran 90/95 compiler:  gfortran -g -O3   
  Obj-C compiler:          
   
  Interfaces supported:   X11  
  External libraries:    readline, BLAS(generic), LAPACK(in blas)  
  Additional capabilities:  JPEG, NLS  
  Options enabled:      R profiling  
   
  Recommended packages:   yes  
   

1 comment:

  1. More examples here: http://connectir.projects.nitrc.org/download/

    ReplyDelete