-
Notifications
You must be signed in to change notification settings - Fork 3
/
slides.html
351 lines (250 loc) · 6.74 KB
/
slides.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html>
<head>
<title>R and C/C++ Code</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# R and C/C++ Code
## [https://github.com/dsidavis/RCInterface](https://github.com/dsidavis/RCInterface)
### Duncan Temple Lang
<div style="clear: both"/>
<!-- <hr width="50%"/> -->
<img src="http://dsi.ucdavis.edu/images/dsi_banner.png" height="40%"></img>
---
layout: true
<img style="float: right" src="http://dsi.ucdavis.edu/images/dsi_brand_logo.png"></img>
---
# Overview
+ Compilation, linking, shared libraries, loading
+ Make
+ R package
+ Debugging
+ configure and autoconf
---
# Why is Compile Code Faster
+ Consider 1 + 2
+ In R
+ In C
---
# Compiled Code
+ See Plus/plus.c
```
double plus0()
{
return(1+2);
}
```
+ Machine Code
```
_plus0:
0: 55 pushq %rbp
1: 48 89 e5 movq %rsp, %rbp
4: f2 0f 10 05 4c 00 00 00 movsd 76(%rip), %xmm0
c: 5d popq %rbp
d: c3 retq
e: 66 90 nop
```
---
# More Complicated Version of plus0
```
double plus1()
{
double ans;
ans = 1;
ans += 2;
return(ans);
}
```
+ Exactly the same compiled code
---
# When to Use Compile Code
+ Avoid reimplementing existing code
+ there is an existing, reliable, maintained compiled routine or library
+ Speed is absolutely essential
+ Implement slow version
+ Profile
+ Implement only slow parts
+ Compare results with slow version to ensure correct.
---
# Calling a Routine
+ Two interfaces in R to C/C++ code
+ .C()
+ .Call()
+ .Call() more flexible.
+ .C() limited, but simple(r) when appropriate.
+ Can only call routines with specific signatures
+ Typically create wrapper routines
+ Called from R, convert and pass arguments to real C routine
+ Direct call possible with, e.g., rdyncall and Rffi.
+ Also .Fortran() and .External()
---
# .C()
+ Only accepts arrays from basic R types
+ logical, integer, numeric, character vectors
+ typically needs their lengths/number of elements
+ passed as an array of length 1!
+ result returned by filling in values in the argument(s)
---
# .C() example
+ In dotC/src
```
dyn.load("dotC.so")
z = as.numeric(1:100)
o = .C("add1", z, length(z), ans = numeric(length(z))) $ans
o
```
---
# .C() wrapper functions (in R)
+ R function to call the C routine
+ Coerce all of the arguments to expected type
+ name the result elements and extract them
+ can be more than one element containing results
```
add1 =
function(x)
{
x = as.numeric(x)
o = .C("add1", x, length(x), ans = numeric(length(x))) $ans
}
```
---
# "Compilation" Steps
+ Compile and convert source files top dynamic shared object (DSO)/DLL
+ R CMD SHLIB plus.c
+ R CMD SHLIB -o foo.so a.c b.c
+ Or compiled automatically in package installation.
+ R CMD INSTALL packageDirectory
+ Actually, several steps
+ Preprocessor
+ Compile to object code
+ Link object code to executable or DSO/DLL
+ Load DSO/DLL
+ dyn.load() in R
+ useDynLib() in NAMESPACE file of package.
+ Beauty of C/C++ is can fail at each step!
---
# Makevars file
+ Often need to specify compilation flags
+ Do this in Makevars
+ Don't specify compiler (CC),
+ allow user or central install override this
---
# Make
+ Purpose - created derived files
+ update specific files when one or more change
+ declared dependencies of one output on one or more inputs.
+ DSOs from .c/.cpp files, compiling .o's and
---
# .Call()
+ Pass arbitrary R objects to C routine
+ Return an R object.
+ Can examine the type of an argument
+ Can create new R objects
+ All R objects in C have type `SEXP`
+ C-level API to manipulate SEXPs
+ Query objects - type, length, dimensions, attributes and values/elements
+ Create new objects
---
# Accessing Values in R Objects
+ TYPEOF(robj) to determine what the R object is
+ like typeof() in R, but returns enumerated constant.
+ Access data portion with corresponding macro
+ arary of primitive values
+ LOGICAL() - int *
+ INTEGER() - int *
+ REAL() - double *
+ Character vectors
+ STRING_ELT(robj, idx)
+ SET_STRING_ELT(robj, idx, value)
+ Lists
+ VECTOR_ELT(robj, idx)
+ SET_VECTOR_ELT(robj, idx, value)
---
# Rqpdf
+ [https://github.com/qpdf/qpdf](https://github.com/qpdf/qpdf)
----
# Matrices
+ Column-oriented vector
+ How to access m[i,j]?
---
# Creating New R Objects in C/C++
+ `allocVector(type, length)`
+ Macros, e.g., NEW_NUMERIC(), NEW_LIST()
---
# Garbage Collection (GC)
+ Since essentially working with R at the C-level,
garbage collection can occur at any time in the C routine.
+ Need to mark local R objects as being used so not GC'ed.
+ PROTECT(obj) just after creating/modifying it
+ UNPROTECT(num) at the "end" of the routine, release <num> objects.
+ Use `gctorture(TRUE)` to debug C code.
+ Very slow, but worth it.
---
# External Pointer Objects
---
# Debugging Compiled Code in R
+ Need to be able to
+ stop at particular lines of code to explore
+ stop on crashes (segmentation faults) for post-mortem debugging.
+ Run R under the debugger
+ Debug R itself and
+ loaded C/C++ code in packages/dyn.load()
+ R -d lldb
+ R -d gdb
+ For lldb on OSX, I need to use the alias
```
command alias Rr process launch -v DYLD_FALLBACK_LIBRARY_PATH=/Users/duncan/R-devel/build/lib
```
to run the R process within the debugger.
---
# See Rqpdf at tag segFault
---
# Configuring Package Code
+ Need to provide machine-specific or local flags to compile C/C++ code
+ Location of include directories
+ pre-processor flags to enable/disable features in C/C++ code
+ library directories
+ Modify R code for particular installation
+ Allow package installer to customize package features and compilation
+ R CMD INSTALL --configure-args='.....'
+ `install.packages(, configure.args = c())`
---
# See Rqpdf package in repos.
+ configure.in
+ src/Makevars.in
+ R/version.R.in
---
# Useful Tools
+ compiler warnings
+ valgrind
+ ldd/otool
+ nm
---
# Auto Generating Bindings
+ Rcpp
+ SWIG
+ RCodeGen
+ See RCUDA
+ RCIndex for programmatically reading C/C++ code
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>