SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
R / C++
Romain François
romain@r-enthusiasts.com
@romain_francois
• Use R since 2002
• Consultant
• Rcpp since 2010
• Rcpp11 since last
september
• C++ for performance
• Occasional comedy
Agenda
• R and C++
• Rcpp11
Success of Rcpp
• Thin wrapper around R/C api
• Users focus on what!
• Rcpp handles how
Example 1: Shiny
Example 2: dplyr
hflights %>%
group_by(Year, Month, DayofMonth) %>%
select(Year:DayofMonth, ArrDelay, DepDelay) %>%
summarise(
arr = mean(ArrDelay, na.rm = TRUE),
dep = mean(DepDelay, na.rm = TRUE)
) %>%
filter(arr > 30 | dep > 30)
int add( int a, int b){
return a + b ;
}
#include <Rcpp.h>
!
// [[Rcpp::export]]
int add( int a, int b){
return a + b ;
}
sourceCpp
#include <Rcpp.h>
!
// [[Rcpp::export]]
int add( int a, int b){
return a + b ;
}
> sourceCpp( "add.cpp" )
> add( 2L, 3L)
[1] 5
R data -> Rcpp classes
• vectors: NumericVector, IntegerVector, …
• lists: List
• functions: Function
• environments: Environment
• …
Rcpp objects are proxies
to R data structures
No additional memory
Example: NumericVector
// [[Rcpp::export]]
double sum( NumericVector x){
int n = x.size() ;
!
double res = 0.0 ;
for( int i=0; i<n; i++){
res += x[i] ;
}
!
return res ;
}
Example : lists
List res = List::create(
_["a"] = 1,
_["b"] = "foo"
);


res.attr( "class" ) = "myclass" ; !

int a = res["a"] ;

