// ************************************************************** // * * // * Spheres -- Sync.cpp * // * * // * (C) Christian Scheideler, 2003 * // * * // ************************************************************** // Sync.cpp demonstrates how to synchronize in some example graph // Special definitions and operations provided by Spheres.h: // // - Sphere: class that enables non-blocking method invocations (NMI). // To ensure the correct execution of NMIs, every method in a class // derived from Sphere that is to be called by NMI has to have the form // // void MethodName(usertype *plist) // // where plist is a pointer to any kind of usertype. // // - void Send(SpherePtr, SphereObj::Method, ParList): // This requests to call the method SphereObj::Method in the user object // referenced by SpherePtr using the parameter list referenced by ParList. // // - void Simulate(Time): // This runs a simulation for Time many rounds. // The Spheres.h class provides an environment for the simulation of // concurrent data structures, where concurrent executions are done at the // level of method invocations (i.e. each method invocation completes before // another method is invoked). To avoid inconsistencies in the data structure, // some general rules have to be obeyed: // // - A method may not have any side-effects other than modifying the // data inside the object. // // - A method must be total, meaning that it is well-defined for // EVERY legal state of the object. #include #include #include "Spheres.h" // some global values #define SIZE 6 // adjacency matrix of graph int A[SIZE][SIZE] = {{0,1,1,0,0,0}, {1,0,1,1,1,0}, {1,1,0,0,1,0}, {0,1,0,0,1,1}, {0,1,1,1,0,1}, {0,0,0,1,1,0}}; // parameter list for Node::Sync method class SPList { public: int ID; // origin of sync message int round; // round number of sync SPList(int i, int r) { ID = i; round = r; } }; class Node: public Sphere { private: int ID; // own ID int round; // current round int current[SIZE]; // neighbor syncs for current round int next[SIZE]; // neighbor syncs for next round Node **neighbor; // array of pointers to neighbors int cost[SIZE]; // cost of edges (just 0 or 1 here) public: Node(int u) { int v; // set node ID and initial round ID = u; round = 1; // initialize edge costs and sync indicators for (v=0; vround << ") from " << p->ID << "\n"; if (p->round == round) { current[p->ID] = 1; complete = 1; for (v=0; v0 && current[v]==0) complete = 0; if (complete) // received syncs from all neighbors? { // go to next round cout << ID << ": all syncs for round " << round << " received \n"; round++; // reset sync indicators for (v=0; v0) { lp = new SPList(ID, round); Send(neighbor[v], Node::Sync, lp); } } } else next[p->ID] = 1; delete p; } void Start(void *p) { int v; SPList *lp; // send syncs to all neighbors for (v=0; v0) { lp = new SPList(ID, round); Send(neighbor[v], Node::Sync, lp); } } }; void main() { int i; // generate set of node objects Node **List = new (Node *)[SIZE]; for (i=0; iConnect(List); // generate requests to call Node::Start in every node of the list for (i=0; i> i; }