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 / 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
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 / 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 / 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 / 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 / internal.R
Created February 17, 2014 16:38
Show what's in .Internal function
library("httr")
library("rjson")
internal <- function(fun){
names.c <- content( GET("https://raw2.github.com/wch/r-source/trunk/src/main/names.c") )
names.c <- strsplit( names.c, "\\n" )[[1L]]
rx <- sprintf('^[{]"%s"', fun )
match <- grep( rx, names.c)
if( length(match) != 1L ){
@romainfrancois
romainfrancois / results.txt
Created January 9, 2014 13:54
Simple implementation of which using threads in C++11
> bench(1000)
Unit: microseconds
expr min lq median uq max neval
which(b) 2.104 4.548 5.1000 6.0955 18.306 100
which_cpp_nthreads(b, 2) 26.401 28.303 28.9285 30.0625 103.980 100
which_cpp_nthreads(b, 4) 45.902 49.494 50.6335 52.1155 115.184 100
which_cpp_nthreads(b, 8) 97.596 100.143 101.3720 104.9895 270.521 100
> bench(1e+05)
Unit: microseconds
ndiff <- 2597797
f <- function(){
n <- round(runif( 1, min = 4, max = 10 ))
paste( sample(letters, n, replace = TRUE ), collapse = "" )
}
a <- replicate( ndiff, f() )
b <- replicate( ndiff, f() )
rows <- sample( 1:ndiff, 3018671, replace = T )
cols <- sample( c(T,F), 3018671, replace = T )
@romainfrancois
romainfrancois / count_positive.cpp
Last active January 1, 2016 16:59
Counting the number of positives values in a numeric vector. serial and multithreaded versions.
#include <Rcpp.h>
#include <thread>
#include <future>
using namespace Rcpp ;
typedef NumericVector::iterator Iterator ;
struct count_positive {
int operator()(Iterator first, Iterator last){
library(Rcpp)
library(microbenchmark)
vectorized <- function() {
a <- c(1, 1)
b <- c(2, 2)
for (i in 1:1000000) {
x <- a + b
}