// $Id$ -*- c++ -*- #include #include "pvm3.h" #include #include #include "matrix.h" #include "assoc.h" #include Matrix calculate (const Matrix &mA, const Matrix &mB) { for (int i = 0; i < 1000 * 1000 * 100; ++i) ; return mA * mB; } int main (int argc, char** argv) { // Get sibling addresses int tidParent = pvm_parent (); if (tidParent == PvmNoParent) { std::cerr << "worker started standalone" << std::endl; return pvm_exit (); } pvm_recv (tidParent, MSG_ID); int iSelf; pvm_upkint (&iSelf, 1, 1); pvm_recv (tidParent, MSG_RGTID); std::vector rgtid = pvm_unpack_rgi (); int n = rgtid.size (); // The actual implementation { pvm_recv (tidParent, MSG_INPUT); Matrix m = Matrix::pvm_unpack (); for (int t = 1; t < n; t *= 2) { int iDst = iSelf + t; if (iDst < n) // Send to the right { pvm_initsend (PvmDataDefault); m.pvm_pack (); pvm_send (rgtid[iDst], MSG_INTERMEDIATE); } int iSrc = iSelf - t; if (iSrc >= 0) // Recieve from the left, and apply function { pvm_recv (rgtid[iSrc], MSG_INTERMEDIATE); m = calculate (m, Matrix::pvm_unpack ()); } } // Send output to parent pvm_initsend (PvmDataDefault); m.pvm_pack (); pvm_send (tidParent, MSG_RESULT); } return pvm_exit(); }