SlideShare a Scribd company logo
1 of 16
Download to read offline
Supporting
the “Rapi” C-language API
in an R-compatible engine
Michael Sannella
msannell@tibco.com
RIOT 2015
TERR, Packages, and Rapi
• TERR: TIBCO® Enterprise Runtime for R
• A commercial R-compatible statistics engine
• We want TERR to support many external packages
• Much of the value of R lies in packages from CRAN and
other repositories (Bioconductor, Github, etc.)
• Many packages are pure “R-language” packages
• About 70% of CRAN packages are pure R-language
• Many packages contain C-language code
• C-language code can access the R engine using the R
engine’s C-language API (“Rapi”)
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 2
TERR Rapi Support
• Ultimate goal: TERR supports loading and executing
binary packages unchanged
• Fallback: Modify package sources and rebuild
before loading
• If necessary, we can distribute modified packages for
TERR in our TRAN repository
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 3
Example: Code Using Rapi
• R code from the CRAN package “splusTimeDate”:
.Call("time_to_hour_min_sec", x, timeZoneList())
• C code:
SEXP time_to_hour_min_sec(SEXP time_vec,
SEXP zone_list)
{
...
PROTECT(ret = NEW_LIST(4));
...
SET_VECTOR_ELT(ret, 0, PROTECT(NEW_INTEGER(lng)));
...
hour_data = INTEGER(VECTOR_ELT(ret, 0));
...
hour_data[i] = td.hour;
...
UNPROTECT(7);
return( ret );
}
• NEW_INTEGER macro expands to a call to the Rf_allocVector Rapi entry
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 4
Rapi Shared Libraries: R.dll, etc.
• Rapi defines hundreds of library entries
• ATTRIB, SET_VECTOR_ELT, R_alloc,
Rf_allocVector, Rf_Protect, dcopy_,
dgemm_, dlaic1_, etc.
• Embedding API (used by Rstudio)
• Global variables: R_GlobalEnv, R_NaInt, etc.
• Packages link to shared library files R.dll, Rblas.dll,
etc. (libR.so, etc. on Linux) that export these Rapi
entries
• To support Rapi, TERR contains R.dll, etc. libraries
that forward Rapi calls to the engine
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 5
TERR Rapi Support: Handles
• Observation: Rapi entries treat SEXP (R object
pointer) values as opaque values passed to and
from the engine
• TERR approach: Treat SEXP as an offset in a “handle
table” containing pointers to internal TERR objects
• Benefit: Can convert TERR-only objects “in-place”
as needed
• Ex: Expand TERR integer sequence object to an integer
vector object when Rapi code calls INTEGER to access
its contents
• Works well for many packages
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 6
Problem: USE_RINTERNALS
• If the C constant USE_RINTERNALS is defined,
many Rapi function calls are redefined as macros
directly accessing R object internals
• USE_RINTERNALS is used in many popular CRAN
packages: Rcpp, Rserve, igraph, etc
• TERR workaround: Make our own versions of
packages without USE_RINTERNALS defined
• Some tweaks needed to compile:
STRING_ELT(x, i) = value; // change this
SET_STRING_ELT(x, i, value); // to this
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 7
USE_RINTERNALS More Efficient?
• Can improve efficiency of some idioms:
for (int i = 0; i<LENGTH(obj); ++i) {
INTEGER(obj)[i] = 0;
}
• However, it is easy to rework code to reduce
function calls:
int len = LENGTH(obj);
int* data = INTEGER(obj);
for (int i = 0; i<len; ++i) {
data[i] = 0
}
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 8
Solution: New Object Layout
• The TERR team is currently reworking the TERR
object layout to be more compatible with R
• SEXP is pointer to R-compatible object
• TERR C++ header stored before R-compatible object
bytes
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 9
SObject* SEXP
TERR C++
Object Header
R-Compatible
Header
Data
New Object Layout: Issues
• Issue: List object must contain array of SEXP
pointers, not TERR C++ object pointers
• Issue: TERR-only objects (like sequence objects)
must be converted before exposing to Rapi code
• Only allow R-compatible objects within “Rapi space”
• Convert arguments to .Call
• Convert value returned by Rapi entries Rf_eval, etc.
• Concerns: Extra complexity, object size,
performance hit
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 10
Beyond USE_RINTERNALS:
The data.table Package
• The data.table package exploits knowledge of
engine behavior and object layout to improve
performance
• Manipulates TRUELENGTH field to reuse vectors
• Uses TRUELENGTH field of CHARSXP as for own uses
during sorting
• Reads object bits to access string encoding quickly
without Rapi function calls
• Problem: It is coding to a particular implementation
of the R engine, rather than to a well-defined API
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 11
Beyond USE_RINTERNALS:
The stringi Package
• Recent discovery:
extern "C" void R_init_stringi(DllInfo* dll)
{
...
stri_set_icu_data_directory(
(char*)*(char**)(dll) /* dll->path */);
...
}
• Uses knowledge of internal DllInfo data structure
• May break if DllInfo structure changed
• Would be better to get path some other way
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 12
Challenge: TERR vs R GC Behavior
• TERR does more aggressive GC, collecting an object
immediately when its reference count goes to zero
• Matrix package: code swapping dimnames
// Csparse.c:Csparse_transpose
SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym)));
tmp = VECTOR_ELT(dn, 0);
SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1)); /* GC tmp here? */
SET_VECTOR_ELT(dn, 1, tmp);
• Problem: TERR collects tmp immediately if SET_VECTOR_ELT
reduces tmp reference count to zero
• Solution: Change SET_VECTOR_ELT so it won’t kill old
element
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 13
Challenge: SET_TYPEOF
• SET_TYPEOF changes an object type
• Mainly used for converting pairlist to language type
• Using Rapi handles, SET_TYPEOF can change object
in place
• With USE_RINTERNALS defined, SET_TYPEOF just
changes type bits
• Problem: We can’t change TERR C++ object type
• Possibility: Auto-convert pairlist object as needed in
engine, if type bits are set to language type
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 14
Bad code: .Call returning void
• In R, .Call can call function returning void
• Ex: in RSQLite:
void rsqlite_driver_init(SEXP records_,
SEXP cache_)
• As long as the value is not used, no problem
• In TERR, .Call manipulates returned object
• TERR can check for valid return object pointers, but this
has a performance cost
• Possibility: Special processing of .Call where value is not
used
• Possibility: Turn on/off valid-pointer check
• Q: worth supporting bad code?
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 15
TERR Rapi Support: Status
• TERR supports many packages with Rapi code,
using handles to SEXP objects
• We are reworking TERR object layout to support
packages that access object internals via
USE_RINTERNALS
• We are dealing with a number of compatibility
challenges
RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 16

