-
Notifications
You must be signed in to change notification settings - Fork 10
/
rw2d.Rdb
442 lines (376 loc) · 10.6 KB
/
rw2d.Rdb
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?xml version="1.0"?>
<article xmlns:r="http://www.r-project.org"
xmlns:xi="http://www.w3.org/2003/XInclude"
xmlns:c="http://www.C.org">
<articleinfo>
<title>Implementing 2-D Random Walks with <r:pkg>Rllvm</r:pkg></title>
<author><firstname>Duncan</firstname><surname>Temple Lang</surname>
<affiliation><orgname>University of California at Davis</orgname>
<orgdiv>Department of Statistics</orgdiv>
</affiliation>
</author>
</articleinfo>
<section id="setup">
<title></title>
<para>
Here we implement the very initial version of Ross Ihaka's
2D random walk example of how to improve the performance of code.
His example starts with the naieve and obvious implementation
of the 2D walk. At the end, he vectorizes this in <r/> code and
gets a speedup of a factor of about 200.
</para>
<para>
The naive implementation is
<r:code>
rw2d1 =
function(n = 100) {
xpos = ypos = numeric(n)
for(i in 2:n) {
# Decide whether we are moving horizontally or vertically.
delta = if(runif(1) > .5) 1 else -1
if (runif(1) > .5) {
xpos[i] = xpos[i-1] + delta
ypos[i] = ypos[i-1]
}
else {
xpos[i] = xpos[i-1]
ypos[i] = ypos[i-1] + delta
}
}
list(x = xpos, y = ypos)
}
</r:code>
We have seen how to write loops, get elements from pointers and do conditional branching
in <filename>cumsum.Rdb</filename>.
So to "compile" this function is very similar.
</para>
</section>
<section id="compileCode">
<title>Compiling the Code</title>
<para>
We are going to implement a slightly different version
that receives the int * pointers for x and y and just
fills them in. We can write a .Call() wrapper for this
so we can compare apples and apples.
<r:code>
library(Rllvm)
InitializeNativeTarget()
mod = Module("2Drw")
ptrInt = pointerType(Int32Type)
fun = Function("rw2d", VoidType, c(x = ptrInt, y = ptrInt, len = Int32Type), mod)
</r:code>
<r:code>
entry = Block(fun, "entry")
cond = Block(fun, "loopCond")
body = Block(fun, "loopBody")
ret = Block(fun, "return")
h = Block(fun, "Horizontal")
v = Block(fun, "Vertical")
increment = Block(fun, "increment")
</r:code>
<r:code>
one = createIntegerConstant(1L)
minusOne = createIntegerConstant(-1L)
</r:code>
<r:code>
ir = IRBuilder(entry)
iv = ir$createLocalVariable(Int32Type, "i")
lena = ir$createLocalVariable(Int32Type, "lenp")
xa = ir$createLocalVariable(ptrInt, "xp")
ya = ir$createLocalVariable(ptrInt, "yp")
delta = ir$createLocalVariable(Int32Type, "delta")
ir$createStore(fun$x, xa)
ir$createStore(fun$y, ya)
ir$createStore(fun$len, lena)
ir$createStore(one, iv)
ir$createBr(cond)
</r:code>
<r:code>
ir$setInsertPoint(cond)
a = ir$createLoad(iv)
b = ir$createLoad(lena)
ok = ir$createICmp(ICMP_SLT, a, b)
ir$createCondBr(ok, body, ret)
</r:code>
<r:code>
ir$setInsertPoint(ret)
ir$createRetVoid()
</r:code>
<r:code>
ir$setInsertPoint(body)
# declare runif which takes a number but ignores it.
runif = Function("runif", DoubleType, c(n = Int32Type), mod)
# compute delta
u = ir$createCall(runif, one)
gt = ir$createFCmp(FCMP_UGE, u, createDoubleConstant(.5))
ir$createStore(ir$createSelect(gt, minusOne, one), delta)
# now determine whether to go horiz or vert.
u = ir$createCall(runif, one)
gt = ir$createFCmp(FCMP_UGE, u, createDoubleConstant(.5))
ir$createCondBr(gt, h, v)
</r:code>
Next we fill in the horizontal move.
We load x[i-1] and add delta to it
and the store this in x[i]
<r:code>
ir$setInsertPoint(h)
a = ir$createLoad(iv)
b = ir$binOp(BinaryOps["Sub"], a, one)
r = ir$createLoad(xa)
idx = ir$createSExt(b, 64L)
x.prev = ir$createLoad(ir$createGEP(r, idx))
nw = ir$binOp(BinaryOps["Add"], x.prev, ir$createLoad(delta))
a = ir$createLoad(xa)
i = ir$createLoad(iv)
idx = ir$createSExt(i, 64L)
xi = ir$createGEP(a, idx)
ir$createStore(nw, xi)
</r:code>
Next we copy y[i-1] to y[i]
<r:code>
a = ir$createLoad(iv)
b = ir$binOp(BinaryOps["Sub"], a, one)
r = ir$createLoad(ya)
idx = ir$createSExt(b, 64L)
y.prev = ir$createLoad(ir$createGEP(r, idx))
a = ir$createLoad(ya)
i = ir$createLoad(iv)
idx = ir$createSExt(i, 64L)
yi = ir$createGEP(a, idx)
ir$createStore(y.prev, yi)
</r:code>
Finally, we jump to the loop increment and then to the condition.
<r:code>
ir$createBr(increment)
</r:code>
This is the increment block that updates i and
then jumps to the loop condition.
<r:code>
ir$setInsertPoint(increment)
i = ir$createLoad(iv)
inc = ir$binOp(BinaryOps["Add"], i, 1L)
ir$createStore(inc, iv)
ir$createBr(cond)
</r:code>
<r:code>
ir$setInsertPoint(v)
a = ir$createLoad(iv)
b = ir$binOp(BinaryOps["Sub"], a, one)
r = ir$createLoad(ya)
idx = ir$createSExt(b, 64L)
y.prev = ir$createLoad(ir$createGEP(r, idx))
nw = ir$binOp(BinaryOps["Add"], y.prev, ir$createLoad(delta))
a = ir$createLoad(ya)
i = ir$createLoad(iv)
idx = ir$createSExt(i, 64L)
yi = ir$createGEP(a, idx)
ir$createStore(nw, yi)
a = ir$createLoad(iv)
b = ir$binOp(BinaryOps["Sub"], a, one)
r = ir$createLoad(xa)
idx = ir$createSExt(b, 64L)
x.prev = ir$createLoad(ir$createGEP(r, idx))
a = ir$createLoad(xa)
i = ir$createLoad(iv)
idx = ir$createSExt(i, 64L)
xi = ir$createGEP(a, idx)
ir$createStore(x.prev, xi)
ir$createBr(increment)
</r:code>
</para>
</section>
<section>
<title>Timings</title>
<para>
We now run our compiled code to see how fast it is relative to the
other implementations. Our compiled code references the
<c:func>runif</c:func> routine and so needs to be able to find that.
We do this before running the code as it only needs to be done once.
<r:code>
print(showModule(mod))
ee = ExecutionEngine(mod)
Optimize(mod, ee)
llvmAddSymbol(runif = getNativeSymbolInfo("runif")$address)
</r:code>
</para>
<para>
We'll set the number of steps to something quite large so that we measure sufficiently
lengthy computations:
<r:code>
n = 1e4
</r:code>
</para>
<para>
We run the interpreted version 5 times and take the medians of the
three different measurements (self, system and elapsed):
<r:code>
interp = system.time({rw2d1(n)})
#interp = replicate(2, system.time({rw2d1(n)}))
#interp = apply(interp, 1, median)
</r:code>
</para>
<para>
Now we run the compiled code
<r:code>
print(showModule(fun))
doit =
function(n = 1000000) {
x = integer(n)
y = integer(n)
.llvm(fun, x = x, y = y, n, .ee = ee, .all = TRUE)[c("x", "y")]
}
tt = system.time({ doit(n)})
tt = system.time({ doit(n)}) # second time runs faster
</r:code>
<r:test>
z = doit(100000)
table(diff(z$x))
table(diff(z$y))
</r:test>
These should be 50% 0 and 25% for each of -1 and 1.
Since this runs much faster than the interpreted version, we
run more repetitions and again compute the median:
<r:code>
tt = replicate(10, system.time({ doit(n)}))
tt = apply(tt, 1, median)
</r:code>
</para>
<para>
We'll also compare how well byte-compiling our interpreted
function improves the performance:
<r:code>
library(compiler)
g = cmpfun(rw2d1)
cmp = replicate(3, system.time(g(n)))
cmp = apply(cmp, 1, median)
interp/cmp
</r:code>
</para>
<para>
Finally, we'll run the manually implemented vectorized
version of the random walk that Ross Ihaka wrote.
This is pure R, but highly leverages C-level computations
within the interpreter.
<r:code>
ross = "rw1.R"
source(ross)
fastInterp = replicate(10, system.time({rw2d5(n)}))
fastInterp = apply(fastInterp, 1, median)
fastInterp/interp
</r:code>
<r:code eval="false">
apply(fastInterp, 1, median)/apply(tt, 1, median)
</r:code>
<r:code>
i = 3
tmp = c(interp[i], cmp[i], fastInterp[i], tt[i])
m = matrix(c(tmp, interp[i]/tmp), length(tmp), ,
dimnames = list(c("Interpeted", "Byte Compiled", "Vectorized", "Rllvm"), c("Time", "Speedup")))
m
</r:code>
<r:code>
res = structure(m, session = sessionInfo(), system = Sys.info(), when = Sys.time(), n = n)
id = sprintf("rw2d.tm.1e7_%s_%s_gcc", Sys.info()["nodename"], Sys.info()["sysname"])
assign(id, res, globalenv())
save( list = id, file = sprintf("%s.rda", id))
</r:code>
On a Macbook Pro (April 29th 2013) - elapsed time.
<r:output><![CDATA[
Time Speedup
Interpeted 167.950 1.00000
Byte Compiled 115.211 1.45776
Vectorized 0.894 187.86353
Rllvm 0.649 258.78274
]]></r:output>
The following are for user time, not elapsed time.
<table>
<tgroup cols="3" align="right">
<colspec colnum="1" align="left"/>
<thead>
<row><entry>Method</entry><entry>Times</entry><entry>Speedup</entry></row>
</thead>
<tbody>
<row><entry>Interpreted</entry><entry>170.9</entry><entry>1</entry></row>
<row><entry>Byte compiled</entry><entry>93.8</entry><entry>1.8</entry></row>
<row><entry>Vectorized</entry><entry>.88</entry><entry>194.2</entry></row>
<row><entry>Rllvm</entry><entry>.50</entry><entry>341.8</entry></row>
</tbody>
</tgroup>
</table>
</para>
<para>
With an optimized R (i.e. compiled with -O3)
<table bgcolor="gray">
<?dbfo keep-together="auto"?>
<tgroup cols="3">
<thead>
<row>
<entry align="right"></entry>
<entry align="center">Time</entry>
<entry align="center">Speedup</entry>
</row>
</thead>
<tbody>
<row>
<entry align="right">Interpeted</entry>
<entry align="right">169</entry>
<entry align="right">1</entry>
</row>
<row>
<entry align="right">Byte Compiled</entry>
<entry align="right">84.5</entry>
<entry align="right">2.00</entry>
</row>
<row>
<entry align="right">Vectorized</entry>
<entry align="right">0.81</entry>
<entry align="right">208</entry>
</row>
<row>
<entry align="right">Rllvm</entry>
<entry align="right">0.487</entry>
<entry align="right">346</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
On a Macbook Pro, OSX 10.7.5, 8G RAM, R-3.1 devel, optimization level -O3,
I get (Apr 27 2013)
<r:output><![CDATA[
Time Speedup
Interpeted 161.2180 1.000000
Byte Compiled 99.4940 1.620379
Vectorized 0.7495 215.100734
Rllvm 0.6220 259.192926
]]></r:output>
</para>
<invisible>
All of these done with the same version of Rllvm & LLVM 3.1 and
R-2.16 on my Mac laptop Aug 4, 2012
Time Speedup
Interpeted 302.488 1.000000
Byte Compiled 203.226 1.488432
Vectorized 1.549 195.279535
Rllvm 0.641 471.900156
Time Speedup
Interpeted 305.5500 1.000000
Byte Compiled 204.3310 1.495368
Vectorized 1.5775 193.692552
Rllvm 0.6430 475.194401
Linux machine: (donald)
Time Speedup
Interpeted 255.310 1.000000
Byte Compiled 167.970 1.519974
Vectorized 1.365 187.040293
Rllvm 0.970 263.206186
eeyore:
Time Speedup
Interpeted 171.62 1.000000
Byte Compiled 85.49 2.007486
Vectorized 0.88 195.022727
Rllvm 0.70 245.171429
</invisible>
</section>
</article>