Module Concurrent Programming
Transcrição
Module Concurrent Programming
Module Concurrent Programming 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 1 Module Concurrent Programming Lecturer: Contact: Daniel Kröni [email protected] Time: Room: Friday: 8:15-9:00 / 9:10-9:55 / 10:05-10:50 4.415 Course Material (Created by Prof. Dr. D. Gruntz and D. Kröni) Active Directory: E1862_Unterrichte_I\E1862_4Ia\conpr Web Page: http://web.fhnw.ch/plattformen/conpr/ Examination: 1 March 2012 27.04.2012 (Room 1.047) 9:00 - 10:30 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 2 The Free Lunch is Over!1 • Past 30 years – Clock speed – Execution optimization as pipelining / branch pred. (more work per cycle) – Caches • Now – Multicore – Hyperthreading – Cache But hyperthreading and multicore CPUs will have nearly no impact on most current applications ! 1 March 2012 1 http://www.gotw.ca/publications/concurrency-ddj.htm (C) Hochschule für Technik Fachhochschule Nordwestschweiz 3 Multicore CPUs are here! Quad Core! 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 4 Amdahl's Law • Formula to predict the maximum speedup using multiple processors (N) based on the proportion of parallelizable (p) and serial (1-p) component of a program. Speedup ≤ 1 (1-p) + p N • Speedup is limited by the time needed for the sequential fraction of the program All concurrent applications have some sources of serialization! 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 5 1 Amdahl's Law: Example (p = 0.5) 0.2 0.5 0.3 S= N=1 1 p N =1 (1 - 0.5) + 0.5 t 1 (1-p) + 0.25 N=2 0.2 0.3 S= 1 = 1.33 (1 - 0.5) + 0.25 t N=∞ 0.2 0.3 S= 1 =2 (1 - 0.5) + 0 t 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 6 1 Amdahl's Law p N Source: http://en.wikipedia.org/wiki/File:AmdahlsLaw.svg (1-p) + 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 7 Concurrent Programming Programs Concurrent Programs Parallel Programs 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 8 Concurrency: Omnipresence • Threads are everywhere – Even if a programmer does not explicitly create threads, frameworks may create threads, and code called from these threads must be thread-safe! • Examples in Java – Threads are used by every Java application GC-Thread Finalization-Thread – AWT/Swing applications: AWT-EventQueue Thread AWT-Windows Thread AWT-Shutdown Thread – Servlets: different requests are invoked by different threads – java.util.Timer 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 9 Consequences • Applications need to be concurrent if you want to exploit multicores – Existing applications will be redesigned • Good support of programming languages/libraries – Java > 1.5 [java.util.concurrent] Biased towards shared memory and the required coordination mechanisms – Special language concepts become mainstream Functional Programming (Immutability, side effect avoidance) Actor model (Erlang, Scala) Software transactions (STM: Clojure, Scala) The biggest sea change in software development since the OO revolution is knocking at the door, and its name is Concurrency. [Herb Sutter] 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 10 Risks / Problems • Safety hazards – Race conditions • Lifeness hazards @NotThreadSafe public class Counter { private int value; public int getNext(){ return value++; } } – Deadlock, Livelock, Starvation • Performance hazards – Over-Synchronization: Amdahl's Law – Context switches are not free: Saving and restoring execution context • Testing is not reliable – Scheduling is not deterministic • Debugging is often impossible – Heisenbugs (bug disappears when trying to observe it) 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 11 Goals • The Students … know typical problems and hassles in the context of concurrent programs … know and can apply abstract solution patterns for such problems … know different paradigms to solve concurrency problems such as actors and software transactions … know the memory model used by Java programs … can write correct concurrent programs 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 12 Module Concurrent Programming • Reference – Java Concurrency in Practice Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea – Addison-Wesley Professional (May 19, 2006) – ISBN-13: 978-0321349606 Writing correct programs is hard; writing correct concurrent programs is harder 1 March 2012 (C) Hochschule für Technik Fachhochschule Nordwestschweiz 13 Schedule 1 2 3 4 5 6 7 8 9 10 11 12 13 14 24.02.2012 02.03.2012 09.03.2012 16.03.2012 23.03.2012 30.03.2012 06.04.2012 13.04.2012 20.04.2012 27.04.2012 04.05.2012 11.05.2012 18.05.2012 25.05.2012 01.06.2012 08.06.2012 15.06.2012 1 March 2012 Intro, Threads Locks Condition Synchronization Java Memory Model Sharing objects Synchronizers Ostern Task based concurrency Non Blocking Algorithms / Atomics Prüfung Scala Intro Projektwoche Auffahrt Scala Concurrency Actor Model Software Transactional Memory Performance, Review, Questions (C) Hochschule für Technik Fachhochschule Nordwestschweiz 14