1 / 45

Programming with Concurrency: Threads, Actors and Coroutines

Programming with Concurrency: Threads, Actors and Coroutines. Zhen Li Eileen Kraemer EduPar 2013 Boston, Ma, USA. Motivation & Observations. Motivation

thom
Download Presentation

Programming with Concurrency: Threads, Actors and Coroutines

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. Programming with Concurrency:Threads, Actors and Coroutines Zhen Li Eileen Kraemer EduPar 2013 Boston, Ma, USA Computer Science Department, University of Georgia

  2. Motivation & Observations • Motivation • Users depend on the effectiveness, efficiency, and reliability of parallel and distributed computing that now permeates most computing activities • CS Education research and materials for teaching concurrency, particularly to undergraduates, are underdeveloped • Observations • In existing courses on concurrency, a disconnect often occurs between conceptual knowledge and practical skills • In the absence of hands-on practice, students do not retain the conceptual knowledge • Practical skills of interest to students: programming with concurrency constructs in Java, Scala and Python

  3. Principle & Implementation • Course Design Principle: • Provide repeated practice to support acquisition of integrated conceptual knowledge (concurrency constructs) and practical skills (programming languages) • Student Demographics: • Little prior knowledge of concurrency topics • Some level of familiarity in 1 to 2 programming languages • Implementation: • A scaffolding approach for the acquisition of a 2nd and 3rd programming language • A hands-on, practice-centered learning environment

  4. Course Design Concurrency 3rd Language Pseudocode 2nd Language 1st Language

  5. Elements of Teaching Models of Concurrency Approaches to Concurrency Classical problems in Concurrency Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  6. Models of Concurrency • Shared memory model • Single, unified memory image • Synchronization: different computing processes communicate through this shared memory • Message passing model • Private memory • Synchronization: different computing processes communicate through the exchange of messages Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  7. Approaches to Concurrency • Thread-based approach • Most operating systems (kernel level, user level, hybrid) • Java, C, C++, etc. • Actor-based approach • Web framework (LiftWeb, SOAP); Chat & messaging (Facebook, Twitter); • Scala, Erlang • Coroutine-based approach • Network library (Gevent) • Haskell, Python Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  8. Approaches to Concurrency Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  9. Classical Problems in Concurrency Multi-tasking (race condition) Conditional synchronization Deadlock & fairness Multiple issues combined Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  10. Classical Problems in Concurrency • Multi-tasking (race condition) • Ornamental garden • Sum and worker • Conditional synchronization • Deadlock & fairness • Multiple issues combined Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  11. Classical Problems in Concurrency • Multi-tasking (race condition) • Conditional synchronization • Bank account • Bounded buffer • Deadlock & fairness • Multiple issues combined Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  12. Classical Problems in Concurrency • Multi-tasking (race condition) • Conditional synchronization • Deadlock & fairness • Dining philosopher • Readers and writers • Multiple issues combined Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  13. Classical Problems in Concurrency • Multi-tasking (race condition) • Conditional synchronization • Deadlock & fairness • Multiple issues combined • Party matching • Sleeping barber • Book inventory • Single lane bridge Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  14. Content and Course Implementation

  15. Overview of Parallelism and Concurrency • 2 weeks • Content • Multi-core architecture • Two models of concurrency • Reading • Introduction to Parallel Computing<book> • Parallel Computer Architectures<book> • Multi-core Processors and Systems<book> • Assignment • Lab: Observing multi-core architecture’s performance • Homework: Survey on contemporary supercomputers Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  16. UML and Concurrency • 1.5 Weeks • Content • UML state and sequence diagrams (multi-media tutorial) • Mapping of UML diagram to C++ codes • Assignment • Lab: Modeling book inventory system as shared memory and message passing systems using UML

  17. Comprehension of Concurrency with Pseudocode • 4 weeks • Content • Introduction to pseudocode system • Concurrency concepts (race conditions, etc.) • Implementation details with pseudocode • Reading • Semaphore versus Mutex (online material) • MySQL bug reports (online material) • Assignment • Homework: pseudocode completion of dining philosopher and readers-writers problem • Lab: implement book inventory system with pseudocode

  18. Pseudocode System (original) Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  19. Pseudocode System (extended) Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  20. Pseudocode System (extended) Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  21. Pseudocode System (extended) Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  22. Pseudocode System (sample) Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  23. Midterm Exam • 1 week • Content • Comprehension of a concurrent system (single-lane bridge, expressed using pseudocode

  24. Midterm Exam Performance

  25. Concurrent Program Comprehension Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  26. General Misconception Hierarchy Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  27. Misconceptions aboutShared Memory Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  28. Misconceptions aboutMessage Passing Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  29. Misconceptions: the “fall back” phenomenon Extensive training  rare misconceptions on terminology level Large number of misconceptions on description level?  fall back from uncertainty Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  30. Implementation of Concurrency • 8 weeks • Content (flipped classroom) • Practice Java, Scala and Python without concurrency constructs • Implement concurrency with threads, actors and coroutines • Reading • Java API and concurrency tutorial • Scala API and actors tutorial • Python API, Google’s python class, and coroutine tutorial • Assignment • Lab: implement party-matching and sleeping barber with threads, actors and coroutines

  31. Final Exam • 0.5 week • Content • Implementation of concurrency (single-lane bridge) • Choose either threads, actors or coroutines approach to finish

  32. Related Curriculum Topics Covered • Flynn’s Taxonomy • Why and what is parallel/distributed computing • Concurrency • Non-determinism • Shared memory • Task/thread spawning • Language extensions • Tasks and threads • Synchronization • Critical regions • Producer-consumer • Monitors • Concurrency defects • Deadlocks • Data Races • Distributed Memory • Message passing • Functional/logic languages • Work stealing • Tools to detect concurrency defects Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  33. Survey Data • Surveys on effort and preferences were collected with each lab and homework assignments • Students consistently reported difficulties with shared memory systems • In homeworks 2 (shared memory) and 3 (message passing), students were asked to write pseudocode for the bounded-buffer and dining-philosopher problems discussed in class. In a survey conducted after homework 3, only 1 student indicated that message-passing is more difficult, and 10 indicated that shared memory is more difficult • In lab 2 (shared memory) and lab 3 (message passing) students were asked to design a book inventory system. In the post-lab survey, 8 of 11 students who responded indicated that shared memory is more difficult, 1 indicated that message passing is more difficult, and 2 students found the assignments equally difficult.

  34. Survey Data • Midterm exam on comprehending concurrency • 11 of the 15 students who responded indicated that questions in the shared memory section were harder to answer than those in the message passing section. • 10 of the 15 chose the message passing section as final graded part. Of the 5 students who chose the shared memory section, 4 took the shared memory portion in the 2nd session. • Of these 15 students, 13 chose correctly, in that they selected the section in which they actually scored higher. The 2 students who chose incorrectly chose the shared memory section but actually scored slightly higher on the message-passing section. Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  35. Conclusions • This class is challenging, especially to undergraduate students who have limited knowledge of concurrency and are inexperienced in programming. • Some students report time pressure on completing homework and lab projects. From the feedback of students who withdrew from the course, 2 of 3 expressed unmanageable course workload as their major reason for dropping • The pseudocode system is useful for students to comprehend and reason about concurrent systems, but it requires further refinements on wording and validation • A standard glossary of well-defined terminology is essential. • Shared memory is harder for students to understand, design, write pseudocode for, and reason about.

  36. Thanks! Questions?

  37. Monitor Pattern Data Object Class Function lock while condition is false wait execution unlock end function end class • Active Object Class • Run function • invoking data object class’s • functions • end run function • end class Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  38. class Buffer<T> { List<T> buf; int capacity, size; Buffer(int capacity) { this.capacity = capacity; buf = new ArrayList<T>(); size = 0; } void synchronized produce(T item) { while (size >= capacity) wait(); buf.add(item); size++; } T synchronized consume() { while (size <= 0) wait(); size--; return buf.remove(0); } } Monitor Pattern Bounded Buffer Buffer.java Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  39. class Producer<T> { Buffer<T> buffer; Producer(Buffer buffer) { this.buffer = buffer; } public void run() { buffer.produce((T)10); } } Monitor Pattern Bounded Buffer Producer.java Consumer.java class Consumer<T> { Buffer<T> buffer; Consumer(Buffer buffer) { this.buffer = buffer; } public void run() { T item = buffer.consume(); } } Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  40. React Pattern Data object class Receive message while condition is false delay processing processing End receive message End class Active object class Run function send messages End run function End class Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  41. class Buf[T](capacity:Int) extends Actor { varbuf:List[T] = List() def prod(m:T) { if (buf.size >= capacity) { self ! produce(m) } else { buf = buf.:+(m) } } React Pattern def cons(consumer:Actor) { if (buf.isEmpty) { self ! consume(consumer) } else { val m = buf.head buf = buf.tail consumer ! cargo(m) } } Bounded Buffer Buffer class def act() { loop { react { case produce(m:String, => prod(m) case consume(consumer:Actor) => cons(consumer) } } } } Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  42. class Producer[T](buffer:Actor) extends Actor { def prod() { buffer ! produce((T)10) } def act() { prod() } } class Consumer[T](buffer:Actor) extends Actor { def cons() { buffer ! consume(self); } def act() { cons() loop { react { case cargo(m:T) => m } } } } React Pattern Bounded Buffer Producer class Consumer class Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  43. Generator Pattern Data object class Function If condition is false return false Else processing return true End if End function End class Active object class Run function while invoking data object class’s function returns false yield yield End run function End class Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  44. class buf: def __init__(self, capacity): self.capacity = capacity self.items = [] def produce(self, item): if len(self.items) >= capacity: return false self.items.append(item) return true def consume(self): if len(self.items) <= 0: return false return self.items[0] Generator Pattern Bounded Buffer Buffer class Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

  45. def producer(buf): while True: while not buf.produce(10): yield yield def consumer(buf): while True: while not buf.consume(): yield yield Generator Pattern Bounded Buffer Producer generator Consumer generator Doctoral Prospectus by Zhen Li, Computer Science Department, University of Georgia

More Related