Rcpp Oliver Heidmann Supervised by: Julian Kunkel University of - - PowerPoint PPT Presentation

rcpp
SMART_READER_LITE
LIVE PREVIEW

Rcpp Oliver Heidmann Supervised by: Julian Kunkel University of - - PowerPoint PPT Presentation

Rcpp Oliver Heidmann Supervised by: Julian Kunkel University of Hamburg oliverheidmann@hotmail.de June 14, 2016 1/19 Overview What is C++? 1 What is Rccp? 2 Rcpp basics 3 RObject and SEXP Rcpp basics: Type Mappings conversion methods


slide-1
SLIDE 1

1/19

Rcpp

Oliver Heidmann

Supervised by: Julian Kunkel

University of Hamburg

  • liverheidmann@hotmail.de

June 14, 2016

slide-2
SLIDE 2

2/19

Overview

1

What is C++?

2

What is Rccp?

3

Rcpp basics RObject and SEXP

Rcpp basics: Type Mappings conversion methods Calling a function

4

Writing R code Rcpp.package.skeleton

5

Example parts cpp Export function c++ function calling the function package inline sourceCpp RuntimeComparison

6

Conclusion

7

sources

slide-3
SLIDE 3

3/19

What is C++?

◮ object orientated programming languge ◮ intermediate level language

◮ high level language features

and also

◮ low level language features

◮ designed to be

◮ fast at executing ◮ efficient with memory ◮ flexible in the way it can be used

slide-4
SLIDE 4

4/19

What is Rccp?

◮ easy integration of c++ code ◮ mappings from r objects to c classes ◮ package skeleton creation ◮ flexible error and exception handling

slide-5
SLIDE 5

5/19

RObject and SEXP

◮ Rcpp::RObject is a very thin wrapper around an SEXP. ◮ Rcpp::RObject defines set of functions applicable to any r object ◮ SEXP only member of Rcpp::RObject. ◮ SEXP represents r object ◮ SEXP is guarded from Garbage collection through Rcpp::RObject

slide-6
SLIDE 6

6/19

Rcpp basics: Type Mappings

What is Wrappable?

◮ int double bool to R atomic vectors ◮ std::string to R atomic character vectors ◮ STL containers ◮ and any class that has a SEXP() operator for conversion ◮ or any class in which wrap() tamplate is specialized

slide-7
SLIDE 7

7/19

conversion methods

Rcpp::wrap for converting c++ types to R template <typename T> SEXP wrap(const T &

  • bject)

Rcpp::as for converting R types to c++ types template <typename T> T as(SEXP x)

slide-8
SLIDE 8

8/19

Calling a c++ function in r

◮ calling done with .call(...) ◮ r function call ◮ calls the c function for r

.call( "function_name", parameter_1, ..., parameter_n, package="packagename" )

slide-9
SLIDE 9

9/19

Rcpp.package.skeleton

◮ r command to create skeleton rcpp package ◮ already in Rcpp package ◮ can be created with example functions ◮ very easy to get into

slide-10
SLIDE 10

10/19

rcpp skeleton package guide

rough guide in package included

◮ edit the help file skeletons in ’man’, possibly combining help files for

multiple functions.

◮ edit the exports in ’NAMESPACE’, and add necessary imports. ◮ put any c/c++/fortran code in ’src’. ◮ R CMD build to build the package tarball. ◮ R CMD check to check the package tarball.

slide-11
SLIDE 11

11/19

cpp Export function

RcppExport SEXP test_add_lists(SEXP r_list1, SEXP r_list2) { std::vector<int> cpp_vector1; std::vector<int> cpp_vector2; BEGIN_RCPP Rcpp::RObject __result; Rcpp::RNGScope __rngScope; cpp_vector1 = Rcpp::as<std::vector<int>>(r_list1); cpp_vector2 = Rcpp::as<std::vector<int>>(r_list2); __result = add_lists(cpp_vector1, cpp_vector2); return Rcpp::wrap(__result); END_RCPP }

slide-12
SLIDE 12

12/19

c++ function

std::vector<int> add_lists(std::vector<int> vec1, std::vector<int> vec2) { std::vector<int> result; unsigned long max_length; max_length = std::min(vec1.size(), vec2.size()); for (unsigned long i = 0; i < max_length; i++) { result.push_back(vec1[i] + vec2[i]); } return result;}

slide-13
SLIDE 13

13/19

calling the function

add_lists <-function(vec1, vec2) { .Call( "test_add_lists", vec1, vec2, PACKAGE = ’test’) }

slide-14
SLIDE 14

14/19

package inline

add <- cppFunction(" double add(double x, double y) { double sum = x + y; return sum; } ") add(-0.01,1.02)

  • utput: 1.01
slide-15
SLIDE 15

15/19

sourceCpp

simple-c-functions.cpp #include <Rcpp.h> #include <iostream> // [[Rcpp::export]] double myCmean(Rcpp::NumericVector x) { double result = 0; for(auto v : x) { result += v; } return result/x.length(); }

slide-16
SLIDE 16

16/19

sourceCpp

main.R require(Rcpp) sourceCpp("simple-c-functions.cpp") myCmean(10.01,1.1,2.26)

  • utput:

13.37

slide-17
SLIDE 17

17/19

Runtime comparison

x = vector with 10000 entrys of type numeric/double each function was executed 1000 times unit: microseconds function min median max r inlineMean(x) 12.054 14.5440 49.531 r mean(x) 2062.821 2151.4090 3021.454 c mean(x) 9.179 9.8325 21.462

slide-18
SLIDE 18

18/19

Conclusion

◮ Rcpp is easy to get into ◮ package creation is fast and easy ◮ sourceCpp() easiest way of using small cpp functions ◮ c++ code is a lot faster

slide-19
SLIDE 19

19/19

sources

◮ http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf

13.6.2016

◮ http://dirk.eddelbuettel.com/code/rcpp/Rcpp-package.pdf

13.6.2016

◮ http://dirk.eddelbuettel.com/code/rcpp/Rcpp-FAQ.pdf

13.6.2016

◮ https:://www.techopedia.com/definition/26184/c-programming-

language 13.6.2016

◮ adv-r.had.co.nz/Rcpp.html

13.6.2016

◮ https://cran.r-

project.org/web/packages/microbenchmark/microbenchmark.pdf 13.6.2016