Docsity

OpenMP: Parallel Programming with OpenMP at University of Massachusetts Amherst, Study notes of Computer Science

Partial preview of the text

Download OpenMP: Parallel Programming with OpenMP at University of Massachusetts Amherst and more Study notes Computer Science in PDF only on Docsity!

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Parallel & ConcurrentProgramming:OpenMP

Emery Berger

CMPSCI 691W

Spring 2006

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Outline „

Last time(s):

MPI

  • point-to-point & collective

„

Library calls

„

Today:

OpenMP

  • parallel directives

„

Language extensions to Fortran/C/C++

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

MPI Solution

MPI_Init

(&argc, &argv);

MPI_Comm_rank (MPI_COMM_WORLD, &rank);MPI_Comm_size (MPI_COMM_WORLD, &size);// Distribute a and bMPI_Scatter

(a, 100, MPI_INT, a1, 100 / size, MPI_INT, 0,

MPI_COMM_WORLD);MPI_Scatter

(b, 100, MPI_INT, b1, 100 / size, MPI_INT, 0,

MPI_COMM_WORLD);// Multiply each chunkfor (int

i = 0; i < 100/size; i++) {

z += a[i] *b1[i];

} // Reduce by summingif (rank == 0) {z1 = new int[size]; }MPI_Reduce (&z, &z, 1, MPI_INT, MPI_OP_PLUS, 0, MPI_COMM_WORLD);// Output resultif (rank == 0) {

cout << z << endl;

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Ideal Solution

int z = 0;parallel for (i = 0; i < nProcessors; i++) {

z += a[i] * b[i];

} cout << z << endl;

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

OpenMP Concepts „

Fork-join

model

„

One thread executes sequential code

„

Upon reaching

parallel directive

Start new

team

of

work-sharing

threads

Wait until all done (usually barrier)

Can be nested!

„

Apparent global shared memory but relaxed consistency model

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Consistency „

Consistency =ordering of reads & writes

In same thread, across threads

„

Most “intuitive” consistency model = sequential consistency

(Lamport)

Behaves like some sequential execution

BUT: seriously

limits parallelism

„

Must synchronize frequently

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Parallel Execution „

#pragma omp parallel

Executes next chunk of code across allor some number of threads

„

num_threads(n)

„

Only

“master thread” continues after

parallel section completes

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Dynamic Threads

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Parallel + Memory „

Memory model:

Heap objects shared

Stack objects private

„

Includes loop iterators

unless indicated otherwise...

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Parallel Example

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Lastprivate Example

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

Threadprivate Example „

Can also declare variables as

always

thread-private

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

OpenMP Solution

int

z

=

0;

#pragma omp

for

reduction(+:z)

for

(int

i =

0;

i

<

100;

i++)

{

z

+=

a[i]


b1[i];

} cout

<<

z

<<

endl;

„

OpenMP

pragma

directives

Omit = sequential program

More declarative style

Add more pragmas for more efficiency

U
U

NIVERSITY OFNIVERSITY OF

MM

ASSACHUSETTSASSACHUSETTS

A
A

MHERST •MHERST

Department of Computer ScienceDepartment of Computer Science

All Together