הרצאה מס ' 3 11/2002 מבוא לעיבוד מקבילי. נושאי ההרצאה...

Post on 21-Dec-2015

249 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

3הרצאה מס' 11/2002

מבוא לעיבוד מקבילי

ההרצאה נושאי

פתיחה•

Point-to-Point תקשורת•

תקשורת קולקטיבית•

הערכת ביצועי תכניות מקביליות•

בדיקת ביצועי ריצות מקביליות•

• Please read Chapter 2 from Wilkinson & Allan and then proceed with this presentation from slide 80.

• Next slides are mostly adopted from W-A chapter 2, 1999 Edition.

Message Passing

Messages are packets of data moving between subprograms

מעבדים רבת מערכת תכנות

מקבילי • לחישוב מיוחדת שפה לתכנןסריאלית • שפה של התחביר את להרחיב

קיימתלה • ולהוסיף קיימת סריאלית בשפה להשתמש

לטיפול חיצוניות פרוצדורות של ספריההמקבילי בחישוב

השלישית באפשרות נתרכז אנחנו

Software Tools – 1/3

PVM (Parallel Virtual Machine)• Using a workstation cluster as a multi-computer

platform developed by Oak Ridge NationalLaboratories.

• A software environment for message passing between homogeneous or heterogeneous computers

• Has a collection of library routines that the user can employ with C or FORTRAN programs

Software Tools – 2/3

MPI (Message Passing Interface)

• Standard developed by group of academics and industrial partners to foster more widespread use and portability.

• Several free implementations exist

Software Tools – 3/3

Web sites:• PVM – http://www.epm.ornl.gov/pvm/pvm_home.html

• MPI – http://www-unix.mcs.anl.gov/mpi/

PVM – 1/2

• The programmer decomposes the problem into separate programs. Each program iswritten in C (or Fortran) and compiled to run on specific types of computers in the network

• The routing of messages between computers is done by PVM daemon processes installed by PVM on the computers that form the virtual machine

PVM – 2/2

Multiple Program Multiple Data (MPMD)

זמן

תהליך

1מס'

תהליך

2מס'

Spawn)(

נפרדת תכניתמעבד לכל

Master-Slave

MPI – 1/2

• Process Creation and Execution– Purposely not defined and will depend upon the

implementation.– Only static process creation is supported in MPI

version 1.x.– All the processes must be defined prior to

execution and started together.– Use SPMD model of computation.

MPI – 2/2

• Communicators– Defines the scope of a communication operation.

– Processes have ranks associated with the communicator.

– Initially, all processes enrolled in a “universe”called MPI_COMM_WORLD, and each process is given a unique rank, a number from 0 to n 1, where there are n processes.

– Other communicators can be established for groups of processes.

SPMD תכנית

קובץ מקור

קבצי הרצה

קבצי הרצה

0מעבד n-1מעבד

SPMDמודל של תכנית

main (int argc, char *argv[]) {MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD,&myrank);if (myrank ==0)

master();else

slave();MPI_Finalize();}

The Communicator – 1/4

The Communicator – 2/4

• Communicators -used in MPI for all point-to-point and collective MPI message-passingcommunications.

• A communicator is a communication domain that defines a set of processes that are allowed to communicate between themselves.

• In this way, the communication domain of the library can be separated from that of a user program.

• Each process has a rank within the communicator,an integer from 0 to n 1,

The Communicator – 3/4שני סוגים

• Intracommunicator -for communicating within a group

• Intercommunicator -for communication between groups.

• A process has a unique rank in a group (an integer from 0 to m 1,where there are m

• processes in the group).

The Communicator – 4/4

• A process could be a member of more than one group.

• Default intracommunicator -MPI_COMM_WORLD, exists as the first communicator for all the processes existing in the application.

• A set of MPI routines exists for forming communicators.

תהליכים בין תקשורת סוגי

נקודה לנקודה

(נל"ן)

Point-to-Point

קולקטיבית

Collective

נקודה לנקודה )נל"ן(Point-to-Point

בתקשורת מסוג זה מעורבים בדיוק שני תהליכים

בסיסיSend/Receiveמנגנון

' מס תהליך1x

Send(&x,2);

תהליך מס' 2y

Recv(&y,1);

