0% found this document useful (0 votes)
46 views22 pages

2008 ICFP DEFUN - Functional in The Field

The document discusses using functional programming concepts like streams and domain specific languages for processing sensor data from wildlife monitoring applications in an embedded context. It advocates for functional programming principles like type safety, polymorphism, and extracting common control patterns into functions. Two case studies are presented: using audio streams to locate marmots by processing direction of arrival estimates, and using computer vision techniques like background subtraction on video streams to classify birds. The author argues that a domain specific streaming language could help optimize and parallelize such applications while providing portability across platforms.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views22 pages

2008 ICFP DEFUN - Functional in The Field

The document discusses using functional programming concepts like streams and domain specific languages for processing sensor data from wildlife monitoring applications in an embedded context. It advocates for functional programming principles like type safety, polymorphism, and extracting common control patterns into functions. Two case studies are presented: using audio streams to locate marmots by processing direction of arrival estimates, and using computer vision techniques like background subtraction on video streams to classify birds. The author argues that a domain specific streaming language could help optimize and parallelize such applications while providing portability across platforms.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

+

Functional in the field


How we locate animals with functional programs

Ryan Newton
+
What I’m advocating: 3 things

App2:
App2: birds
birds
 Functional programming DetectEvent

 Type/memory safety
Audio0
 Polymorphism (generics)
Audio1
 Factoring control patterns, functions/closures
sync

Audio2

 Stream programming Audio3

 Well understood parallel paradigm


 As library/middleware, but better with compiler support

 Domain Specific Languages


 StreamIt, Brook, WaveScript App1:
App1: marmots
marmots
Streaming: similarities to functional
+
Audio Sensor Node
Detect
Input

Node
Locations sync4 Buffer N
>3sec?

Server Side Send AML Run Local Y


Output AML DOA

Server Side Send Raw


AML DOA Data

Event Pseudo-likelihood
association Map

Evaluation
Infrastructure,
Sensors Desktop/Server
Not TCP/IP

TinyOS

iPhone SDK
+
Brew Why stream languages
Android • A big mess of hardware
JavaME Phone
Symbian • No convenient way to write
programs that use all pieces
• Def. not high perf ones (Erlang, Java)
•No phone/pc p2p services
•Would require migrating comp.
+
Vision:
A language for a streaming era

 Cross platform and fast

Phone
 Convenience – automate what can be automated
 Network transparency, scheduling, distribution (sometimes)

 But recognize limits


 Compiler – can help but can’t do everything
 Parameterization, design exploration
+
S2 = iterate x in S1 { if (iseven(x))
{ emit x; emit x; }
else emit x; }
S3 = merge(S1,S2)

Execution Model
Execute!
•iterate – run code on each iterate
element

•One input element per S1 S2


execution, zero or more output
elements

• State – not shared merge

•One input, one output stream,


multiple subscribers

Must be of the same type


Lift into a union type…
Execution Model
+
Stream parallelism

 Explicit: between tasks

 Implicit: between consecutive stream elements


 Good when everything we need is in a contiguous sequence of stream
elements
 Best when data rates are known (synchronous dataflow)
+
Stream Graph Optimizations
Fusion

Fission / Replication
Must be
(inlining)
stateless!

Fission / Array building

All source to source transformations


Optimization
+ Case Study 1: Locating Marmots

•Gothic, CO deployment August 2007


•Voxnet Platform
• 2x PXA255, 64MB RAM, 8GB Flash,
802.11B, Mica2 supervisor, Li+ battery,
Charge controller
• Sensors: 4x48KHz audio, 3-axis accel,
GPS, Internal temp
+ AML DOA estimator

Meters

Meters

 Use local sub-arrays to estimate bearing to source


 “Beam crossing”
+ Combine DOA pseudo-likelihoods
to generate 2D likelihood map

100

104 105
110

Evaluation
Easy to move node/server boundary
+
Audio Sensor Node
Detect
Input

Node
Locations sync4 Buffer N
>3sec?

Server Side Send AML Run Local Y


Output AML DOA

Server Side Send Raw


AML DOA Data

Event Pseudo-likelihood
association Map
+
WaveScript Speedup

C WS Speedup
VoxNet DOA 3.00s 2.18s 1.38
VoxNet Detect 87.9% 56.5% Wins
Wins for
1.56 for this
this app:
app:

•• Portable
Portable HLL
HLL
LOC / WS LOC / ••C Network
Network transparent
transparent
Detector 92 252 •• Whole
Whole program
program compile
compile
DOA 124 239 •• Stream
Stream graph
graph opts
opts

 For this app: speedup in embedded with less code


in core algorithm implementation

Evaluation
+
Case Study 2: background subtraction
for bird classification
+
Case Study 2: background subtraction
for bird classification
+
Case Study 2: background subtraction
for bird classification
 Application in use by UCLA Sensing/Computer Vision
group
 Not experts in embedded computation
pixel

 Not parallel in obvious, streaming way


neighborhood
 Stateful – saves model around each pixel
 Parallel access to matrix – but overlapping tiles

 Not easy at all in, say, StreamIt

Per pixel stored state


+
Case Study 2: background subtraction
for bird classification
 Original code – massive duplication (1300 lines of C++)

 Step 1: port verbatim

 Step 2: factor duplicated code, use HOF

 Step 3: remove unnecessary floating point

 Step 4: parameterize design, expose parallelism


 (and further simplify in the process!)
+
Abstracting control patterns

 Map, fold (reduce) are cleaner than for loops

 Complex nested for loops


 Obligation for the block of code within:
use indices to find the data we need, and likewise
find where to put results, do computation

 Map/fold for relevant data structures


 Array.map(
Operate fun(x)..
fun(x)
on one data …at) a time
element for (i=0;i<n;i++) {
List.fold( fun(y) .. …
for (j=… pixel
 Better still: factor out relevant
Matrix.foreach( fun… control } for (k=…
structures
)))for your application … neighborhood

}
pixel_kernel(fun(nbrhd) { nbrhd(0,0) + nbrhd(1,1) }, strm)
pixel_kernel(fun(nbrhd,st) { … }, init_state, strm)
+
Case Study: background subtraction
application
 Original code – massive duplication (1300 lines of C++)

 Step 1: port verbatim

 Step 2: factor duplicated code, use HOF

 Step 3: remove unnecessary floating point

 Step 4: parameterize design, expose parallelism


 (and further simplify in the process!)
+
Parallelization

Image
+
Server side: Parallel Speedup

Wins
Wins for
for this
this app:
app:

•• Types
Types &
& higher
higher order
order
functions
functions enable
enable aa fully
fully
polymorphic
polymorphic parallel
parallel matrix
matrix
library
library

 Without stream graph opts – 60 threads, lock free fifos


Evaluation
+ Conclusion: FP enhances streaming
 Contributions:

 Reusable Libraries: Parallel matrix library

 WaveScope: compiler and runtime

 Contact: Ryan Newton - [email protected]

http://wavescope.csail.mit.edu

You might also like