8/8/2019 2008 ICFP DEFUN - Functional in the Field
1/22
+
Functional in thefieldHow we locate animals with functionalprograms
Ryan Newton
8/8/2019 2008 ICFP DEFUN - Functional in the Field
2/22
+
App2: birds
What Im advocating: 3 things
n Functional programming
n Type/memory safety
n Polymorphism (generics)
n Factoring control patterns, functions/closures
n Stream programming
n Well understood parallel paradigm
n As library/middleware, but better with compiler support
n Domain Specific Languages
n StreamIt, Brook, WaveScript
Audio0
Audi
o1
Audio2
Audio3
DetectEvent
sy
nc
App1: marmots
8/8/2019 2008 ICFP DEFUN - Functional in the Field
3/22
Evaluation
+Streaming: similarities to functional
A u d ioIn p u t Detect
erverSideML DOA
e n d R a wD a ta
Eventassociation-seudolikelihoodMap
B u ffe r> 3se c?
un LocalML DOA
N
e n d A M LO u tp u t Y
ensor Node
erver Side
N o d eLo ca tio n s sn4
8/8/2019 2008 ICFP DEFUN - Functional in the Field
4/22
+ Why streamlanguages A big mess of hardware
No convenient way to writeprograms that use all pieces
Def. not high perf ones (Erlang,Java)
No phone/pc p2p services
Would re uire mi ratin
/D esktop Serve r
P h o n
e
/TC
PI
P
,In fra stru ctu reSensors
N o t/TC P IP
Ja v a M
E S y m b ia n
B re
w
iP h o n eS D K
A n d ro i
d
Tin y O
S
8/8/2019 2008 ICFP DEFUN - Functional in the Field
5/22
+Vision:A language for a streaming era
n Cross platform andfastn
n Convenience automate what can be automatedn Network transparency, scheduling, distribution
(sometimes)
n But recognize limitsn Compiler can help but cant do everything
n Parameterization, design exploration
P h o n
e
8/8/2019 2008 ICFP DEFUN - Functional in the Field
6/22
Execution Model
+
Execute!
Execution Model
iterate run code oneach element
One input element perexecution, zero or moreoutput elements
State not shared
One input, one outputstream, multiplesubscribers
ite rate
m erg e
= { ( ( ))S2 iterate xin S 1 if iseve n x{ ;emit x emit
; }x; }e lse e m it x= ( , )S 3 m e rg e S 1 S 2
S 1 S 2
M ust b e of the sam e typ e
Lift in to a u n io n ty p e
8/8/2019 2008 ICFP DEFUN - Functional in the Field
7/22
+Stream parallelism
n Explicit: between tasks
n Implicit: between consecutive stream elements
n Good when everything we need is in a contiguoussequence of stream elements
n Best when data rates are known (synchronous dataflow)
8/8/2019 2008 ICFP DEFUN - Functional in the Field
8/22
Optimization
+Stream Graph Optimizations
Fu sio n
/Fissio n R e p lica tio n
( )inliningM ust b e
!sta te le ss
/Fissio n A rra y b u ild in g
A ll so u rce to so u rce tra n sfo rm a tio n s
8/8/2019 2008 ICFP DEFUN - Functional in the Field
9/22
+ Case Study 1: LocatingMarmots
,G o th ic C O d e p lo y m e n t A u g u s t 2 0 0 7 V ox n e t P la tfo rm
, ,2 x P X A 2 5 5 6 4 M B R A M 8 G B, . ,Flash 80 2 11 B M ica2
, + ,sup e rvisor Li b a tte ryC h a rg e co n tro lle r
: , -S e n so rs 4 x 4 8 K H z a u d io 3, ,a xis acce l G PS In te rn a lte m
8/8/2019 2008 ICFP DEFUN - Functional in the Field
10/22
8/8/2019 2008 ICFP DEFUN - Functional in the Field
11/22
Evaluation
+Combine DOA pseudo-likelihoodsto generate 2D likelihood map
100
104
110105
8/8/2019 2008 ICFP DEFUN - Functional in the Field
12/22
+ Easy to move node/server boundary
A u d ioIn p u t Detect
erverSideML DOA
e n d R a wD a ta
Eventassociation-seudolikelihoodMap
B u ffe r> 3se c?
un LocalML DOA
N
e n d A M LO u tp u t Y
ensor Node
erver Side
N o d eLo ca tio n s sn4
8/8/2019 2008 ICFP DEFUN - Functional in the Field
13/22
Evaluation
+WaveScript Speedup
nFor this app: speedup in embeddedwith less code in core algorithmimplementation
C WS Speedup
VoxNetDOA
3.00s 2.18s 1.38
VoxNetDetect
87.9% 56.5% 1.56
LOC / WS LOC / C
Detector 92 252
DOA 124 239
:W in s fo r th is a p p
Po rta b le H LL N etw ork transparent W h o le p ro g ra m co m p ile
S tre am g rap h op ts
8/8/2019 2008 ICFP DEFUN - Functional in the Field
14/22
+Case Study 2: backgroundsubtraction for birdclassification
8/8/2019 2008 ICFP DEFUN - Functional in the Field
15/22
+Case Study 2: backgroundsubtraction for birdclassification
8/8/2019 2008 ICFP DEFUN - Functional in the Field
16/22
+Case Study 2: backgroundsubtraction for birdclassificationn Application in use by UCLA Sensing/Computer
Vision groupn Not experts in embedded computation
n Not parallel in obvious, streaming wayn Stateful saves model around each pixel
nParallel access to matrix but overlapping tilesn
n Not easy at all in, say, StreamIt
p ixe l
n eig h b o rh o od
Pe r p ixe l sto re d sta te
8/8/2019 2008 ICFP DEFUN - Functional in the Field
17/22
+Case Study 2: backgroundsubtraction for birdclassificationn Original code massive duplication (1300 lines of C++)
n Step 1: port verbatim
n Step 2: factor duplicated code, use HOF
n Step 3: remove unnecessary floating point
n Step 4: parameterize design, expose parallelism
n(and further simplify in the process!)
8/8/2019 2008 ICFP DEFUN - Functional in the Field
18/22
+Abstracting control patterns
n Map, fold (reduce) are cleaner than for loops
n Complex nested for loops
n Obligation for the block of code within:use indices to find the data we need, and likewise
find where to put results, do computation
n Map/fold for relevant data structures
n Operate on one data element at a time
nBetter still: factor out relevant controlstructures for your application
pixel
neighborhood
( = ; < ; ++)for i 0 i n i{
( =for j ( =for k
}
. ( ( )..Array map fun x. ( ( ) ..List fold fun y
. ( Matrix foreach fun)))
( = ; < ; ++) {for i 0 i n i
}
. ( ( ) )Array map fun x
(pixel_kernel fun( ) { ( , ) + ( , ) }, )nbrhd nbrhd 0 0 nbrhd 1 1 strm(pixel_kernel fun( , ) { }, , )nbrhd st init_state strm
8/8/2019 2008 ICFP DEFUN - Functional in the Field
19/22
+Case Study: backgroundsubtraction application
n Original code massive duplication (1300 lines of C++)
n Step 1: port verbatim
n Step 2: factor duplicated code, use HOF
n Step 3: remove unnecessary floating point
n Step 4: parameterize design, expose parallelism
n
(and further simplify in the process!)
8/8/2019 2008 ICFP DEFUN - Functional in the Field
20/22
+
Image
Parallelization
8/8/2019 2008 ICFP DEFUN - Functional in the Field
21/22
Evaluation
+Server side: Parallel Speedup
nWithoutstream graph opts 60 threads, lock free fifos
:W in s fo r th is a p p
&Typ e s h ig h e r o rd e r fu n ctio n s e n a b le a fu lly p o ly m o r
8/8/2019 2008 ICFP DEFUN - Functional in the Field
22/22
+ Conclusion: FP enhancesstreaming Contributions:
Reusable Libraries: Parallel matrix library
WaveScope: compiler and runtime
Contact: Ryan Newton - [email protected]
:// . . .h ttp w a ve scop e csailm it ed u