Extend R with Rcpp!!!

3,387 views
3,275 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,387
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Extend R with Rcpp!!!

  1. 1. Extend R with Rcpp!!! 2010/09/19 Tsukuba.R#8 id:mickey24
  2. 2. id: mickey24 (@mickey24)
  3. 3. Tsukuba.R
  4. 4. Tsukuba.R#4 •R Brainf*ck • Brainf*ck > hello <- "+++++++++[>++++++++>++++++++++ +>+++++<<<-]>.>++.+++++++..++ +.>-.------------.<++++++++.--------.++ +.------.--------.>+." > brainfxxk(hello) [1] "Hello, world!" http://www.slideshare.net/mickey24/rbrainfck-1085191
  5. 5. Tsukuba.R#5 • Animation with R • library(animation) http://d.hatena.ne.jp/mickey24/20090614
  6. 6. Tsukuba.R#6 • Extend R with C!!! • C R (C OpenCV ) http://d.hatena.ne.jp/mickey24/20091123/r_de_extension
  7. 7. Tsukuba.R#7 • Parallel Conputing in R • snow http://d.hatena.ne.jp/mickey24/20100510/tsukuba_r7
  8. 8. Tsukuba.R#8 Rcpp R
  9. 9. Outline • R (C++) • Rcpp •
  10. 10. R (C++)
  11. 11. C++ R • C++ R (C/FORTRAN OK) • C++ R
  12. 12. 1. • • C R (40 50 ) www - Seeking for my unique color. http://d.hatena.ne.jp/syou6162/20090117/1232120983 2. R C
  13. 13. C++ R • C++ R API extern "C" SEXP foo(SEXP x) { ... }
  14. 14. C++ R 1. C++ R 2. .so 3. R .so 4. .Call .so R C++ 1. add_vector 2. cat_string
  15. 15. add_vector.cc #include <R.h> #include <Rdefines.h> extern "C" SEXP add_vector(SEXP x, SEXP y) { PROTECT(x = AS_NUMERIC(x)); PROTECT(y = AS_NUMERIC(y)); const int n = LENGTH(x); SEXP z; PROTECT(z = allocVector(REALSXP, n)); for (int i = 0; i < n; ++i) { REAL(z)[i] = REAL(x)[i] + REAL(y)[i]; } UNPROTECT(3); return z; }
  16. 16. add_vector.cc • $ R CMD SHLIB add_vector.cc • .so $ ls add_vector.cc add_vector.o add_vector.so
  17. 17. add_vector.cc •R .so > dyn.load("add_vector.so") • > .Call("add_vector", 1:3, 4:6) [1] 5 7 9 OK!
  18. 18. cat_string.cc #include <R.h> #include <Rdefines.h> #include <string> extern "C" SEXP cat_string(SEXP x, SEXP y) { PROTECT(x = AS_CHARACTER(x)); PROTECT(y = AS_CHARACTER(y)); SEXP z; PROTECT(z = allocVector(STRSXP, 1)); std::string str(CHAR(STRING_ELT(x, 0))); str += CHAR(STRING_ELT(y, 0)); SET_STRING_ELT(z, 0, mkChar(str.c_str())); UNPROTECT(3); return z; }
  19. 19. cat_string.cc • $ R CMD SHLIB cat_string.cc •R > dyn.load("cat_string.so") > .Call("cat_string", "foo", "bar") [1] "foobar" OK!
  20. 20. R • PROTECT / UNPROTECT • • UNPROTECT(n) → n … • GC • SEXP C++ • • REAL(x) • C++
  21. 21. Outline • R (C++) • Rcpp •
  22. 22. Rcpp
  23. 23. Rcpp •R C++ • • C++ R
  24. 24. • > install.packages("Rcpp") • Rcpp
  25. 25. Overview • > vignette("Rcpp-introduction") •
  26. 26. OK • Rcpp R add_vector.cc Rcpp
  27. 27. add_vector.cc ( ) #include <R.h> #include <Rdefines.h> extern "C" SEXP add_vector(SEXP x, SEXP y) { PROTECT(x = AS_NUMERIC(x)); PROTECT(y = AS_NUMERIC(y)); const int n = LENGTH(x); SEXP z; PROTECT(z = allocVector(REALSXP, n)); for (int i = 0; i < n; ++i) { REAL(z)[i] = REAL(x)[i] + REAL(y)[i]; } UNPROTECT(3); return z; }
  28. 28. add_vector_rcpp.cc #include <Rcpp.h> RcppExport SEXP add_vector_rcpp(SEXP xx, SEXP yy) { Rcpp::NumericVector x(xx); Rcpp::NumericVector y(yy); int n = x.length(); Rcpp::NumericVector z(n); for (int i = 0; i < n; ++i) { z[i] = x[i] + y[i]; } return z; }
  29. 29. 1. PROTECT / UNPROTECT • Rcpp • UNPROTECT 2. REAL(x) • [] • C++ COOL!!!
  30. 30. #include <R.h> #include <Rcpp.h> #include <Rdefines.h> RcppExport SEXP add_vector_rcpp(SEXP xx, extern "C" SEXP add_vector(SEXP x, SEXP y) { SEXP yy) { PROTECT(x = AS_NUMERIC(x)); Rcpp::NumericVector x(xx); PROTECT(y = AS_NUMERIC(y)); Rcpp::NumericVector y(yy); const int n = LENGTH(x); const int n = x.length(); SEXP z; Rcpp::NumericVector z(n); PROTECT(z = allocVector(REALSXP, n)); for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) { z[i] = x[i] + y[i]; REAL(z)[i] = REAL(x)[i] + REAL(y)[i]; } } return z; UNPROTECT(3); } return z; } 1. PROTECT / UNPROTECT 2. REAL(x)
  31. 31. Rcpp • Makevars • Rcpp.h PKG_CXXFLAGS=$(shell Rscript -e "Rcpp:::CxxFlags()") PKG_LIBS=$(shell Rscript -e "Rcpp:::LdFlags()")
  32. 32. Rcpp • $ R CMD SHLIB add_vector_rcpp.cc > dyn.load("add_vector_rcpp.so") > .Call("add_vector_rcpp", 1:3, 4:6) [1] 5 7 9
  33. 33. NumericVector • Rcpp::NumericVector #include <Rcpp.h> RcppExport SEXP add_vector_rcpp(SEXP xx, SEXP yy) { Rcpp::NumericVector x(xx); Rcpp::NumericVector y(yy); Rcpp::NumericVector z(x + y); return z; }
  34. 34. C++ • SEXP C++ #include <Rcpp.h> #include <string> RcppExport SEXP cat_string_rcpp(SEXP xx, SEXP yy) { std::string x(Rcpp::as<std::string>(xx)); std::string y(Rcpp::as<std::string>(yy)); std::string z(x + y); return Rcpp::wrap(z); }
  35. 35. as wrap • SEXP C++ 1. primitive int, double, etc. 2. std::string, const char* 3. std::vector<T>, std::list<T> 4. std::map<string, T> 5. SEXP 6. wrap
  36. 36. Rcpp PROTECT UNPROTECT REAL(x), CHAR(x), [] ( ) STRING_ELT(x, i) SEXP C++ (as, wrap) Makervars Rcpp
  37. 37. Outline • R (C++) • Rcpp •
  38. 38. • Rcpp • Rcpp
  39. 39. • 0.7 n 1. r2norm_for : R for (syou6162 ) 2. r2norm_cpp : C++ (syou6162 ) 3. r2norm_rcpp : Rcpp > x <- r2norm_cpp(1000) > cor(x[,1], x[,2]) [1] 0.7144986 > plot(x) http://d.hatena.ne.jp/syou6162/20090117/1232120983
  40. 40. r2norm_cpp.cc r2norm_rcpp.cc #include <R.h> #include <Rcpp.h> #include <Rdefines.h> RcppExport SEXP r2norm_rcpp(SEXP num) { extern "C" SEXP r2norm_cpp(SEXP num) { const int n = Rcpp::as<int>(num); PROTECT(num = AS_INTEGER(num)); Rcpp::NumericMatrix ans(n, 2); const int n = INTEGER(num)[0]; GetRNGstate(); SEXP ans; PROTECT(ans = allocMatrix(REALSXP, n, 2)); double prevX1 = 2.0; double prevX2 = 1.0; GetRNGstate(); for (int i = 0; i < n; ++i) { prevX1 = 1.0 + 0.7 * (prevX2 - 2.0) + double prevX1 = 2.0; (norm_rand() * (1.0 - 0.7 * 0.7)); double prevX2 = 1.0; ans(i, 0) = prevX1; for (int i = 0; i < n; ++i) { prevX2 = 2.0 + 0.7 * (prevX1 - 1.0) + prevX1 = 1.0 + 0.7 * (prevX2 - 2.0) + (norm_rand() * (1.0 - 0.7 * 0.7)); (norm_rand() * (1.0 - 0.7 * 0.7)); ans(i, 1) = prevX2; REAL(ans)[i] = prevX1; } prevX2 = 2.0 + 0.7 * (prevX1 - 1.0) + PutRNGstate(); (norm_rand() * (1.0 - 0.7 * 0.7)); REAL(ans)[i+n] = prevX2; return ans; } } PutRNGstate(); UNPROTECT(2); return ans; }
  41. 41. n = 1,000 n = 10^4 n = 10^5 n = 10^6 R for 0.035 0.330 3.372 33.982 C++ 0.001 0.003 0.028 0.312 Rcpp 0.001 0.005 0.050 0.507 sec
  42. 42. R for C++ Rcpp sec 40.00 30.00 20.00 10.00 0 n=1000 n=10^4 n=10^5 n=10^6 R for 67
  43. 43. ( vs Rcpp) C++ Rcpp sec 0.60 0.45 0.30 0.15 0 n=1000 n=10^4 n=10^5 n=10^6
  44. 44. • Rcpp C++ R • R (PROTECT / UNPROTECT ) • SEXP C++ •
  45. 45. • C++ R • R API C++ • C++ R
  46. 46. • Rcpp: Seamless R and C++ Integration http://dirk.eddelbuettel.com/code/rcpp.html • Rcpp - Seeking for my unique color. http://d.hatena.ne.jp/syou6162/20100316/1268734140 • C R (40 50 ) www - Seeking for my unique color. http://d.hatena.ne.jp/syou6162/20090117/1232120983 • Writing R Extensions http://cran.r-project.org/doc/contrib/manuals-jp/R-exts.jp.pdf
  47. 47. Tsukuba.R#7
  48. 48. RTetris 2010/09/19 Tsukuba.R#8 id:mickey24
  49. 49. RTetris • R( C++) • Tsukuba.R#7 ( ) • tetris C++ • R • ncurses • R R
  50. 50. RTetris R • C++(Rcpp) • ncurses C++ • R • • Tsukuba.R#8
  51. 51.
  52. 52.
  53. 53. 1. 2. 3. 4.
  54. 54. • • ncurses • C++ ncurses ( ) RcppExport SEXP ncurses_initialize() { initscr(); noecho(); nodelay(stdscr, true); curs_set(0); return R_NilValue; }
  55. 55. R • • R (ry • C++ & ncurses
  56. 56. • ncurses getch • ASCII -1 RcppExport SEXP ncurses_getch() { return wrap(getch()); } • R • • proc.time() while (running) { key = ncurses_getch() if (key != -1) { } now = proc.time() if (now[3] >= next_time) { } Sys.sleep(0.001) }
  57. 57. • • • all • all(field[i,] == BLOCK)
  58. 58. •     a11 a12 a13 a14 1 a21 a22 a23 a24   1  A= a31  R=  a32 a33 a34   1  a41 a42 a43 a44 1
  59. 59. ( ) •     a11 a12 a13 a14 1 a21 a22 a23 a24   1  AR =  a31 ×  a32 a33 a34   1  a41 a42 a43 a44 1   a14 a13 a12 a11 a24 a23 a22 a21  =a34  a33 a32 a31  a44 a43 a42 a41
  60. 60. ( ) •  T a14 a13 a12 a11 a24 a23 a22 a21  (AR) =  T a34  a33 a32 a31  a44 a43 a42 a41   a14 a24 a34 a44 a13 a23 a33 a43  = a12  a22 a32 a42  a11 a21 a31 a41
  61. 61. ( ) •     a11 a12 a13 a14 a41 a31 a21 a11 a21 a22 a23 a24  a42 a32 a22 a12  A= a31  (RA)T =   a32 a33 a34  a43 a33 a23 a13  a41 a42 a43 a44 a44 a34 a24 a14
  62. 62. • • R • • •

×