More Related Content

What's hot

Building APIs with Kotlin and Spark
Building APIs with Kotlin and SparkBuilding APIs with Kotlin and Spark
Building APIs with Kotlin and SparkNordic APIs
 
Recent Changes and Challenges for Future Presto
Recent Changes and Challenges for Future PrestoRecent Changes and Challenges for Future Presto
Recent Changes and Challenges for Future PrestoKai Sasaki
 
An Introduction to ANTLR
An Introduction to ANTLRAn Introduction to ANTLR
An Introduction to ANTLRMorteza Zakeri
 
Developer insight into why applications run amazingly Fast in CF 2018
Developer insight into why applications run amazingly Fast in CF 2018Developer insight into why applications run amazingly Fast in CF 2018
Developer insight into why applications run amazingly Fast in CF 2018Pavan Kumar
 
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Taro L. Saito
 
.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling
.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling
.NET Fest 2018. Vagif Abilov. Akka + F# = AkklingNETFest
 
Apriori algorithm
Apriori algorithm Apriori algorithm
Apriori algorithm DHIVYADEVAKI
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language ProgrammingRavikumar Tiwari
 

What's hot (9)

Building APIs with Kotlin and Spark
Building APIs with Kotlin and SparkBuilding APIs with Kotlin and Spark
Building APIs with Kotlin and Spark
 
Recent Changes and Challenges for Future Presto
Recent Changes and Challenges for Future PrestoRecent Changes and Challenges for Future Presto
Recent Changes and Challenges for Future Presto
 
An Introduction to ANTLR
An Introduction to ANTLRAn Introduction to ANTLR
An Introduction to ANTLR
 
Developer insight into why applications run amazingly Fast in CF 2018
Developer insight into why applications run amazingly Fast in CF 2018Developer insight into why applications run amazingly Fast in CF 2018
Developer insight into why applications run amazingly Fast in CF 2018
 
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
Airframe: Lightweight Building Blocks for Scala - Scale By The Bay 2018
 
Apriori
AprioriApriori
Apriori
 
.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling
.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling
.NET Fest 2018. Vagif Abilov. Akka + F# = Akkling
 
