-
Notifications
You must be signed in to change notification settings - Fork 10
/
fuseLoop.Rdb
368 lines (312 loc) · 9.04 KB
/
fuseLoop.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
<?xml version="1.0"?>
<article xmlns:xi="http://www.w3.org/2003/XInclude"
xmlns:r="http://www.r-project.org">
<articleinfo>
<title>Fusing Loops in R</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>
<title></title>
<para>
The goal is to compile expressions such as
<r:code eval="false">
Reduce(`+`, Map(log, Map(dnorm, x, mu, sigma)))
</r:code>
<r:code>
library(RLLVMCompile)
</r:code>
<r:function><![CDATA[
Dnorm <-
function(x, mu, sigma)
{
( 1.0/(sqrt(2 * pi) * sigma)) * exp( - .5 * ((x - mu)/sigma)^2)
}
]]></r:function>
<r:code eval="false">
d = compileFunction(Dnorm, DoubleType, list(DoubleType, DoubleType, DoubleType), .insertReturn = TRUE)
.llvmCallFunction(d, 2, 0, 3)
identical(.llvmCallFunction(d, 2, 0, 3), dnorm(2, 0, 3))
</r:code>
<r:function><![CDATA[
f = function(x, mu, sigma)
{
total = 0
for(val in x)
total = total + log(Dnorm(val, mu, sigma))
total
}
]]></r:function>
<r:function id="f2">
f2 = function(x, mu, sigma, len = length(x))
{
total = 0
for(i in 1:len) {
val = x[i]
total = total + log(Dnorm(val, mu, sigma))
}
total
}
</r:function>
<r:code id="compile">
mod = Module("fuse")
d = compileFunction(Dnorm, DoubleType, list(DoubleType, DoubleType, DoubleType), .insertReturn = TRUE, mod = mod, name = "Dnorm")
fc = compileFunction(f2, DoubleType, list(DoublePtrType, DoubleType, DoubleType, Int32Type), name = "f", mod = mod)
</r:code>
<r:code>
if(FALSE) {
# note tested!
mod = Module("fuse")
d = compileFunction(Dnorm, DoubleType, list(DoubleType, DoubleType, DoubleType), .insertReturn = TRUE, mod = mod)
fc = compileFunction(f, DoubleType, list(DoublePtrType, DoubleType, DoubleType), .insertReturn = TRUE, mod = mod)
}
</r:code>
<r:test>
x = rnorm(1e5)
a = .llvm(fc, x, 0, 1, length(x))
b = sum(log(dnorm(x)))
print(a - b, digits = 16)
</r:test>
<r:test>
x = rnorm(1e5) #XXXX 1e6 causes a segfault!!!
a = .llvm(fc, x, 0, 1, length(x))
# Note no check for NAs, etc. ....
tm.a = system.time(replicate(20, .llvm(fc, x, 0, 1, length(x))))
tm.b = system.time(replicate(20, sum(log(dnorm(x)))))
tm.a = replicate(50, system.time(.llvm(fc, x, 0, 1, length(x))))
tm.b = replicate(50, system.time(sum(log(dnorm(x)))))
res = structure(list(llvm = tm.a, r = tm.b), info = sessionInfo(), when = Sys.time())
save(res, file = "fuseLoop_osx.rda")
</r:test>
This does not give us the projected 3-fold speedup we expected.
However, it does show that we can compile R code to get equivalent
speedup
</para>
<para>
<r:test><![CDATA[
tm.loop = system.time(replicate(50, normalLogLik(x, 0, 1)))
tm.vector = system.time(replicate(50, sum(log(dnorm(x, 0, 1)))))
tm.reduceMap = system.time(replicate(50, Reduce(`+`, Map(log, Map(dnorm, x, MoreArgs = list(0, 1)))) ))
]]></r:test>
</para>
</section>
<section>
<title></title>
<para>
So let's look at creating an arbitrary
Reduce-Map sequence of operations, i.e.
a single Reduce call with one or more nested Map calls within it.
We'll use our example above
<r:code>
e = quote(Reduce(`+`, Map(log, Map(dnorm, x, mu, sigma), 2)))
</r:code>
<note><para>I have added the 2 for the base for the <r:var>log</r:var> call just
to make this more interesting. It should not be there for the actual computation.
</para></note>
We want to rewrite this into our loop form above given by <r:var>f2</r:var>
but with the general template
<r:function><![CDATA[
template =
function(x, len = length(x))
{
total = 0
for(i in 1:len) {
val = x[i]
total = total + mr
}
total
}
]]></r:function>
This is the general form for any Reduce-Map - at least one with + as the Reduce operation.
We need to rewrite our Map steps to a) compute the expression on the RHS (Right Hand Side) in the loop
and also identify the different inputs we need capture or allow the caller to provide as inputs to our function.
The Map steps are in <r:expr>e[[3]]</r:expr>.
We can loop over this sequence and capture the information with
<r:function id="getMapInfo"><![CDATA[
getMapInfo =
function(e)
{
ans = list()
while(!is.null(e) && is.call(e) && e[[1]] == "Map") {
tmp = list(func = e[[2]], args = as.list(e[-(1:3)]))
ans[[length(ans) + 1L]] = tmp
e = e[[3]]
}
ans
}
]]></r:function>
<r:code>
info = getMapInfo(e[[3]])
</r:code>
Now that we have this information we can construct the
call in our function to compute each element.
<r:function><![CDATA[
writeMapCall =
function(info)
{
cur = NULL
for(i in 1:length(info)) {
el = info[[i]]
k = makeCall(el$func, quote(val), el$args)
if(!is.null(cur))
cur[[2]] = k
else
cur = k
}
cur
}
]]></r:function>
Our helper function to create each call is defined as
<r:function><![CDATA[
makeCall =
function(func, val = NULL, args = list())
{
ans = substitute(f(), list(f = func))
if(length(val))
ans[[2]] = val
if(length(args))
ans[seq(along = args) + length(ans)] = args
ans
}
]]></r:function>
Note we don't deal with named arguments.
That is an entirely different issue.
</para>
<para>
We can call this with
<r:code>
writeMapCall(info)
</r:code>
and we get the call object
<r:output><![CDATA[
log(dnorm(x[i], mu, sigma), 2)
]]></r:output>
</para>
<para>
Now we can insert this into our template for our
loop fusion.
<r:code>
body(template)[[3]][[4]][[3]][[3]][[3]] = writeMapCall(info)
</r:code>
This is hideous indexing of the expressions in the body of
the function. We could do it more elegantly with some tree walking
and code rewriting functionality.
</para>
<para>
We can also now insert the names of our extra arguments/inputs,
i.e. <r:var>mu</r:var> and <r:var>sigma</r:var>.
<r:code>
args = unlist(lapply(info, function(x) x$args))
args = args[sapply(args, is.name)]
</r:code>
We can then insert these as formal parameters for <r:func>f2</r:func> with
<r:code>
fl = formals(template)
ids = names(fl)
tmp = fl[[2]]
for(i in seq(3, len = length(args)))
fl[[i-1]] = fl[[1]]
fl[[i]] = tmp
names(fl) = c(ids[1], sapply(args, as.character), ids[2])
formals(f2) = fl
</r:code>
</para>
<para>
Note that this doesn't deal with the case where
any of the extra arguments to the Map call are
calls and not just simple names of variables, i.e. symbols.
We can deal with this by compiling those expressions outside of the loop
and using intermediate variables for those.
</para>
<para>
So let's put this all together now.
<r:function><![CDATA[
makeReduceMap =
function(expr, templateFun = template)
{
info = getMapInfo(expr[[3]])
body(templateFun)[[3]][[4]][[3]][[3]][[3]] = writeMapCall(info)
args = unlist(lapply(info, function(x) x$args))
args = args[sapply(args, is.name)]
fl = formals(templateFun)
ids = names(fl)
tmp = fl[[2]]
for(i in seq(3, len = length(args)))
fl[[i-1]] = fl[[1]]
fl[[i]] = tmp
names(fl) = c(ids[1], sapply(args, as.character), ids[2])
formals(templateFun) = fl
structure(list(func = templateFun, functions = lapply(info, `[[`, "func"), args = args),
class = "ReduceMapFunction")
}
]]></r:function>
We can write code to compile this with
<r:function><![CDATA[
compileFunction.ReduceMapFunction =
function(fun, name, types, ...) # make same as compileFunction
{
mod = Module("fuse")
for(i in fun$functions)
compileFunction(i, mod = mod)
argTypes = list(DoublePtrType, types[fun$args], Int32Type)
fc = compileFunction(fun[[1]], DoubleType, argTypes, name = name, mod = mod)
}
]]></r:function>
</para>
<para>
Note that if/when we can do adequate type inference, we can
determine the types of the extra input parameters and compile the
entire block of code without requiring the R user to specify these.
</para>
<para>
Of course, there is no need to actually use the R language
to describe the operation as
<r:code eval="false">
Reduce(`+`, Map(f1, Map(f2, ....)))
</r:code>
We can describe this more directly in R. The above is just
syntax, a la Ross (Ihaka)!
</para>
</section>
<section eval="false">
<title>Vectorized Version of <r:func>Dnorm</r:func></title>
<para>
# Create a vectorized version of this.
<r:function><![CDATA[
Dnorm_v <-
function(x, len, mu, sigma, ans)
{
# apply(x, Dnorm, mu, sigma)
for(i in 1:len)
ans[i] = Dnorm(x[i], mu, sigma)
}
]]></r:function>
<r:code>
library(XML)
#xmlSourceFunctions("../Rllvm/explorations/fuseLoop.Rdb")
mod = Module("vec")
d = compileFunction(Dnorm, DoubleType, list(DoubleType, DoubleType, DoubleType), .insertReturn = TRUE, module = mod)
fc = compileFunction(Dnorm_v, VoidType, list(DoublePtrType, Int32Type, DoubleType, DoubleType, DoublePtrType), name = "Dnorm_v", .insertReturn = FALSE, module = mod)
</r:code>
</para>
<para>
x = rnorm(10)
ans = numeric(length(x))
ans = .llvm(fc, x, length(x), 0, 1, ans = ans, .all = TRUE)$ans
all.equal(dnorm(x), ans)
</para>
<para>
<r:function><![CDATA[
Dnorm_v <-
function(x, mu, sigma, ans)
{
for(i in seq(along = x))
ans[i] = Dnorm(x[i], mu, sigma)
}
]]></r:function>
</para>
</section>
</article>