four variations of matrix multiplication about 30 minutes – by mike 1

13
Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Upload: marshall-leonard

Post on 18-Dec-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Four Variations ofMatrix Multiplication

About 30 minutes – by Mike

1

Page 2: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Matrix Multiplication Challenge

• See – http://www.cs.utah.edu/formal_verification– Look under Concurrency Education– Look at MPI teaching resources

• Many of these resources are due to Simone Atzeni• Many are due to Geof Sawaya

– All the examples from Pacheco’s MPI book!– This will soon be available as projects within our ISP Eclipse GUI!

• Matrix Challenge is due to Steve Siegel– See http://www.cs.utah.edu/ec2– Steve’s challenge stems from an MPI book It is based on an

example from the book Using MPI: Portable Parallel Programming with the Message-Passing Interface by William Gropp, Ewing Lusk, and Anthony Skjellum.

2

Page 3: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Matrix Multiplication Illustration

• For this tutorial, we include four variants

• These try various versions of mat-mult

• Includes one buggy version

• Also reveals one definite future work item– Detect symmetries in MPI programs– Avoid redundant searches– Very apparent when you run our fourth version

3

Page 4: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Example of MPI Code (Mat Mat Mult)

4

X =

MPI_Send

MPI_Recv

Page 5: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Salient Code Features

5

if (myid == master) { ... MPI_Bcast(b, brows*bcols, MPI_FLOAT, master, …); ... } else { // All Slaves do this ... MPI_Bcast(b, brows*bcols, MPI_FLOAT, master, …); ... }

Page 6: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Salient Code Features

6

if (myid == master) { ... for (i = 0; i < numprocs-1; i++) { for (j = 0; j < acols; j++) { buffer[j] = a[i*acols+j]; } MPI_Send(buffer, acols, MPI_FLOAT, i+1, …); numsent++; } }

else { // slaves ... while (1) { ... MPI_Recv(buffer, acols, MPI_FLOAT, master, …); ... } }

Block till buffer is copied into System Buffer

System Buffer

Page 7: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Handling Rows >> Processors …

7MPI_Send

MPI_Recv

Send Next Row toFirst Slave whichBy now must be free

Page 8: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Handling Rows >> Processors …

8MPI_Send

MPI_Recv

OR

Send Next Row toFirst Slave that returns the answer!

Page 9: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Optimization

9

if (myid == master) { ... for (i = 0; i < crows; i++) { MPI_Recv(ans, ccols, MPI_FLOAT, FROM ANYBODY, ...); ... if (numsent < arows) { for (j = 0; j < acols; j++) { buffer[j] = a[numsent*acols+j]; } MPI_Send(buffer, acols, MPI_FLOAT, BACK TO THAT BODY, ...); numsent++; ... } }

Page 10: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Optimization

10

if (myid == master) { ... for (i = 0; i < crows; i++) { MPI_Recv(ans, ccols, MPI_FLOAT, FROM ANYBODY, ...); ... if (numsent < arows) { for (j = 0; j < acols; j++) { buffer[j] = a[numsent*acols+j]; } MPI_Send(buffer, acols, MPI_FLOAT, BACK TO THAT BODY, ...); numsent++; ... } }

Shows that wildcard receivescan arise quite naturally …

Page 11: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Further Optimization

11

if (myid == master) { ... for (i = 0; i < crows; i++) { MPI_Recv(ans, ccols, MPI_FLOAT, FROM ANYBODY, ...); ... if (numsent < arows) { for (j = 0; j < acols; j++) { buffer[j] = a[numsent*acols+j]; }

… here, WAIT for previous Isend to finish (software pipelining) …

MPI_Isend(buffer, acols, MPI_FLOAT, BACK TO THAT BODY, ...);

numsent++; ... } }

Page 12: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

Summary of Some MPI Commands

• MPI_Irecv(source, msg_bug, req_struct, ..) • This is a non-blocking receive call

• MPI_Wait(req_struct) awaits completion

• Source could be

– “wildcard” or * or ANY_SOURCE

• Receive from any eligible (matching) sender

12

Page 13: Four Variations of Matrix Multiplication About 30 minutes – by Mike 1

End of E

13