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


Ralf Stubner


June 28, 2019

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

#include <RcppArrayFire.h>
af::array times_two(const RcppArrayFire::typed_array<f32, AF_STORAGE_CSR>& x) {
    return 2 * x;

and returns it multiplied by two:

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

## 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!