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

[This article was first published on R on Ralf Stubner, 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 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<af::dtype> class was changed to typed_array<af::dtype, af::storage> 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 <RcppArrayFire.h>
//[[Rcpp::export]]
af::array times_two(const RcppArrayFire::typed_array<f32, AF_STORAGE_CSR>& 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 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.

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)