Your SlideShare is downloading. ×
0
Introduction to .Call interface                                         Hao Chai                       Department of Stati...
Outline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Hao Chai (U of...
IntroductionOutline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Ha...
Introduction.C and .Call are two interfaces in R to call C routines. Similar foreignlanguage interface functions are .Fort...
IntroductionAdvantages of R       Straight forward syntax.       Easy to learn, to use, to track the results and to debug....
IntroductionAdvantages of R       Straight forward syntax.       Easy to learn, to use, to track the results and to debug....
IntroductionToy exampleOur goal is to generate a variance-covariance matrix Σ = (σij ), where        a · b|i−j| , if i = j...
IntroductionToy example   An R function using matrix indexing matgen Rind:   matgen_Rind = function(P, a, b)   {     Covma...
Introduction    An R function calling C routines matgen RC:    matgen_RC = function(P, a, b)    {      .Call("matrix_gen",...
IntroductionToy exampleResult> P = 1000           test                                            elapsed     user.self   ...
C partOutline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Hao Chai...
C partHeader filesTo enable the communication between R and C, the following header filesare needed in the C code.#include  ...
C part     .C interfaceTo use .C interface, main() function in C code needs to be replaced bystatic void CFuncName(arg1, a...
C part     .C interface.C interface in toy example#include <R.h>#include <Rinternals.h>#include <R_ext/Rdynload.h>#include...
C part     .C interfaceMapping types between R and C                          Table: Comparison of R and C data types.1   ...
C part     .Call interfaceWhat can we expect from .Call?2       Pass R objects to C       Create R objects in C       Mani...
C part     .Call interfaceTo use .Call interface, main() function in C code needs to be replaced bySEXP CFuncName(SEXP arg...
C part     .Call interfaceBasic structure of C functionsSEXP CFuncName(SEXP arg1, arg2, ...){  [arg1 = coerceVector(arg1, ...
C part     .Call interfaceUseful data types in Rinternals.h                                                               ...
C part     .Call interfaceUseful macros in Rinternals.hThere are plenty of useful functions defined in Rinternals.h. Useful...
C part     .Call interfaceUseful functions in Rinternals.h                    Table: List of useful functions in Rinternal...
C part     .Call interfaceSpecial ValuesThere are some constants for special values in R    R NaN    R PosInf    R NegInf ...
C part     .Call interface.Call interface in toy example#include <R.h>#include <Rinternals.h>#include <R_ext/Rdynload.h>#i...
C part     .Call interface.Call interface in toy example    double *mat, a = REAL(Ra)[0], b = REAL(Rb)[0];    mat = REAL(R...
R partOutline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Hao Chai...
R partInterfaces in RTo create a dynamic library, run the following code in the command line:R CMD SHLIB CFileName.cCFileN...
R partThere are three interfaces in R to load the dynamic library generated by C.  1   .C(”CFuncName” arg1, arg2, ...)    ...
ExamplesOutline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Hao Ch...
ExamplesPass numeric values to Cfunc1 is a function with two arguments Rn and Rvec. It returns the largestelement of the fi...
ExamplesPass numeric values to CIn R, we do the followinga = c(7, 4, 8, 9, 2, 5)n = 4dyn.load("f:/presentation/intro2dotCa...
ExamplesPass strings, return a listfunc2 is able to calculate the quadratic form or the linear product of amatrix and a ve...
ExamplesPass strings, return a listSEXP func2(SEXP RM, SEXP Ry, SEXP Rstr){  Rstr = coerceVector(Rstr, STRSXP);  RM = coer...
Examplesif (((strcmp(method, "both") == 0) ||     (strcmp(method,"linear") == 0)) && (nc != l)) error("The matrix and the ...
Examplesif (strcmp(method, "linear") == 0)  {   PROTECT(vec = allocVector(REALSXP, nr));   result2 = REAL(vec);  }if (strc...
Examplesif ((strcmp(method, "linear") == 0) ||      (strcmp(method, "both") == 0))    for (i = 0; i < nr; i++)    {       ...
Examples    if (strcmp(method, "quadratic") == 0)      SET_VECTOR_ELT(return_lst, 0, num);    if (strcmp(method, "linear")...
ExamplesPass strings, return a listThe R code to call the function func2 isdyn.load("f:/presentation/intro2dotCall/illust....
OtherOutline1   Introduction2   C part      .C interface      .Call interface3   R part4   Examples5   Other     Hao Chai ...
OtherEvaluating R expressions in CIt is also possible to evaluate R expressions in C code. The followingfunctions/macros a...
OtherSome misc.    The “Writing R Extensions” manual is the main source for this talk.    More functions and macros can be...
OtherRefrencesWriting R ExtensionsR InternalsUsing .Call in R by Brian Caffo.dot Call Interface by Gopi Goswami.Rinternals....
Other                       Thank you!Hao Chai (U of Iowa)    Introduction to .Call interface   41 / 41
Upcoming SlideShare
Loading in...5
×

Dot Call interface

718

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
718
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Dot Call interface"

  1. 1. Introduction to .Call interface Hao Chai Department of Statistics and Actuarial Science The University of Iowa Productive Seminar March 6, 2012Hao Chai (U of Iowa) Introduction to .Call interface 1 / 41
  2. 2. Outline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 2 / 41
  3. 3. IntroductionOutline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 3 / 41
  4. 4. Introduction.C and .Call are two interfaces in R to call C routines. Similar foreignlanguage interface functions are .Fortran and .External. Hao Chai (U of Iowa) Introduction to .Call interface 4 / 41
  5. 5. IntroductionAdvantages of R Straight forward syntax. Easy to learn, to use, to track the results and to debug. Over 3,000 packages available to carry out many kinds of statistical analysis and produce professional graphics. Vectorized language. ...Disadvantage of R Loops.Hao Chai (U of Iowa) Introduction to .Call interface 5 / 41
  6. 6. IntroductionAdvantages of R Straight forward syntax. Easy to learn, to use, to track the results and to debug. Over 3,000 packages available to carry out many kinds of statistical analysis and produce professional graphics. Vectorized language. ...Disadvantage of R Loops.Hao Chai (U of Iowa) Introduction to .Call interface 5 / 41
  7. 7. IntroductionToy exampleOur goal is to generate a variance-covariance matrix Σ = (σij ), where a · b|i−j| , if i = j;σij = 1, if i = j.We have at least three methods: A simple R function matgen R: matgen_R = function(P, a, b) { Covmatrix = matrix(rep(1, P * P), nrow = P) for (i in 1 : P) for (j in 1 : P) if (i != j) Covmatrix[i, j] = exp(log(a) + log(b) * abs(i - j)) Covmatrix } Hao Chai (U of Iowa) Introduction to .Call interface 6 / 41
  8. 8. IntroductionToy example An R function using matrix indexing matgen Rind: matgen_Rind = function(P, a, b) { Covmatrix = matrix(rep(1, P * P), nrow = P) for (i in 1 : (P - 1)) {Covmatrix[cbind((1 + i) : P, 1 : (P - i))] = exp(log(a) + log(b) * abs(i)) Covmatrix[cbind(1 : (P - i), (1 + i) : P)] = exp(log(a) + log(b) * abs(i))} Covmatrix } Hao Chai (U of Iowa) Introduction to .Call interface 7 / 41
  9. 9. Introduction An R function calling C routines matgen RC: matgen_RC = function(P, a, b) { .Call("matrix_gen", as.integer(P), as.double(a), as.double(b)) }Also we can use R compiler to compile the first two pure R functions andget matgen Rcmp and matgen Rindcmp. Hao Chai (U of Iowa) Introduction to .Call interface 8 / 41
  10. 10. IntroductionToy exampleResult> P = 1000 test elapsed user.self relative 5 matgen Rindcmp(P, 0.5, 0.9) 0.80 0.75 1.00 4 matgen Rind(P, 0.5, 0.9) 0.97 0.86 1.21 1 matgen RC(P, 0.5, 0.9) 1.04 1.01 1.30 2 matgen Rcmp(P, 0.5, 0.9) 19.98 19.51 24.98 3 matgen R(P, 0.5, 0.9) 65.44 64.45 81.80 Hao Chai (U of Iowa) Introduction to .Call interface 9 / 41
  11. 11. C partOutline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 10 / 41
  12. 12. C partHeader filesTo enable the communication between R and C, the following header filesare needed in the C code.#include <R.h>#include <Rinternals.h> /% For .C or .Call %/#include <Rdefines.h> /% For .External %/#include <R_ext/Rdynload.h> Hao Chai (U of Iowa) Introduction to .Call interface 11 / 41
  13. 13. C part .C interfaceTo use .C interface, main() function in C code needs to be replaced bystatic void CFuncName(arg1, arg2, ...) Arguments above have to be of pointer type. Therefore, we need to coerce the scalar arguments to scalars in C. C function can only return values to R through its arguments. In other words, there are two kinds of arguments: arguments that pass values from R to C and arguments that return values from C to R.The C code using .C interface in the toy example is Hao Chai (U of Iowa) Introduction to .Call interface 12 / 41
  14. 14. C part .C interface.C interface in toy example#include <R.h>#include <Rinternals.h>#include <R_ext/Rdynload.h>#include <math.h>static void matrix_gen(double *Rp, double *Ra, double *Rb, double *Rmatrix){ R_len_t i, j, p = Rp[0]; double a = Ra[0], b = Rb[0]; for (i = 0; i < p; i++) for (j = 0; j < p; j++) if (i == j) Rmatrix[i * p + j] = 1; else Rmatrix[i * p + j] = exp(log(a) + abs(i - j) * log(b));} Hao Chai (U of Iowa) Introduction to .Call interface 13 / 41
  15. 15. C part .C interfaceMapping types between R and C Table: Comparison of R and C data types.1 R storage mode C types logical int * integer int * double double * character char ** complex Rcomplex * 1 Cited from “Writing R Extensions” Hao Chai (U of Iowa) Introduction to .Call interface 14 / 41
  16. 16. C part .Call interfaceWhat can we expect from .Call?2 Pass R objects to C Create R objects in C Manipulate R objects in C Return R objects from C Call R functions from C 2 See Brian Caffo Hao Chai (U of Iowa) Introduction to .Call interface 15 / 41
  17. 17. C part .Call interfaceTo use .Call interface, main() function in C code needs to be replaced bySEXP CFuncName(SEXP arg1, SEXP arg2, ...) SEXP is a structure in C standing for S expression. The returned value from C will also be SEXP type. If new SEXP objects are defined within the C function, use PROTECT to protect them in the memory so that it will not be cleaned by R as garbage. Hao Chai (U of Iowa) Introduction to .Call interface 16 / 41
  18. 18. C part .Call interfaceBasic structure of C functionsSEXP CFuncName(SEXP arg1, arg2, ...){ [arg1 = coerceVector(arg1, INTSXP)] # Coerce the vector to the right type. SEXP Robj, return_val; # Define new R objects. PROTECT(return_val = allocMatrix(REALSXP, arg1, arg2)) # Protect return_val. [body of function] UNPROTECT(1) return(return_val) # Return the values to R.} Hao Chai (U of Iowa) Introduction to .Call interface 17 / 41
  19. 19. C part .Call interfaceUseful data types in Rinternals.h 3 Table: SEXPTYPE and R equivalent SEXPTYPE R equivalent REALSXP numeric with storage mode double INTSXP integer LGLSXP logical STRSXP character VECSXP list (generic vector) DOTSXP a ‘...’ object ENVSXP environment 3 Cited from “Writing R Extensions” Hao Chai (U of Iowa) Introduction to .Call interface 18 / 41
  20. 20. C part .Call interfaceUseful macros in Rinternals.hThere are plenty of useful functions defined in Rinternals.h. Usefulfunctions can be found in “Writing R Extensions” or “R Internals”. Table: List of useful functions in Rinternals.h4 Name Usage Example coerceVector Coerce R object to SEXPTYPE arg1=..(arg1,INTSXP) allocVector Allocate memory for R vector Robj=..(STRSXP,2) allocMatrix Allocate memory for R matrix Robj=..(STRSXP,2,2) nrows Get the No. of rows of a matrix ..nrow(Rmatrix) ncols Get the No. of cols of a matrix ..ncol(Rmatrix) PROTECT Protect an R object See examples. UNPROTECT UNPROTECT some R objects See examples. 4 .. represents the function name in the same row. Hao Chai (U of Iowa) Introduction to .Call interface 19 / 41
  21. 21. C part .Call interfaceUseful functions in Rinternals.h Table: List of useful functions in Rinternals.h Cont.5 Name Usage Example STRING ELT Returns the memo. add. of R string ..(Rstr, 0) VECTOR ELT Returns the memo. add. of R list ..(Rlist, 1) INTEGER Return the memo. add. of R int ..(Rint) REAL Return the memo. add. of R real ..(Rdoub) CHAR Convert a Rchar to characters ..(Rchar) SET VECTOR ELT Assign value to an element of R list See example 5 .. represents the function name in the same row. Hao Chai (U of Iowa) Introduction to .Call interface 20 / 41
  22. 22. C part .Call interfaceSpecial ValuesThere are some constants for special values in R R NaN R PosInf R NegInf R NaReal R NilValueThere are also some macros for error checking in C ISNA, ISNAN,R FINITE, NA INTEGER, NA LOGICAL, NA STRING, NA REAL Hao Chai (U of Iowa) Introduction to .Call interface 21 / 41
  23. 23. C part .Call interface.Call interface in toy example#include <R.h>#include <Rinternals.h>#include <R_ext/Rdynload.h>#include "math.h"SEXP matrix_gen(SEXP RP, SEXP Ra, SEXP Rb){ R_len_t i, j, p = INTEGER(RP)[0]; SEXP Rmatrix; RP = coerceVector(RP, INTSXP); Ra = coerceVector(Ra, REALSXP); Rb = coerceVector(Rb, REALSXP); PROTECT(Rmatrix = allocMatrix(REALSXP, p, p)); Hao Chai (U of Iowa) Introduction to .Call interface 22 / 41
  24. 24. C part .Call interface.Call interface in toy example double *mat, a = REAL(Ra)[0], b = REAL(Rb)[0]; mat = REAL(Rmatrix); for (i = 0; i < p; i++) for (j = 0; j < p; j++) if (i == j) mat[i * p + j] = 1; else mat[i * p + j] = exp(log(a) + abs(i - j) * log(b)); UNPROTECT(1); return(Rmatrix);} Hao Chai (U of Iowa) Introduction to .Call interface 23 / 41
  25. 25. R partOutline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 24 / 41
  26. 26. R partInterfaces in RTo create a dynamic library, run the following code in the command line:R CMD SHLIB CFileName.cCFileName.dll or CFileName.so will be generated in the same directory.Then load the dynamic library which is generated by C code using> dyn.load("CFileName.so")in Linux/MacOS, or> dyn.load("CFileName.dll")in Windows, or a universal solution> dyn.load(paste("CFileName", .Platform$dynlib.ext,+ sep = "")) Hao Chai (U of Iowa) Introduction to .Call interface 25 / 41
  27. 27. R partThere are three interfaces in R to load the dynamic library generated by C. 1 .C(”CFuncName” arg1, arg2, ...) , 2 .Call(”CFuncName” arg1, arg2, ...) , 3 .External(”CFuncName”, arg1, arg2, ...) There are some options when using .C, such as NAOK and PACKAGE. For detailed information, refer to “Writing R Extensions”. Hao Chai (U of Iowa) Introduction to .Call interface 26 / 41
  28. 28. ExamplesOutline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 27 / 41
  29. 29. ExamplesPass numeric values to Cfunc1 is a function with two arguments Rn and Rvec. It returns the largestelement of the first Rn elements of vector Rvec.SEXP func1(SEXP Rvec, SEXP Rn){ Rvec = coerceVector(Rvec, REALSXP); R_len_t i = 0, n = INTEGER(Rn)[0]; double *vec, max = REAL(Rvec)[0]; SEXP return_val; PROTECT(return_val = allocVector(REALSXP, 1)); vec = REAL(Rvec); for (i = 1; i < n; i++) if (vec[i] >= max) max = vec[i]; REAL(return_val)[0] = max; UNPROTECT(1); return(return_val);} Hao Chai (U of Iowa) Introduction to .Call interface 28 / 41
  30. 30. ExamplesPass numeric values to CIn R, we do the followinga = c(7, 4, 8, 9, 2, 5)n = 4dyn.load("f:/presentation/intro2dotCall/illust.dll").Call("func1", as.double(a), as.integer(n)) Hao Chai (U of Iowa) Introduction to .Call interface 29 / 41
  31. 31. ExamplesPass strings, return a listfunc2 is able to calculate the quadratic form or the linear product of amatrix and a vector. It has three arguments: Rstr, Ry and RM. Ry is avector, RM is a matrix. If Rstr is ”quadratic”, then t(Ry) %*% RM %*%Ry is returned. If Rstr is ”linear”, then RM %*% Ry is returned. If Rstr is”both” then both of the above will be returned as an R list. , Hao Chai (U of Iowa) Introduction to .Call interface 30 / 41
  32. 32. ExamplesPass strings, return a listSEXP func2(SEXP RM, SEXP Ry, SEXP Rstr){ Rstr = coerceVector(Rstr, STRSXP); RM = coerceVector(RM, REALSXP); Ry = coerceVector(Ry, REALSXP); SEXP return_lst, vec, num; double *M = REAL(RM), *y = REAL(Ry); double result1 = 0, *result2; R_len_t i, j, nr, nc, l; const char *method; method = CHAR(STRING_ELT(Rstr, 0)); nr = nrows(RM); nc = ncols(RM); l = length(Ry); Hao Chai (U of Iowa) Introduction to .Call interface 31 / 41
  33. 33. Examplesif (((strcmp(method, "both") == 0) || (strcmp(method,"linear") == 0)) && (nc != l)) error("The matrix and the vector need to be conformable!");if (((strcmp(method, "both") == 0) || (strcmp(method, "quadratic") == 0)) && ((nc != l) || (nr != l))) error("The matrix and the vector need to be conformable!");if (strcmp(method, "both") == 0) { PROTECT(return_lst = allocVector(VECSXP, 2)); PROTECT(vec = allocVector(REALSXP, nr)); PROTECT(num = allocVector(REALSXP, 1)); result2 = REAL(vec); result1 = REAL(num)[0]; }else PROTECT(return_lst = allocVector(VECSXP, 1)); Hao Chai (U of Iowa) Introduction to .Call interface 32 / 41
  34. 34. Examplesif (strcmp(method, "linear") == 0) { PROTECT(vec = allocVector(REALSXP, nr)); result2 = REAL(vec); }if (strcmp(method, "quadratic") == 0) { PROTECT(num = allocVector(REALSXP, 1)); result1 = REAL(num)[0]; }if ((strcmp(method, "quadratic") == 0) || (strcmp(method, "both") == 0)) for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) result1 = result1 + M[i + j * nr] * y[i] * y[j]; Hao Chai (U of Iowa) Introduction to .Call interface 33 / 41
  35. 35. Examplesif ((strcmp(method, "linear") == 0) || (strcmp(method, "both") == 0)) for (i = 0; i < nr; i++) { result2[i] = 0; for (j = 0; j < nc; j++) result2[i] = result2[i] + M[i + j * nr] * y[j]; } Hao Chai (U of Iowa) Introduction to .Call interface 34 / 41
  36. 36. Examples if (strcmp(method, "quadratic") == 0) SET_VECTOR_ELT(return_lst, 0, num); if (strcmp(method, "linear") == 0) SET_VECTOR_ELT(return_lst, 0, vec); if (strcmp(method, "both") == 0) { SET_VECTOR_ELT(return_lst, 0, num); SET_VECTOR_ELT(return_lst, 1, vec); } UNPROTECT(2); if (strcmp(method, "both") == 0) UNPROTECT(1); return(return_lst);} Hao Chai (U of Iowa) Introduction to .Call interface 35 / 41
  37. 37. ExamplesPass strings, return a listThe R code to call the function func2 isdyn.load("f:/presentation/intro2dotCall/illust.dll")M <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)y <- c(1, 0, -1).Call("func2", M, as.double(y), "both") Hao Chai (U of Iowa) Introduction to .Call interface 36 / 41
  38. 38. OtherOutline1 Introduction2 C part .C interface .Call interface3 R part4 Examples5 Other Hao Chai (U of Iowa) Introduction to .Call interface 37 / 41
  39. 39. OtherEvaluating R expressions in CIt is also possible to evaluate R expressions in C code. The followingfunctions/macros are useful in this case. More details can be found in“Writing R extensions” . defineVar findVar findFun install Hao Chai (U of Iowa) Introduction to .Call interface 38 / 41
  40. 40. OtherSome misc. The “Writing R Extensions” manual is the main source for this talk. More functions and macros can be found in Rinternals.h, or alternatively Rdefines.h. Choose one and stick to it. Several times, when ran .Call in R, R crashed. Very likely, it was caused by the difference between number of arguments in .Call and number of arguments in the original C functions. Related C and R files are available at Related files. Hao Chai (U of Iowa) Introduction to .Call interface 39 / 41
  41. 41. OtherRefrencesWriting R ExtensionsR InternalsUsing .Call in R by Brian Caffo.dot Call Interface by Gopi Goswami.Rinternals.h Hao Chai (U of Iowa) Introduction to .Call interface 40 / 41
  42. 42. Other Thank you!Hao Chai (U of Iowa) Introduction to .Call interface 41 / 41
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×