ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2...

54
ISBN 0-321-33025-0 Chapter 13 Concurrency
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2...

Page 1: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

ISBN 0-321-33025-0

Chapter 13

Concurrency

Page 2: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 2

Chapter 13 Topics

• Introduction• Introduction to Subprogram-Level

Concurrency• Semaphores• Monitors• Message Passing• Java Threads• C# Threads• Statement-Level Concurrency

Page 3: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 3

Introduction

• Concurrency can occur at four levels:– Machine instruction level - 341– High-level language statement level– Unit level– Program level - 443

• Because there are no language issues in machine instruction and program-level concurrency, they are not addressed here

Page 4: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 4

Why use concurrent execution?

Some problem domains lead naturally to concurrency

• Often used in simulation• Also used sometimes in event-driven

systems• Multiple-processor computers currently

being used

Page 5: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Categories of Concurrency

• Physical concurrency – execute on more than one processor

• Logical concurrency – execute in interleaved fashion on one processor

• From programmer’s perspective, physical and logical concurrency can be treated the same

• Thread of control – sequence of program points reached as control flows through a program

• Multithreaded – program designed to have more than one thread of control

• Quasi-concurrent – coroutines (chapter 9)

Page 6: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 6

Introduction to Subprogram-Level Concurrency• A task or process is a program unit that can be in

concurrent execution with other program units• Tasks differ from ordinary subprograms in that:

– A task may be implicitly started (not called)– When a program unit starts the execution of a task, it

may continue to execute (normally when a subprogram is called, execution of the caller is suspended)

– When a task’s execution is completed, control may not return to the caller

• Tasks usually work together

Astarts

B

Page 7: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 7

Two General Categories of Tasks

• Heavyweight tasks execute in their own address space and have their own run-time stacks

• Lightweight tasks all run in the same address space and use the same run-time stack. Easier to implement.

• Tasks can communicate via shared nonlocal variables, message passing or parameters.

• A task is disjoint if it does not communicate with or affect the execution of any other task in the program in any way

A B

A stack/mem B stack/mem

Heavyweight

A & B

shared stack/mem

Lightweight

Page 8: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 8

Task Synchronization

• A mechanism that controls the order in which tasks execute

• Two kinds of synchronization– Cooperation synchronization– Competition synchronization

Page 9: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 9

Kinds of synchronization

• Cooperation: Task A must wait for task B to complete some specific activity before task A can continue its execution. Often one task creates a resource another task needs e.g., the producer-consumer problem.

• Competition: Two or more tasks must use some resource that cannot be simultaneously used, e.g., a shared resource such as a counter– Competition is usually provided by mutually exclusive

access (approaches are discussed later)

Page 10: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 10

Need for Competition Synchronization

Each task must: fetch TOTAL, perform arithmetic op, replace TOTAL

Result could be: 8 (correct), 4, 6, or 7 (incorrect?). Called a race condition

Page 11: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Brainstorm

• How do you identify race conditions?• What mechanisms would you use to

prevent race conditions?

Page 12: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Mutually Exclusive Access

• General idea: consider the resource to be shared as something that a task can possess

• Provide appropriate mechanisms for task to acquire and release that resource

Page 13: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 13

Scheduler

• Providing synchronization requires a mechanism for managing tasks, including delaying task execution when required

• The scheduler maps task execution onto available processors depending on the state of the task

• Must have algorithm for selecting task to run when several are ready

Page 14: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 14

Task Execution States

• New - created but not yet started• Runnable or Ready - ready to run but

not currently running (no available processor)

• Running – currently executing• Blocked - has been running, but

cannot now continue (usually waiting for some event to occur, e.g., I/O)

• Dead - no longer active in any sense, execution complete or killed

Page 15: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 15

Liveness and Deadlock

• Liveness is a characteristic that a program unit may or may not have

• In sequential code, it means the unit will eventually complete its execution

• In a concurrent environment, liveness means if some event is supposed to occur it eventually will (continual progress)

• If all tasks in a concurrent environment lose their liveness, it is called deadlock

Page 16: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 16

Methods of Providing Synchronization

