Hello world example
The code can be downloaded from: hello.c:
#include <stdio.h> #include <pvm3.h> int main() { int mytid; mytid = pvm_mytid(); printf("My TID is %d\n", mytid); pvm_exit(); return 0; }
In order to compile the example issue the following command:
$ gcc -lpvm3 -o hello hello.c
And then run the hello program.
Master/slave
This is a simple PVM application, consisting of two processes ― master and slave ― communicating with each other.
The master
The code can be downloaded from: master.c:
#include <stdio.h> #include <stdlib.h> #include <pvm3.h> int main() { int myTID; int x = 12; int children[10]; int res; myTID = pvm_mytid(); printf("Master: TID is %d\n", myTID); res = pvm_spawn("slave", NULL, PvmTaskDefault, "", 1, children); if (res<1) { printf("Master: pvm_spawn error\n"); pvm_exit(); exit(1); } pvm_initsend(PvmDataDefault); pvm_pkint(&x, 1, 1); pvm_send(children[0], 1); pvm_recv(-1, -1); pvm_upkint(&x, 1, 1); printf("Master has received x=%d\n", x); pvm_exit(); return 0; }
The slave
The code can be downloaded from: slave.c:
#include <stdio.h> #include <pvm3.h> int main() { int myTID, masterTID; int x = 12; myTID = pvm_mytid(); printf("Slave: TID is %d\n", myTID); pvm_recv(-1, -1); pvm_upkint(&x, 1, 1); printf("Slave has received x=%d\n", x); sleep(3); x = x + 5; pvm_initsend(PvmDataDefault); pvm_pkint(&x, 1, 1); pvm_send(pvm_parent(), 1); pvm_exit(); return 0; }
Ring
The following ring.c example initializes communication in a
ring of processes:
#include <stdio.h> #include <pvm3.h> #define PROC_NUM 3 int main() { int myTID; int children[PROC_NUM]; int res, i; myTID = pvm_mytid(); printf("Master: TID is %d\n", myTID); res = pvm_spawn("ring-slave", NULL, PvmTaskDefault, "", PROC_NUM, children); if (res < 1) { printf("Master: pvm_spawn error (res=%d)\n", res); pvm_exit(); exit(1); } // send neighbors' TIDs for(i=0; i<PROC_NUM; i++) { pvm_initsend(PvmDataDefault); pvm_pkint(&children[(i-1+PROC_NUM)%PROC_NUM], 1, 1); pvm_pkint(&children[(i+1)%PROC_NUM], 1, 1); pvm_send(children[i], 1); } // test the ring i=1; pvm_initsend(PvmDataDefault); pvm_pkint(&i, 1, 1); pvm_send(children[0], 1); pvm_exit(); return 0; }
The slave process is ring-slave.c:
#include <stdio.h> #include <pvm3.h> int main() { int myTID, leftTID, rightTID, senderTID; int res, i, bufid; FILE* cons; cons = fopen("/dev/pts/2", "a"); myTID = pvm_mytid(); sleep(1); fprintf(cons, "\nSlave [%d]: started\n", myTID); // get neighbors' TIDs pvm_recv(-1, -1); pvm_upkint(&leftTID, 1, 1); pvm_upkint(&rightTID, 1, 1); fprintf(cons, "Slave [%d]: left=%d right=%d\n", myTID, leftTID, rightTID); sleep(1); while(1) { bufid = pvm_recv(-1, -1); pvm_upkint(&i, 1, 1); pvm_bufinfo(bufid, NULL, NULL, &senderTID); fprintf(cons, "Slave [%d]: msg %2d from %d\n", myTID, i, senderTID); sleep(1); i++; pvm_initsend(PvmDataDefault); pvm_pkint(&i, 1, 1); pvm_send(rightTID, 1); if (i>20) break; } pvm_exit(); return 0; }
Example configuration file (hostfile)
# comment line lab-430-1.cs.put.poznan.pl lab-430-2.cs.put.poznan.pl lab-430-3.cs.put.poznan.pl # different user and password lab-430-13.cs.put.poznan.pl lo=user_pvm so=pw # different pvmd path lab-430-14.cs.put.poznan.pl dx=/bin/progs/pvm/pvmd # different directory with executables lab-430-15.cs.put.poznan.pl ep=$HOME/xxx/pvm3/bin/PVM_ARCH # default option * lo=std_user lab-430-16.cs.put.poznan.pl lab-430-17.cs.put.poznan.pl lab-430-18.cs.put.poznan.pl * lo= # not in the initial configuration &lab-430-4.cs.put.poznan.pl &lab-430-5.cs.put.poznan.pl &lab-430-6.cs.put.poznan.pl