Hello World (PVM)
May 7th, 2008Contoh parallel processing di PVM seperti ini …. (nyontek dari PVM sample).
Satu node/cluster akan berperan sebagai master, dan lainya akan menjadi slave. Pada saat program dijalankan, master akan memanggil program hello_other di semua node yang tergabung dalam pvm, lalu slave akan mengirimkan pesan “hello, world from + (hostname dari slave)” ke master. Pesan akan diterima master dan ditampilkan di console pvm master.
Untuk master :
#include
#include “pvm3.h”
main()
{
int cc, tid;
char buf[100];
printf(”i’m t%x\n”, pvm_mytid());
cc = pvm_spawn(”hello_other”, (char**)0, 0, “”, 1, &tid);
if (cc == 1) {
cc = pvm_recv(-1, -1);
pvm_bufinfo(cc, (int*)0, (int*)0, &tid);
pvm_upkstr(buf);
printf(”from t%x: %s\n”, tid, buf);
} else
printf(”can’t start hello_other\n”);
pvm_exit();
exit(0);
}
untuk Slave :
#include “pvm3.h”
main()
{
int ptid;
char buf[100];
ptid = pvm_parent();
strcpy(buf, “hello, world from “);
gethostname(buf + strlen(buf), 64);
pvm_initsend(PvmDataDefault);
pvm_pkstr(buf);
pvm_send(ptid, 1);
pvm_exit();
exit(0);
}