• Semaphores• Monitors• Message Passing

Page 17: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 17

Semaphores

• Dijkstra - 1965• A semaphore is a data structure consisting of a counter and

a queue for storing task descriptors (e.g., for waiting tasks)• Semaphores can be used to implement guards around the

code that accesses shared data structures. Guards execute when specified condition is true. Must ensure that all attempted executions eventually take place.

• Semaphores have only two operations, wait and release (originally called P and V by Dijkstra)

• Semaphores can be used to provide both competition and cooperation synchronization

Guard

counter

queue

Page 18: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 18

Example: Cooperation Synchronization with Semaphores

• Example: A shared buffer• The buffer is implemented as an ADT with the

operations DEPOSIT and FETCH as the only ways to access the buffer

• Use two semaphores for cooperation: emptyspots and fullspots

• The semaphore counters are used to store the numbers of empty spots and full spots in the buffer

these are not the semaphore ops!

Page 19: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 19

Cooperation Synchronization with Semaphores (continued)

• DEPOSIT must first check emptyspots to see if there is room in the buffer

• If there is room, the counter of emptyspots is decremented and the value is inserted

• If there is no room, the caller is stored in the queue of emptyspots

• When DEPOSIT is finished, it must increment the counter of fullspots

Page 20: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 20

Cooperation Synchronization with Semaphores (continued)• FETCH must first check fullspots to see if

there is a value– If there is a full spot, the counter of fullspots

is decremented and the value is removed– If there are no values in the buffer, the caller

must be placed in the queue of fullspots – When FETCH is finished, it increments the

counter of emptyspots

• The operations of FETCH and DEPOSIT on the semaphores are accomplished through two semaphore operations named wait and release

Page 21: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 21

Producer Consumer Code

semaphore fullspots, emptyspots;Fullspots.count = 0;emptyspots.count = BUFLEN;task producer;

loop-- produce VALUE –-wait (emptyspots); {wait for space}DEPOSIT(VALUE);release(fullspots); {increase filled}end loop;

end producer;

Page 22: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 22

Semaphores: Wait Operation

// wait will be called when a task wants the // resource that is guarded by this semaphore

wait(aSemaphore)if aSemaphore’s counter > 0 then decrement aSemaphore’s counterelse put the caller in aSemaphore’s queue attempt to transfer control to a ready task -- if the task ready queue is empty, -- deadlock occurs end

Page 23: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 23

Semaphores: Release Operation

// release will be called when a task is finished// with the resource guarded by this semaphore

release(aSemaphore)if aSemaphore’s queue is empty then increment aSemaphore’s counterelse put the calling task in the task ready queue transfer control to a task from aSemaphore’s queueend

Page 24: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 24

Producer Consumer Code

task consumer;loopwait (fullspots);{wait till not empty}}FETCH(VALUE);release(emptyspots); {increase empty}-- consume VALUE –-end loop;

end consumer;

Page 25: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Exercise

Do the producer-consumer trace exercise

Page 26: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 26

Competition Synchronization with Semaphores

• A third semaphore, named access, is used to control access (competition synchronization, mutually exclusive access to buffer)– The counter of access will only have the values

0 and 1– Such a semaphore is called a binary

semaphore• Note that wait and release must be atomic! A

semaphore is a shared object, so operations on semaphores must not be interrupted.

Page 27: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 27

Competition Code

semaphore access, fullspots, emptyspots;access.count = 0;fullspots.count = 0;emptyspots.count = BUFLEN;task producer;

loop-- produce VALUE –-wait(emptyspots); {wait for space}wait(access); {wait for access)DEPOSIT(VALUE);release(access); {relinquish access}release(fullspots); {increase filled}end loop;

end producer;

Page 28: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 28

Producer Consumer Code

task consumer;loopwait(fullspots);{wait till not empty}wait(access); {wait for access}FETCH(VALUE);release(access); {relinquish access}release(emptyspots); {increase empty}-- consume VALUE –-end loop;

end consumer;

Page 29: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 29

Evaluation of Semaphores

