// ************************************************************** // * * // * Spheres -- Grid.cpp * // * * // * (C) Christian Scheideler, 2003 * // * * // ************************************************************** // Grid.cpp generates an arbitrary nxn-grid and sends sync messages in it // 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" // 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 degree; // own degree int round; // current round int *current; // neighbor syncs for current round int *next; // neighbor syncs for next round Node **neighbor; // pointers to neighbors int *neighID; // IDs of neighbors public: Node(int u) { ID = u; } void Connect(int d, Node** np, int *IDs) { int v; degree = d; neighbor = np; neighID = IDs; // initialize sync variables round = 1; current = new int[degree]; next = new int[degree]; for (v=0; vround << ") from " << p->ID << "\n"; // locate neighbor in list v=0; while (neighID[v] != p->ID) v++; // sync if (p->round == round) { current[v] = 1; complete = 1; for (v=0; v> n; // generate set of node objects List = new (Node *)[n*n]; for (v=0; vConnect(deg, NL, NID); } } // generate requests to call Node::Start in every node of the list for (v=0; v> i; // just to see the output }