Skip to content

Instantly share code, notes, and snippets.

View romainfrancois's full-sized avatar
🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)

Romain François romainfrancois

🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)
View GitHub Profile
@romainfrancois
romainfrancois / ccall.R
Created February 19, 2014 15:51
calling C function directly (a la julia)
ccall <- function(fun, ..., depends = character(), plugins = character(), verbose = FALSE ){
dots <- list(...)
if( length(dots) ){
code <- sprintf( '
SEXP fun(%s){
return wrap( %s( %s ) ) ;
}',
paste( sprintf( "SEXP arg%d", seq_along(dots)), collapse = ", " ),
fun,
paste( sprintf( "Rcpp::internal::converter(arg%d)", seq_along(dots) ), collapse = ", " )
@romainfrancois
romainfrancois / result.md
Last active August 29, 2015 13:56
C level try catch
$ R CMD SHLIB try_catch.c
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include    -fPIC  -g -O3 -Wall -pipe -c try_catch.c -o try_catch.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o try_catch.so try_catch.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation

When throwing a real condition, e.g. a simpleError, I can get hold of it

romain@naxos /tmp $ Rscript -e "dyn.load('try_catch.so') ; .Call( 'test', 1L)"
@romainfrancois
romainfrancois / README.md
Last active August 29, 2015 13:58
How should I count the number of unique rows in a 'binary' matrix?

Several implementations of the problem from this SO thread. http://stackoverflow.com/questions/22886040/how-should-i-count-the-number-of-unique-rows-in-a-binary-matrix

The counts_cpp2.cpp is similar to the one I detailed in my SO answer. The counts_cpp3.cpp goes further by splitting the job into 4 threads, each dealing with a subset of rows of the matrix.

The graph shows the times of both solutions.

  • The first line on the bottom corresponds to the sequential version. Red is hashing and blue is training the hash map
  • The other 4 lines above are the times of the 4 threads of the parallel solution. Black is when the main threads joins the orker threads, and orange is merging the hash maps.
@romainfrancois
romainfrancois / README.md
Last active August 29, 2015 13:59
compiler error when coimpiling a very simple file against Rcpp11 on windows with g++ 4.6.3 and -std=cxx0x

Trying to sourceCpp the test.cpp, I get all that on windows with gcc 4.6.3

@romainfrancois
romainfrancois / testRcpp11.R
Last active August 29, 2015 13:59
test Rcpp11
library("devtools")
library("testthat")
install_github( "Rcpp11/Rcpp11")
install_github( "Rcpp11/Rcpp-test")
library( "RcppTest" )
test_package( "RcppTest" )
@romainfrancois
romainfrancois / README.md
Last active August 29, 2015 13:59
rev with Rcpp and Rcpp11
@romainfrancois
romainfrancois / benchmarks.md
Last active August 29, 2015 14:01
Compilation times

This little benchmark compares the times it take to compile a really simple Rcpp example (the example you get from RStudio when you do new C++ file) with Rcpp and Rcpp11.

require(microbenchmark)
compile_Rcpp <- function(){
  Rcpp::sourceCpp("/tmp/timesTwo.cpp", rebuild = TRUE )
}
compile_Rcpp11 <- function(){
  attributes::sourceCpp( "/tmp/timesTwo.cpp" )
}
@romainfrancois
romainfrancois / README.md
Created May 22, 2014 09:46
Extending wrap in Rcpp11

Context

Extending wrap to custom classes has always been a struggle in Rcpp, and led to the split between RcppCommon.h and Rcpp.h, the idea being that you load a "minimal" subset of Rcpp, declare your class, declare that you are going to provide a specialization of wrap, load the rest of Rcpp (the meat), and finally define your specialization.

Let's consider this template class :

template <typename T>
class MyType {} ;
#!/usr/bin/R
# this script is poorly-written, so use at your own risk.
# ---------------------------------------------------------------
#
# THIS HAS A VERY GOOD CHANCE OF COMPLETELY DESTROYING YOUR REPO;
# ONLY RUN THIS ON A COPY OF THE REPO!!!
#
# ----------------------------------------------------------------
#include <Rcpp.h>
using namespace Rcpp ;
class abc {
public:
abc( SEXP x ) : data(x){}
inline operator SEXP() const {
return data ;
}