Apriori algorithm
Apriori algorithm Apriori algorithm
Apriori algorithm
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language Programming
 

Viewers also liked

BPFR_11644022_Sagar_Sathe
BPFR_11644022_Sagar_SatheBPFR_11644022_Sagar_Sathe
BPFR_11644022_Sagar_SatheSagar Sathe
 
CRIT Library Presentation (3)
CRIT Library Presentation (3)CRIT Library Presentation (3)
CRIT Library Presentation (3)Ramona Duran
 
TWRA.tv 2016 deck
TWRA.tv 2016 deckTWRA.tv 2016 deck
TWRA.tv 2016 deckScott Scrip
 
Entrevistando al entrevistador
Entrevistando al entrevistadorEntrevistando al entrevistador
Entrevistando al entrevistadorsys army
 
Cómo hacer troubleshooting y no morir en el intento
Cómo hacer troubleshooting y no morir en el intentoCómo hacer troubleshooting y no morir en el intento
Cómo hacer troubleshooting y no morir en el intentosys army
 
Headshot! FPS networking
Headshot! FPS networkingHeadshot! FPS networking
Headshot! FPS networkingsys army
 
Revision of syllabus_of_gnm_last_3_dec _2__final
Revision of syllabus_of_gnm_last_3_dec _2__finalRevision of syllabus_of_gnm_last_3_dec _2__final
Revision of syllabus_of_gnm_last_3_dec _2__finalpravallika vasikarla
 
determinants og organizational effectiveness
determinants og organizational effectivenessdeterminants og organizational effectiveness
determinants og organizational effectivenesspravallika vasikarla
 
Ines De Castro George E Lucia
Ines De Castro   George E LuciaInes De Castro   George E Lucia
Ines De Castro George E Luciadmcb
 
Tecnologia Sep 11
Tecnologia Sep 11Tecnologia Sep 11
Tecnologia Sep 11seisvalt
 
Trata.aspectos.basicos
Trata.aspectos.basicosTrata.aspectos.basicos
Trata.aspectos.basicosMarcos Sanchez
 

Viewers also liked (20)

BPFR_11644022_Sagar_Sathe
BPFR_11644022_Sagar_SatheBPFR_11644022_Sagar_Sathe
BPFR_11644022_Sagar_Sathe
 
CRIT Library Presentation (3)
CRIT Library Presentation (3)CRIT Library Presentation (3)
CRIT Library Presentation (3)
 
Mi ple isabel miramontes
Mi ple  isabel miramontesMi ple  isabel miramontes
Mi ple isabel miramontes
 
TWRA.tv 2016 deck
TWRA.tv 2016 deckTWRA.tv 2016 deck
TWRA.tv 2016 deck
 
Entrevistando al entrevistador
Entrevistando al entrevistadorEntrevistando al entrevistador
Entrevistando al entrevistador
 
Cómo hacer troubleshooting y no morir en el intento
Cómo hacer troubleshooting y no morir en el intentoCómo hacer troubleshooting y no morir en el intento
Cómo hacer troubleshooting y no morir en el intento
 
Git 101+
Git 101+Git 101+
Git 101+
 
Headshot! FPS networking
Headshot! FPS networkingHeadshot! FPS networking
Headshot! FPS networking
 
Revision of syllabus_of_gnm_last_3_dec _2__final
Revision of syllabus_of_gnm_last_3_dec _2__finalRevision of syllabus_of_gnm_last_3_dec _2__final
Revision of syllabus_of_gnm_last_3_dec _2__final
 
Extra hepatic biliary atresia
Extra hepatic biliary atresiaExtra hepatic biliary atresia
Extra hepatic biliary atresia
 
Patent ductus arteriosus
Patent ductus arteriosusPatent ductus arteriosus
Patent ductus arteriosus
 
determinants og organizational effectiveness
determinants og organizational effectivenessdeterminants og organizational effectiveness
determinants og organizational effectiveness
 
Philosophy of staffing in nursing
Philosophy of staffing in nursingPhilosophy of staffing in nursing
Philosophy of staffing in nursing
 
Atrial septal defect
Atrial septal defectAtrial septal defect
Atrial septal defect
 
Ines De Castro George E Lucia
Ines De Castro   George E LuciaInes De Castro   George E Lucia
Ines De Castro George E Lucia
 
MãE Adriely
MãE AdrielyMãE Adriely
MãE Adriely
 
Homero5ºA
Homero5ºAHomero5ºA
Homero5ºA
 
