Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial •...

53
11/4/2009 1 Parallel Programming Concepts Jon Johansson AICT University of Alberta AICT , University of Alberta Copyright 2009, University of Alberta 1 Parallel Programming, Nov. 4, 2009 Help!!! the people who can help port, compile, debug, and run your parallel programs: support@westgrid.ca Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 2

Transcript of Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial •...

Page 1: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

1

Parallel Programming Concepts

Jon JohanssonAICT University of AlbertaAICT, University of Alberta

Copyright 2009, University of Alberta 1Parallel Programming, Nov. 4, 2009

Help!!!

• the people who can help port, compile, debug, and run your parallel programs:

[email protected] @ g

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 2

Page 2: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

2

Serial Programming

• von Neumann architecture executes a program serially (one instruction at a time)serially (one instruction at a time)

• to speed up we can:– optimize the program

– move to the fastest hardware

– change the algorithms

• it’s difficult to speed up a well‐written program

• the program is generally straightforward to write

Copyright 2009, University of Alberta 3Parallel Programming, Nov. 4, 2009

It’s Not Easy to Make it Fast

"Sequential programming is really hard, and parallel programming is a step beyond that “parallel programming is a step beyond that.– Andrew Tanenbaum, quoted at the June 2008 Usenix

conference

MINIX 3MINIX 3:http://www.minix3.org/

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 4

Page 3: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

3

What and How to Program??parallel programming environments

parallel processing hardware

OpenMP, MPI, HPF, TBB, Erlang, Shmem, Ct, CUDA, DirectCompute, OpenCL, Portals, ZPL, BSP, CHARM++, Cilk, Co‐array Fortran, PVM, Pthreads, Windows threads, Tstreams GA Java UPC

GPGPU

x86 Cluster

Tstreams, GA, Java, UPC, Titanium, Parlog, NESL, Split‐C, etc.

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 5

Cell processor

FPGA

A Big Job in the Kitchen

• what does the chef do when faced with a bigwhen faced with a big job?

• too many meals for one cook to prepare in the given time

• split the work over a• split the work over a team of cooks

Copyright 2009, University of Alberta 6Parallel Programming, Nov. 4, 2009

Page 4: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

4

A Big Job in the Kitchen

• try to split the work into pieces that can be donepieces that can be done by different people

• distribute the work to the team members

• we can organize the workers and resourcesworkers and resources in a couple a ways

Copyright 2009, University of Alberta 7Parallel Programming, Nov. 4, 2009

A Big Job in the Kitchen

• put all the chefs in a common environment

• all the resources are available to all the chefs

• large pile of food and utensils

• some resources are limitedth d t i• they need to organize sharing– take turns with the only 

knife

Copyright 2009, University of Alberta 8Parallel Programming, Nov. 4, 2009

Page 5: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

5

A Big Job in the Kitchen

• chefs can communicate with messages on spacewith messages on space they can all see

Copyright 2009, University of Alberta 9Parallel Programming, Nov. 4, 2009

A Big Job in the Kitchen

• give each chef access to his own resourceshis own resources

• everyone has his own set of food, utensils and ovens

• each chef has only enough resources to doenough resources to do his task

Copyright 2009, University of Alberta 10Parallel Programming, Nov. 4, 2009

Page 6: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

6

A Big Job in the Kitchen

• to communicate use a phone to coordinatephone to coordinate finishing the meal at the same time

• appetizer needs to be done and served before main course beforemain course, before dessert

Copyright 2009, University of Alberta 11Parallel Programming, Nov. 4, 2009

Dividing the Tasks

• some of the work can be done in parallelbe done in parallel

• some work can only be done serially

• some work needs to be completed before other work can continue

• consider a team of chefs with one person to clear and serve

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 12

Page 7: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

7

Why Parallel Programming?

• would like to use a number of processors at th ti t d l l tithe same time to speed up calculations

• the problem must be broken into parts that can be solved concurrently

• each part of the problem becomes a program to run on its own processorto run on its own processor

• programming becomes more complex

Copyright 2009, University of Alberta 13Parallel Programming, Nov. 4, 2009

Why Parallel Programming?

• if we can use many CPUs efficiently, we canrun simulations faster– run simulations faster

– increase problem sizes– run simulations at greater accuracy

• run a program on a cpu that can provide 1 gigaflop/s (109 flop/s)

• if you need 1 teraflop/s (1012 flop/s) to finish the• if you need 1 teraflop/s (10 flop/s) to finish the calculation in a reasonable time you can use 1000 cpus– you need to use them efficiently!

Copyright 2009, University of Alberta 14Parallel Programming, Nov. 4, 2009

Page 8: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

8

Terminology

• parallel computing – harnessing a bunch of th hi tprocessors on the same machine to run your 

computer program

• note that this is onemachine

• generally a homogeneous architecturesame processors memory operating system– same processors, memory, operating system

• all the machines in the Top 500 are in this category