• Misuse of semaphores can cause failures in cooperation synchronization, e.g., the buffer will overflow if the wait of fullspots is left out, deadlock results if either task forgets to release

• Misuse of semaphores can cause failures in competition synchronization, e.g., the program will deadlock if the release of access is left out

• “The semaphore is an elegant synchronization tool for an ideal programmer who never makes mistakes.” (Brinch Hansen, 1973)

Page 30: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 30

Monitors

• Concurrent Pascal, Modula, Mesa, Ada, Java, C#

• The idea: encapsulate the shared data and its operations to restrict access

• A monitor is an abstract data type for shared data

Page 31: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 31

Competition Synchronization

• Shared data is resident in the monitor (rather than in the client units)

• All access resident in the monitor– Monitor implementation guarantees

synchronized access by allowing only one access at a time

– Calls to monitor procedures are implicitly queued if the monitor is busy at the time of the call

Page 32: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 32

Cooperation Synchronization

• Cooperation between processes is still a programming task– Programmer must guarantee that a shared

buffer does not experience underflow or overflow

Page 33: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 33

Evaluation of Monitors

• A better way to provide competition synchronization than are semaphores

• Semaphores can be used to implement monitors

• Monitors can be used to implement semaphores

• Support for cooperation synchronization is very similar as with semaphores, so it has the same problems

Page 34: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 34

Message Passing

• Message passing is a general model for concurrency– It can model both semaphores and monitors– It is not just for competition synchronization

• Central idea: task communication is like seeing a doctor--most of the time she waits for you or you wait for her, but when you are both ready, you get together, or rendezvous

Page 35: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 35

Message Passing Rendezvous

• A mechanism to allow a task to indicate when it is willing to accept messages

• Tasks need a way to remember who is waiting to have its message accepted and some “fair” way of choosing the next message (e.g., guarded commands)

• When a sender task’s message is accepted by a receiver task, the actual message transmission is called a rendezvous

MPI covered in CSCI Parallel Computing course

Page 36: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 36

Java Threads

• The concurrent units in Java are methods named run• The run method code can be in concurrent execution with

other such methods• The process (i.e., Object) in which the run methods

execute is called a thread• Tasks are lightweight• Two ways to define a class with a run method:

– Define a subclass of the Thread class and override its run method

– Inherit from some other class and implement the Runnable interface. Still requires a Thread object.

Page 37: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 37

Thread classClass myThread extends Threadpublic void run () {…}}…Thread myTh = new MyThread ();myTh.start();

• run is always overridden by subclasses

• start initializes a thread and calls its run method. Control returns immediately to the caller, in parallel with the new thread.

• main runs in its own thread.

• Threads are often run in round-robin fashion, if same priority (depends on implementation)

Page 38: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 38

Controlling Thread Execution

• The Thread class has several methods to control the execution of threads– The yield is a request from the running thread to

voluntarily surrender the processor. It goes in the ready queue, so it can run again if no other threads are ready.

– The sleep method can be used by the caller of the method to block the thread for a specified period of time (minimum amount of time… depends on other threads)

– The join method is used to force a method to delay its execution until the run method of another thread has completed its execution. Can specify a max time to avoid deadlock.

Page 39: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Deprecated methods

• stop, suspend and resume• Deprecated for safety reasons• So, how does a thread end?

– interrupt method tells thread to stop, but not complete solution because thread may be sleeping or waiting

– InterruptedException causes thread to awaken

Page 40: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 40

Thread Priorities

• A thread’s default priority is the same as the thread that create it– If main creates a thread, its default priority is NORM_PRIORITY (usually 5)

• Threads defined two other priority constants, MAX_PRIORITY and MIN_PRIORITY

• The priority of a thread can be changed with the methods setPriority

• A thread with lower priority will only run if no threads with higher priority are on ready queue

Page 41: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 41

Competition Synchronization with Java Threads• A method that includes the synchronized

modifier disallows any other method from running on the object while it is in execution…public synchronized void deposit( int i) {…}public synchronized int fetch() {…}…

• The above two methods are synchronized which prevents them from interfering with each other (i.e., they “lock” the object)

• If only a part of a method deals with shared data, it can be synchronized thru synchronized statementSynchronized (expression) statement

