Site icon R-bloggers

Installing cuda toolkit and related R packages

[This article was first published on Data Science Notes - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The main purpose of this post is to keep all steps of installing cuda toolkit (and R related packages) and in one place. Also I hope this may be useful for someone.

Installing cuda toolkit ( Ubuntu )

First of all we need to install nvidia cuda toolkti. I’am on latest ubuntu 15.04, but found this article well suited for me. But there are few additions:

  1. It is very important to have no nvidia drivers before installation ( first I corrupted my system and have to reinstall it 🙁 ). So I recommend to switch to real terminal (ctrl + alt + f1), remove all nvidia stuff sudo apt-get purge nvidia-* and then follow steps from article above.

  2. This will install cuda toolkit and corresponding nvidia drivers.

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1410/x86_64/cuda-repo-ubuntu1410_7.0-28_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1410_7.0-28_amd64.deb
sudo apt-get update
sudo apt-get install cuda
  1. After installation we need to modify our .bashrc file. Add following lines:
export CUDA_HOME=/usr/local/cuda-7.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64

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

Note, that I added path to nvcc compiler.

Installing gputools

First simply try:

install.packages('gputools', repos = 'http://cran.rstudio.com/')

After that I recieved:

Unsupported gpu architecture ‘compute_10’

Solving this issue I found this link useful. I have gt525m card and have compute capability 2.1. You can verify your GPU capabilities here. So I downloaded gputools source package:

cd ~
wget http://cran.r-project.org/src/contrib/gputools_0.28.tar.gz
tar -zxvf gputools_0.28.tar.gz

and replace following string

NVCC := $(CUDA_HOME)/bin/nvcc -gencode arch=compute_10,code=sm_10 -gencode arch=compute_13,code=sm_13 -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30

in gputools/src/Makefile by

NVCC := $(CUDA_HOME)/bin/nvcc -gencode arch=compute_20,code=sm_21

Next try to gzip it back and install from source:

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

Than I recieved:

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

We have to adjust R header dir location. First of all look for R.h:

locate /R.h

replace string R_INC := $(R_HOME)/include in gputools/src/config.mk string by found path: R_INC := /usr/share/R/include

In case we recieve error regarding shared libcublas.so we also need to adjust links for libcublas shared library:

sudo ln -s /usr/local/cuda/lib64/libcublas.so.7.0 /usr/lib/libcublas.so.7.0

thanks to this thread.

Testing performance

here is simple benchmark:

library(gputools)
N <- 1e3
m <- matrix(sample(100, size = N*N, replace = T), nrow = N)
system.time(dist(m))
##    user  system elapsed 
##   4.864   0.008   4.874
system.time(gpuDist(m))
##    user  system elapsed 
##   0.640   0.168   0.809

To leave a comment for the author, please follow the link and comment on their blog: Data Science Notes - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.