res["b"] = 42 ;
Example: R function
Function rnorm( "rnorm" ) ;
!
NumericVector x = rnorm(
10,
_["mean"] = 30,
_["sd"] = 100
);
Rcpp11
Rcpp11
• C++ = C++11
• Smaller code base (than Rcpp)
• Header only
#include <Rcpp.h>
!
// [[Rcpp::export]]
int foo(){
return 2 ;
}
Rcpp
Rcpp11
0 1 2 3
Less code -> faster compiles
!
Interface simplification
NumericVector constructors
Vector() {
Vector( const Vector& other){
Vector( SEXP x ) {
!
Vector( const GenericProxy<Proxy>& proxy ){
explicit Vector( const no_init& obj) {
Vector( const int& size, const stored_type& u ) {
Vector( const std::string& st ){
Vector( const char* st ) {
Vector( const int& siz, stored_type (*gen)(void) ) {
Vector( const int& size ) {
Vector( const Dimension& dims) {
Vector( const Dimension& dims, const U& u) {
Vector( const VectorBase<RTYPE,NA,VEC>& other ) {
Vector( const int& size, const U& u) {
Vector( const sugar::SingleLogicalResult<NA,T>& obj ) {
Vector( const int& siz, stored_type (*gen)(U1), const U1& u1) {
Vector( const int& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2) {
Vector( const int& siz, stored_type (*gen)(U1,U2,U3), const U1& u1, const U2& u2, const U3& u3) {
Vector( InputIterator first, InputIterator last){
Vector( InputIterator first, InputIterator last, int n) {
Vector( InputIterator first, InputIterator last, Func func) {
Vector( InputIterator first, InputIterator last, Func func, int n){
Vector( std::initializer_list<init_type> list ) {
23 constructors in Rcpp
NumericVector constructors
11 constructors in Rcpp
Vector(){
Vector( const Vector& other){
Vector( Vector&& other){
!
explicit Vector(int n) {
explicit Vector(R_xlen_t n) {
!
Vector( R_xlen_t n, value_type x ) {
Vector( std::initializer_list<value_type> list ){
Vector( std::initializer_list<traits::named_object<value_type>> list ){
!
Vector( const SugarVectorExpression<eT, Expr>& other ) {
Vector( const LazyVector<RT,Expr>& other ) {
Vector( const GenericProxy<Proxy>& proxy ){
NumericVector constructors
Vector( const int& siz, stored_type (*gen)(U1), const U1& u1) -> replicate
double fun( int power ){
return pow(2.0, power) ;
}
!
// Rcpp
NumericVector x(10, fun, 10.0) ;
// Rcpp11
NumericVector x = replicate(10, fun, 10.0) ;
NumericVector constructors
std::vector<double> v = /* ... */ ;
!
// Rcpp
NumericVector x(v.begin(), v.end()) ;
// Rcpp11
NumericVector x = import(begin(v), end(v)) ;
NumericVector x = import(v) ;
Vector( InputIterator first, InputIterator last) -> import
Some examples
NumericVector ctors
// C++ uniform initialization
NumericVector x = {1.0, 2.0, 3.0} ;
!
// init with given size
NumericVector y( 2 ) ; // data is not initialized
NumericVector y( 2, 3.0 ) ; // initialize all to 3.0
!
// fuse several vectors (similar to c in R)
NumericVector z = fuse(x, y) ;
NumericVector z = fuse(x, y, 3.0) ;
create
// old school NumericVector::create (Rcpp style).
NumericVector x = NumericVector::create(1.0, 2.0, 3.0) ;
NumericVector x = NumericVector::create(
_["a"] = 1.0, _["b"] = 2.0, _["c"] = 3.0
) ;
!
!
// create as a free function. Rcpp11 style
NumericVector x = create(1.0, 2.0, 3.0) ;
NumericVector x = create(
_["a"] = 1.0, _["b"] = 2.0, _["c"] = 3.0
) ;
sapply + lambda
sapply + lambda
#include <Rcpp11>
!
// [[export]]
NumericVector foo( NumericVector x ){
return sapply(x, [](double d){
return exp(d * 2) ;
});
}
x <- 1:3
sapply( x, function(d){
exp(d*2) ;
})
sapply + lambda + …
#include <Rcpp11>
!
// [[export]]
NumericVector foo( NumericVector x ){
auto fun = [](double d, double y, double z){
return exp(d * y) + z ;
} ;
return sapply(x, fun, 3.0, 4.6);
}
x <- 1:3
sapply( x, function(d, y, z){
exp(d*y) + z ;
}, 3.0, 4.6 )
Rcpp11 release cycle
• Available from CRAN:
!
• Evolves quickly. Get it from github
!
• Next Release: 3.1.1 (same time as R 3.1.1) in a
few days (I've been travelling)
> install.packages( "Rcpp11" )
> install_github( "Rcpp11/Rcpp11" )
attributes companion package
• Standalone impl of Rcpp attributes
[[Rcpp::export]], sourceCpp, compileAttributes
!
• Only available from github:
> install_github( "Rcpp11/attributes" )
Use Rcpp11 in your package
• Write your .cpp files
#include <Rcpp11>
!
// [[export]]
void foo( … ) { … } // your code
SystemRequirements: C++11
LinkingTo: Rcpp11
library(attributes)
compileAttributes( "mypkg" )
• Express dep on C++11 and Rcpp11 headers
• Generate the boiler plate
Header only
• No runtime dependency
!
• Snapshot the code base
!
• Better control for package authors
Romain François
romain@r-enthusiasts.com @romain_francois
Questions ?

Más contenido relacionado

La actualidad más candente

CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogrammingdudarev
 
A formalization of complex event stream processing
A formalization of complex event stream processingA formalization of complex event stream processing
A formalization of complex event stream processingSylvain Hallé
 
Modern c++ Memory Management
Modern c++ Memory ManagementModern c++ Memory Management
Modern c++ Memory ManagementAlan Uthoff
 
C c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoC c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoKim Phillips
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits IIGouthaman V
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?Adam Dudczak
 
2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.js2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.jsNoritada Shimizu
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.lnikolaeva
 
Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Sri Ambati
 

La actualidad más candente (20)

CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
 
A formalization of complex event stream processing
A formalization of complex event stream processingA formalization of complex event stream processing
A formalization of complex event stream processing
 
Modern c++ Memory Management
Modern c++ Memory ManagementModern c++ Memory Management
Modern c++ Memory Management
 
C c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoC c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdo
 
user2015 keynote talk
user2015 keynote talkuser2015 keynote talk
user2015 keynote talk
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 
week-5x
week-5xweek-5x
week-5x
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits II
 
Libtcc and gwan
Libtcc and gwanLibtcc and gwan
Libtcc and gwan
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
timingExercise
timingExercisetimingExercise
timingExercise
 
2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.js2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.js
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
3 rd animation
3 rd animation3 rd animation
3 rd animation
 
20151224-games
20151224-games20151224-games
20151224-games
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 
FSE 2008
FSE 2008FSE 2008
FSE 2008
 
Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala
 

Destacado (16)

RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
 
Rcpp
RcppRcpp
Rcpp
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Rcpp is-ready
Rcpp is-readyRcpp is-ready
Rcpp is-ready
 
Object Oriented Design(s) in R
Object Oriented Design(s) in RObject Oriented Design(s) in R
Object Oriented Design(s) in R
 
R and cpp
R and cppR and cpp
R and cpp
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
R and C++
R and C++R and C++
R and C++
 
SevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittrSevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittr
 
R/C++
R/C++R/C++
R/C++
 
dplyr and torrents from cpasbien
dplyr and torrents from cpasbiendplyr and torrents from cpasbien
dplyr and torrents from cpasbien
 
dplyr
dplyrdplyr
dplyr
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
Manejo defensivo 1
Manejo defensivo 1Manejo defensivo 1
Manejo defensivo 1
 

Similar a Rcpp11 genentech

Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!mickey24
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerAndrey Karpov
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codePVS-Studio LLC
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Jan Wedekind
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework HelpC++ Homework Help
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methodsphil_nash
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoThe Software House
 

Similar a Rcpp11 genentech (20)

Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
The STL
The STLThe STL
The STL
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Cpl
CplCpl
Cpl
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
C programming
C programmingC programming
C programming
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 

Último

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
 
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
 
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
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
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
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
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
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
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
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
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
 

Último (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
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
 
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
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
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
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
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
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
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
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
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
 

Rcpp11 genentech

  • 1. R / C++ Romain François romain@r-enthusiasts.com @romain_francois
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. • Use R since 2002 • Consultant • Rcpp since 2010 • Rcpp11 since last september • C++ for performance • Occasional comedy
  • 10.
  • 11.
  • 12.
  • 13. Agenda • R and C++ • Rcpp11
  • 14.
  • 15.
  • 16.
  • 17. Success of Rcpp • Thin wrapper around R/C api • Users focus on what! • Rcpp handles how
  • 19. Example 2: dplyr hflights %>% group_by(Year, Month, DayofMonth) %>% select(Year:DayofMonth, ArrDelay, DepDelay) %>% summarise( arr = mean(ArrDelay, na.rm = TRUE), dep = mean(DepDelay, na.rm = TRUE) ) %>% filter(arr > 30 | dep > 30)
  • 20. int add( int a, int b){ return a + b ; }
  • 21.
  • 22. #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; }
  • 23. sourceCpp #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; } > sourceCpp( "add.cpp" ) > add( 2L, 3L) [1] 5
  • 24. R data -> Rcpp classes • vectors: NumericVector, IntegerVector, … • lists: List • functions: Function • environments: Environment • …
  • 25. Rcpp objects are proxies to R data structures No additional memory
  • 26. Example: NumericVector // [[Rcpp::export]] double sum( NumericVector x){ int n = x.size() ; ! double res = 0.0 ; for( int i=0; i<n; i++){ res += x[i] ; } ! return res ; }
  • 27. Example : lists List res = List::create( _["a"] = 1, _["b"] = "foo" ); 
 res.attr( "class" ) = "myclass" ; !
 int a = res["a"] ;
 res["b"] = 42 ;
  • 28. Example: R function Function rnorm( "rnorm" ) ; ! NumericVector x = rnorm( 10, _["mean"] = 30, _["sd"] = 100 );
  • 30. Rcpp11 • C++ = C++11 • Smaller code base (than Rcpp) • Header only
  • 31.
  • 32. #include <Rcpp.h> ! // [[Rcpp::export]] int foo(){ return 2 ; } Rcpp Rcpp11 0 1 2 3 Less code -> faster compiles
  • 34. NumericVector constructors Vector() { Vector( const Vector& other){ Vector( SEXP x ) { ! Vector( const GenericProxy<Proxy>& proxy ){ explicit Vector( const no_init& obj) { Vector( const int& size, const stored_type& u ) { Vector( const std::string& st ){ Vector( const char* st ) { Vector( const int& siz, stored_type (*gen)(void) ) { Vector( const int& size ) { Vector( const Dimension& dims) { Vector( const Dimension& dims, const U& u) { Vector( const VectorBase<RTYPE,NA,VEC>& other ) { Vector( const int& size, const U& u) { Vector( const sugar::SingleLogicalResult<NA,T>& obj ) { Vector( const int& siz, stored_type (*gen)(U1), const U1& u1) { Vector( const int& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2) { Vector( const int& siz, stored_type (*gen)(U1,U2,U3), const U1& u1, const U2& u2, const U3& u3) { Vector( InputIterator first, InputIterator last){ Vector( InputIterator first, InputIterator last, int n) { Vector( InputIterator first, InputIterator last, Func func) { Vector( InputIterator first, InputIterator last, Func func, int n){ Vector( std::initializer_list<init_type> list ) { 23 constructors in Rcpp
  • 35. NumericVector constructors 11 constructors in Rcpp Vector(){ Vector( const Vector& other){ Vector( Vector&& other){ ! explicit Vector(int n) { explicit Vector(R_xlen_t n) { ! Vector( R_xlen_t n, value_type x ) { Vector( std::initializer_list<value_type> list ){ Vector( std::initializer_list<traits::named_object<value_type>> list ){ ! Vector( const SugarVectorExpression<eT, Expr>& other ) { Vector( const LazyVector<RT,Expr>& other ) { Vector( const GenericProxy<Proxy>& proxy ){
  • 36. NumericVector constructors Vector( const int& siz, stored_type (*gen)(U1), const U1& u1) -> replicate double fun( int power ){ return pow(2.0, power) ; } ! // Rcpp NumericVector x(10, fun, 10.0) ; // Rcpp11 NumericVector x = replicate(10, fun, 10.0) ;
  • 37. NumericVector constructors std::vector<double> v = /* ... */ ; ! // Rcpp NumericVector x(v.begin(), v.end()) ; // Rcpp11 NumericVector x = import(begin(v), end(v)) ; NumericVector x = import(v) ; Vector( InputIterator first, InputIterator last) -> import
  • 39. NumericVector ctors // C++ uniform initialization NumericVector x = {1.0, 2.0, 3.0} ; ! // init with given size NumericVector y( 2 ) ; // data is not initialized NumericVector y( 2, 3.0 ) ; // initialize all to 3.0 ! // fuse several vectors (similar to c in R) NumericVector z = fuse(x, y) ; NumericVector z = fuse(x, y, 3.0) ;
  • 40. create // old school NumericVector::create (Rcpp style). NumericVector x = NumericVector::create(1.0, 2.0, 3.0) ; NumericVector x = NumericVector::create( _["a"] = 1.0, _["b"] = 2.0, _["c"] = 3.0 ) ; ! ! // create as a free function. Rcpp11 style NumericVector x = create(1.0, 2.0, 3.0) ; NumericVector x = create( _["a"] = 1.0, _["b"] = 2.0, _["c"] = 3.0 ) ;
  • 42. sapply + lambda #include <Rcpp11> ! // [[export]] NumericVector foo( NumericVector x ){ return sapply(x, [](double d){ return exp(d * 2) ; }); } x <- 1:3 sapply( x, function(d){ exp(d*2) ; })
  • 43. sapply + lambda + … #include <Rcpp11> ! // [[export]] NumericVector foo( NumericVector x ){ auto fun = [](double d, double y, double z){ return exp(d * y) + z ; } ; return sapply(x, fun, 3.0, 4.6); } x <- 1:3 sapply( x, function(d, y, z){ exp(d*y) + z ; }, 3.0, 4.6 )
  • 44. Rcpp11 release cycle • Available from CRAN: ! • Evolves quickly. Get it from github ! • Next Release: 3.1.1 (same time as R 3.1.1) in a few days (I've been travelling) > install.packages( "Rcpp11" ) > install_github( "Rcpp11/Rcpp11" )
  • 45. attributes companion package • Standalone impl of Rcpp attributes [[Rcpp::export]], sourceCpp, compileAttributes ! • Only available from github: > install_github( "Rcpp11/attributes" )
  • 46. Use Rcpp11 in your package • Write your .cpp files #include <Rcpp11> ! // [[export]] void foo( … ) { … } // your code SystemRequirements: C++11 LinkingTo: Rcpp11 library(attributes) compileAttributes( "mypkg" ) • Express dep on C++11 and Rcpp11 headers • Generate the boiler plate
  • 47. Header only • No runtime dependency ! • Snapshot the code base ! • Better control for package authors