Copyright 2009, University of Alberta 15Parallel Programming, Nov. 4, 2009

Terminology• cluster: 

– a set of generally homogeneous machines– originally built using low‐cost commodity– originally built using low‐cost commodity 

hardware• to increase density, clusters are now commonly built with 1‐u rack servers or blades

– can use standard network interconnect or high performance interconnect such as Infiniband or Myrinet

l h d i b i i• cluster hardware is becoming quite specialized

– thought of as a single machine with a name, e.g. “checkers” – checkers.westgrid.ca

Copyright 2009, University of Alberta 16Parallel Programming, Nov. 4, 2009

Page 9: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

9

Types of ParallelismTask parallelism Data parallelism

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 17

Different programs operate on copies of the same data‐ functional decomposition

Copies of the same program operate on different data‐ domain decomposition

Types of Parallelism

• consider 3 tasks that take equal times to D D2 D3take equal times to complete: T1, T2, T3

• and a bunch of data to run the tasks on

• we break the data into 9 equal sized pieces

D1 D2 D3

D4 D5 D6

D7 D8 D9

T1 T2 T3

• there are 27 time units of work to be done in this situation

time

9 equal sized pieces– each task operating on a chunk of data takes 1 unit of time

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 18

situation• for example:

• T1 →multiply by 5• T2 → apply sin(x)• T3 → take square root

Page 10: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

10

Types of ParallelismTask parallelism Data parallelism

D1 D2 D31 2 3

D4 D5 D6

D7 D8 D9

D1 D2 D3

D4 D5 D6

D7 D8 D9

D D D

D1 D2 D3

D4 D5 D6

D7 D8 D9

time

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 19

• either tasks are concurrent or data is crunched concurrently

• processing time is now 9 time units

D1 D2 D3

D4 D5 D6

D7 D8 D9

time

time

Pipelining

• we can mix the parallelism

D1

T T T

D2 D3

parallelism

• a task processes some data and then passes the data on to the next task

• total time to process all

T1 T2 T3

D1

T1 T2 T3

D2 D3 D4

D1

T1 T2 T3

D2 D3 D4 D5

• total time to process all the data is 11 time units

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 20

1 2 3

T1 T2 T3

D6 D7 D8 D9

time

Page 11: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

11

Domain Decomposition

• data can be decomposed in 1 Ddecomposed in different ways

• depends on how you want to schedule the work

• finer grain may make l d b l i i

1 D

2 D

load balancing easier• depends on the problem

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 21

Processes and threads

• A process is created by the operating system, and requires a fair amount of "overhead". 

• Processes contain information about program resources and program execution state, including: – Process ID, process group ID, user ID, and group ID – Environment – Program instructions – Registers , Stack , Heap – Inter‐process communication tools (such as message queues, p ( g q ,

pipes, semaphores, or shared memory). • a thread is defined as an independent stream of 

instructions that can be scheduled to run as such by the operating system

Copyright 2009, University of Alberta 22Parallel Programming, Nov. 4, 2009

Page 12: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

12

A serial program runs a single thread

• a computer program exists as an executable fil tfile on your computer

• a process is an instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently.p p g y

Copyright 2009, University of Alberta 23Parallel Programming, Nov. 4, 2009

Parallel Processing Categories

• we can broadly classify parallel processing into these categories:these categories:– Thread Parallelism – use a single process with multiple threads to perform a set of tasks

• OpenMP, Pthreads (shared memory for communication)

– Process Parallelism – use more than one process to perform a set of tasks

MPI ( i f i ti )• MPI (message passing for communication)

– Hybrid Parallelism – use multiple processes, some of which can have multiple threads

• MPI + OpenMP

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 24

Page 13: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

13

Operating systems

• an Operating System(OS) is an interface(OS) is an interface between the computer hardware and the user

• the operating system manages and coordinates the sharingcoordinates the sharing of the computer’s resources

Copyright 2009, University of Alberta 25Parallel Programming, Nov. 4, 2009

Architectures• Symmetric 

Multiprocessing (SMP)– Uniform Memory Access 

(UMA)– multiple CPUs, residing in 

one cabinet, share the same memory

– processors and memory are tightly coupled

– the processors share

Copyright 2009, University of Alberta

the processors share memory and the I/O bus or data path

26Parallel Programming, Nov. 4, 2009

Page 14: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

14

Architectures

• SMP– a single copy of the 

operating system is in charge of all the processors

– SMP systems range from two to as many as 32 or more processors

Copyright 2009, University of Alberta 27Parallel Programming, Nov. 4, 2009

Architectures

• SMPSMP

• "capability computing"– one CPU can use all the 

memory

– all the CPUs can work on a little memory

• whatever you need

Copyright 2009, University of Alberta

y

28Parallel Programming, Nov. 4, 2009

Page 15: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

15

Architectures

• UMA‐SMP negatives– as the number of CPUs get large the buses become saturated

– long wires cause latency problems

Copyright 2009, University of Alberta 29Parallel Programming, Nov. 4, 2009

Architectures

• Non‐Uniform Memory Access (NUMA)– NUMA is similar to SMP ‐multiple CPUs share a single memory spacep g y p

• hardware support for shared memory – memory is separated into close and distant banks

• basically a cluster of SMPs– memory on the same processor board as the CPU (local memory) is 

accessed faster than memory on other processor boards (shared memory)

• hence "non‐uniform"

Copyright 2009, University of Alberta

– NUMA architecture scales much better to higher numbers of CPUs than SMP

30Parallel Programming, Nov. 4, 2009

Page 16: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

16

Architectures

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 31

Architectures

Copyright 2009, University of Alberta

University of Alberta SGI Origin SGI NUMA cables

32Parallel Programming, Nov. 4, 2009

Page 17: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

17

Architectures

• Cache Coherent NUMA (ccNUMA)• each CPU has an associated cacheeach CPU has an associated cache• ccNUMA machines use special‐purpose hardware to maintain 

cache coherence – typically done by using inter‐processor communication between cache 

controllers to keep a consistent memory image when the same memory location is stored in more than one cache 

– ccNUMA performs poorly when multiple processors attempt to access the same memory area in rapid succession 

Copyright 2009, University of Alberta

y p

33Parallel Programming, Nov. 4, 2009

ArchitecturesDistributed Memory 

Multiprocessor (DMMP)h t h it• each computer has its own 

memory address space• looks like NUMA but there is 

no hardware support for remote memory access

– the special purpose switched network is replaced by a general purpose network such as Ethernet or more specialized interconnects: 

• Infiniband

Copyright 2009, University of Alberta

• Infiniband• Myrinet

• all computer clusters look like this now

• Lattice was decommissioned on Nov. 3, 2009 

Lattice: Calgary’s HP ES40 and ES45 cluster – each node has 4 processors

34Parallel Programming, Nov. 4, 2009

Page 18: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

18

Architectures

• Massively Parallel Processing (MPP) Cluster of commodity PCs– processors and memory are loosely coupledp y y p– "capacity computing"– each CPU contains its own memory and copy of the operating system 

and application. – each subsystem communicates with the others via a high‐speed 

interconnect.– in order to use MPP effectively, a problem must be breakable into 

pieces that can all be solved simultaneously

Copyright 2009, University of Alberta 35Parallel Programming, Nov. 4, 2009

Architectures

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 36

Page 19: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

19

Today’s Common System Architecture

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 37

It’s up to this network to create a single memory space for an SMP, otherwise it’s just a cluster

Architectures

• lots of “how to build a cluster” tutorials on the b j t G lweb – just Google:

• http://www.beowulf.org/

• http://www.cacr.caltech.edu/beowulf/tutorial/building.html

Copyright 2009, University of Alberta 38Parallel Programming, Nov. 4, 2009

Page 20: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

20

Parallel Programming

• need to do something to your program to use multiple processorsmultiple processors

• need to incorporate commands into your program which allow multiple threads to run

• one thread per processor• each thread gets a piece of the work

Copyright 2009, University of Alberta

• several ways (APIs) to do this … 

39Parallel Programming, Nov. 4, 2009

OpenMP

• Open Multi‐Processing (OpenMP)• introduce statements into your code that look like commentsintroduce statements into your code that look like comments 

to a serial compiler– in C:  #pragma– in FORTRAN:  C$OMP or   !$OMP

• can compile serial and parallel executables from the same source code

• restricted to shared memory machines

Copyright 2009, University of Alberta

– not clusters!

• www.openmp.org

40Parallel Programming, Nov. 4, 2009

Page 21: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

21

Threads

• a thread is defined as an independent stream f i t ti th t b h d l d tof instructions that can be scheduled to run as 

such by the operating system– this means that executing threads can be started and stopped by the OS

– threads can move from one CPU to another while executing

• a process can contain many threads

Copyright 2009, University of Alberta 41Parallel Programming, Nov. 4, 2009

How to See The Threads?

• Windows Task Manager on a 4‐core systemon a 4 core system

• “spinparallel.exe”

• OMP_NUM_THREADS=2

• OMP_NUM_THREADS=3

• OMP_NUM_THREADS=4

Copyright 2009, University of Alberta 42Parallel Programming, Nov. 4, 2009

Page 22: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

22

How to See The Threads?• use “Top” on the cluster: “spinparallel”• toggle views with “shift‐h” i.e. “H”

process view

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 43

thread viewOMP_NUM_THREADS=4

To see threads on the Cluster

• use the command:

ps ‐eLf | grep <your_program_name>

Copyright 2009, University of Alberta 44Parallel Programming, Nov. 4, 2009

Page 23: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

23

OpenMP Threads

• OpenMP uses the “fork‐join” model of paralleljoin  model of parallel execution

• all OpenMP programs begin as a single process: the master threadh h h d• when the master thread 

encounters a parallel region it creates a teamof parallel threads 

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 45

OpenMP Threads

• to create a parallel region in OpenMP useregion in OpenMP use the pragma (in C):#pragma omp parallel{ // code here}}

• the block of code in the parallel region is run by all threads

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 46

Page 24: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

24

Compile and Run

• to compile your program with OpenMP using f tgfortran

gfortran ‐fopenmp ‐o prog prog.f90

• to run with N threads set the environment variable OMP_NUM_THREADS

export OMP NUM THREADS 2export OMP_NUM_THREADS=2

• you might need to set the following

export OMP_DYNAMIC=false

Copyright 2009, University of Alberta 47Parallel Programming, Nov. 4, 2009

Activity 2 ‐ A Parallel Program

• add OMP directives to the serial program

Program SpinParallelImplicit Nonethe serial program

• they look like comments to the compiler unless compiling for OpenMP

• compile with:

Implicit None!$OMP Parallel Do While( .true. )! Spinning my wheelsEnd Do !$OMP E d P ll lgfortran ‐fopenmp ‐o 

spinparallelspinparallel.f90

!$OMP End Parallel End Program SpinParallel

Copyright 2009, University of Alberta 48Parallel Programming, Nov. 4, 2009

Page 25: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

25

Activity 2 ‐ A Parallel Program

• run the program with 1, 2 and 4 threads

Program SpinParallel

I li it N2 and 4 threads

export OMP_NUM_THREADS=4

• observe the threads with both top and the pscommand

Implicit None

!$OMP Parallel 

Do While( .true. )

! Spinning my wheels

End Do command!$OMP End Parallel 

End Program SpinParallel

Copyright 2009, University of Alberta 49Parallel Programming, Nov. 4, 2009

Activity 3 ‐ A Parallel Program

• change the infinite loop to a print statement

Program HelloWorld

I li it Nto a print statement

• compile and run serial and parallel versions of the program with 1, 2 and 4 threads

export

Implicit None

!$OMP Parallel 

Print*, “Hello World”

!$OMP End Parallel 

End Program HelloWorldexport OMP_NUM_THREADS=4

g

Copyright 2009, University of Alberta 50Parallel Programming, Nov. 4, 2009

Page 26: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

26

Posix Threads (pthreads)

• Posix threads provide a standardized i i t f f th ti fprogramming interface for the creation of 

threads on a Unix system

• creating and managing a process requires much more operating system overhead than thread creation and managementg

• the work is packaged in a function which is passed to the thread when it is created

Copyright 2009, University of Alberta 51Parallel Programming, Nov. 4, 2009

Thread Creation is Relatively Fast

Platformfork() pthread_create()

Real Time (= Wall  Real  Time (= Wall Ratio

time??) time??)Ratio

AMD 2.4 GHz Opteron (8cpus/node)

41.07 0.66 62

IBM 1.9 GHz POWER5 p5‐575 (8cpus/node)

64.24 1.75 37

IBM 1.5 GHz POWER4 (8cpus/node)

104.05 2.01 52

INTEL 2.4 GHz Xeon (2  54.95 1.64 34cpus/node)

INTEL 1.4 GHz Itanium2 (4 cpus/node)

54.54 2.03 27

Timings reflect 50,000 process/thread creations, were performed with the time utility, and units are in seconds, no optimization flags

Copyright 2009, University of Alberta 52Parallel Programming, Nov. 4, 2009

Page 27: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

27

Thread Creation

• pthread_create() arguments: – thread: a unique identifier for the new thread returned by q ythe subroutine

– attr: an attribute object that may be used to set thread attributes

• you can specify a thread attributes object, or NULL for the default values

– start_routine: the C routine that the thread will execute once it is created.

– arg: a single argument that may be passed to start_routine()

• It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed. 

Copyright 2009, University of Alberta 53Parallel Programming, Nov. 4, 2009

Posix Threads

• your main() program is the single, default firstthe single, default first thread

• all other threads must be explicitly created by the programmer

• after a thread is d h OScreated, the OS 

schedules when it will execute

Copyright 2009, University of Alberta 54Parallel Programming, Nov. 4, 2009

Page 28: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

28

Threads

• we have a bunch of threads doing the same thithing

• we need to split the work between threads

• each thread in a team has a unique identifier

• get the thread id with the OMP runtime functionfunction

Omp_Get_Thread_Num()

• the function returns an integer

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 55

Activity 4 – Thread IDs

• use the runtime function “ t th d ()” t t th th d“omp_get_thread_num()”  to get the thread id of each thread in the team and print it

• change the print statement to include the output of the function

• run the program with 1 2 4 and 16 threadsrun the program with 1, 2, 4 and 16 threads

export OMP_NUM_THREADS=4

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 56

Page 29: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

29

Sharing the Work

• we now have enough to split the work between

!$OMP Parallel Private(thrId)! Use an OMP function to get 

split the work between threads

• use a “Select‐Case” structure to control the work that a thread does

• this construct allows

!the thread IDthrdId = omp_get_thread_num()

Select Case(thrId)Case(0)   ! The master thread! Put work code hereCase(1)   ! Team member• this construct allows 

both task and data parallelism

( )! Put work code hereCase Default! Default work

End Select

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 57

Activity 5(a) ‐ Sharing the Work

• write serial program that does the following t k d i t th lttasks and prints the results:– 6 + 5 = ?

– 16 / 4 = ?

– Print “I am a computer”

– Print “Boo!”Print  Boo!

• use a “Select‐Case” structure to control the work that a thread does

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 58

Page 30: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

30

Activity 5(b) ‐ Sharing the Work

• modify your serial program by placing the tasks in a parallel section and run the program with 4a parallel section and run the program with 4 threads– how many results are printed?

• use a “Select‐Case” structure to control the work that a thread does and split the work between 4 threads

• run the program with 4 threads• run the program with 5 threads, does it behave sensibly? (Use the “Case Default”)

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 59

OpenMP

• compiler directive based; can use serial code • jointly defined and endorsed by a group of major• jointly defined and endorsed by a group of major computer hardware and software vendors– OpenMP Fortran API was released October 28, 1997– C/C++ API was released in late 1998

• portable/multi‐platform, including Unix and Windows NT platforms 

b d i l id f• can be very easy and simple to use – provides for "incremental parallelism" 

• www.openmp.org

Copyright 2009, University of Alberta 60Parallel Programming, Nov. 4, 2009

Page 31: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

31

OpenMP Directives

• free format Fortran files can contain only one OMP sentinel (the text string that begins a directive)

!$OMP• the directive has the form

!$OMP directive‐name optional_clauses...• a directive may appear anywhere on a line

– preceded only by whitespace– an ampersand (&) at the end of the line identifies a continued 

lineline.• Fortran comments may appear on the directive line, 

initiated by an exclamation mark (!)– the rest of the line is ignored

Copyright 2009, University of Alberta 61Parallel Programming, Nov. 4, 2009

Conditional Compilation

• the OpenMP Fortran API accepts a conditional compilation sentinel

• for free format source files this is!$

• this sentinel can appear in any column, preceded only by white space– must appear as a single word– Fortran free format conventions apply to the rest of the line

!$ iam = OMP GET THREAD NUM() + &!$ iam = OMP_GET_THREAD_NUM() + & !$ index

• with OpenMP compilation enabled, the sentinel is replaced by two blanks

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 62

Page 32: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

32

Conditional Compilation

• the following shows the most straightforward and common use of the conditional compilationcommon use of the conditional compilation sentinel in F95

integer :: Thrd_id…Thrd_id = 0!$ Th d id t th d ()!$ Thrd_id = omp_get_thread_num()

• the OMP runtime function is only executed if the file is compiled with OpenMP enabled

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 63

Message Passing Interface (MPI)

• a standard set of communication subroutine librariesk f SMP d l t– works for SMPs and clusters

• programs written with MPI are highly portable 

• information and downloads– http://www.mpi‐forum.org/

– MPICH: http://www‐unix.mcs.anl.gov/mpi/mpich/

/ h // l /

Copyright 2009, University of Alberta

– LAM/MPI: http://www.lam‐mpi.org/

– Open MPI: http://www.open‐mpi.org/

64Parallel Programming, Nov. 4, 2009

Page 33: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

33

MPI

• supports the SPMD, single program multiple data modeldata model– all processors use the same program– each processor has its own data 

• think of a cluster – each node is getting a copy of the program but running a specific portion f it ith it d t

Copyright 2009, University of Alberta

of it with its own data

65Parallel Programming, Nov. 4, 2009

Parallel Programming

• starting mpi jobs is not standardstandard– for mpich2 use “mpiexec”

• start a job with 6 processes

• 6 copies of the program run in the default Communicator Group “MPI_COMM_WORLD”

• each process has an ID its• each process has an ID – its “rank”

Copyright 2009, University of Alberta 66Parallel Programming, Nov. 4, 2009

Page 34: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

34

Parallel Programming

• example: start N processes to calculate N‐1 factorialto calculate N 1 factorial

• 0! = 1

• 1! = 1

• 2! = 2 x 1 = 2

• 3! = 3 x 2 x 1 = 6

• …

• n! = n x (n‐1) x…x 2 x 1

Copyright 2009, University of Alberta 67Parallel Programming, Nov. 4, 2009

Parallel Programming

• generally the master process will:– send work to other processes

– receive results from processes that complete

– send more work to those processes

– do final calculations

– output resultsoutput results

• designing an efficient algorithm for all this is up to you

Copyright 2009, University of Alberta 68Parallel Programming, Nov. 4, 2009

Page 35: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

35

Parallel Programming

• note that getting data through messages canthrough messages can be 10 to 1000 times slower than accessing local main memory

• try to keep the amount of data that you need toof data that you need to send between processes down

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 69

Parallel Programming

• the basic structure of an MPI program is what

Select Case(rank)Case(0) ! The masterMPI program is what 

we’ve seen in exercise 5(b)

• get the rank of the process

• do work allocation b d h k

Case(0)   ! The master process! Put work code hereCase(1)   ! Process 1! Put work code hereCase Defaultbased on the rank Case Default! Default work

End Select

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 70

Page 36: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

36

Parallel Programming

• it’s possible to combine OpenMP and MPI for running on clusters of SMP machinesrunning on clusters of SMP machines

• the trick in parallel programming is to keep all the processors– working (“load balancing”) – working on data that no other processor needs to touch (there aren’t any cache conflicts)

Copyright 2009, University of Alberta

touch (there aren’t any cache conflicts)

• parallel programming is generally harder than serial programming

71Parallel Programming, Nov. 4, 2009

Loops

• the bulk of parallelizable work in a

Do i = 1, 1000R(i) = A(i) + B(i)*C(i)parallelizable work in a 

program typically resides in repetitive sections

• how to split the work in a loop between

End Do

Thread 1:Do i = 1, 500R(i) = A(i) + B(i)*C(i)End Doa loop between 

threads?

• split range of the index between threads

Thread 2:Do i = 501, 1000R(i) = A(i) + B(i)*C(i)End Do

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 72

Page 37: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

37

Activity 6(a) – Sharing Loops

• write a serial program which initializes an array

Real :: a(1000)

a 1 0which initializes an array of length 1000 

• all array elements have value 1.0

• sum the array elements and print out the result

a = 1.0

sum = 0.0

Do i = 1, 1000

sum = sum + a(i)

End Do

• use a Select‐Case to split the work over 2 threads

• what happens?

Print*, “The sum is: ”, sum

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 73

Activity 6(b) – Sharing Loops

• use a Select‐Case  • !$OMP Parallel• thrdId = omp_get_thread_num()

S l C ( h dId)to split the work over 2 threads

• what happens?

• any ideas what 

• Select Case(thrdId)• Case(0)   ! The master thread• Do i = 1, 500• sum = sum + a(i)• End Do• Write(*, "(F12.5)") sum• Case(1)   ! Team member• Do i = 501, 1000

might be going on?

o i 50 , 000• Case Default• Write(*, “(I2)") thrdId• End Select• !$OMP End Parallel

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 74

Page 38: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

38

Shared Memory

• a process has access to memorymemory

• the threads that run as part of that process have access to that memory

Copyright 2009, University of Alberta 75Parallel Programming, Nov. 4, 2009

Speedup

• how can we measure how much faster our program runs when using more than one processor? Tg p

• define Speedup S as:– the ratio of 2 program execution times

– constant problem size

• T1 is the execution time for the problem on a single processor (use the “best” serial time)

• TP is the execution time for the problem on P processors

PTTS 1=

Copyright 2009, University of Alberta 76Parallel Programming, Nov. 4, 2009

Page 39: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

39

Speedup

• Linear speedupp p– the time to execute the 

problem decreases by the number of processors

– if a job requires 1 week with 1 processor it will take less that 10 minutes with 1024 processors

Copyright 2009, University of Alberta 77Parallel Programming, Nov. 4, 2009

Speedup

• Sublinear speedup– the usual case

– there are generally some limitations to the amount of speedup that you get

• communication

Copyright 2009, University of Alberta 78Parallel Programming, Nov. 4, 2009

Page 40: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

40

Speedup

• Superlinear speedup– very rare

– memory access patterns may allow this for some algorithms

Copyright 2009, University of Alberta 79Parallel Programming, Nov. 4, 2009

Speedup• why do a speedup test?• it’s hard to tell how a program 

will behave• e.g.

– “Strange” is actually fairly common behaviour for un‐tuned code

– in this case:• linear speedup to ~10 cpus

Copyright 2009, University of Alberta

• after 24 cpus speedup is starting to decrease

80Parallel Programming, Nov. 4, 2009

Page 41: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

41

Speedup

• to use more processors ffi i tl h thiefficiently change this behaviour– change loop structure 

– adjust algorithms

– ??

• run jobs with 10‐20 processors so the machines are used efficiently

Copyright 2009, University of Alberta

efficiently 

81Parallel Programming, Nov. 4, 2009

Speedup

• one class of jobs that have linear speed up are called “embarrassingly parallel”embarrassingly parallel– a better name might be “perfectly” parallel

• doesn’t take much effort to turn the problem into a bunch of parts that can be run in parallel:– parameter searches

– rendering the frames in a computer animation

– brute force searches in cryptography

Copyright 2009, University of Alberta

brute force searches in cryptography

82Parallel Programming, Nov. 4, 2009

Page 42: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

42

Speedup

• we have been discussing Strong Scaling– the problem size is fixed and we increase the number of processorsthe problem size is fixed and we increase the number of processors

• decrease computational time (Amdahl Scaling)

– the amount of work available to each processor decreases as the number of processors increases

– eventually, the processors are doing more communication than number crunching and the speedup curve flattens

– difficult to have high efficiency for large numbers of processors

Copyright 2009, University of Alberta 83Parallel Programming, Nov. 4, 2009

Speedup

• we are often interested in Weak Scaling– double the problem size when we double the number of processorsdouble the problem size when we double the number of processors

• constant computational time (Gustafson scaling)

– the amount of work for each processor has stays roughly constant

– parallel overhead is (hopefully) small compared to the real work the processor does

• e.g. Weather prediction

Copyright 2009, University of Alberta 84Parallel Programming, Nov. 4, 2009

Page 43: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

43

Amdahl’s Law• Gene Amdahl: 1967• parallelize some of the program  serial parallelp p g

– some must remain serial• f is the fraction of the 

calculation that is serial • 1‐f is the fraction of the 

calculation that is parallel• the maximum speedup that can 

be obtained by using P processors is: S 1

=

f 1‐f

serial parallel

Copyright 2009, University of Alberta

processors is:

Pff

S )1(max −+

=

85Parallel Programming, Nov. 4, 2009

Amdahl’s Law

• if 25% of the calculation must remain serial th b t d bt i i 4the best speedup you can obtain is 4

• need to parallelize as much of the program as possible to get the best advantage from multiple processors

Copyright 2009, University of Alberta 86Parallel Programming, Nov. 4, 2009

Page 44: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

44

Granularity

• a qualitative measure of the ratio between computation and communication or synchronization

• fine‐grain: a small amount of work is done before communication is required

• coarse‐grain: a large amount of work is done before communication is required

Copyright 2009, University of Alberta 87Parallel Programming, Nov. 4, 2009

Parallel Errors

• there are two types of errors that occur only in a parallel program:parallel program:

• Race Conditions– a result depends on which thread executes a section of code first

– this leads to unpredictable results

• DeadlocksDeadlocks– two threads are each waiting for a result from the other

– no work gets done

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 88

Page 45: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

45

Example: Convolution ‐ discrete

• in 2 dimensions the convolution is:

∑∞

−∞=−−=

jijnimjinm gfgf

,,,,)*(

convolution is:

• apply a 3x3 filter to the image

∑∞

−∞=−−=

jijijnim gf

,,,

→f→g

89Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009

Convolution ‐ discrete

x x x• for each image point:

• multiply the x x x

x x x

• multiply the corresponding filter and image values

• sum the result

• multiply by a normalizing factor if necessarynecessary

• for a 3x3 filter each new image point requires 9 multiplies and 8 adds

90Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009

Page 46: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

46

Convolution ‐ discrete3

1,2,2,2,2)*(

=−−

++

= ∑gfgfgf

gfgfji

jiji

1,1 1,0 1,‐1

1,1 1,2 1,3 1,4

2,1 2,2 2,3 2,4

3,1 3,2 3,3 3,4

1,13,30,12,31,11,3

1,03,20,02,21,01,2

1,13,10,12,11,11,1

−−−−

+++

+++

++=

gfgfgfgfgfgf

gfgfgf , , ,

0,1 0,0 0,‐1

‐1,1 ‐1,0 ‐1,‐1

‐1,‐1 ‐1,0 ‐1,1

0,‐1 0,0 0,1

1,‐1 1,0 1,1

4,1 4,2 4,3 4,4

→f→g

91Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009

Image Padding

• pixels at the edge of the image aren’t surroundedsurrounded

• convolution operations can’t be done• need some special handling• add pixels around the image: “padding”

– pad with zeros– copy the edge row or column as many times as necessary

– mirror the image

92Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009

Page 47: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

47

Convolution Calculation – Serial• the calculation involves 4 nested 

loops• two outside loops move over the

for(i = offset; i < nx + offset; i++) {for(j = offset; j < ny + offset; j++) {// O i h i l i h• two outside loops move over the 

image• two inside loops do multiplication 

and sum for new image point

• image size: 7000x7000• filter size: 7x7

S i l ti

// Operate in each pixel in the image with the filter

sum = 0.0;for(m = 0; m < nf; m++) {for(n = 0; n < nf; n++) {sum = sum + filter[m][n] * paddedimage[i‐offset+m][j‐offset+n];}• Serial times:

– program: 34.13 sec– loops: 29.7 sec– serial section: 4.42 sec– max speedup = 34.13/4.42 = 7.7

}}newimage[i][j] = sum;}}

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 93

Convolution Calculation ‐ OpenMP• to create OpenMP threads 

and tell OpenMP that we ll li i l

#pragma omp parallel for private (i,j,m,n,sum) schedule(dynamic,1)

{are parallelizing a loop we can combine two directives:

#pragma omp parallel

• and

#pragma omp for

• into the directive

for(i = offset; i < nx + offset; i++) {for(j = offset; j < ny + offset; j++) {// Operate in each pixel in the image with the 

filtersum = 0.0;for(m = 0; m < nf; m++) {for(n = 0; n < nf; n++) {sum = sum + filter[m][n] * paddedimage[i‐offset+m][j‐offset+n];

}}

#pragma omp parallel for}newimage[i][j] = sum;}}}

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 94

Page 48: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

48

Convolution Calculation ‐ OpenMP• clausesmodify the 

behaviour of the loop#pragma omp parallel for private (i,j,m,n,sum) 

schedule(dynamic,1){

private (i,j,m,n,sum)

• each thread has its own copy of these variables

schedule(dynamic,1)

• each thread gets a “chunk” of work of size 1

for(i = offset; i < nx + offset; i++) {for(j = offset; j < ny + offset; j++) {// Operate in each pixel in the image with the 

filtersum = 0.0;for(m = 0; m < nf; m++) {for(n = 0; n < nf; n++) {sum = sum + filter[m][n] * paddedimage[i‐offset+m][j‐offset+n];

}}

• when the work is done the thread requests another chunk

}newimage[i][j] = sum;}}}

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 95

OpenMP Speedup

Loop parallelizes nicely

Amdahl limit S = 7.7

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 96

Serial work prevents the program from speeding up well

Page 49: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

49

Gustafson’s Law

• keep the total time of execution fixedexecution fixed

• the serial part of the program is fixed

• increase the parallel work as the number of processors N increases– increase grid size

• work done by each thread is constant

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 97

)1( −−= NfNSf is the serial fraction of the programS is the speedup

Gustafson’s Law

28x28 filter16 threads

21x21 filter

9 threads

4 threads

1

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 98

7x7 filter

14x14 filter

Page 50: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

50

Convolution Calculation – MPI

• rewrite the program to include the MPI functions

MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_

WORLD & k)• make decisions about what work to do based on the rank of the process

• the master process gathers the results

• MPI_GATHER is a serial process

WORLD, &rank);MPI_Comm_size(MPI_COMM_W

ORLD, &size);

if (rank == 0) {// do something

}// G t th ltprocess // Get the resultsMPI_Gather(…);

MPI_Finalize();

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 99

MPI

Increase filter size  so each process gets more work

13x13 filter

21x21 filter

more work

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 100

7x7 filter

Page 51: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

51

MPI_Gather()

• Gathers together values from a group of processes

• This subroutine collects individual messages from each task in comm at the root task and stores them in rank order

• image: 7000x7000• filter: 7x7• filter: 7x7• time for the MPI_Gather()

– 4 sec for 2 processes– 15 sec for 14 processes

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 101

Implicit Parallelism in Matlab• Matlab can use multiple threads 

running in  a single Matlab instance

Element wise:d(15000 15000)instance

• speed up element wise computations, or those that use the BLAS library

• set/get the number of threads using

maxNumCompThreads• this is enabled by default as of 

Matlab 2009

>> x = rand(15000, 15000);>> z = sin(x);

Using BLAS:>> x = rand(3000, 3000);>> y = x;>> x * y;

• disable multithreaded computation by starting MATLAB with the option

matlab ‐singleCompThread

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 102

Page 52: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

52

Explicit Parallelism in Matlab

• Matlab can use multiple processes running on one

Start 3 worker processes:matlabpool 3processes running on one 

machine, or a cluster of machines

• requires the Parallel Computing Toolbox– one license gives up to 8 

worker processes

Serial work:x = rand(15000, 15000);for k = 1:15000;vec = x(:,k);y(: k) = log(vec+1 0);

• use “parfor” to parallelize a loop

• work in the loop must be independent for each iteration

y(:,k) = log(vec+1.0);endParallel work:parfor k = 1:15000;...matlabpool close

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 103

Summary

• parallel programming is a bit harder than serial• threads (OpenMP) vs processes (MPI)• threads (OpenMP) vs processes (MPI)

– each thread/process has a unique identity that it can use to control the work that it does

• you don’t know how efficiently your program uses multiple processors until you do a speedup test

• do a speedup test• do a speedup test• DO a speedup test!• MPI session in 2 weeks with Masao …

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 104

Page 53: Parallel Programming Concepts - WestGrid · 2013-02-09 · 47 Convolution Calculation –Serial • the calculation involves 4 nested loops • two outside loops move over the for(i

11/4/2009

53

Resources

• OpenMP docs:

http://openmp.org/wp/

• MPICH2 docs:http://www.mcs.anl.gov/research/projects/mpich2/

• MPI, The Complete Reference – online book// / / / /http://www.netlib.org/utk/papers/mpi‐book/mpi‐

book.html

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 105

Questions?

Copyright 2009, University of Alberta Parallel Programming, Nov. 4, 2009 106