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

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

2. Processes and Interactions 2.1 The Process Notion 2.2 Defining and Instantiating Processes –Precedence Relations –Implicit Process Creation –Dynamic.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Potential Languages of the Future Chapel,
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
May 2, 2015©2006 Craig Zilles1 (Easily) Exposing Thread-level Parallelism  Previously, we introduced Multi-Core Processors —and the (atomic) instructions.
1 Programming Explicit Thread-level Parallelism  As noted previously, the programmer must specify how to parallelize  But, want path of least effort.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 36: Global Optimization, Loop Improvement and Profiling COMP 144 Programming Language.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 4: Threads.
Introduction in algorithms and applications Introduction in algorithms and applications Parallel machines and architectures Parallel machines and architectures.
1 Tuesday, November 07, 2006 “If anything can go wrong, it will.” -Murphy’s Law.
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Object-Oriented Software Engineering Concurrent Programming.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 38: Implementing Concurrency COMP 144 Programming Language Concepts Spring 2002.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
Threads Just Java: C10–pages 251- C11–pages 275-
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
– 1 – Basic Machine Independent Performance Optimizations Topics Load balancing (review, already discussed) In the context of OpenMP notation Performance.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Parallel Programming in Java with Shared Memory Directives.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Lecture 8: OpenMP. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism / Implicit parallelism.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
MLS 10/18/ Don’t Start with Dekker’s Algorithm: Top-Down Introduction of Concurrency Michael L. Scott Multicore Programming Education Workshop 8.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
Work Replication with Parallel Region #pragma omp parallel { for ( j=0; j
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Parallel Programming. Introduction Idea has been around since 1960’s –pseudo parallel systems on multiprogram-able computers True parallelism –Many processors.
1 Parallel Programming Aaron Bloomfield CS 415 Fall 2005.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
University of Washington What is parallel processing? Spring 2014 Wrap-up When can we execute things in parallel? Parallelism: Use extra resources to solve.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Parallel Programming 0024 Week 10 Thomas Gross Spring Semester 2010 May 20, 2010.
1. 2 Pipelining vs. Parallel processing  In both cases, multiple “things” processed by multiple “functional units” Pipelining: each thing is broken into.
Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: Anders Västberg Phone:
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Page :Algorithms in the Real World Parallelism: Lecture 1 Nested parallelism Cost model Parallel techniques and algorithms
CS/EE 217 GPU Architecture and Parallel Programming Lecture 23: Introduction to OpenACC.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
University of Washington Today Quick review? Parallelism Wrap-up 
Programming Language Principles Lecture 29 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Concurrent Programming.
Special Topics in Computer Engineering OpenMP* Essentials * Open Multi-Processing.
CPE779: More on OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
University of Washington 1 What is parallel processing? When can we execute things in parallel? Parallelism: Use extra resources to solve a problem faster.
Lecture 16: Introduction to Data Types
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Computer Engg, IIT(BHU)
Chapter 4: Threads & Concurrency
Representation and Management of Data on the Internet
Gentle Introduction to Threads
CMSC 202 Threads.
Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of.
Presentation transcript:

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 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, – We will refer to an execution context as a threadWe will refer to an execution context as a thread

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 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 COMP 144 Programming Language Concepts Felix Hernandez-Campos Concurrent Programming Java Threads We will follow the Java TutorialWe will follow the Java Tutorial – Threads are the units of execution context in JavaThreads are the units of execution context in Java Concurrent Program Sequential Program

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

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

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Thread Lifetime 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 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 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 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 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 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 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 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 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 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 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott –Ch. 12 intro –Ch. 12.2