250 likes | 593 Views
Lecture 5: Parallel Virtual Machine (PVM). Parallel Programming Models. Message Passing Model Used on Distributed memory MIMD architectures Multiple processes execute in parallel asynchronously Process creation may be static or dynamic
E N D
Parallel Programming Models Message Passing Model • Used on Distributed memory MIMD architectures • Multiple processes execute in parallel asynchronously • Process creation may be static or dynamic • Processes communicate by using send and receive primitives
Parallel Programming Models Example: Pi calculation P = f01 f(x) dx = f014/(1+x2) dx = w∑f(xi) f(x) = 4/(1+x2) n = 10 w = 1/n xi = w(i-0.5) f(x) x 0 0.1 0.2 xi1
Parallel Programming Models Sequential Code #define f(x) 4.0/(1.0+x*x); main(){ int n,i; float w,x,sum,pi; printf(“n?\n”); scanf(“%d”, &n); w=1.0/n; sum=0.0; for (i=1; i<=n; i++){ x=w*(i-0.5); sum += f(x); } pi=w*sum; printf(“%f\n”, pi); } f(x) x 0 0.1 0.2 xi1 P = w ∑ f(xi) f(x) = 4/(1+x2) n = 10 w = 1/n xi = w(i-0.5)
Parallel Virtual Machine (PVM) www.netlib.org/pvm3
/* worker */ #include <stdio.h> #include <pvm3> #define f(x) 4.0/(1.0+x*x); main(){ int mytid, nproc, nt, msgtype, tids[10]; int n,I, start, end; float w,x,sum; msgtype=1; pvm_recv(pvm_parent(), msgtype); pvm_upkint(&myid, 1, 1); pvm_upkint(&nproc, 1, 1); pvm_upkint(tids, nproc, 1); pvm_upkint(&n, 1, 1); w=1.0/n; sum=0.0; start = myid*(n/nproc); end = (myid+1)*(n/nproc); for (i=start; i<end; i++){ x = w*(i-0.5); sum += f(x); } msgtype=2; pvm_initsend(PvmTaskDefault); pvm_pkfloat(&sum, 1, 1); pvm_send(pvm_parent(), msgtype); pvm_exit(); } Parallel PVM Code /* master */ #include <stdio.h> #include <pvm3> #define WORKER “/home/esin/worker” main(){ int mytid, nproc, nt, msgtype, tids[10]; int n,i; float w,sum,pi; mytid = pvm_mytid(); nproc = 4; nt = pvm_spawn(WORKER, NULL, PvmTaskDefault, “”, nproc, tids); printf(“%d - spawned %d tasks\n”, mytid, nt); n = 10; msgtype=1; for (i=0; i<nproc; i++){ pvm_initsend(PvmTaskDefault); pvm_pkint(&i, 1, 1); pvm_pkint(&nproc, 1, 1); pvm_pkint(tids, nproc, 1); pvm_pkint(&n, 1, 1); pvm_send(tids[i], msgtype); } w=1.0/n; sum=0.0; msgtype=2; for (i=0; i<nproc; i++){ pvm_recv(-1, msgtype); pvm_upkfloat(&result, 1, 1); sum += result; } pi=w*sum; printf(“%f\n”, pi); pvm_exit(); }
Parallel Programming Models Parallel PVM program Master: • Creates workers • Sends initial values to workers • Receives local “sum”s from workers • Calculates and prints “pi” Workers: • Receive initial values from master • Calculate local “sum”s • Send local “sum”s to Master Master W0 W1 W2 W3 Master
Parallel Virtual Machine (PVM) Data Distribution f(x) f(x) x x 0 0.1 0.2 xi1 0 0.1 0.2 xi1
/* worker */ #include <stdio.h> #include <pvm3> #define f(x) 4.0/(1.0+x*x); main(){ int mytid,nproc, nt, msgtype,tids[10]; int n,i, start, end; float w,x,sum, result; msgtype=1; pvm_recv(pvm_parent(), msgtype); pvm_upkint(&myid, 1, 1); pvm_upkint(&nproc, 1, 1); pvm_upkint(tids, nproc, 1); pvm_upkint(&n, 1, 1); w=1.0/n; sum=0.0; start = myid*(n/nproc); end = (myid+1)*(n/nproc); for (i=start; i<end; i++){ x = w*(i-0.5); sum += f(x); } if (myid==0) { msgtype=2; for (i=1; i<nproc; i++){ pvm_recv(tids[i], msgtype); pvm_upkfloat(&result, 1, 1); sum += result; } pi=w*sum; msgtype=3; pvm_initsend(PvmTaskDefault); pvm_pkfloat(&sum, 1, 1); pvm_send(pvm_parent(), msgtype); } else { msgtype=2; pvm_initsend(PvmTaskDefault); pvm_pkfloat(&sum, 1, 1); pvm_send(tids[0], msgtype); } pvm_exit(); } SPMD Parallel PVM Code /* master */ #include <stdio.h> #include <pvm3> #define WORKER “/home/esin/worker” main(){ int mytid, nproc, nt, msgtype, tids[10]; int n,i; float w,sum,pi; mytid = pvm_mytid(); nproc = 4; nt = pvm_spawn(WORKER, NULL, PvmTaskDefault, “”, nproc, tids); printf(“%d - spawned %d tasks\n”, mytid, nt); n = 10; msgtype=1; for (i=0; i<nproc; i++){ pvm_initsend(PvmTaskDefault); pvm_pkint(&i, 1, 1); pvm_pkint(&nproc, 1, 1); pvm_pkint(tids, nproc, 1); pvm_pkint(&n, 1, 1); pvm_send(tids[i], msgtype); } msgtype=3; pvm_recv(tids[0], msgtype); pvm_upkfloat(&pi, 1, 1); printf(“%f\n”, pi); pvm_exit(); }
Parallel Programming Models SPMD Parallel PVM program Master: • Creates workers • Sends initial values to workers • Receives “pi” from W0 and prints Workers: • Receive initial values from master • Calculate local “sum”s • Workers other than W0: • Send local “sum”s to W0 • W0: • Receives local “sum”s from other workers • Calculates “pi” • Sends “pi” to Master Master W0 W1 W2 W3 Master
Parallel Virtual Machine (PVM) C Interface #include “pvm3.h” Process Control int tid = pvm_mytid(void) int info = pvm_exit(void) int info = pvm_kill(int tid) int info = pvm_addhosts(char **hosts, int nhost, int *infos) int info = pvm_delhosts(char **hosts, int nhost, int *infos) int numt = pvm_spawn(char *task, char **argv, int flag, char *where, int ntask, int *tids) Information int tid = pvm_parent(void) int dtid = pvm_tidtohost(int tid) int info = pvm_perror(char *msg) int info = pvm_config(int *nhost, int *narch, struct hostinfo **hostp) int info = pvm_tasks(int which, int *ntask, struct taskinfo **taskp) int val = pvm_getopt(int what) int oldval = pvm_setopt(int what, int val)
Parallel Virtual Machine (PVM) C Interface Signalling int info = pvm_sendsig(int tid, int signum) int info = pvm_notify(int about, int msgtag, int ntask, int *tids) Message Buffers int bufid = pvm_mkbuf(int encoding) int info = pvm_freebuf(int bufid) int bufid = pvm_getsbuf(void) int bufid = pvm_getrbuf(void) int oldbuf = pvm_setsbuf(int bufid) int oldbuf = pvm_setrbuf(int bufid) int bufid = pvm_initsend(int encoding) int info = pvm_upklong(long *np, int cnt, int std ) int info = pvm_upkshort(short *np, int cnt, int std ) int info = pvm_upkstr(char *cp )
Parallel Virtual Machine (PVM) C Interface Sending int info = pvm_packf( printflike format... ) int info = pvm_pkbyte( char *cp, int cnt, int std ) int info = pvm_pkcplx( float *xp, int cnt, int std ) int info = pvm_pkdcplx( double *zp, int cnt, int std ) int info = pvm_pkdouble(double *dp, int cnt, int std ) int info = pvm_pkfloat( float *fp, int cnt, int std ) int info = pvm_pkint( int *np, int cnt, int std ) int info = pvm_pklong( long *np, int cnt, int std ) int info = pvm_pkshort( short *np, int cnt, int std ) int info = pvm_pkstr( char *cp ) int info = pvm_send( int tid, int msgtag ) int info = pvm_mcast( int *tids, int ntask, int msgtag ) int info = pvm_psend( int tid, int msgtag, void *vp, int cnt, int type ) Receiving int bufid = pvm_recv( int tid, int msgtag ) int bufid = pvm_probe( int tid, int msgtag ) int bufid = pvm_nrecv( int tid, int msgtag ) int bufid = pvm_precv( int tid, int msgtag, void *vp, int cnt, int type int *rtid, int *rtag, int *rlen ) int bufid = pvm_trecv( int tid, int msgtag, struct timeval *tmout ) int info = pvm_bufinfo( int bufid, int *bytes, int *msgtag, int *tid) int info = pvm_unpackf( printflike format... ) int info = pvm_upkbyte( char *cp, int cnt, int std ) int info = pvm_upkcplx( float *xp, int cnt, int std ) int info = pvm_upkdcplx( double *zp, int cnt, int std ) int info = pvm_upkdouble(double *dp, int cnt, int std ) int info = pvm_upkfloat( float *fp, int cnt, int std ) int info = pvm_upkint( int *np, int cnt, int std ) int info = pvm_upklong( long *np, int cnt, int std ) int info = pvm_upkshort( short *np, int cnt, int std ) int info = pvm_upkstr( char *cp )
Parallel Virtual Machine (PVM) C Interface Group Operations int inum = pvm_joingroup(char *group) int info = pvm_lvgroup(char *group) int size = pvm_gsize(char *group) int tid = pvm_gettid(char *group, int inum) int inum = pvm_getinst(char *group, int tid) int info = pvm_barrier(char *group, int count) int info = pvm_bcast(char *group, int msgtag) int info = pvm_reduce(void *op, void *vp, int cnt, int type, int msgtag, char *group, int root) op options vp type options PvmMax PVM—BYTE PVM—FLOAT PvmMin PVM—SHORT PVM—DOUBLE PvmSum PVM—INT PVM—CPLX PvmProduct PVM—LONG PVM—DCPLX
Parallel Virtual Machine (PVM) PVM Console Commands help [command] -- get information about commands conf -- lists hosts in virtual machine add host(s) -- add host(s) to virtual machine delete host(s) -- delete host(s) spawn [opt] file -- spawn process !count? -- number of tasks to spawn !host? -- host to spawn on ? -- redirect task output to console ?file -- redirect task output to file ??file -- append task output to file ps [a] -- lists processes on virtual machine alias -- define/list command aliases unalias -- undefine command alias setenv -- set/show environment variables echo -- echo arguments version -- print libpvm version id -- print console tid sig num tid -- send signal num to process kill tid -- terminate a process reset -- kill all processes and reset PVM quit -- exit console (PVM continues) halt -- kill all pvmds and console C Interface Starting PVM pvmd [nhostname] [d!debugmask?] [hostfile] pvm [hostfile] (starts console) Compiling PVM Applications cc o task myprog.c libpvm3.a For groups add libgpvm3.a before libpvm3.a 1