הזזת נתונים

העברת להצלחת תנאיםהודעה

Message Tags

• Used to differentiate between different types of messages being sent.

• If special type matching is not required, a wild card message tag is used, so that the recv() will match with any send().

• Examplesend(&x, 2, 5);recv(&y, 1, 5);

מסרים העברת סוגי שני

Send

Non Blocking Blocking

פקודות ישנן אלה קבוצות משתי אחת בכל- סינכרוניות א ו סינכרוניות תקשורת

Blocking and Non-Blocking Message Passing

• Blocking -has been used to describe routines that do not return until the transfer is completed.

• The routines are “blocked” from continuing.• In that sense, the terms synchronous and blocking

are synonymous.• Non-blocking -has been used to describe routines

that return whether or not the message had been received.

Blocking Routines

Return when they are locally complete -when the location used to hold the message can be used again or altered without affecting the message being sent.

MPI Definitions of Blocking and Non-Blocking

• Blocking -return after their local actions complete, though the message transfer may not have been completed.

• Non-blocking -return immediately. Assumed that the data storage being used for the transfer is not modified by the subsequent statements prior to the data storage being used for the transfer, and it is left to the programmer to ensure this.

Non-Blocking send

“Wait” or “Test”

Non-Blocking Operations

All non-blocking operations should have matching wait operations. Some systems cannot free resources until wait has been called.

A non-blocking operation immediately followed by a matching wait is equivalent to a blocking operation.

סינכרונית – 1/2שליחה

• Routines that actually return when the message transfer has been completed.

• Do not need message buffer storage. A synchronous send routine could wait until the complete message can be accepted by the receiving process before sending the message.

סינכרונית – 2/2שליחה

יש צורך בקבלת מידע על סיום המסר

שליחה א-סינכרונית

השולח יודע רק על עזיבת

ההודעה

Message Passingמסונכרן

• A synchronous receive routine will wait until the message it is expecting arrives.

• Synchronous routines intrinsically perform two actions:They transfer data and they synchronize processes.

• Suggest some form of signaling,such as a three-way protocol:

Three-Way Protocol

Using a Message Buffer

וגלובליים – לוקאליים משתנים1/2

• Any global declarations of variables will be duplicated in each process.

• Variables that are not to be duplicated will need to be declared within code only

executed by that process.

וגלובליים – לוקאליים משתנים2/2

MPI_Comm_rank(MPI_COMM_WORLD,&myrank);/*find process rank */if (myrank ==0){ /*process 0 actions/local variables */int x,y;.}else if (myrank ==1){/*process 1 actions/local variables */int x,y;.}

הם שונים בכל בלוקy ו- xדוגמא:

בטוחה לא מסרים העברת

Tagsבתקשורת נל"ן

Message tags are present, and wild cards can be used in place of the tag (MPI_ANY_TAG) and in place of the source in receive routines (MPI_ANY_SOURCE).

1/6 - אופני שליחת המסרים

• Standard Mode Send

• Buffered Mode

• Synchronous Mode

• Ready Mode

2/6 - אופני שליחת המסרים

Standard Mode Send:

Not assumed that corresponding receive routine has started.

Amount of buffering not defined by MPI.

If buffering is provided, send could complete before receive reached.

3/6 - אופני שליחת המסרים

Buffered Mode

Send may start and return before a matching receive.

Necessary to specify buffer space

via routine MPI_Buffer_attach().

Removed with MPI_Buffer_detach().

4/6 - אופני שליחת המסרים

Synchronous Mode

Send and receive can start before each other but can only complete together.

5/6 - אופני שליחת המסרים

Ready Mode

Send can only start if matching receive already reached, otherwise an error will occure.

Use with care to avoid erroneous operation.

6/6 - אופני שליחת המסרים

Each of the four modes can be applied to both blocking and non-blocking send routines.

Communications Modes

Communication Modes(Blocking)

Communication Modes(Non-Blocking)

Sendתחביר פקודת

MPI_Send(buf,count,datatype,dest,tag,comm)

Address of send buffer

Number ofitems to send

Data type of each item

Rank of destination process

Message tag

Communicator

Receiveתחביר פקודת

MPI_Recv(buf,count,datatype,src,tag,comm,status)

Address of receive buffer

