Skip to content

Instantly share code, notes, and snippets.

@LTLA
Created January 13, 2019 07:42
Show Gist options
  • Save LTLA/4a15a99474b17d8e485fd03ae2c306aa to your computer and use it in GitHub Desktop.
Save LTLA/4a15a99474b17d8e485fd03ae2c306aa to your computer and use it in GitHub Desktop.
#include "beachtest.h"
// Don't forget to set appropriate PKG_CXXFLAGS.
#include <omp.h>
#include <iostream>
extern "C"{
SEXP parallel_reader(SEXP input) {
BEGIN_RCPP
auto dptr=beachmat::create_numeric_matrix(input);
const int nthreads=(dptr->get_matrix_type()==beachmat::HDF5 ?
1 : omp_get_max_threads());
#pragma omp parallel num_threads(nthreads)
{
beachmat::numeric_matrix* rptr=NULL;
std::unique_ptr<beachmat::numeric_matrix> uptr=nullptr;
if (omp_get_thread_num()==0) {
rptr=dptr.get();
} else {
uptr=dptr->clone();
rptr=uptr.get();
}
const size_t NC=rptr->get_ncol();
Rcpp::NumericVector output(rptr->get_nrow());
#pragma omp for schedule(static)
for (size_t col=0; col<NC; ++col) {
// Do parallel operation here.
rptr->get_col(col, output.begin());
std::cout << col << "\n"; // Rprintf is not thread-safe.
}
}
return R_NilValue;
END_RCPP
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment