openmpand the pgas model - umd department …hollings/cs714/f15/lect04.pdfopenmpand the pgas model c...

43
OpenMP and the PGAS Model CMSC714 – Sept 15, 2015 Guest Lecturer: Ray Chen

Upload: dangthuan

Post on 01-Apr-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP and

the PGAS Model

CMSC714 – Sept 15, 2015

Guest Lecturer: Ray Chen

Page 2: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

Last Time: Message Passing

• Natural model for distributed-memory systems

– Remote (“far”) memory must be retrieved before use

– Programmer responsible for specifying:

• Participants (Single peer, collective communication, etc.)

• Data types (MPI_CHAR, MPI_DOUBLE, etc.)

• Logical synchronization

• How about shared-memory systems?

– All processors can directly access any memory

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 2

Page 3: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP: Open Multi-Processing

• Portable interface for multiple processor systems

– Agnostic to architecture or language

• Alternative to POSIX threads

– Thread management too low-level for HPC applications

– Task parallelism vs. data parallelism

• Not a language

– Extends existing language (C/C++/Fortran)

– Compiler responsible for low-level thread management

– Allows for incremental approach to parallelism

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 3

Page 4: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Fork/Join Model

• Single master thread executes until parallel region

• When a parallel region is reached

– At start, N-1 additional threads are spawned (Fork)

– All N threads execute the same code

• Different code paths achieved through unique thread ID

– At end, threads are synchronized via barrier (Join)

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 4Totally

Based on image by A1 licensed under CC BY 3.0

Page 5: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Syntax

• Compiler directives

– Express parallel structure of code

– Clauses modify directives

• Run-time library routines

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 5

#pragma omp <directive> <clauses>

{

// Code region affected by directive.

}

#include <omp.h>

int id = omp_get_thread_num();

Page 6: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP parallel Directive

• Begin a new parallel region

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 6

#include <stdio.h>

#include <omp.h>

int main(int argc, char* argv[])

{

#pragma omp parallel

{

printf(“Thread %d of %d.\n”,

omp_get_thread_num(),

omp_get_num_threads());

}

return 0;

}

$ gcc -fopenmp a.c

$ ./a.out

Thread 0 of 4

Thread 3 of 4

Thread 2 of 4

Thread 1 of 4

Page 7: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Data Environment

• All variables have a data-sharing attribute– Shared: all threads use the same copy

– Private: all threads use local storage for this variable

– Firstprivate: Like private, but value is initialized on fork

– Lastprivate: Like private, but value updated on join

• In general:– Variables defined inside a parallel region are private

– Variables defined outside a parallel region are shared

– Not always true: more details available in the spec

• Data-sharing attributes can be overridden

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 7

Page 8: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Worksharing Constructs

• Used to parallelize loops

– HPC programs contain computationally intensive loops

• Restrictions

– Loop iterations must be independent

– Strict rules for loop initialization, test, and increment

– Premature termination of loops not supported

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 8

#pragma omp loop

for (i = 0; i < MAX; ++i) {

}

Page 9: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Example: Finding Primes

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 9

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char* argv[])

{

int cap = atoi(argv[1]);

int count = 0;

int num = 0;

#pragma omp parallel loop reduction(max:num) schedule(dynamic)

for (int i = 0; i < cap; ++i) {

int prime = 1;

for (int j = 0; prime && j * j <= i; ++j)

prime = i % j;

if (prime) {

#pragma omp atomic

++count;

num = i;

}

}

printf(“Prime #%d is %d\n”, count, num);

return 0;

}

Keep a private copy of num for each thread, and copy

the maximum value back when the loop is finished.

Parallelize the following loop.

Use a work queue to

load-balance threads.

Synchronize the next line; count is a shared variable.

Page 10: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Synchronization

Threads pause here until all reach this point

Threads will execute one at a time

Only one thread will ever executeOther threads wait in an implied barrier at end of region

Only the master thread will executeOther threads skip the region without waiting

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 10

#pragma omp barrier

#pragma omp critical

#pragma omp single

#pragma omp master

Page 11: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Sync Puzzles

• Puzzle #1

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 11

int x = 1;

#pragma omp parallel num_threads(2)

{

#pragma omp single

{

++x;

}

printf(“%d\n”, x);

}

2

2

Page 12: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Sync Puzzles

• Puzzle #2

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 12

int x = 1;

#pragma omp parallel num_threads(2)

{

#pragma omp master

{

++x;

}

printf(“%d\n”, x);

}

1

2

2

2

or

Page 13: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

OpenMP Sync Puzzles

• Puzzle #3

access is not automatically

Shared variable

access is not automatically

synchronized.

Behavior is undefined.

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 13

int x = 1;

#pragma omp parallel num_threads(2)

{

++x;

#pragma omp barrier

printf(“%d\n”, x);

}

Page 14: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

Limitations of OpenMP

• Not fool-proof

– Data dependencies, conflicts, race conditions

– Gives you plenty of rope to hang yourself with

• OpenMP and Amdahl’s Law

– What is Dagum and Menon take on this issue?

• Computational Problem Size

– USA #1 supercomputer (Titan) has 32GB/node

– World #1 supercomputer (Tianhe-2) has 64GB/node

– What if our datasets are larger than 64GB?

• “You’re gonna need a bigger boat…”

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 14

Page 15: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

MPI + OpenMP

• Programming in MPI + OpenMP is hard

– Necessary for today’s large distributed memory systems

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 15

Page 16: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

PGAS Programming Model

• Partitioned Global Address Space

• Presents an abstracted shared address space

– Simplifies the complexity of MPI + OpenMP

• Exposes data/task locality for performance

• Several languages use this model

– UPC, Fortress, HPF, X10, etc.

– Ever heard of these?

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 16

Page 17: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

Chapel Themes

• Designed from scratch

– Blank slate as opposed to a language extension

– Allows for clearer, more expressive language semantics

• Multi-resolution design

– Higher-level abstractions built from lower-level ones

– Allows users to mix and match as needed

• Global-view programming

– Data can be declared using global problem size

– Data can be accessed using global indices

CMSC714 - Sept. 15, 2015 OpenMP and the PGAS Model - Ray S. Chen 17

Page 18: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen18

Slide used for educational purposes under US Copyright Law Title 17 U.S.C. section 107.

Page 19: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen19

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 20: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen20

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 21: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen21

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 22: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen22

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 23: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen23

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 24: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen24

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 25: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen25

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 26: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen26

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 27: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen27

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 28: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen28

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 29: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen29

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 30: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen30

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 31: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen31

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 32: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen32

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 33: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen33

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 34: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen34

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 35: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen35

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 36: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen36

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 37: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen37

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 38: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen38

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 39: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen39

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 40: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen40

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 41: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen41

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 42: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen42

Slide used under US Copyright Law Title 17 U.S.C. section 107.

Page 43: OpenMPand the PGAS Model - UMD Department …hollings/cs714/f15/lect04.pdfOpenMPand the PGAS Model C C714 –Sept 15, 2015 Guest Lecturer: Ray Chen LastTime: Message Passing • Natural

CMSC714 -

Sept. 15, 2015

OpenMP and the PGAS Model - Ray

S. Chen43

Slide used under US Copyright Law Title 17 U.S.C. section 107.