Getting MAGMA to work with R

MAGMA is a fascinating project. It is short for “Matrix Algebra on GPU and Multicore Architectures” and aims to allow users to easily “hybridise” their code. If I have a super-GPU I can do my operations there, if tomorrow I decide to sell my GPU and get a nice plant for my office instead, no worries, with MAGMA I will not need to change anything in my code (maybe just a flag), it’s that portable.

I’m going to be a bit of a wet blanket here and immediately point out that unless you have a great GPU (or multiple GPUs), or else you’re going to be using highly specialised code, you probably don’t need to install MAGMA, as CPUs today are highly optimised to perform linear algebraic routines efficiently.

However, if you DO want to install MAGMA in R, you can. Unfortunately it’s not as simple as “Installing MAGMA, then install the R package magma” and you’re done, for one simple reason, MAGMA has moved on, the R package hasn’t.

1. Installing MAGMA

For MAGMA you first need to have your BLAS libraries installed. I spent a good couple of days trying to install MAGMA with OpenBLAS. This did NOT work (although it installed and all). I was so happy with OpenBLAS integrated with R that I stayed at it forever. In the end I gave up. It turns out that installing ATLAS (another great BLAS package), however, is not that hard. In Ubuntu simply do ‘sudo apt-get install libatlas-base-dev’. Like this you get a basic ATLAS (not optimised to your system) but for us that is good enough for now.

When you download MAGMA (1.6.1) you will see many files, where xxx is the BLAS package you have. Since we have ATLAS, simply copy as Then open with an editor and uncomment the line FPIC= -fPIC. We need this so we get a shared library which we then can integrate with R. Another thing to remove from this file is the “-lifcore” argument for the LIB variable. This is a link to Intel FORTRAN compiler libraries and are not available freely. Luckily, everything seems to be in the gfortran libraries and I had no problems from leaving this out.

Once the is set up, make sure you set all your paths! I had to set LAPACKDIR=/usr/lib/lapack, ATLASDIR=/usr/lib/atlas-base/ and CUDADIR=/usr/local/cuda-6.5. Once these are set, type “make” and 30 minutes later you should have MAGMA installed. Make sure you go in the ‘testing’ folder and try out a few tests there, they should run with no problems. If they don’t, try and sort out the problem, and recompile.

2. Integrating with R

Unfortunately, the R package ‘magma’ and the current version of MAGMA are incompatible (I’ll be trying to change that with the maintainer at some point).

The first thing is that the R-magma requires a library, but this is no longer being created by MAGMA and everything is in Therefore, download the R-magma tar.gz, extract it in a folder, then go into the ‘configure’ file and scroll down to Line 2468. Here instead of LIBS=”${LIBS} -lmagmablas -lmagma” do LIBS=”${LIBS} -L/home/andrew/magma-1.6.1/lib -lmagma -llapack” or wherever your MAGMA library is (you probably do not need -llapack). You might also need to set your “CUDA_HOME” variable to point to your CUDA directory (see above).

The second thing is that the interface of some crucial MAGMA functions have changed. You have to go into the src/ directory and some lines need to be changed. For example,

char UPLO = (LOGICAL_VALUE(uprtri) ? 'U' : 'L'),
TRANSA = (LOGICAL_VALUE(transa) ? 'T' : 'N');

needs to become

int UPLO = (LOGICAL_VALUE(uprtri) ? MagmaUpper : MagmaLower),
TRANSA = (LOGICAL_VALUE(transa) ? MagmaTrans : MagmaNoTrans);

Essentially, some functions now require integers (defined through MagmaUpper etc.) instead of character variables. For example, in magSolvers.c with the function magTriSolve,

magma_dtrsm('L', UPLO, TRANSA, 'N', K, N, 1.0, dA, M, dB, M);

now becomes

magma_dtrsm(MagmaLeft, UPLO, TRANSA, MagmaNonUnit, K, N, 1.0, dA, M, dB, M);

There aren’t too many of these, and what I did was compile, try out some functions in R, and when I got an error went to change the source, recompiled and so on.

The last thing, is that MAGMA now requires magma_init() to be called before the GPU is used. Open magUtils.c and scroll down to the function magLoad(). Put magma_init(); in the first line and you should be done.

Go back to the top folder, and do ./configure. This will make the appropriate makefiles. Then type R CMD INSTALL . and hopefully you will not get any errors and you’re ready to go!