Bring the power of Julia into R through XRJulia package

R is the software for statistics. It is easy to use and have great flexibility. The most important of all, it has a lot of packages which greatly expand the power of R. But R sometimes suffer from low performance issues. Julia is a new programming language whose main goal is to let high performance scientific computing becomes easy. In this series of blogs, I will talk about how to use XRJulia package to bring the high performance of Julia into the great ecosystem of R, which combines the best of the two worlds.
I will first talk about some general recipes to speed up R code, and then talk about why choose Julia and XRJulia. And the introduction to R and XRJulia will be in the second blog of this series.

General Advice on speeding up your R script

When you have some performance issue with your R script, there are several things you can do:
  1. Profile your code, identify the bottleneck for your script, and then try to alleviate it. For example, if the bottleneck of your code is a deeply nested loop, you can try to deal with it by vectorizing your code. I once found my R script of ADMM (alternating direction method of multipliers) to be incredibly slow. After profiling the code, I found the bottleneck is a step where I kept solve(A, b) over and over in a loop with a constant matrix A and some variant vectors b. Since multiplication of matrices is far more efficient than calculation matrix inverse, I calculated B <- solve(A) at the beginning of my script and do B %*% b in the loop instead of my original version. The efficiency gain was tremendous. My code was more than 100 times faster than before! I will talk about it more as an R code optimization example in my later blog.
  2. After profiling your code and make corresponding improvements as far as you can, but you still have the bottleneck in your code, what can you do? You can implement the core computational part of your algorithm in other languages and then let your R code to call it through some interfaces. One of the good choices is Rcpp, which I will also write more blogs to talk about. With the Rcpp package, you can integrate c++ code nicely into R. But here I will talk about XRJulia, which brings the power of Julia’s high-efficient computation into R seamlessly.

Why Julia? Why XRJulia?

Since Rcpp is already well integrated into the R language and is quite powerful, why do we bother to use Julia and XRJulia? I think there are several reasons to use Julia and XRJulia:
  1. Julia is oriented toward scientific computing. It has built-in facilities to write high-performance numeric codes. Instead of using extensions like RcppEigen and RcppArmadillo, you can write Julia code for matrix inverse, multiplication, or qr decomposition directly.
  2. Julia is easy to learn for an R user. You don’t have to learn a lot of things like c++’s object and template, just search the Julia’s manual and start using it. Forget the type notation, Julia’s high-performance JIT can do the work for you! And you can use Julia interactively, which can greatly facilitate the learning and coding process.
  3. After coding Julia script to handle the computation, package XRJulia can let you call the Julia’s functionality from R quite easily. XRJulia is powerful: you can harness all the computational power provided by Julia through the interface. And it is also easy to use: if you just want to implement your computation in Julia, you can ignore the technical details most of the time and XRJulia will just do all of the work for you.

Introduction to Julia and XRJulia package by example (To be continued)

http://datapleasure.blogspot.com/2017/06/bring-power-of-julia-into-r-through.html

Comments

Popular posts from this blog

Improve R's Performance using JuliaCall

Convex Optimization in R by convexjlr

Bring the power of Julia into R through XRJulia package (2)