Tecnologia Sep 11
Tecnologia Sep 11Tecnologia Sep 11
Tecnologia Sep 11
 
Juan Carlos
Juan CarlosJuan Carlos
Juan Carlos
 
Trata.aspectos.basicos
Trata.aspectos.basicosTrata.aspectos.basicos
Trata.aspectos.basicos
 

Similar to Supporting R C-language API in R-compatible Engine

Sannella use r2013-terr-memory-management
Sannella use r2013-terr-memory-managementSannella use r2013-terr-memory-management
Sannella use r2013-terr-memory-managementLou Bajuk
 
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native Way
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native WayMigrating Airflow-based Apache Spark Jobs to Kubernetes – the Native Way
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native WayDatabricks
 
Deploying R in BI and Real time Applications
Deploying R in BI and Real time ApplicationsDeploying R in BI and Real time Applications
Deploying R in BI and Real time ApplicationsLou Bajuk
 
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...Datacratic
 
TERR in BI and Real Time applications
TERR in BI and Real Time applicationsTERR in BI and Real Time applications
TERR in BI and Real Time applicationsLou Bajuk
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-iDr. Awase Khirni Syed
 
MuleSoft Manchester Meetup #3 slides 31st March 2020
MuleSoft Manchester Meetup #3 slides 31st March 2020MuleSoft Manchester Meetup #3 slides 31st March 2020
MuleSoft Manchester Meetup #3 slides 31st March 2020Ieva Navickaite
 
Functional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSFunctional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSVivek Tikar
 
Using the R Language in BI and Real Time Applications (useR 2015)
Using the R Language in BI and Real Time Applications (useR 2015)Using the R Language in BI and Real Time Applications (useR 2015)
Using the R Language in BI and Real Time Applications (useR 2015)Lou Bajuk
 
Extend the Reach of R to the Enterprise (for useR! 2013)
Extend the Reach of R to the Enterprise (for useR! 2013)Extend the Reach of R to the Enterprise (for useR! 2013)
Extend the Reach of R to the Enterprise (for useR! 2013)Lou Bajuk
 
Stupid DITA Tricks: After-The-Fact Specialization: Treating Aircraft Manuals ...
Stupid DITA Tricks:After-The-Fact Specialization: Treating Aircraft Manuals ...Stupid DITA Tricks:After-The-Fact Specialization: Treating Aircraft Manuals ...
Stupid DITA Tricks: After-The-Fact Specialization: Treating Aircraft Manuals ...Contrext Solutions
 
West Yorkshire Mulesoft Meetup #6
West Yorkshire Mulesoft Meetup #6West Yorkshire Mulesoft Meetup #6
West Yorkshire Mulesoft Meetup #6Francis Edwards
 
Extending the R language to BI and Real-time Applications JSM 2015
Extending the R language to BI and Real-time Applications JSM 2015Extending the R language to BI and Real-time Applications JSM 2015
Extending the R language to BI and Real-time Applications JSM 2015Lou Bajuk
 
Introducing TiDB Operator
Introducing TiDB OperatorIntroducing TiDB Operator
Introducing TiDB OperatorKevin Xu
 
HKG18-318 - OpenAMP Workshop
HKG18-318 - OpenAMP WorkshopHKG18-318 - OpenAMP Workshop
HKG18-318 - OpenAMP WorkshopLinaro
 

Similar to Supporting R C-language API in R-compatible Engine (20)

Sannella use r2013-terr-memory-management
Sannella use r2013-terr-memory-managementSannella use r2013-terr-memory-management
Sannella use r2013-terr-memory-management
 
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native Way
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native WayMigrating Airflow-based Apache Spark Jobs to Kubernetes – the Native Way
Migrating Airflow-based Apache Spark Jobs to Kubernetes – the Native Way
 
Deploying R in BI and Real time Applications
Deploying R in BI and Real time ApplicationsDeploying R in BI and Real time Applications
Deploying R in BI and Real time Applications
 
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
 
React advance
React advanceReact advance
React advance
 
TERR in BI and Real Time applications
TERR in BI and Real Time applicationsTERR in BI and Real Time applications
TERR in BI and Real Time applications
 
Manchester Meetup #3
Manchester Meetup #3Manchester Meetup #3
Manchester Meetup #3
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
MuleSoft Manchester Meetup #3 slides 31st March 2020
MuleSoft Manchester Meetup #3 slides 31st March 2020MuleSoft Manchester Meetup #3 slides 31st March 2020
MuleSoft Manchester Meetup #3 slides 31st March 2020
 
