// ************************************************************** // * * // * Spheres -- Balance.cpp * // * * // * (C) Christian Scheideler, 2003 * // * * // ************************************************************** // Balance.cpp demonstrates the T-balancing algorithm on 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 8 // adjacency matrix of graph int A[SIZE][SIZE] = {{0,1,0,0,1,1,0,0}, {1,0,1,0,0,1,0,0}, {0,1,0,1,1,0,0,0}, {0,0,1,0,0,0,1,1}, {1,0,1,0,0,0,0,1}, {1,1,0,0,0,0,1,0}, {0,0,0,1,0,1,0,1}, {0,0,0,1,1,0,1,0}}; // parameter list for Node::Sync method class SPList { public: int ID; // origin of sync message int round; // round number of sync float val; // flow or height value SPList(int i, int r, float v) { ID = i; round = r; val = v; } }; class Node: public Sphere { private: int ID; // own ID float height; // own height int T; // threshold int d; // max. degree 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) float nheight[SIZE]; // current height of neighbors public: Node(int u, int th, int deg) { int v; // set node ID and initialize values ID = u; height = 0; T = th; d = deg; round = 1; // initialize edge costs and sync indicators for (v=0; v0) { flow = (height-nheight[v]-T*d)/(1.0*d); if (flow<0) flow = 0; if (flow>1) flow = 1; cout << ID+1 << ": send " << flow << " flow to " << v+1 << "\n"; total += flow; lp = new SPList(ID, round, flow); Send(neighbor[v], Node::Sync, lp); } height -= total; // subtract sent flow from buffer } void SendHeight() { int v; SPList *lp; if (ID == 0) height +=2; // source node: inject new flow if (ID == 7) height = 0; // destination node: absorb all flow // send height to neighbors for (v=0; v0) { cout << ID+1 << ": send height " << height << " to " << v+1 << "\n"; lp = new SPList(ID, round, height); Send(neighbor[v], Node::Sync, lp); } } void Sync(SPList *p) { int v, complete; SPList *lp; if (p->round % 2) height += p->val; // odd round: add flow from neighbor else nheight[p->ID] = p->val; // even round: update neighbor height 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 round++; if (round % 2) SendFlows(); // odd round: send flows else SendHeight(); // even round: send height // reset sync indicators for (v=0; vID] = 1; delete p; } void Start(void *p) { // send flows to neighbors cout << "start " << ID+1 << "\n"; SendFlows(); } }; void main() { int i, T, d; cout << "Enter T and d:\n"; cin >> T; cin >> d; // 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; }