400 likes | 505 Views
Region Streams. Functional Macroprogramming for Sensor Networks. Ryan Newton MIT CSAIL newton@mit.edu. Matt Welsh Harvard University mdw@eecs.harvard.edu. Tracking a vehicle. Tracking many vehicles. Programming Sensornets is HARD. Message passing is painful
E N D
Region Streams Functional Macroprogramming for Sensor Networks Ryan Newton MIT CSAIL newton@mit.edu Matt Welsh Harvard University mdw@eecs.harvard.edu
Programming Sensornets is HARD • Message passing is painful • Programming global behavior at the local level • Life at the node level is messy • Low level hardware and communication details • Limited node resources (cpu, memory) • Lost messages and failed nodes
Macroprogramming Vision • The programmer thinks in terms of the whole network, manually translates into node programs. • Can some of this translation be automated by a tool or compiler? • An example: TinyDB • Declarative, global-level queries • No message passing or node details • Limited to certain kinds of data gathering
Compile Down
Regiment’s Purpose Provide a rich set of (fully composable) operators to work with data distributed over time and space.
Regiment Programming Model • Data Model: Sensor network data represented as streams of tuples, one stream per node. • Regions group streams • Map allows data parallel computation across a region • Fold aggregates the streams in a region
Simple and Derived Regions • Simple Regions, created from geometric or radio relationships: • K-hop-neighborhood • K-nearest nodes • All nodes within circle (square, etc) • All nodes in network (“world”) • Derived Regions, built from other regions • Union, Intersection • Map, Filter • (Many other library procedures: border, sparsify, cluster, etc)
Relationship to SQL queries SELECT nodeid,light,temp FROM sensors WHERE light > 400 SAMPLE PERIOD 1024 let r1 = filter (\n -> read LIGHT n > 400) world r2 = map (\n -> (read ID n, read LIGHT n, read TEMP n)) r1 in set_freq 1024 r2
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node = (read PROXIMITY node, get_location node) in centroid (filter aboveThresh (map select world))
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node = (read PROXIMITY node, get_location node) in centroid (filter aboveThresh (map select world))
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node = (read PROXIMITY node, get_location node) in centroid (filter aboveThresh (map select world))
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) in centroid (filter aboveThresh (map select world))
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) in centroid (filter aboveThresh (map select world))
Tracking a vehicle in Regiment let abovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) incentroid(filter aboveThresh (map select world))
Tracking a vehicle in Regiment letabovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) incentroid(filter aboveThresh (map select world))
Multi-target Tracking letabovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) selected = filter aboveThresh (map select world) in centroid selected
letabovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) selected = filter aboveThresh (map select world) globs = cluster selected targets = map centroid globs intargets Multi-target Tracking
letabovethresh (p,x) = p > threshold select node=(read PROXIMITY node, get_location node) selected = filter aboveThresh (map select world) globs = cluster selected targets = map centroid globs sentries = map read (border world) event = whenAny abovethresh sentries inuntil event nullArea targets Tracking with sentries
Current Compiler Status • Compiles to Token Machines • Node level state machine exposing “tokens” for region membership • Whole program analysis on token machines enables optimization • Token machines currently run in simulation • Soon will compile to NesC/TinyOS
Future work, Research challenges • Finish prototype, evaluate on real motes • Provide effective controls over stream rates and energy usage • Can adaptive runtimes reduce energy usage • Attack specific problem domains to uncover useful domain primitives • Understand general global-to-local compilation and optimization strategies • Preliminary work; would love your feedback!
End Ryan Newton MIT CSAIL newton@mit.edu Matt Welsh Harvard University mdw@eecs.harvard.edu Region Streams: Functional Macroprogramming for Sensor Networks
With what glue?: The language • General purpose purely functional macroprogramming language • Uses monads, Functional Reactive Programming to encapsulate Regions/Streams safely • Has Conditionals • Has user defined functions • BUT: No arbitrary depth recursion • User defined procedures inline at compile time • Control flow is known, programs terminate • Thus Regiment has no stack/heap at runtime • But, procedures stick around in argument position
Lazy (call-by-need) semantics • Regiment is a lazy functional language; values are computed when they’re needed. • If you only use only part of a data structure, compute only that part. • We might not want to compute a region at all places and times. • Consider the intersection of contiguous regions • Don’t think of region operations as strict, imperative commands. • Instead, they declare relationships.
Compilation Strategy • Safe ADT for Regions and Streams • Only interface through map/fold/filter • Can reason about time/space properties of code with type system • Source level rewrite optimizations based on algebraic properties • Program analysis to recognize efficient communication patterns • analyze region contiguity, area, diameter
Token Machines • Token handlers are like a functions whose last call is cached • Can be broadcast to neighbors as well as called locally • Each region has some place(s) from which its formed • Formation and membership tokens • Compiler’s place analysis
Full Tracking Program let abovethresh (p,x)= p > threshold read node =(read_sensor PROXIMITY node, get_location node) in centroid (afilter aboveThresh (amap read world)) centroid area = divide (afold accum (0,0) area) accum (wsum, xsum) (w,x) = (w + wsum, x*w + xsum) dividestream = smap (\(w,x)-> x/w) stream
Core types in Regiment typeStream 'a ≈≈ (Time -> 'a) typeSpace 'a ≈≈ (Location -> Multiset'a) typeEvent 'a ≈≈ (Time, 'a) typeArea'a =Stream(Space 'a) typeRegion=AreaNodeSnapshot typeAnchor=SignalNodeSnapshot smap :: ('a ->'b) ->Signal 'a ->Signal 'b amap :: ('a ->'b) -> Area 'a ->Area 'b afold :: ('a ->'b ->'a) ->'a ->Area‘b ->Signal ('a)
Anchor Free Localization anchorOptimizing ls = let compare x y = let loop comps = case comps of { []->True; -- better break ties somehow h:t | h x y -> true; _:t | loop t } in loop ls in anchorWhere (\_ -> True) compare
Anchor Free Localization between a b =(\ x y -> abs (dist_from a x - dist_from b x) > abs (dist_from a y - dist_from b y)) awayfrom a = (\ x y -> dist_from a x > dist_from a y) awayfrom2 a b = (\ x y -> abs (dist_from a x + dist_from b x) > abs (dist_from a y + dist_from b y)) n0 = anchorOptimizing [ ] n1 = anchorOptimizing [awayfrom n0] n2 = anchorOptimizing [awayfrom n1] n3 = anchorOptimizing [between n1 n2, awayfrom2 n1 n2] n4 = anchorOptimizing [between n1 n2, awayfrom n3] n5 = anchorOptimizing [between n1 n2, between n3 n4]