Page 42: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 42

Cooperation Synchronization with Java Threads

• Cooperation synchronization in Java is achieved via wait, notify, and notifyAll methods– All methods are defined in Object, which is

the root class in Java, so all objects inherit them

• The wait method must be called in a loop• The notify method is called to tell one

waiting thread that the event it was waiting has happened

• The notifyAll method awakens all of the threads on the object’s wait list

Page 43: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 43

Java’s Thread Evaluation

• Java’s support for concurrency is relatively simple but effective

• Not as powerful as Ada’s tasks (not covered)

Page 44: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

Exercise

• Do the Java Producer-Consumer Exercise

Page 45: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 45

C# Threads

• Loosely based on Java but significant differences• Basic thread operations

– Any method can run in its own thread– A thread is created by creating a Thread object– Creating a thread does not start its concurrent

execution; it must be requested through the Start method

– A thread can be made to wait for another thread to finish with Join

– A thread can be suspended with Sleep– A thread can be terminated with Abort

Page 46: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 46

Synchronizing Threads

• Three ways to synchronize C# threads– The Interlock class

• Used when the only operations that need to be synchronized are incrementing or decrementing of an integer

– The lock statement• Used to mark a critical section of code in a thread

lock (expression) {… }

– The Monitor class• Provides four methods that can be used to provide

more sophisticated synchronization

Page 47: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 47

C#’s Concurrency Evaluation

• An advance over Java threads, e.g., any method can run its own thread

• Thread termination cleaner than in Java• Synchronization is more sophisticated

Page 48: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 48

Statement-Level Concurrency

• Objective: Provide a mechanism that the programmer can use to inform compiler of ways it can map the program onto multiprocessor architecture

• Minimize communication among processors and the memories of the other processors

Page 49: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 49

High-Performance Fortran

• A collection of extensions that allow the programmer to provide information to the compiler to help it optimize code for multiprocessor computers

• Specify:– the number of processors, – the distribution of data over the memories of

those processors, and – the alignment of data

• Specifications appear as special comments

Page 50: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 50

Primary HPF Specifications

• Number of processors!HPF$ PROCESSORS procs (n)

• Distribution of data !HPF$ DISTRIBUTE (kind) ONTO procs ::

identifier_list– kind can be BLOCK (distribute data to

processors in blocks) or CYCLIC (distribute data to processors one element at a time)

• Relate the distribution of one array with that of anotherALIGN array1_element WITH array2_element

Page 51: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved.

HPFS Specification

• Array with 500 elements named LIST with BLOCK distribution on 5 processors

• Array with CYCLIC distribution

0-99100-199

200-299

300-399

400-499

05

10. . .

16

11. . .

27

12. . .

38

13. . .

49

14. . .

Page 52: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 52

Statement-Level Concurrency Example

REAL list_1(1000), list_2(1000)

INTEGER list_3(500), list_4(501)

!HPF$ PROCESSORS proc (10)

!HPF$ DISTRIBUTE (BLOCK) ONTO procs ::

list_1, list_2

!HPF$ ALIGN list_1(index) WITH

list_4 (index+1)

list_1 (index) = list_2(index)

list_3(index) = list_4(index+1)

Page 53: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 53

Statement-Level Concurrency (continued)

• FORALL statement is used to specify a list of statements that may be executed concurrently

FORALL (index = 1:1000)

list_1(index) = list_2(index)

• Specifies that all 1,000 RHSs of the assignments can be evaluated before any assignment takes place

Page 54: ISBN 0-321-33025-0 Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.

Copyright © 2006 Addison-Wesley. All rights reserved. 54

Summary

• Concurrent execution can be at the instruction, statement, or subprogram level

• Physical concurrency: when multiple processors are used to execute concurrent units

• Logical concurrency: concurrent united are executed on a single processor

• Two primary facilities to support subprogram concurrency: competition synchronization and cooperation synchronization

• Mechanisms: semaphores, monitors, rendezvous, threads

• High-Performance Fortran provides statements for specifying how data is to be distributed over the memory units connected to multiple processors