Functional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJSFunctional programming, TypeScript and RXJS
Functional programming, TypeScript and RXJS
 
Zend server for IBM i update 5.6
Zend server for IBM i update 5.6Zend server for IBM i update 5.6
Zend server for IBM i update 5.6
 
Code generation
Code generationCode generation
Code generation
 
Using the R Language in BI and Real Time Applications (useR 2015)
Using the R Language in BI and Real Time Applications (useR 2015)Using the R Language in BI and Real Time Applications (useR 2015)
Using the R Language in BI and Real Time Applications (useR 2015)
 
Extend the Reach of R to the Enterprise (for useR! 2013)
Extend the Reach of R to the Enterprise (for useR! 2013)Extend the Reach of R to the Enterprise (for useR! 2013)
Extend the Reach of R to the Enterprise (for useR! 2013)
 
Application Deployment on IBM i
Application Deployment on IBM iApplication Deployment on IBM i
Application Deployment on IBM i
 
Stupid DITA Tricks: After-The-Fact Specialization: Treating Aircraft Manuals ...
Stupid DITA Tricks:After-The-Fact Specialization: Treating Aircraft Manuals ...Stupid DITA Tricks:After-The-Fact Specialization: Treating Aircraft Manuals ...
Stupid DITA Tricks: After-The-Fact Specialization: Treating Aircraft Manuals ...
 
West Yorkshire Mulesoft Meetup #6
West Yorkshire Mulesoft Meetup #6West Yorkshire Mulesoft Meetup #6
West Yorkshire Mulesoft Meetup #6
 
Extending the R language to BI and Real-time Applications JSM 2015
Extending the R language to BI and Real-time Applications JSM 2015Extending the R language to BI and Real-time Applications JSM 2015
Extending the R language to BI and Real-time Applications JSM 2015
 
Introducing TiDB Operator
Introducing TiDB OperatorIntroducing TiDB Operator
Introducing TiDB Operator
 
HKG18-318 - OpenAMP Workshop
HKG18-318 - OpenAMP WorkshopHKG18-318 - OpenAMP Workshop
HKG18-318 - OpenAMP Workshop
 

Recently uploaded

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 

Recently uploaded (20)

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 

