Docsity

MSBD5009 lecture 2 parallel computing, Lecture notes of Mathematics

Partial preview of the text

Download MSBD5009 lecture 2 parallel computing and more Lecture notes Mathematics in PDF only on Docsity!

Parallel Programming

Parallel Software Slides adapted from the lecture notes by Peter Pacheco

Roadmap

  • Parallel software
  • Input and output
  • Performance
  • Parallel program design
  • Writing and running parallel programs
  • Concluding Remarks

The burden is on software

  • Hardware and compilers can keep up the pace needed for parallelism.
  • How does parallel software work?
    • In shared memory programs:
      • Start a single process and fork threads.
      • Threads carry out tasks.
    • In distributed memory programs:
      • Start multiple processes.
      • Processes carry out tasks.

SPMD – single program multiple data

  • A SPMD program consists of a single executable that can behave as if it were multiple different programs through the use of conditional branches. if (I’m thread/process i) do this; else do that;

Shared Memory

  • Dynamic threads
    • Master thread waits for work, forks new threads, and when threads are done, they terminate
    • Efficient use of resources, but thread creation and termination is time consuming.
  • Static threads
    • Pool of threads created and are allocated work, but do not terminate until cleanup.
    • Better performance, but potential waste of system resources. !"#$% &'()* + ),-.#/ ),-.#/ +^ 122(

Nondeterminism

printf ( "Thread %d > my_val = %d\n" , my_rank , my_x ) ;

... Thread 0 > my_val = 7 Thread 1 > my_val = 19 Thread 1 > my_val = 19 Thread 0 > my_val = 7 The same input can result in different output.

Critical Section

  • Programmer’s job to ensure mutually exclusive access to the critical section - Mutual exclusion lock (mutex, or simply lock) my_val = Compute_val ( my_rank ) ; Lock(&add_my_val_lock ) ; x += my_val ; Unlock(&add_my_val_lock ) ; A block of code that can only be executed by one thread at a time.

busy-waiting

my_val = Compute_val ( my_rank ) ; i f ( my_rank == 1) whi l e (! ok_for_1 ) ; /* Busy−wait loop / x += my_val ; / Critical section / i f ( my_rank == 0) ok_for_1 = true ; / Let thread 1 update x */

Notes on the message-passing

program

  • It is SPMD. The two processes are using the same executable.
  • The variable message refers to memory blocks in different processes.
  • Process 0 is able to write to stdout.
  • Typically both Send and Receive may block until the message is sent/received.
  • There are other MPI API functions.

Input and Output

  • In distributed memory programs, only process 0 will access stdin. In shared memory programs, only the master thread or thread 0 will access stdin.
  • In both distributed memory and shared memory programs all the processes/threads can access stdout and stderr.

Input and Output

  • Only a single process/thread will attempt to access any single file other than stdin , stdout , or stderr. So, for example, each process/thread can open its own, private file for reading or writing, but no two processes/threads will open the same file.

PERFORMANCE

Efficiency of a parallel program

E =

Tserial Tparallel

S

p

p

Tserial

p T^. parallel

Speedup and efficiency example