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:

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 PATH

Installing gputools version 1.1

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


Now my tutorial differs a bit more from the tutorial I mentioned before. I received the message: 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:

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:


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)

##    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)

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


Add yours →

  1. 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.


    • 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.


  2. Branimir K. Hackenberger January 26, 2017 — 4:51 am

    Thanks for this VERY usefull site! Short and clear!


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


Leave a Reply

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

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: