SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Downloaden Sie, um offline zu lesen
Native Interfaces for R

           Seth Falcon

Fred Hutchinson Cancer Research Center


         20-21 May, 2010
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
which   as implemented in R prior to R-2.11




   which <- function(x) {
       seq_along(x)[x & !is.na(x)]
   }
which   as implemented in R prior to R-2.11



   which <- function(x) {
       seq_along(x)[x & !is.na(x)]
   }

        1. is.na
        2. !
        3. &
        4. seq_along
        5. [
which   in C


    1 SEXP nid_which(SEXP v) {
    2     SEXP ans;
    3     int i, j = 0, len = Rf_length(v), *buf, *tf;
    4     buf = (int *) R_alloc(len, sizeof(int));
    5     tf = LOGICAL(v);
    6     for (i = 0; i < len; i++) {
    7         if (tf[i] == TRUE) buf[j] = i + 1; j++;
    8     }
    9     ans = Rf_allocVector(INTSXP, j);
   10     memcpy(INTEGER(ans), buf, sizeof(int) * j);
   11     return ans;
   }
which   is now 10x faster



   system.time(which(v))



    R Version    System time    Elapsed time
    R-2.10       0.018          0.485
    R-2.11       0.001          0.052


   v is a logical vector with 10 million elements
Making use of existing code




 Access algorithms            Interface to other systems
     RBGL                         RSQLite
     RCurl                        netcdf
     Rsamtools                    SJava
Review of C

   #include <stdio.h>
   double _nid_avg(int *data, int len) {
       int i;
       double ans = 0.0;
       for (i = 0; i < len; i++) {
         ans += data[i];
       }
       return ans / len;
   }
   main() {
       int ex_data[5] = {1, 2, 3, 4, 5};
       double m = _nid_avg(ex_data, 5);
       printf("%fn", m);
   }
The .Call interface
Symbolic Expressions (SEXPs)




      The SEXP is R’s fundamental data type at the C-level
      SEXP is short for symbolic expression and is borrowed from
      Lisp
      History at http://en.wikipedia.org/wiki/S-expression
.Call Start to Finish Demo




   Demo
.Call Disection

   R
   # R/somefile.R
   avg <- function(x) .Call(.nid_avg, x)
   # NAMESPACE
   useDynLib("yourPackage", .nid_avg = nid_avg)


   C
   /* src/some.c */
   #include <Rinternals.h>
   SEXP nid_avg(SEXP data) { ... INTEGER(data) ... }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
.Call C function


   #include <Rinternals.h>

   SEXP nid_avg(SEXP data)
   {
       SEXP ans;
       double v = _nid_avg(INTEGER(data), Rf_length(data));
       PROTECT(ans = Rf_allocVector(REALSXP, 1));
       REAL(ans)[0] = v;
       UNPROTECT(1);
       return ans;
       /* shortcut: return Rf_ScalarReal(v); */
   }
Function Registration




      In NAMESPACE
      In C via R_CallMethodDef (See
      ShortRead/src/R_init_ShortRead.c for a nice example.
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
Just one thing to learn




       Everything is a SEXP
       But there are many different types of SEXPs
       INTSXP, REALSXP, STRSXP, LGLSXP, VECSXP, and more.
What’s a SEXP?
Common SEXP subtypes



   R function    SEXP subtype   Data accessor
   integer()     INTSXP         int *INTEGER(x)
   numeric()     REALSXP        double *REAL(x)
   logical()     LGLSXP         int *LOGICAL(x)
   character()   STRSXP         CHARSXP STRING ELT(x, i)
   list()        VECSXP         SEXP VECTOR ELT(x, i)
   NULL          NILSXP         R NilValue
   externalptr   EXTPTRSXP      SEXP (accessor funcs)
STRSXP/CHARSXP Exercise




  Try this:


       .Internal(inspect(c("abc", "abc", "xyz")))
STRSXP/CHARSXP Exercise




  > .Internal(inspect(c("abc", "abc", "xyz")))

   @101d3f280 16 STRSXP g0c3 []   (len=3, tl=1)
     @101cc6278 09 CHARSXP g0c1   [gp=0x20] "abc"
     @101cc6278 09 CHARSXP g0c1   [gp=0x20] "abc"
     @101cc6308 09 CHARSXP g0c1   [gp=0x20] "xyz"
character vectors == STRSXPs + CHARSXPs
CHARSXPs are special
STRSXP/CHARSXP API Example




  SEXP s = Rf_allocVector(STRSXP, 5);
  PROTECT(s);
  SET_STRING_ELT(s, 0, mkChar("hello"));
  SET_STRING_ELT(s, 1, mkChar("goodbye"));
  SEXP c = STRING_ELT(s, 0);
  const char *v = CHAR(c);
  UNPROTECT(1);
STRSXP/CHARSXP API Example




  SEXP s = Rf_allocVector(STRSXP, 5);
  PROTECT(s);
  SET_STRING_ELT(s, 0, mkChar("hello"));
  SET_STRING_ELT(s, 1, mkChar("goodbye"));
  SEXP c = STRING_ELT(s, 0);
  const char *v = CHAR(c);
  UNPROTECT(1);
STRSXP/CHARSXP API Example




  SEXP s = Rf_allocVector(STRSXP, 5);
  PROTECT(s);
  SET_STRING_ELT(s, 0, mkChar("hello"));
  SET_STRING_ELT(s, 1, mkChar("goodbye"));
  SEXP c = STRING_ELT(s, 0);
  const char *v = CHAR(c);
  UNPROTECT(1);
STRSXP/CHARSXP API Example




  SEXP s = Rf_allocVector(STRSXP, 5);
  PROTECT(s);
  SET_STRING_ELT(s, 0, mkChar("hello"));
  SET_STRING_ELT(s, 1, mkChar("goodbye"));
  SEXP c = STRING_ELT(s, 0);
  const char *v = CHAR(c);
  UNPROTECT(1);
STRSXP/CHARSXP API Example




  SEXP s = Rf_allocVector(STRSXP, 5);
  PROTECT(s);
  SET_STRING_ELT(s, 0, mkChar("hello"));
  SET_STRING_ELT(s, 1, mkChar("goodbye"));
  SEXP c = STRING_ELT(s, 0);
  const char *v = CHAR(c);
  UNPROTECT(1);
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
R’s memory model (dramatization)



                                 All SEXPs

     PROTECT                                 reachable
       stack



      Precious
         list




                 DRAMATIZATION
R’s memory model
      R allocates, tracks, and garbage collects SEXPs
      gc is triggered by R functions
      SEXPs that are not in-use are recycled.
      A SEXP is in-use if:
          it is on the protection stack (PROTECT/UNPROTECT)
          it is in the precious list (R PreserveObject/R ReleaseObject)
          it is reachable from a SEXP in the in-use list.

                                        All SEXPs

         PROTECT                                       reachable
           stack



          Precious
             list




                      DRAMATIZATION
Preventing gc of SEXPs with the protection stack




    PROTECT(s)     Push s onto the protection stack
    UNPROTECT(n)   Pop top n items off of the protection stack
Generic memory: When everything isn’t a SEXP




    Allocation Function   Lifecycle
    R_alloc               Memory is freed when returning from .Call
    Calloc/Free           Memory persists until Free is called
Case Study: C implementation of        which




   Code review of nidemo/src/which.c
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
Alphabet frequency of a text file

 Yeast Gene YDL143W                         English Dictionary


   a                                          e
    t                                          i
   g                                          a
   c                                          o
   d                                          r
    0.00   0.10               0.20   0.30     0.07   0.08     0.09      0.10
                  frequency                                 frequency
Self-Study in nidemo package



    1. Alphabet Frequency
           Explore R and C implementations
           Make C implementation robust
           Attach names attribute in C
           multiple file, matrix return enhancement
    2. External pointer example
    3. Calling R from C example and enhancement
    4. Debugging: a video how-to
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
Odds and Ends




    1. Debugging
    2. Public API
    3. Calling R from C
    4. Making sense of the remapped function names
    5. Finding C implementations of R functions
    6. Using a TAGS file
Debugging Native Code



  Rprintf("DEBUG: v => %d, x => %sn", v, x);

  $ R -d gdb
  (gdb) run
  > # now R is running

      There are two ways to write error-free programs; only the
      third one works.
      – Alan Perlis, Epigrams on Programming
R’s Public API



   mkdir R-devel-build; cd R-devel-build
   ~/src/R-devel-src/configure && make
   ## hint: configure --help

   ls include

     R.h      Rdefines.h    Rinterface.h   S.h
     R_ext/   Rembedded.h   Rinternals.h

   Details in WRE
Calling R functions from C




    1. Build a pairlist
    2. call Rf_eval(s, rho)
Remapped functions




      Source is unadorned
      Preprocessor adds Rf_
      Object code contains Rf_
Finding C implementations




   cd R-devel/src/main

   grep '"any"' names.c
   {"any", do_logic3, 2, ...}

   grep -l do_logic3 *.c
   logic.c
R CMD rtags




  cd R-devel/src
  R CMD rtags --no-Rd
  # creates index file TAGS
Outline


   The .Call Native Interface

   R types at the C-level

   Memory management in R

   Self-Study Exercises

   Odds and Ends

   Resources
Writing R Extensions




   RShowDoc("R-exts")
Book: The C Programming Language (K & R)




   Java in a Nutshell             1264 pages
   The C++ Programming Language   1030 pages
   The C Programming Language     274 pages
Resources
      Read WRE and “K & R”
      Use the sources for R and packages
      R-devel, bioc-devel mailing lists
      Patience

Weitere ähnliche Inhalte

Was ist angesagt?

Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!José Paumard
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Sungchul Park
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Sumant Tambe
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Vadim Dubs
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)changehee lee
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonYi-Lung Tsai
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 

Was ist angesagt? (20)

The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Lazy java
Lazy javaLazy java
Lazy java
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
C++11
C++11C++11
C++11
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 

Andere mochten auch

Stock imagebank.com sibsa 2014
Stock imagebank.com sibsa 2014Stock imagebank.com sibsa 2014
Stock imagebank.com sibsa 2014Sugandha Dubey
 
RCRH - Candidats
RCRH - CandidatsRCRH - Candidats
RCRH - CandidatsCarl Ward
 
Collage elsa en madrid
Collage elsa en madridCollage elsa en madrid
Collage elsa en madridguestcb4f805
 
Know how to cash in your Creativity ! Stock Photography Today
Know how to cash in your Creativity ! Stock Photography Today Know how to cash in your Creativity ! Stock Photography Today
Know how to cash in your Creativity ! Stock Photography Today Sugandha Dubey
 
你喜歡吃美國牛肉嗎(商業週刊內容)
你喜歡吃美國牛肉嗎(商業週刊內容)你喜歡吃美國牛肉嗎(商業週刊內容)
你喜歡吃美國牛肉嗎(商業週刊內容)guest443c4ccc
 
Common wealth games 2010
Common wealth games 2010Common wealth games 2010
Common wealth games 2010aashman
 
Portfolio carstylistdk
Portfolio carstylistdkPortfolio carstylistdk
Portfolio carstylistdkydilipkumar
 
Traditional Media Presentation
Traditional Media PresentationTraditional Media Presentation
Traditional Media Presentationderekleethompson
 
Learn how to cash in your Creativity !! Stock Photography
Learn how to cash in your Creativity !! Stock Photography Learn how to cash in your Creativity !! Stock Photography
Learn how to cash in your Creativity !! Stock Photography Sugandha Dubey
 
While i was walking ___
While i was walking  ___While i was walking  ___
While i was walking ___Johann Botha
 
Telephone english for secretaries 2014
Telephone english for secretaries 2014Telephone english for secretaries 2014
Telephone english for secretaries 2014Regina Efs
 

Andere mochten auch (17)

Stock imagebank.com sibsa 2014
Stock imagebank.com sibsa 2014Stock imagebank.com sibsa 2014
Stock imagebank.com sibsa 2014
 
Dev ops of die (
Dev ops of die (Dev ops of die (
Dev ops of die (
 
RCRH - Candidats
RCRH - CandidatsRCRH - Candidats
RCRH - Candidats
 
Collage elsa en madrid
Collage elsa en madridCollage elsa en madrid
Collage elsa en madrid
 
Know how to cash in your Creativity ! Stock Photography Today
Know how to cash in your Creativity ! Stock Photography Today Know how to cash in your Creativity ! Stock Photography Today
Know how to cash in your Creativity ! Stock Photography Today
 
Ad slide show
Ad slide showAd slide show
Ad slide show
 
about me
about meabout me
about me
 
about me
about meabout me
about me
 
你喜歡吃美國牛肉嗎(商業週刊內容)
你喜歡吃美國牛肉嗎(商業週刊內容)你喜歡吃美國牛肉嗎(商業週刊內容)
你喜歡吃美國牛肉嗎(商業週刊內容)
 
Anàlisi
AnàlisiAnàlisi
Anàlisi
 
Common wealth games 2010
Common wealth games 2010Common wealth games 2010
Common wealth games 2010
 
Portfolio carstylistdk
Portfolio carstylistdkPortfolio carstylistdk
Portfolio carstylistdk
 
Traditional Media Presentation
Traditional Media PresentationTraditional Media Presentation
Traditional Media Presentation
 
Wiki Power point.
Wiki Power point.Wiki Power point.
Wiki Power point.
 
Learn how to cash in your Creativity !! Stock Photography
Learn how to cash in your Creativity !! Stock Photography Learn how to cash in your Creativity !! Stock Photography
Learn how to cash in your Creativity !! Stock Photography
 
While i was walking ___
While i was walking  ___While i was walking  ___
While i was walking ___
 
Telephone english for secretaries 2014
Telephone english for secretaries 2014Telephone english for secretaries 2014
Telephone english for secretaries 2014
 

Ähnlich wie Native interfaces for R

Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout source{d}
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 
Java Intro
Java IntroJava Intro
Java Introbackdoor
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packagesAjay Ohri
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_mapslcplcp1
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 

Ähnlich wie Native interfaces for R (20)

Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
Java Intro
Java IntroJava Intro
Java Intro
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
R language
R languageR language
R language
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 

Kürzlich hochgeladen

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Kürzlich hochgeladen (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 

Native interfaces for R

  • 1. Native Interfaces for R Seth Falcon Fred Hutchinson Cancer Research Center 20-21 May, 2010
  • 2. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 3. which as implemented in R prior to R-2.11 which <- function(x) { seq_along(x)[x & !is.na(x)] }
  • 4. which as implemented in R prior to R-2.11 which <- function(x) { seq_along(x)[x & !is.na(x)] } 1. is.na 2. ! 3. & 4. seq_along 5. [
  • 5. which in C 1 SEXP nid_which(SEXP v) { 2 SEXP ans; 3 int i, j = 0, len = Rf_length(v), *buf, *tf; 4 buf = (int *) R_alloc(len, sizeof(int)); 5 tf = LOGICAL(v); 6 for (i = 0; i < len; i++) { 7 if (tf[i] == TRUE) buf[j] = i + 1; j++; 8 } 9 ans = Rf_allocVector(INTSXP, j); 10 memcpy(INTEGER(ans), buf, sizeof(int) * j); 11 return ans; }
  • 6. which is now 10x faster system.time(which(v)) R Version System time Elapsed time R-2.10 0.018 0.485 R-2.11 0.001 0.052 v is a logical vector with 10 million elements
  • 7. Making use of existing code Access algorithms Interface to other systems RBGL RSQLite RCurl netcdf Rsamtools SJava
  • 8. Review of C #include <stdio.h> double _nid_avg(int *data, int len) { int i; double ans = 0.0; for (i = 0; i < len; i++) { ans += data[i]; } return ans / len; } main() { int ex_data[5] = {1, 2, 3, 4, 5}; double m = _nid_avg(ex_data, 5); printf("%fn", m); }
  • 10. Symbolic Expressions (SEXPs) The SEXP is R’s fundamental data type at the C-level SEXP is short for symbolic expression and is borrowed from Lisp History at http://en.wikipedia.org/wiki/S-expression
  • 11. .Call Start to Finish Demo Demo
  • 12. .Call Disection R # R/somefile.R avg <- function(x) .Call(.nid_avg, x) # NAMESPACE useDynLib("yourPackage", .nid_avg = nid_avg) C /* src/some.c */ #include <Rinternals.h> SEXP nid_avg(SEXP data) { ... INTEGER(data) ... }
  • 13. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 14. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 15. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 16. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 17. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 18. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 19. .Call C function #include <Rinternals.h> SEXP nid_avg(SEXP data) { SEXP ans; double v = _nid_avg(INTEGER(data), Rf_length(data)); PROTECT(ans = Rf_allocVector(REALSXP, 1)); REAL(ans)[0] = v; UNPROTECT(1); return ans; /* shortcut: return Rf_ScalarReal(v); */ }
  • 20. Function Registration In NAMESPACE In C via R_CallMethodDef (See ShortRead/src/R_init_ShortRead.c for a nice example.
  • 21. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 22. Just one thing to learn Everything is a SEXP But there are many different types of SEXPs INTSXP, REALSXP, STRSXP, LGLSXP, VECSXP, and more.
  • 24. Common SEXP subtypes R function SEXP subtype Data accessor integer() INTSXP int *INTEGER(x) numeric() REALSXP double *REAL(x) logical() LGLSXP int *LOGICAL(x) character() STRSXP CHARSXP STRING ELT(x, i) list() VECSXP SEXP VECTOR ELT(x, i) NULL NILSXP R NilValue externalptr EXTPTRSXP SEXP (accessor funcs)
  • 25. STRSXP/CHARSXP Exercise Try this: .Internal(inspect(c("abc", "abc", "xyz")))
  • 26. STRSXP/CHARSXP Exercise > .Internal(inspect(c("abc", "abc", "xyz"))) @101d3f280 16 STRSXP g0c3 [] (len=3, tl=1) @101cc6278 09 CHARSXP g0c1 [gp=0x20] "abc" @101cc6278 09 CHARSXP g0c1 [gp=0x20] "abc" @101cc6308 09 CHARSXP g0c1 [gp=0x20] "xyz"
  • 27. character vectors == STRSXPs + CHARSXPs
  • 29. STRSXP/CHARSXP API Example SEXP s = Rf_allocVector(STRSXP, 5); PROTECT(s); SET_STRING_ELT(s, 0, mkChar("hello")); SET_STRING_ELT(s, 1, mkChar("goodbye")); SEXP c = STRING_ELT(s, 0); const char *v = CHAR(c); UNPROTECT(1);
  • 30. STRSXP/CHARSXP API Example SEXP s = Rf_allocVector(STRSXP, 5); PROTECT(s); SET_STRING_ELT(s, 0, mkChar("hello")); SET_STRING_ELT(s, 1, mkChar("goodbye")); SEXP c = STRING_ELT(s, 0); const char *v = CHAR(c); UNPROTECT(1);
  • 31. STRSXP/CHARSXP API Example SEXP s = Rf_allocVector(STRSXP, 5); PROTECT(s); SET_STRING_ELT(s, 0, mkChar("hello")); SET_STRING_ELT(s, 1, mkChar("goodbye")); SEXP c = STRING_ELT(s, 0); const char *v = CHAR(c); UNPROTECT(1);
  • 32. STRSXP/CHARSXP API Example SEXP s = Rf_allocVector(STRSXP, 5); PROTECT(s); SET_STRING_ELT(s, 0, mkChar("hello")); SET_STRING_ELT(s, 1, mkChar("goodbye")); SEXP c = STRING_ELT(s, 0); const char *v = CHAR(c); UNPROTECT(1);
  • 33. STRSXP/CHARSXP API Example SEXP s = Rf_allocVector(STRSXP, 5); PROTECT(s); SET_STRING_ELT(s, 0, mkChar("hello")); SET_STRING_ELT(s, 1, mkChar("goodbye")); SEXP c = STRING_ELT(s, 0); const char *v = CHAR(c); UNPROTECT(1);
  • 34. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 35. R’s memory model (dramatization) All SEXPs PROTECT reachable stack Precious list DRAMATIZATION
  • 36. R’s memory model R allocates, tracks, and garbage collects SEXPs gc is triggered by R functions SEXPs that are not in-use are recycled. A SEXP is in-use if: it is on the protection stack (PROTECT/UNPROTECT) it is in the precious list (R PreserveObject/R ReleaseObject) it is reachable from a SEXP in the in-use list. All SEXPs PROTECT reachable stack Precious list DRAMATIZATION
  • 37. Preventing gc of SEXPs with the protection stack PROTECT(s) Push s onto the protection stack UNPROTECT(n) Pop top n items off of the protection stack
  • 38. Generic memory: When everything isn’t a SEXP Allocation Function Lifecycle R_alloc Memory is freed when returning from .Call Calloc/Free Memory persists until Free is called
  • 39. Case Study: C implementation of which Code review of nidemo/src/which.c
  • 40. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 41. Alphabet frequency of a text file Yeast Gene YDL143W English Dictionary a e t i g a c o d r 0.00 0.10 0.20 0.30 0.07 0.08 0.09 0.10 frequency frequency
  • 42. Self-Study in nidemo package 1. Alphabet Frequency Explore R and C implementations Make C implementation robust Attach names attribute in C multiple file, matrix return enhancement 2. External pointer example 3. Calling R from C example and enhancement 4. Debugging: a video how-to
  • 43. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 44. Odds and Ends 1. Debugging 2. Public API 3. Calling R from C 4. Making sense of the remapped function names 5. Finding C implementations of R functions 6. Using a TAGS file
  • 45. Debugging Native Code Rprintf("DEBUG: v => %d, x => %sn", v, x); $ R -d gdb (gdb) run > # now R is running There are two ways to write error-free programs; only the third one works. – Alan Perlis, Epigrams on Programming
  • 46. R’s Public API mkdir R-devel-build; cd R-devel-build ~/src/R-devel-src/configure && make ## hint: configure --help ls include R.h Rdefines.h Rinterface.h S.h R_ext/ Rembedded.h Rinternals.h Details in WRE
  • 47. Calling R functions from C 1. Build a pairlist 2. call Rf_eval(s, rho)
  • 48. Remapped functions Source is unadorned Preprocessor adds Rf_ Object code contains Rf_
  • 49. Finding C implementations cd R-devel/src/main grep '"any"' names.c {"any", do_logic3, 2, ...} grep -l do_logic3 *.c logic.c
  • 50. R CMD rtags cd R-devel/src R CMD rtags --no-Rd # creates index file TAGS
  • 51. Outline The .Call Native Interface R types at the C-level Memory management in R Self-Study Exercises Odds and Ends Resources
  • 52. Writing R Extensions RShowDoc("R-exts")
  • 53. Book: The C Programming Language (K & R) Java in a Nutshell 1264 pages The C++ Programming Language 1030 pages The C Programming Language 274 pages
  • 54. Resources Read WRE and “K & R” Use the sources for R and packages R-devel, bioc-devel mailing lists Patience