1 / 30

David Evans cs.virginia/~evans

Lecture 22: Abstractions for Concurrency. 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.

iren
Download Presentation

David Evans cs.virginia/~evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 22: Abstractions for Concurrency 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. David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science

  2. Menu • Form going around • Signup for Project Presentations • Vote for Next Lecture (no cheating!) • Abstractions for Concurrency • Algol 68 • Monitors • Linda and JavaSpaces CS 655: Lecture 21

  3. Last Time • Concurrent programming is programming with partial ordering on time • A concurrent programming language gives programmers mechanisms for expressing that partial order • We can express many partial orders using the thread control primitives fork and join and locking primitives protect, acquire and release. CS 655: Lecture 21

  4. Abstractions • Programming at that low level would be a pain – are there better abstractions? • Hundreds of attempts...we’ll see a few today. • Issues • Thread creation • Thread synchronization • Resource contention fork join protect, acquire, release CS 655: Lecture 21

  5. Algol 68: Collateral Clauses • Collateral Clauses stmt0; (stmt1, stmt2) stmt3; Defines a partial order: stmt0 stmt1 stmt2 stmt3 CS 655: Lecture 21

  6. Algol 68: Semaphores • Dijkstra, “Cooperating Sequential Processes” • type sema up – increments down – decrements (must > 0 before) CS 655: Lecture 21

  7. Semaphore Example begin sema mutex := level 1; proc producer while not finished do down mutex ... insert item up mutex od; CS 655: Lecture 21

  8. Semaphore Example, cont. proc consumer while not finished do down mutex ... remove item up mutex od; par (producer, consumer) // start them in parallel CS 655: Lecture 21

  9. What can go wrong? • Programmer neglects to up semaphore • Programmer neglects to down semaphore before accessing shared resource • Programmer spends all her time worrying about up and down instead of the algorithm CS 655: Lecture 21

  10. Monitors • Concurrent Pascal [Hansen 74], Modula [Wirth 77], Mesa [Lampson80] • Integrated data abstraction and resource synchronization • Routines that use a shared resource grouped in a monitor, accesses only allowed through exported procedures • Conditions can control when threads may execute exported procedures CS 655: Lecture 21

  11. Monitor Example monitor boundedbuffer buffer: array 0..N-1 of int; count: 0..N; nonempty, nonfull: condition; procedure append (x: int) if count = N then nonfull.wait; buffer[count] := x; count := count + 1; nonempty.signal end append Example adapted from [Hansen93] CS 655: Lecture 21

  12. Monitor Example, cont. procedure remove () returns portion if count = 0 then nonempty.wait; x := ... nonfull.signal end remove; CS 655: Lecture 21

  13. Java Synchronization • synchronized method qualifier • Once a synchronized method begins execution, it will complete before any other thread enters a method of the same object • Run-time must associate a lock with every object • Is this enough to implement a semaphore? • Is this better/worse than monitors? CS 655: Lecture 21

  14. Synchronized Example class ProducerConsumer { private int x = 1; synchronized void produce () { x = x + 1; } synchronized void consume () { x = x – 1; } } How could we require x stay positive? CS 655: Lecture 21

  15. 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) CS 655: Lecture 21

  16. 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 CS 655: Lecture 21

  17. 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! CS 655: Lecture 21

  18. Tuples CS 655: Lecture 21

  19. 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) CS 655: Lecture 21

  20. Meaning of take Tuple Space take (“f”, int n) take (“f”, 23) take (“t”, bool b, int n) take (string s, int n) take (“cookie”) (“f”, 23) (“t”, 25) (“t”, true) (“t”, false) (“f”, 17) CS 655: Lecture 21

  21. 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 CS 655: Lecture 21

  22. 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); CS 655: Lecture 21

  23. Semaphore • Create (int n, String resource) for (i = 0; i < n; i++) out (resource); • Down (String resource) take (resource) • Up (String resource) out (resource) CS 655: Lecture 21

  24. Distributed Ebay • Offer Item (String item, int minbid, int time): out (item, minbid, “owner”); sleep (time); 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) How could a bidder cheat? CS 655: Lecture 21

  25. 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); Eventually, tuple space contains one entry which is the answer. What if last two elements are taken concurrently? Better way to order Setup? CS 655: Lecture 21

  26. 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! CS 655: Lecture 21

  27. 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); CS 655: Lecture 21

  28. 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]. • Task: Devise a Linda sorting program and analyze its performance (can you match MergeSort?) CS 655: Lecture 21

  29. 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. CS 655: Lecture 21

  30. Charge • You can download JavaSpaces implementation from: http://java.sun.com/products/javaspaces/ • Project presentations for next week – advice for them Thursday • Projects are due 2 weeks from today CS 655: Lecture 21

More Related