Docsity

Multicore Architecture Design and Parallel Programming Techniques, Slides of Assembly Language Programming

Partial preview of the text

Download Multicore Architecture Design and Parallel Programming Techniques and more Slides Assembly Language Programming in PDF only on Docsity!

Dr. Rodric Rabbah, IBM.

1

6.189 IAP 2007 MIT

6.189 IAP 2007

Lecture 5

Parallel Programming Concepts

2

6.189 IAP 2007 MIT

Recap^ „^ Shared memory–^ Dr. Rodric Rabbah, IBM.

Ex: Intel Core 2 Duo/Quad– One copy of data sharedamong many cores– Atomicity, locking andsynchronizationessential for correctness– Many scalability issues

„^ Distributed memory–^ Ex: Cell–^ Cores primarily access localmemory–^ Explicit data exchangebetween cores–^ Data distribution andcommunication orchestrationis essential for performance

PP^1

P^3

Pn MemoryInterconnection Network

Interconnection NetworkPP 1 2

P^3

Pn MM^1

M 2 3

Mn

●^ Two primary patterns of multicore architecture design

4

6.189 IAP 2007 MIT

Example Parallelization for (i = 0; i < 12; i++)C[i] = A[i] + B[i]; ●^ Data parallel^ „^ Perform same computationbut operate on different data ●^ A single process can forkmultiple concurrent threads^ „^ Each thread encapsulate its own execution path^ „^ Each thread has local state and shared resources^ „^ Threads communicate through shared resourcessuch as global memoryDr. Rodric Rabbah, IBM.

i = 0i = 1i = 2i = 3

i = 4i = 5i = 6i = 7

i = 8i = 9i = 10i = 11 fork (threads) join (barrier)

5

6.189 IAP 2007 MIT

Example Parallelization With Threads int^ A[12] Dr. Rodric Rabbah, IBM.

=^ {...};

int^ B[12]

=^ {...};

int^ C[12];

void^ add_arrays(int

start)

{ int^

i; for (i^ =^ start;

i^ <^ start

+^ 4;^ i++)

C[i]^ =^

A[i]^ +^

B[i]; } int^ main

(int^ argc,^ char

*argv[])

