Installing R package gputools and cuda 8.0 on Ubuntu 16.04

This is a quick tutorial of how to install the R package ‘gputools’ version 1.1 using R version 3.3.2 (2016-10-31) and cuda 8.0 on Ubuntu 16.04. Most of these versions are new so I did some search on the internet and I could not find a tutorial about that. However most of this tutorial is based on this page which is for ‘gputools’ version 0.28 and cuda 7.0 on Ubuntu 15.04. At the end I just changed a few lines.

I have tested it on a ASUS ROG G752VM with NVIDIA GeForce GTX 965M graphics card. The instruction assumes you have the necessary CUDA compatible hardware support. In my case I also installed the NVIDIA driver 367.57 first. My computer was new so I did not have any nvidia driver or compatibility issues. However I strongly recommend to look on the internet how to remove the old drivers first, before install the new ones (things like sudo apt-get purge nvidia-cuda*).

Installing CUDA 8.0

First, to install CUDA 8.0 we can do:

wget https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda

Or you can download the CUDA repository package for Ubuntu 16.04 from the CUDA download site and follow the instructions according to your necessities.

Environment Variables

I tried to install the gputools package without adding the variables to the environment and i got an error related to nvcc. Thus, as part of the CUDA environment, we should add the nvcc compiler in the .bashrc file of your home folder.

export CUDA_HOME=/usr/local/cuda-8.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64

PATH=${CUDA_HOME}/bin:${PATH}
PATH=${CUDA_HOME}/bin/nvcc:${PATH}
export PATH

Installing gputools version 1.1

The fastest way to install gpuplots if you are using R version 3.3.2 is:

install.packages('gputools')

Now my tutorial differs a bit more from the tutorial I mentioned before. I received the message:

rinterface.cu:1:14: fatal error: R.h: No such file or directory #include

So we have to check where R header dir location is. First lets locate the file R.h:

locate \/R.h
## /usr/share/R/include/R.h

Then next step is to tell to gputools where the R.h is located. Thus it is necessary to change a line in the source package. First download and extract the source package:

wget http://cran.r-project.org/src/contrib/gputools_1.1.tar.gz
tar -zxvf gputools_1.1.tar.gz

Look into the folder you just extracted then open the file configure on your favourite Ubuntu editor to replace the string R_INCLUDE="${R_HOME}/include" for R_INCLUDE="/usr/share/R/include" (which is the location of my R.h file).

The two finals steps are compress the modified source code

tar -czvf gputools_1.1_new.tar.gz gputools

and install the modified package

install.packages("~/gputools_1.1_new.tar.gz", repos = NULL, type = "source")

I had lots of warning messages but no error.

Testing performance

Now we can try some simple benchmarks and see how much time the CPU and gpu time will spend. First a small matrix multiplication:

library(gputools)

magnitude <- 10
dimA <- 2*magnitude;dimB <- 3*magnitude;dimC <- 4*magnitude
matA <- matrix(runif(dimA*dimB), dimA, dimB)
matB <- matrix(runif(dimB*dimC), dimB, dimC)

system.time(matA%*%matB);
##    user  system elapsed 
##   0.000   0.000   0.001
system.time(gpuMatMult(matA, matB))
##    user  system elapsed 
##   0.076   0.140   0.215

then using larger matrices:

magnitude <- 1000
dimA <- 2*magnitude;dimB <- 3*magnitude;dimC <- 4*magnitude
matA <- matrix(runif(dimA*dimB), dimA, dimB)
matB <- matrix(runif(dimB*dimC), dimB, dimC)

system.time(matA%*%matB);
##    user  system elapsed 
##  15.552   0.028  15.579
system.time(gpuMatMult(matA, matB))
##    user  system elapsed 
##   0.792   0.124   0.914

10 Comments

  1. Did anybody encounter this error:

    Error: package or namespace load failed for ‘gputools’ in dyn.load(file, DLLpath = DLLpath, …):
    unable to load shared object ‘/home/gabriel/R/x86_64-pc-linux-gnu-library/3.0/gputools/libs/gputools.so’:
    libcublas.so.8.0: cannot open shared object file: No such file or directory

    I have tried making /home/gabriel/R writable by non-root user but same problem reoccurs.
    Thanks.

    Like

  2. Thanks for that tutorial, its really helpful. BTW, what kind of Nvidia card you have? mine should be new but its very slow like one second slower than yours.

    Like

    • Hi, I don’t know what you mean by “one second slower”. As I mentioned in the post my video card is the NVIDIA GeForce GTX 965M.
      I don’t know what it is your speed problem but I noticed when I am trying to invert a matrix using the pseudo inversion function, the GPU is only better if the matrix is large. If the matrix is not large enough the time to send information to the GPU, calculate the inverse and get the information back is bigger than using the CPU only. But it is hard to me to say something concrete because the package gputools uses the QR decomposition to invert the matrix and the function ginv() uses the singular value decomposition (SVD).
      I hope it helps.

      Like

    • ..please correct the line (in the first code box):
      sudo dpkg -i cuda-repo-ubuntu1410_7.0-28_amd64.deb) to
      sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb
      and after that please delete this comment 🙂

      Like

  3. unable to load shared object ‘/usr/local/lib/R/site-library/gputools/libs/gputools.so’:
    libcublas.so.8.0: cannot open shared object file: No such file or directory

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s