<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/do-gpu-based-basic-linear-algebra-subprograms-blas-improve-the-performance-of-standard-modeling-techniques-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.inwt-statistics.com/read-blog/basic-linear-algebra-subprograms-in-r.html"> INWT-Blog-RBloggers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h2>Introduction</h2>
<p>The speed or run-time of models in R can be a critical factor, especially considering the size and complexity of modern datasets. The number of data points as well as the number of features can easily be in the millions. Even relatively trivial modeling procedures can consume a lot of time, which is critical both for optimization and update of models. An easy way to speed up computations is to use an optimized BLAS (Basic Linear Algebra Subprograms). Especially since R’s default BLAS is well regarded for its stability and portability, not necessarily its speed, this has potential. Alternative libraries are for example ATLAS and OpenBLAS which we will use below. <a rel="nofollow" href="https://www.r-bloggers.com/for-faster-r-use-openblas-instead-better-than-atlas-trivial-to-switch-to-on-ubuntu/" target="_blank">Multiple</a> <a rel="nofollow" href="https://qdvr.blogspot.com/2015/06/improved-r-performance-with-openblas.html" target="_blank">blog-posts</a> <a rel="nofollow" href="http://www.parallelr.com/r-hpac-benchmark-analysis/" target="_blank">showed</a> that they are able to improve the performance of linear algebra operations in R, especially those of the infamous <a rel="nofollow" href="http://r.research.att.com/benchmarks/" target="_blank">R-benchmark-25.R</a>.</p>
<p>With <strong>nvblas</strong>, nvidia offers a GPU-based BLAS-library, which it claims to be significantly faster than standard procedures. <a rel="nofollow" href="https://docs.nvidia.com/cuda/nvblas/" target="_blank">nvblas</a> is part of the latest CUDA-implementations such as CUDA 9. <a rel="nofollow" href="http://on-demand.gputechconf.com/gtc/2015/presentation/S5145-Patric-Zhao.pdf" target="_blank">According to nvidia</a>, nvblas is able to speed up procedures such as the computation of the determinant of a 2500×2500 matrix by a factor of more than ten. However, most users from the data science sphere don’t use basic linear algebra operations directly – they usually use high-level-abstractions such as <span>lm()</span> or <span>gam()</span> for modeling.</p>
<p>With little effort we can swap the default BLAS to OpenBLAS and already get significant performance gains. And so we want to find out if nvblas can speed up computations just as easily! For this comparison we compare R’s default BLAS, the optimized libraries ATLAS and OpenBLAS (all of which are using CPUs only) with nvblas (which uses CPU and GPU).</p>
</div>
<div>
<h2>Setup</h2>
<p>We simulated data for three scenarios:</p>
<ul>
<li><strong>Regression:</strong> We draw data for a fixed number of numerical and dummy variables (using a normal distribution with poison-distributed variance and mean 0 and a Bernoulli distribution with uniformly distributed probability, respectively) and calculate the target as a linear combination of the features, adding nonlinear interaction effects and a random error term. The following models are set up to solve this problem:</li>
<li><em>Linear Regression:</em> using <span>glm(family = “gaussian”)</span> from <span>stats</span></li>
<li><em>Additive Regression:</em> using <span>bam(family = “gaussian”)</span> with max. 20 smoothed variables from <span>mgcv</span></li>
<li><em>Extreme Gradient Boosting:</em> using <span>xgb.train(nrounds = 30, tree_method = “hist”)</span> from <span>xgboost</span>, using a 75%-25% train-test-split</li>
</ul>
<ul>
<li><strong>Classification:</strong> We draw data in a similar fashion as with the regression example, but derive the binary outcome from the former dependent variable by splitting at the median of the outcome variable and adding an additional 5%-dropout. The following models are set up to solve this problem:</li>
<li><em>Generalized linear Regression:</em> using <span>glm(family = “binomial”)</span> from <span>stats</span></li>
<li><em>Generalized additive Regression:</em> using <span>bam(family = “binomial”)</span> with max. 20 smoothed variables from <span>mgcv</span></li>
<li><em>Extreme Gradient Boosting:</em> using <span>xgb.train(nrounds = 30, tree_method = “hist”)</span> from <span>xgboost</span>, using a 75%-25% train-test-split</li>
</ul>
<ul>
<li><strong>Random Intercept:</strong> We draw data in a similar fashion as with the regression example, but adding an additional random intercept for random groups. The following models are set up to solve this problem:</li>
<li><em>Linear Mixed Model:</em> using <span>lmer()</span> from <span>lme4</span></li>
<li><em>Linear Mixed Model:</em> using<span> lme()</span> from <span>nlme</span></li>
<li><em>Additive Mixed Model:</em> using <span>bam(family = “gaussian”)</span> with max. 20 smoothed variables and the <span>bs</span> parameter from <span>mgcv</span></li>
</ul></div>
<div>
<p>The particular pick of functions simply reflects which models and packages we currently use at INWT Statistics. So those are the most interesting ones for us to look at. When you have a thorough understanding of how these methods are implemented, you may, in part, anticipate some of the results. To execute the experiments, we used an AWS machine capable of both efficient CPU and GPU computations: <a rel="nofollow" href="https://aws.amazon.com/de/ec2/instance-types/p3/" target="_blank">p3.2xlarge</a> with <a rel="nofollow" href="https://aws.amazon.com/marketplace/pp/B076TGJHY1" target="_blank">Machine Learning AMI</a> containing the latest CUDA version. With this setup, it is relatively easy to change the BLAS library by configuring it via an environment variable:</p>
</div>
<pre><code>env LD_PRELOAD=libnvblas.so Rscript code.R</code></pre>
<div>
<p>To permanently switch the BLAS version, one can use update-alternatives:</p>
</div>
<pre><code>sudo update-alternatives --config libblas.so.3gf</code></pre>
<div>
<p>To check which BLAS and LAPACK-version a running R-console is using, do</p>
</div>
<pre><code>ps aux | grep exec/R</code></pre>
<div>
<p>to get the PID and then do:</p>
</div>
<pre><code>lsof -p &lt;PID> | grep 'blas\|lapack'</code></pre>
<div>
<p>The xgboost-package plays a special role in our tests: additionally to change the BLAS-package to nvblas (the optimization done by xgboost does not improve much by using different blas-versions), one can change the optimization-algorithm to <span>gpu-hist</span>, <a rel="nofollow" href="http://xgboost.readthedocs.io/en/latest/build.html#installing-r-package-with-gpu-support" target="_blank">if the package is installed correctly</a>. This approach to the histogram tree growth method is optimized for GPUs and <a rel="nofollow" href="http://xgboost.readthedocs.io/en/latest/gpu/#benchmarks" target="_blank">should outperform the cpu-version</a>. This was done for our tests.</p>
</div>
<div>
<h2>Results</h2>
<p>The experiments were done for different sample sizes and numbers of categorical and numerical features. We show the results for a sample of 100,000 and 100 categorical as well as 100 numerical features. If you think this is small: we do not analyse images, videos or gene expression data; this is a somewhat realistic scenario in which performance becomes interesting and relevant for us.</p>
<p>The R-benchmark-25 needs 5.54 seconds using OpenBLAS, 11.21 seconds using ATLAS, 34.62 with the default BLAS and 7.78 – 8.08 seconds using nvblas, depending on OpenBLAS or ATLAS as fallback (which is specified in the nvblas.config). Even for R-benchmark-25, nvblas is not faster compared to OpenBLAS, however both run significantly faster than the default BLAS.</p>
<p>So how do the different BLAS-versions perform for high-level-functions in R? The plots show the speed of the methods using different BLAS-versions.</p>
</div>
<div>
<figure> <img width="320" alt="" src="https://i2.wp.com/www.inwt-statistics.com/assets/images/1/regression-9d8ec321.png?resize=320%2C200&ssl=1" data-recalc-dims="1" height="200"> </figure>
</div>
<div>
<figure> <img width="320" alt="" src="https://i0.wp.com/www.inwt-statistics.com/assets/images/9/classification-9c017679.png?resize=320%2C200&ssl=1" data-recalc-dims="1" height="200"> </figure>
</div>
<div>
<figure> <img width="320" alt="" src="https://i0.wp.com/www.inwt-statistics.com/assets/images/7/random-6d3d1cc7.png?resize=320%2C200&ssl=1" data-recalc-dims="1" height="200"> </figure>
</div>
<div>
<p>When we started out we were hoping for a clear winner and somehow we also found one. However depending on your pick of models and applications there are some unexpected results.</p>
<ul>
<li>No matter which BLAS library we used, <span>bam</span> picked it up and the run-time improved significantly. When we have a machine with sufficiently strong CPUs ATLAS is about as fast as using nvblas and a GPU. OpenBLAS still shows an improvement over the default BLAS. A good thing about <span>bam</span> is that it covers a wide range of model classes. Especially random intercept or generally random effect models (sadly) do not get a lot of attention in the machine learning era.</li>
<li><span>xgboost</span> gains from <span>nvblas</span>. This is not very surprising since it uses an optimized algorithm for GPUs. However, it does not gain anything from ATLAS or OpenBLAS.</li>
<li>For the traditionalists: <span>glm</span> and <span>lme</span> will profit from OpenBLAS. Very surprising however is the performance loss when we used ATLAS and nvblas. Either we have a considerable overhead when using these libraries or the configuration is not as plug-and-play as we hoped for. In any case it is reason enough to praise OpenBLAS to speed up a lot of computations out of the box. Even when specialized models may gain more from other libraries.</li>
<li>What is wrong with <span>lmer</span>: lme4 uses the RcppEigen package to replace the default BLAS library. For a lot of users this has the advantage that <span>lmer</span> will run fast without you doing anything. But since it is not using the default BLAS it will also not benefit from replacing it with ATLAS, OpenBLAS or nvblas. lmer and lme (OpenBLAS) have been comparable in run time in most of our tests. More pressing is the question which one implements the method you need.</li>
</ul>
<p>We hoped to see that nvblas would easily outperform all other libraries. Of course it is not that simple. For statistical modeling, the libraries which have been tuned, optimized and debugged for decades are hard to beat. If you are looking for a plug-and-play speed improvement I would still vote for OpenBLAS.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.inwt-statistics.com/read-blog/basic-linear-algebra-subprograms-in-r.html"> INWT-Blog-RBloggers</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/4T2tSH2r0xA" height="1"></a></div>