Maximum numberof items to receive

Data type of each item

Rank of source process

Message tag

Communicator

Status after operation

חוסמות – שאינן 1/4רוטינות

• Non-Blocking send -MPI_Isend()

will return “immediately”even before source location is safe to be altered.

• Non-Blocking receive -MPI_Irecv()

will return even if there is no message to accept.

)Non-Blocking(

חוסמות – שאינן 2/4רוטינות) הפקודות) תחביר

• MPI_Isend(buf,count,datatype,dest,tag,comm,request)

• MPI_Irecv(buf,count,datatype,

source,tag,comm,request)

חוסמות – שאינן 3/4רוטינותגילוי סיום העברת המסר מתגלה על-ידי •

הפונקציותMPI_Wait() -ו MPI_Test().

• MPI_Wait()waits until the operation has actually completed and will return then.

• MPI_Test() returns with a flag set indicating whether operation completed at that time.

• These routines need to know whether the particular operation has completed,which isdetermined by accessing the request parameter.

חוסמות – שאינן 4/4רוטינותMPI_Comm_rank(MPI_COMM_WORLD,&myrank(if (myrank ==0){int x;MPI_Isend(&x,1,MPI_INT,1,msgtag,

MPI_COMM_WORLD,req1);compute();MPI_Wait(req1,status);{else if (myrank ==1){int x;MPI_Recv(&x,0,MPI_INT,1,msgtag,

MPI_COMM_WORLD,status);{

Send

Ssend

Rsend

קולקטיבית תקשורת

קולקטיבית – 1/2תקשורת

• Involves set of processes, defined by an intra-communicator.Message tags not present.

• Broadcast and Scatter Routines

קולקטיבית – 2/2תקשורת פקודות עיקריות7

• MPI_Bcast()-Broadcast from root to all other processes

• MPI_Gather()-Gather values for group of processes

• MPI_Scatter()-Scatters buffer in parts to group of processes

• MPI_Alltoall()-Sends data from all processes to all processes

• MPI_Reduce()-Combine values on all processes to single value

• MPI_Reduce_scatter()-Combine values and scatter results

• MPI_Scan()-Compute prefix reductions of data on processes

Broadcast – 1/4

One-to-Many Communication

Broadcast – 2/4

Sending the same message to all the processes concerned with the problem.

Multicast -sending the same message to a defined group of processes.

Broadcast – 3/4

Broadcast 4/4

Reduce – 1/3

Reduce – 2/3

Gather operation combined with a specified arithmetic or logical operation.

Reduce – 3/3

Scatter – 1/2

Sending each element of an array of data in the root to a separate process.

The contents of the ith location of the array is sent to the ith process.

Scatter – 2/2

GatherOne process collects individual values from a set of processes.

(MPI_Gather( – דוגמאint data[10];/*data to be gathered from processes*/

.

MPI_Comm_rank)MPI_COMM_WORLD,&myrank(;

if )myrank ==0({

MPI_Comm_size)MPI_COMM_WORLD,&grp_size(;/*find group size*/

buf =)int *(malloc)grp_size*10*sizeof)int((;/*allocate memory*/

{

MPI_Gather)data,10,MPI_INT,buf,grp_size*10,MPI_INT,0,

MPI_COMM_WORLD(;

Scatter/Gather

Barrier – 1/2

As in all message-passing systems, MPI provides a means of synchronizing processes by stopping each one until they all have reached a specific “barrier” call.

Barrier – 2/2

תכניות ביצועי הערכתמקביליות

המקבילית הריצה זמן

tp = tcomp + tcomm

זמן התקשורת ניתן בקרוב על-ידי:

tcomm = tstartup + ntdata

tstartup=startup time = latency

tdata =transmission time of 1 word

Latency Hiding

Non-blocking send routines provided particularly to enable latency hiding.

דוגמא

נניח שברשותנו מחשב בעל כח חישוב של •1GFLOP זמן ההתנעה של התקשורת הוא .

10sec.

לכן המחשב היה יכול לחשב בזמן זה לבצע • פעולות!10,000

Complexityסיבוכיות -

הערכת זמן הריצה הכולל אשר לוקח בחשבון הן •את זמן החישוב והן את זמן התקשורת

ההערכה ניתנת במונחים של התנהגות החסומה •על-ידי פונקציה מוגדרת, כגון:

O(n), O(n2), O(n log n), …

תרגיל: הערכת הסיבוכיות

מספרים בשני nהערך את הסיבוכיות של חיבור •מחשבים במקביל.

1/2פתרון –

המספרים והוא גם זה n מכיל את 1נניח שמחשב מס' שמדפיס את התוצאה הסופית.

מספרים ובנוסף מחשב מס' n/2כל אחד מהמחשבים יחבר יחבר את שתי תוצאות הביניים.1

n/2 2 למחשב מס' 1במהלך החישוב יעביר מחשב מס' מספרים ואחר-כך יקבל ממנו את תוצאת הביניים שלו.

2/2פתרון –

הערכת זמן החישוב:•• tcomp = (n/2) + 1

הערכת זמן התקשורת:•• tcomp = (tstartup + n/2tdata) + (tstartup + tdata) = 2

tstartup + (n/2 + 1) tdata

וכך גם סיבוכיות O(n)סיבוכיות החישוב היא •התקשורת. לכן, הסיבוכיות הכללית תהיה אף

O(n)היא:

Cost Optimal – 1/2

• Cost= (execution time) X ( #processors)

• Cost Optimal:

• (Parallel time complexity) (number of processors) = sequential time complexity

E

t

nS

ntCost ss

)(

sp tkntlCostOptima

Cost Optimal – 1/2

• Example:

Suppose the best known sequential algorithm for a problem has time complexity of (n log n). A parallel algorithm for the same problem that uses n processes and has a time complexity of (log n) is cost optimal,

תרגיל: Time Complexity of Broadcast. Hypercubeהמערך המקבילי בעל מבנה של •

ברצוננו לשדר מסר מצומת

, 101, 100, 011, 010, 001 לכל יתר הצמתים: 000110, 111

פתרון: Time Complexity of Broadcast

Node Node

1st step: 000 001

2nd step: 000 010

001 011

3rd step: 000 100

001

פתרון: Time Complexity of Broadcast

המשך פתרון: Time Complexity of Broadcast

• The time complexity for a hypercube system will be (log n), using this algorithm, which is optimal because the diameter of a hypercube network is log n.

It is necessary at least to use this number of links in the broadcast to reach the furthest node.

המשך פתרון: Time Complexity of Broadcast

תרגיל: Gather on a Hypercube Network

פתרון• הפתרון דומה אך בסדר Gatherעבור –

ההפוך 000ככל שאיסוף הנתונים יתקרב ל-–

יגדל נפח הנתונים(log n)לכן זמן התהליך יהיה גדול מ- –

תרגילBroadcast on a Mesh Network

מצא את התהליך האופטימאלי ל- Broadcast?על רשת דו-ממדית

פתרוןBroadcast on a Mesh Network

Deadlocksלהזהר מ-

.Buffer יקדים ההצלחה תלויה ב- sendאם

אם תתקיים אם הפקודות בטוחהתקשורת יוצלבו

Debugging and Evaluating Parallel Programs

אם ניתן, הרץ את התכנית על מעבד יחיד ובצע •debug על תכנית סריאלית. – בדיקת נכונות

התכניתהרץ את התכנית עם מספר תהליכים על-גבי •

מעבד יחיד. – בדיקת המיקבולהרץ התכנית עם אותו מספר של תהליכים •

על-גבי מספר מעבדים. – בדיקת השפעות של רשת התקשורת

Visualization Tools – 1/2Upshot – alog file (requires Tcl/Tk)

Jumpshot – slog file (requires Java)

MPI generates clog file which then needs to be converted by:

clog2alog or clog2slog

Visualization Tools - 2/2

Profiling

•Profile של תכנית הוא גרף או היסטוגרמה המראה את הזמן שלקח לבצע חלקים שונים

של התכנית. מנגנון זה יכול לאתר נקודות חמות )hot spots( בהן בקרה התכנית מספר רב של

פעמים.

•MPE -ספריה ל profiling -אשר אינה חלק מ MPI -אך כלולה ב MPICH.

Timing MPI Porgrams

• double MPI_Wtime()• double MPI_Wtick()• Example:

starttime=MPI_Wtime();

.

endtime=MPI_Wtime();

tottime=endtime-starttime

Makefiles מידע נוסף לגבי

http://www.eng.hawaii.edu/Tutor/Make/

http://www.cs.wustl.edu/~cplus/makefiles.html

http://oucsace.cs.ohiou.edu/~bhumphre/makefile.html

http://www.cs.uncc.edu/~jlrobins/make/

1/6תרגיל –

חיבור מקבילי של מספרים (אקראיים).

המספרים הוכנו מבעוד מועד בקובץ קלט.

, יתר )master( הוא מנהל את הריצה 0מחשב )workers(המחשבים הם העובדים

כפי Reduce ו- Broadcastפקודות התקשורת הן שהכרנו בשיעור הקודם

2/6תרגיל – #include "mpi.h“

#include <stdio.h>

#include <math.h>

#define MAXSIZE 1000

int add(int *A, int low, int high){

int res, i;

for(i=low; i<high; i++)

res += A[i];

return(res);

}

3/6תרגיל –

void main(argc,argv)int argc;char *argv[];{ int done = 0, n, myid, numprocs; int data[MAXSIZE], i, low, high, myres, res; char fn[255]; char *fp; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); n = 0;

תרגיל while (!done) { if (myid == 0) { if (n==0) n=100; else n=0; strcpy(fn,getenv("HOME")); strcat(fn,"/MPI/rand_data.txt"); /* Open Input File and Initialize Data */ if ((fp = fopen(fn,"r")) == NULL) {

printf("Can't open the input file: %s\n\n", fn);exit(1);

} for(i=0; i<MAXSIZE; i++)fscanf(fp,"%d", &data[i]); } MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD);

if (n == 0)תרגיל

done = 1;

else {

low = myid * 100; high = low + 100;

myres = add(data, low, high); printf("I got %d from %d\n", myres, myid);

MPI_Reduce(&myres, &res, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if (myid == 0) printf("The sum is %d.\n", res);

}

}

MPI_Finalize();

}

6/6תרגיל –

תרגיל – Scalability of a Parallel

Systemחשב האצה ויעילות כפונקציה של מס'

מעבדים. p מספרים ב- nהמעבדים עבור חיבור הנח כי יחידת זמן אחת שווה לזמן פעולת

חישוב אחת השווה גם לזמן העברת תקשורת אחת.

סכימת הרשת היא היפר-קיוב.

1/4פתרון –

n/p-1 מספרים. משך הזמן הוא לכןn/pכל מעבד מחבר יחידות זמן.

חיבורים של הסכומים pלאחר החיבור יש צורך לבצע .log(p)החלקיים.מספר הצעדים יהיה

בכל צעד תהיה פעולת חיבור אחת ופעולת תקשורת אחת.

צעדי זמן.n/p-1+2log(p)לכן הזמן הכולל יהיה:

2/4פתרון –

גדולים, יהיה הזמן בקרוב: p ו- nעבור

pp

nt p log2

. n או בקרוב n-1הזמן הסריאלי הוא

כעת ניתן לחשב את ההאצה ואת היעילות

3/4פתרון –

ppn

n

p

SE

ppn

np

ppn

nS

log2

log2log2/

4/4פתרון –

nP=1P=4P=8P=16P=32

641.00.800.570.330.17

1921.00.920.800.600.38

3201.00.950.870.710.50

5121.00.970.910.800.62

:p ו- nהיעילות כפונקציה של

פרוייקטים

הנכם מתבקשים לחשוב ולהציע נושאים •לפרוייקטים

pp@ee.bgu.ac.ilנא לפנות בהקדם אל •

Summary – 1/4

• PVM – Parallel Virtual Machine

• MPI – Message Passing Interface

• SPMD – Single Program Multiple Data

• MPMD-Multiple Program Multiple Data

• Sens/Recv Message Passing

• Synchronous Message Passing

• Blocking/Non Blocking Message Passing

Summary – 2/4

• Message Buffer

• Broadcast

• Scatter

• Gather

• Reduce

• Communicator

Summary – 3/4

• Standard Mode Send

• Buffered Mode

• Synchronous Mode

• Ready Mode

Summary – 4/4

• Complexity

• Optimal Cost

• Visualization tools

• Profiling

top related