{ pthread_t

threads_ids[3]; int^ rc,

t; for(t^ =^ 0;^ t^

<^ 4;^ t++)

rc^ =^ pthread_create(&thread_ids[t],

NULL^

/*^ attributes

add_arrays /*

function

t^ *^4

/*^ args

to^ function

} pthread_exit(NULL);}

i = 0i = 4i = 1i = 5i = 2i = 6i = 3i = 7^ join (barrier)

i = 8i = 9i = 10i = 11 fork (threads)

7

6.189 IAP 2007 MIT

Parallel Programming with OpenMP ●^ Start with a parallelizable algorithm^ „^ SPMD model (same program, multiple data) ●^ Annotate the code with parallelization andsynchronization directives (pragmas)^ „^ Assumes programmers knows what they are doing^ „^ Code regions marked parallel are considered independent^ „^ Programmer is responsibility for protection against races ●^ Test and DebugDr. Rodric Rabbah, IBM.

8

6.189 IAP 2007 MIT

Simple OpenMP Example #pragma omp parallel#pragma omp forfor(i = 0; i < 12; i++)C[i] = A[i] + B[i];^ ●^ (data) parallel pragmaexecute as many as thereare processors (threads)^ ●^ for pragmaloop is parallel, can dividework (work-sharing)Dr. Rodric Rabbah, IBM.

i = 0i = 4i = 1i = 5i = 2i = 6i = 3i = 7^ join (barrier)

i = 8i = 9i = 10i = 11 fork (threads)

10

6.189 IAP 2007 MIT

Message Passing ●^ Architectures with distributed memories use explicitcommunication to exchange data^ „^ Data exchange requires synchronization (cooperation)between senders and receivers–^ Dr. Rodric Rabbah, IBM.

P^1 How is “data” described– How are processes identified– Will receiver recognize or screen messages– What does it mean for a send or receive to complete

P^2

Send(data)

Receive(data)

11

6.189 IAP 2007 MIT

Example Message Passing Program Dr. Rodric Rabbah, IBM.

for^ (i^

=^1 to^

for^ (j =^1 to

C[i][j]

=^ distance(A[i],

B[j])

P^1 M^1

M P 2 2

x

●^ Calculate the distance from each point iny^ B A

A[^ ..

4]

to every other point in

B[1..

4]^ and store results to

C[^ ..

4][

..4]

13

6.189 IAP 2007 MIT

Dr. Rodric Rabbah, IBM.

P^1 M^1

M P 2 2

for^ (i^

=^1 to^

for^ (j =^1 to

C[i][j]

=^ distance(A[i],

B[j])

Example Message Passing Program ●^ Calculate the distance from each point in

A[^ ..

4]

to every other point in

B[1..

4]^ and store results to

C[^ ..

4][

..4] ●^ Can break up workbetween the twoprocessors^ „^ Psends data to^1

P^2

14

6.189 IAP 2007 MIT

●^ Can break up workbetween the twoprocessors^ „^ P^1 Dr. Rodric Rabbah, IBM.

sends data to

P^2

„^ Pand^1

Pcompute^2

P^1 M^1

M P 2 2

for^ (i^

=^1 to^

for^ (j =^1 to

C[i][j]

=^ distance(A[i],

B[j])

Example Message Passing Program ●^ Calculate the distance from each point in

A[^ ..

4]

to every other point in

B[1..

4]^ and store results to

C[^ ..

4][

..4]

16

6.189 IAP 2007 MIT

A[n]^ =^ {…}B[n]^ =^ {…} Send^ (A[n/2+1..n], Dr. Rodric Rabbah, IBM.

B[1..n]) for^ (i =^1 to

n/2) for^ (j^

=^1 to^

n) C[i][j]

=^ distance(A[i],

B[j])

Receive(C[n/2+1..n][1..n])

A[n]^ =^

B[n]^ =^

Receive(A[n/2+1..n],

B[1..n])

for^ (i^

=^ n/2+

to^ n) for^ (j^

=^1 to^

n) C[i][j]

=^ distance(A[i],

B[j])

Send^ (C[n/2+1..n][1..n])

processor 1

processor 2

processor 1

sequentialparallel with messages

for^ (i^

=^1 to^

for^ (j^

=^1 to^

C[i][j]

=^ distance(A[i],

B[j])

Example Message Passing Program

17

6.189 IAP 2007 MIT

Performance Analysis ●^ Distance calculations between points areindependent of each other^ „^ Dividing the work betweentwo processors Dr. Rodric Rabbah, IBM.

Æ^ 2x speedup „^ Dividing the work betweenfour processors

Æ^ 4x speedup

●^ Communication^ „^ 1 copy of

B[]^ sent to each processor „^ 1 copy of

subset

of^ A[]

to each processor

●^ Granularity of

A[]^ subsets directly impact communication costs „^ Communication is not free

x y

19

6.189 IAP 2007 MIT

Rendering Scenes by Ray Tracing^ „^ Shoot rays into scene through pixels in image plane^ „^ Follow their paths–^ Dr. Rodric Rabbah, IBM.

Rays bounce around as they strike objects– Rays generate new rays „ Result is color and opacity for that pixel „ Parallelism across rays

transmission ray

primary ray

normal

reflection

20

6.189 IAP 2007 MIT

Limits to Performance Scalability ●^ Not all programs are “embarrassingly” parallel ●^ Programs have sequential parts and parallel partsDr. Rodric Rabbah, IBM.

a = b + c;d = a + 1;e = d + a;for (i=0; i < e; i++)M[i] = 1;

Sequential part(data dependence)Parallel part(no data dependence)