RcppArrayFire v0.1.0: Sparse Matrices and support for Mac OS

June 27, 2019
By

(This article was first published on R on Ralf Stubner, and kindly contributed to R-bloggers)

The RcppArrayFire package provides an
interface from R to and from the ArrayFire library,
an open source library that can make use of GPUs and other hardware accelerators
via CUDA or OpenCL. In order to use RcppArrayFire you will need the ArrayFire
library and header files which you can build from source
or use up-stream’s binary installer.
See previous articles for a general introduction.

Version 0.1.0 brings to important changes: Support for sparse matrices and Mac OS

Support for sparse matrices

RcppArrayFire was started by Kazuki Fukui under the name RcppFire.
Last September he came back to offer sparse matrix support in
#9. The typed_array
class was changed to typed_array with AF_STORAGE_DENSE
as default value. Existing code will work unchanged with using dense matrices, but you
can now define a function that expects a sparse matrix

//[[Rcpp::depends(RcppArrayFire)]]
#include 
//[[Rcpp::export]]
af::array times_two(const RcppArrayFire::typed_array& x) {
    return 2 * x;
}

and returns it multiplied by two:

library('Matrix')
x <- as(matrix(c(1, 0, 0, 2, 3,
                 0, 0, 1, 0, 2), 2, 5), 'dgRMatrix')
times_two(x)

## 2 x 5 sparse Matrix of class "dgRMatrix"
##               
## [1,] 2 . 6 . .
## [2,] . 4 . 2 4

Besides such simplistic operations, you can use af::matmul to multiply
sparse-dense matrices.
Currently only f32 (float) and f64 (double) are supported and mapped to
numeric matrices, since the Matrix package does not support complex sparse
matrices. The storage types CSR, CSC and COO are supported via dgRMatrix,
dgCMatrix and dgTMatrix.

Support for Mac OS

This was started more than a year ago (full history here:
#5), but it seemed impossible
to link with ArrayFire’s unified back-end libaf. I even asked on
stackoverflow, but
that brought me only a tumbleweed badge.

The macos branch started to gather dust when François Cocquemas
opened issue #14 saying that (unsurprisingly)
neither the master nor the macos branch worked with R 3.6.0, but that combining configure
from master with the macos branch did work. This was surprising, since configure
from master did use the unified back-end. In the end I only had to handle a few conflicts
upon merging to get RcppArrayFire fully supported on Mac OS!

To leave a comment for the author, please follow the link and comment on their blog: R on Ralf Stubner.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)