Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002.

Similar presentations


Presentation on theme: "1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002."— Presentation transcript:

1 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 24 The University of North Carolina at Chapel Hill

2 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming The previous lectures dealt with sequential programsThe previous lectures dealt with sequential programs –Programs with a single active execution context It is also possible to develop programs with more than one execution contextIt is also possible to develop programs with more than one execution context –They are concurrent programs For example,For example, –http://java.sun.com/docs/books/tutorial/essential/threads/ http://java.sun.com/docs/books/tutorial/essential/threads/ We will refer to an execution context as a threadWe will refer to an execution context as a thread

3 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming Motivation Capture the logical structure of the problemCapture the logical structure of the problem –E.g., servers and graphical applications Cope with independent physical devicesCope with independent physical devices –E.g., multiple processors in a real-time control system Execute a single program in more than one processorsExecute a single program in more than one processors –Parallel computing –E.g., scientific simulations such as weather prediction

4 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming Language Support Most languages support some sort of concurrent executionMost languages support some sort of concurrent execution –We will discuss a number of approaches The most important issues in concurrent programming areThe most important issues in concurrent programming are –Thread lifetime –Communication –Synchronization Let’s illustrate these concepts briefly with JavaLet’s illustrate these concepts briefly with Java

5 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming Java Threads We will follow the Java TutorialWe will follow the Java Tutorial –http://java.sun.com/docs/books/tutorial/essential/threads/ http://java.sun.com/docs/books/tutorial/essential/threads/ Threads are the units of execution context in JavaThreads are the units of execution context in Java Concurrent Program Sequential Program

6 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Thread Lifetime

7 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Creating Threads Extending Class Thread public class SimpleThread extends Thread { public SimpleThread(String str) { super(str);} public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); System.out.println(i + " " + getName()); try { try { sleep((long)(Math.random() * 1000)); sleep((long)(Math.random() * 1000)); } catch (InterruptedException e) {} } catch (InterruptedException e) {}} System.out.println("DONE! " + getName()); }}

8 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Creating Threads Implementing Interface Runnable SeeSee –http://java.sun.com/docs/books/tutorial/essential/threads/cl ock.html http://java.sun.com/docs/books/tutorial/essential/threads/cl ock.htmlhttp://java.sun.com/docs/books/tutorial/essential/threads/cl ock.html Java APIJava API –Thread »http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Thread.ht ml http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Thread.ht mlhttp://java.sun.com/products/jdk/1.2/docs/api/java/lang/Thread.ht ml –Runnable »http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Runnable. html http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Runnable. htmlhttp://java.sun.com/products/jdk/1.2/docs/api/java/lang/Runnable. html

9 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Thread Lifetime http://java.sun.com/docs/books/tutorial/essential/thre ads/lifecycle.htmlhttp://java.sun.com/docs/books/tutorial/essential/thre ads/lifecycle.htmlhttp://java.sun.com/docs/books/tutorial/essential/thre ads/lifecycle.htmlhttp://java.sun.com/docs/books/tutorial/essential/thre ads/lifecycle.html

10 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Co-begin Co-Begin (Algol-68, Occam, SR)Co-Begin (Algol-68, Occam, SR) –Sequential begin a := 3, b := 4 end –Concurrent par begin a := 3, b := 4 end

11 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Co-begin Co-BeginCo-Begin par begin p(a, b, c), begin d := q(e, f); d := q(e, f); r(d, g, h) r(d, g, h)end, s(i, j) end p(a, b, c) d:=q(e, f) r(d, g, h) s(i, j)

12 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parallel Loops Each iteration is executed concurrentlyEach iteration is executed concurrently –SR co (i := 5 to 10 -> p(a, b, i) oc –Occam par i = 5 for 6 p(a, b, i)

13 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parallel Loops FortranFortran –OpenMP !$OMP PARALLEL DO do i=1,n-1 A(i) = B(i) + C(i) enddo !$OMP END PARALLEL DO –HPF forall (i = 1:n-1) A(i) = B(i) + C(i) end forall

14 14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parallel Loops FortranFortran –HPF forall (i = 1:n-1) A(i) = B(i) + C(i) A(i + 1) = A(i) + A(i + 1) end forall Data dependency

15 15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Parallel Loops FortranFortran –HPF forall (i = 1:n-1) A(i) = B(i) + C(i) A(i + 1) = A(i) + A(i + 1) end forall Data dependencies make sequences of statements inherently sequentialData dependencies make sequences of statements inherently sequential –The two statement must execute sequentially –Updates to A(i+1) should not be seen by A(i) in the subsequent operation Data dependency

16 16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Launch-At-Elaboration AdaAda procedure P is task T is … end T; begin --P … end P;

17 17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Fork/Join In previous mechanisms, the creation of threads is implicit, while it is explicit in the fork-join modelsIn previous mechanisms, the creation of threads is implicit, while it is explicit in the fork-join models –The fork join model is more general –E.g., Ada task type T is …begin… end T; pt : access T := new T; Java threads follow the fork/join modelJava threads follow the fork/join model Forks a new thread

18 18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Fork/Join (a) Threads are always properly nested with co- begin statements(a) Threads are always properly nested with co- begin statements (b) The fork/join mechanism is more general(b) The fork/join mechanism is more general –In Java, »Fork by creating a Thread and executing start() »Join by executing join() on the thread

19 19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott –Ch. 12 intro –Ch. 12.2


Download ppt "1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 37: Beyond Sequential Programs COMP 144 Programming Language Concepts Spring 2002."

Similar presentations


Ads by Google