tatami_r
R bindings to tatami matrices
Loading...
Searching...
No Matches
Read R objects via tatami

Overview

tatami_r is an header-only library for reading abstract R matrices in tatami. This allows tatami-based C++ functions to accept and operate on any matrix-like R object containing numeric data. Usage is as simple as:

SEXP some_typical_rcpp_function(Rcpp::RObject x) {
auto ptr = tatami_r::UnknownMatrix(x);
// Do stuff with the tatami::Matrix.
ptr->nrow();
auto row_extractor = ptr->dense_row();
auto first_row = row_extractor->fetch(0);
}
Unknown matrix-like object in R.
Definition UnknownMatrix.hpp:48
tatami bindings for arbitrary R matrices.

And that's it, really. If you want more details, you can check out the reference documentation.

Implementation

tatami_r assumes that the hosting R instance has installed the DelayedArray package. The UnknownMatrix getters will then use the extract_array() and extract_sparse_array() R functions to retrieve data from the abstract R matrix. Note that this involves calling into R from C++, so high performance should not be expected here. Rather, the purpose of tatami_r is to keep tatami-based functions working when a native representation cannot be found for a particular matrix-like object.

It is worth mentioning that the UnknownMatrix will always call the extract_*_array() functions, even when a native representation exists in tatami or one of its extension libraries. R package developers should instead use the initializeCpp() function from the beachmat package to map an arbitrary matrix to its appropriate representation. When such mappings exist, this allows the C++ code to operate without calling back into R for maximum efficiency. Nonetheless, if no mapping is known, beachmat will gracefully fall back to an UnknownMatrix to keep things running.

Enabling parallelization

Given a tatami_r::UnknownMatrix or a tatami::Matrix* that might refer to one, we can easily parallelize operations with the tatami_r::parallelize() function. This accepts a lambda/functor with the thread ID and the range of jobs (in the example below, rows) to be processed.

tatami_r::parallelize([&](size_t thread_id, int start, int len) -> void {
// Do something with the UnknownMatrix.
auto ext = ptr->dense_row();
std::vector<double> buffer(ptr->ncol());
for (int r = start, end = start + len; start < end; ++r) {
auto out = ext->fetch(r, buffer.data());
// Do something with each row.
}
}, ptr->nrow(), num_threads);
void parallelize(Function_ fun, size_t njobs, size_t nthreads)
Definition parallelize.hpp:59

Any calls to the extract_*_array() R functions are made thread-safe by the manticore library. Developers can also access the manticore executor to safely perform their own R API calls from each thread.

auto& mexec = tatami_r::executor();
tatami_r::parallelize([&](size_t thread_id, int start, int len) -> void {
mexec.run([&]() -> void {
// Do something that touches the R API.
});
}, ptr->nrow(), num_threads);
manticore::Executor & executor()
Definition parallelize.hpp:34

Check out the comments about safe parallelization for more gory details.

Deployment

tatami_r is intended to be compiled with other relevant C++ code inside an R package. This is most easily done by modifying the package DESCRIPTION with:

LinkingTo: beachmat

which will automatically use the vendored copies of tatami_r (and tatami) inside the beachmat package. Note that C++17 is required.

If beachmat cannot be used, then the R package developer will need to copy the tatami_r and tatami include/ directories into the package's inst/include, and then add a Makevars file like:

PKG_CPPFLAGS = -I../inst/include