Lecture 20: Linda and JavaSpaces

advertisement
Lecture 20:
Linda and
JavaSpaces
When you have a world-wide tuple space, you’ll be able
to tune it in from any computer anywhere – or from any
quasi-computer: any cell phone, any TV, any toaster.
David Gelernter’s introduction to
JavaSpaces Principles, Patterns, and Practice.
CS655: Programming Languages
David Evans
University of Virginia
http://www.cs.virginia.edu/~evans
Computer Science
Menu
•
•
•
•
Manifest Today: on line only
Jinze’s Proof
Linda and JavaSpaces
Programming using Linda
27 July 2016
University of Virginia CS 655
2
Linda
• Program Concurrency by using
uncoupled processes with shared data
space
• Add concurrency into a sequential
language by adding:
– Simple operators
– Runtime kernel (language-independent)
– Preprocessor (or compiler)
27 July 2016
University of Virginia CS 655
3
Design by Taking Away
• Backus: von Neumann bottleneck results
from having a store
– Remove the store  Functional Languages
• Gelernter: distributed programming is hard
because of inter-process scheduling and
communication due to order of mutation
– We don’t have to remove the store, just mutation
– Remove mutation  read-and-remove only store
 tuple spaces
27 July 2016
University of Virginia CS 655
4
Basic Idea
• Have a shared space (“tuple space”)
– Processes can add, read, and take away
values from this space
• Bag of processes, each looks for work it
can do by matching values in the tuple
space
• Get load balancing, synchronization,
messaging, etc. for free!
Warning: not a good strategy for
managing your project team.
27 July 2016
University of Virginia CS 655
5
Tuples
Conventional
Memory
Linda/JavaSpaces
Unit
Bit
Logical Tuple
(23, “test”, false)
Access Using
Address (variable)
Selection of values
Operations
read, write
read, add, remove
JavaSpaces:
read, write, take
immutable
27 July 2016
University of Virginia CS 655
6
Tuple Space Operations
• out (t) – add tuple t to tuple space
• take (s)  t – returns and removes
tuple t matching template s
• read (s)  t – same as in, except
doesn’t remove t.
• Operations are atomic (even if space is
distributed)
27 July 2016
University of Virginia CS 655
7
Meaning of take
take (“f”, int n)
Tuple Space
take (“f”, 23)
(“f”, 23)
take (“t”, bool b, int n)
take (string s, int n)
(“t”, 25)
(“t”, true)
(“t”, false)
(“f”, 17)
take (“cookie”)
27 July 2016
University of Virginia CS 655
8
Operational Semantics
• Extend configurations with a tuple space (just
a bag of tuples)
• Transition rule for out:
– Just add an entry to the tuple space
• Transition rule for take:
– If there is a match (ignoring binding):
• Remove it from the tuple space
• Advance the thread
– Similar to join last time – it just waits if there is no
match
27 July 2016
University of Virginia CS 655
9
Shared Assignment
Loc := Expression
take (“Loc”, formal loc_value);
out (“Loc”, Expression);
e.g.:
x := x + 1;
 take (“x”, formal x_value)
out (“x”, x_value + 1);
27 July 2016
University of Virginia CS 655
10
Semaphore
• Create (int n, String resource)
for (i = 0; i < n; i++) out (resource);
• Down (String resource)
take (resource)
• Up (String resource)
out (resource)
27 July 2016
University of Virginia CS 655
11
Distributed Ebay
• Offer Item (String item, int minbid, int
timeout):
out (item, minbid, “owner”);
sleep (timeout);
take (item, formal bid, formal bidder);
if (bidder  “owner”) SOLD!
• Bid (String bidder, String item, int bid):
take (item, formal highbid, formal highbidder);
if (bid > highbid) out (item, bid, bidder)
else out (item, highbid, highbidder)
27 July 2016
University of Virginia CS 655
12
Factorial
Setup:
for (int i = 1; i <= n; i++) out (i);
start FactTask (replicated n-1 times)
FactTask:
take (int i); take (int j); out (i * j);
What is last two elements are taken concurrently?
Eventually, tuple space contains one
entry which is the answer.
Better way to order Setup?
27 July 2016
University of Virginia CS 655
13
Finishing Factorial
Setup:
for (int i = 1; i <= n; i++) out (i);
out (“workleft”, n - 1);
take (“workleft”, 0);
take (result);
FactTask:
take (“workleft”, formal w);
if (w > 0)
take (int i); take (int j); out (i * j);
out (“workleft”, w – 1);
endif;
Opps – we’ve sequentialized it!
27 July 2016
University of Virginia CS 655
14
Concurrent Finishing Factorial
Setup:
start FactWorker (replicated n-1 times)
out (“done”, 0);
for (int i = 1; i <= n; i++) {
out (i); if i > 1 out (“work”); }
take (“done”, n-1);
take (result);
FactWorker:
take (“work”);
take (formal int i); take (formal int j); out (i * j);
take (“done”, formal int n); out (“done”, n + 1);
27 July 2016
University of Virginia CS 655
15
Sorting in Linda
• Problem: Sorting an array of n integers
• Initial tuple state: (“A”, [A[0], ..., A[n-1]])
• Final tuple state: (“A”, [A’[0], ..., A’[n-1]])
such A’ has a corresponding element for every
element in A, and
for all 0 <= j < k <= n-1, A’[j] <= A’[k].
• In your project groups: devise a Linda
sorting program and analyze its
performance (can you match MergeSort?)
27 July 2016
University of Virginia CS 655
16
Summary
• Linda/JavaSpaces provides a simple, but
powerful model for distributed computing
• JavaSpaces extends Linda with:
– Leases (tuples that expire after a time limit)
• Implementing an efficient, scalable tuple
space (that provides the correct global
semantics) is hard; people have designed
custom hardware to do this.
27 July 2016
University of Virginia CS 655
17
Charge
• You can download JavaSpaces
implementation from:
http://java.sun.com/products/javaspaces/
• Next time: Aspect-Oriented Programming
– Abstract out things that cross-cut objects or
procedures
• Projects are due 2 weeks from tomorrow
27 July 2016
University of Virginia CS 655
18
Download