Trying to sourceCpp the test.cpp, I get all that on windows with gcc 4.6.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("devtools") | |
library("testthat") | |
install_github( "Rcpp11/Rcpp11") | |
install_github( "Rcpp11/Rcpp-test") | |
library( "RcppTest" ) | |
test_package( "RcppTest" ) |
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.
$ 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)"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ", " ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
#include <thread> | |
#include <future> | |
using namespace Rcpp ; | |
typedef NumericVector::iterator Iterator ; | |
struct count_positive { | |
int operator()(Iterator first, Iterator last){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(Rcpp) | |
library(microbenchmark) | |
vectorized <- function() { | |
a <- c(1, 1) | |
b <- c(2, 2) | |
for (i in 1:1000000) { | |
x <- a + b | |
} |