Supporting R C-language API in R-compatible Engine

  • 1. Supporting the “Rapi” C-language API in an R-compatible engine Michael Sannella msannell@tibco.com RIOT 2015
  • 2. TERR, Packages, and Rapi • TERR: TIBCO® Enterprise Runtime for R • A commercial R-compatible statistics engine • We want TERR to support many external packages • Much of the value of R lies in packages from CRAN and other repositories (Bioconductor, Github, etc.) • Many packages are pure “R-language” packages • About 70% of CRAN packages are pure R-language • Many packages contain C-language code • C-language code can access the R engine using the R engine’s C-language API (“Rapi”) RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 2
  • 3. TERR Rapi Support • Ultimate goal: TERR supports loading and executing binary packages unchanged • Fallback: Modify package sources and rebuild before loading • If necessary, we can distribute modified packages for TERR in our TRAN repository RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 3
  • 4. Example: Code Using Rapi • R code from the CRAN package “splusTimeDate”: .Call("time_to_hour_min_sec", x, timeZoneList()) • C code: SEXP time_to_hour_min_sec(SEXP time_vec, SEXP zone_list) { ... PROTECT(ret = NEW_LIST(4)); ... SET_VECTOR_ELT(ret, 0, PROTECT(NEW_INTEGER(lng))); ... hour_data = INTEGER(VECTOR_ELT(ret, 0)); ... hour_data[i] = td.hour; ... UNPROTECT(7); return( ret ); } • NEW_INTEGER macro expands to a call to the Rf_allocVector Rapi entry RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 4
  • 5. Rapi Shared Libraries: R.dll, etc. • Rapi defines hundreds of library entries • ATTRIB, SET_VECTOR_ELT, R_alloc, Rf_allocVector, Rf_Protect, dcopy_, dgemm_, dlaic1_, etc. • Embedding API (used by Rstudio) • Global variables: R_GlobalEnv, R_NaInt, etc. • Packages link to shared library files R.dll, Rblas.dll, etc. (libR.so, etc. on Linux) that export these Rapi entries • To support Rapi, TERR contains R.dll, etc. libraries that forward Rapi calls to the engine RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 5
  • 6. TERR Rapi Support: Handles • Observation: Rapi entries treat SEXP (R object pointer) values as opaque values passed to and from the engine • TERR approach: Treat SEXP as an offset in a “handle table” containing pointers to internal TERR objects • Benefit: Can convert TERR-only objects “in-place” as needed • Ex: Expand TERR integer sequence object to an integer vector object when Rapi code calls INTEGER to access its contents • Works well for many packages RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 6
  • 7. Problem: USE_RINTERNALS • If the C constant USE_RINTERNALS is defined, many Rapi function calls are redefined as macros directly accessing R object internals • USE_RINTERNALS is used in many popular CRAN packages: Rcpp, Rserve, igraph, etc • TERR workaround: Make our own versions of packages without USE_RINTERNALS defined • Some tweaks needed to compile: STRING_ELT(x, i) = value; // change this SET_STRING_ELT(x, i, value); // to this RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 7
  • 8. USE_RINTERNALS More Efficient? • Can improve efficiency of some idioms: for (int i = 0; i<LENGTH(obj); ++i) { INTEGER(obj)[i] = 0; } • However, it is easy to rework code to reduce function calls: int len = LENGTH(obj); int* data = INTEGER(obj); for (int i = 0; i<len; ++i) { data[i] = 0 } RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 8
  • 9. Solution: New Object Layout • The TERR team is currently reworking the TERR object layout to be more compatible with R • SEXP is pointer to R-compatible object • TERR C++ header stored before R-compatible object bytes RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 9 SObject* SEXP TERR C++ Object Header R-Compatible Header Data
  • 10. New Object Layout: Issues • Issue: List object must contain array of SEXP pointers, not TERR C++ object pointers • Issue: TERR-only objects (like sequence objects) must be converted before exposing to Rapi code • Only allow R-compatible objects within “Rapi space” • Convert arguments to .Call • Convert value returned by Rapi entries Rf_eval, etc. • Concerns: Extra complexity, object size, performance hit RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 10
  • 11. Beyond USE_RINTERNALS: The data.table Package • The data.table package exploits knowledge of engine behavior and object layout to improve performance • Manipulates TRUELENGTH field to reuse vectors • Uses TRUELENGTH field of CHARSXP as for own uses during sorting • Reads object bits to access string encoding quickly without Rapi function calls • Problem: It is coding to a particular implementation of the R engine, rather than to a well-defined API RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 11
  • 12. Beyond USE_RINTERNALS: The stringi Package • Recent discovery: extern "C" void R_init_stringi(DllInfo* dll) { ... stri_set_icu_data_directory( (char*)*(char**)(dll) /* dll->path */); ... } • Uses knowledge of internal DllInfo data structure • May break if DllInfo structure changed • Would be better to get path some other way RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 12
  • 13. Challenge: TERR vs R GC Behavior • TERR does more aggressive GC, collecting an object immediately when its reference count goes to zero • Matrix package: code swapping dimnames // Csparse.c:Csparse_transpose SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))); tmp = VECTOR_ELT(dn, 0); SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1)); /* GC tmp here? */ SET_VECTOR_ELT(dn, 1, tmp); • Problem: TERR collects tmp immediately if SET_VECTOR_ELT reduces tmp reference count to zero • Solution: Change SET_VECTOR_ELT so it won’t kill old element RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 13
  • 14. Challenge: SET_TYPEOF • SET_TYPEOF changes an object type • Mainly used for converting pairlist to language type • Using Rapi handles, SET_TYPEOF can change object in place • With USE_RINTERNALS defined, SET_TYPEOF just changes type bits • Problem: We can’t change TERR C++ object type • Possibility: Auto-convert pairlist object as needed in engine, if type bits are set to language type RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 14
  • 15. Bad code: .Call returning void • In R, .Call can call function returning void • Ex: in RSQLite: void rsqlite_driver_init(SEXP records_, SEXP cache_) • As long as the value is not used, no problem • In TERR, .Call manipulates returned object • TERR can check for valid return object pointers, but this has a performance cost • Possibility: Special processing of .Call where value is not used • Possibility: Turn on/off valid-pointer check • Q: worth supporting bad code? RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 15
  • 16. TERR Rapi Support: Status • TERR supports many packages with Rapi code, using handles to SEXP objects • We are reworking TERR object layout to support packages that access object internals via USE_RINTERNALS • We are dealing with a number of compatibility challenges RIOT 2015 --- July 2015 © Copyright 2000-2015 TIBCO Software Inc. 16