fixed compilation errors in Sameer's half abandoned comlib interface calling persiste...
[charm.git] / src / conv-com / graphrouter.C
1 /*****************************************************************************
2  * $Source$
3  * $Author$
4  * $Date$
5  * $Revision$
6  *****************************************************************************/
7
8 #include "graphrouter.h"
9 #include "hypercubetopology.h"
10
11 #define gmap(pe) {if (gpes) pe=gpes[pe];}
12
13 GraphRouter::GraphRouter(int n, int me){
14     init(n, me, new HypercubeTopology(n, me));
15 }
16
17 void GraphRouter::init(int n, int me, TopologyDescriptor *tp)
18 {  
19     NumPes=n;
20     MyPe=me;
21     gpes=NULL;
22     this->tp = tp;
23     
24     PeGraph = new PeTable(NumPes);
25     pesToSend = new int[NumPes];
26     nstages = tp->getNumStages() + 1;
27     currentIteration = 0;
28     
29     stageComplete = new int[nstages];
30     recvExpected = new int[nstages];
31     recvCount = new int[nstages];
32
33     memset(stageComplete, 0, nstages * sizeof(int));
34     memset(recvCount, 0, nstages * sizeof(int));
35     
36     for(int count = 1; count < nstages; count++)
37         recvExpected[count] = tp->getNumMessagesExpected(count);
38     
39     curStage = 0;
40     ComlibPrintf("me=%d NUMPES=%d nstages=%d\n", MyPe, n, nstages);
41 }
42
43 GraphRouter::~GraphRouter()
44 {
45     delete PeGraph;
46     delete pesToSend;
47     delete tp;
48     delete [] stageComplete;
49     delete [] recvExpected;
50     delete [] recvCount;
51     delete [] neighborPeList;
52 }
53
54 void GraphRouter::NumDeposits(comID, int num)
55 {
56 }
57
58 void GraphRouter::EachToAllMulticast(comID id, int size, void *msg, int more)
59 {
60     int npe=NumPes;
61     int * destpes=(int *)CmiAlloc(sizeof(int)*npe);
62     for (int i=0;i<npe;i++) destpes[i]=i;
63     EachToManyMulticast(id, size, msg, npe, destpes, more);
64 }
65
66 void GraphRouter::sendMessages(comID id, int cur_stage){
67     int nsteps = tp->getNumSteps(cur_stage);
68     int nextpe = 0, npestosend = 0;
69     
70     for(int stepcount = 0; stepcount < nsteps; stepcount ++){
71         tp->getPesToSend(stepcount, cur_stage, npestosend, pesToSend, nextpe);
72         
73         gmap(nextpe);
74         ComlibPrintf("%d:sending to %d for %d pes in stage %d\n", MyPe, nextpe, npestosend, cur_stage);
75
76         int len;
77         char *newmsg;
78         newmsg=PeGraph->ExtractAndPack(id, cur_stage + 1, npestosend, 
79                                        pesToSend, &len);
80         
81 #if CMK_PERSISTENT_COMM
82 /*
83         if(len < PERSISTENT_BUFSIZE)
84             if(currentIteration % 2)
85                 CmiUsePersistentHandle(&handlerArrayOdd[cur_stage], 1);
86             else
87                 CmiUsePersistentHandle(&handlerArrayEven[cur_stage], 1);
88 */
89 #endif          
90         
91         if (newmsg) {
92             if(cur_stage < nstages - 2)
93                 CmiSetHandler(newmsg, CkpvAccess(RecvHandle));
94             else
95                 CmiSetHandler(newmsg, CkpvAccess(ProcHandle));
96
97             CmiSyncSendAndFree(nextpe, len, newmsg);
98         }
99         else {
100             SendDummyMsg(id, nextpe, cur_stage + 1);
101         }
102         
103 #if CMK_PERSISTENT_COMM
104 /*
105         if(len < PERSISTENT_BUFSIZE)
106             CmiUsePersistentHandle(NULL, 0);
107 */
108 #endif          
109     }
110 }
111
112 void GraphRouter::EachToManyMulticast(comID id, int size, void *msg, 
113                                       int numpes, int *destpes, int more)
114 {
115     PeGraph->InsertMsgs(numpes, destpes, size, msg);
116     if (more) return;
117
118     ComlibPrintf("All messages received %d\n", MyPe);
119     sendMessages(id, 0);
120
121     curStage = 1;
122
123     int stage_itr;
124     for(stage_itr = curStage; stage_itr < nstages - 1; stage_itr ++){
125         if(stageComplete[stage_itr]){
126             sendMessages(id, stage_itr);
127             stageComplete[stage_itr] = 0;
128         }
129         else break;
130     }
131     curStage = stage_itr;
132     if(curStage == nstages - 1)
133         ProcManyMsg(id, NULL);
134     else 
135         PeGraph->ExtractAndDeliverLocalMsgs(MyPe);
136 }
137
138 void GraphRouter::RecvManyMsg(comID id, char *msg)
139 {
140     int stage = 0;
141     stage = PeGraph->UnpackAndInsert(msg);
142     
143     recvCount[stage] ++;
144     if (recvCount[stage] == recvExpected[stage]) {
145         ComlibPrintf("%d recvcount=%d recvexpected = %d stage=%d\n", MyPe, recvCount[stage], recvExpected[stage], stage);
146         
147         recvCount[stage] = 0;
148         stageComplete[stage] = 1;
149     }
150     
151     int stage_itr;
152     for(stage_itr = curStage; stage_itr < nstages - 1; stage_itr ++){
153         if(stageComplete[stage_itr]){
154             sendMessages(id, stage_itr);
155             stageComplete[stage_itr] = 0;
156         }
157         else break;
158     }
159     curStage = stage_itr;
160     if(curStage == nstages - 1)
161         ProcManyMsg(id, NULL);
162     else 
163         PeGraph->ExtractAndDeliverLocalMsgs(MyPe);
164 }
165
166 void GraphRouter::DummyEP(comID id, int stage)
167 {
168     if(stage < nstages - 1) {
169         recvCount[stage] ++;
170         if (recvCount[stage] == recvExpected[stage]) {
171             ComlibPrintf("%d DUMMY recvcount=%d recvexpected = %d\n", MyPe, recvCount[stage], recvExpected[stage]);
172             recvCount[stage] = 0;
173             stageComplete[stage] = 1;
174         }
175
176         int stage_itr;
177         for(stage_itr = curStage; stage_itr < nstages - 1; stage_itr ++){
178             if(stageComplete[stage_itr]){
179                 sendMessages(id, stage_itr);
180                 stageComplete[stage] = 0;
181             }
182             else break;
183         }
184         curStage = stage_itr;
185         if(curStage == nstages - 1)
186             ProcManyMsg(id, NULL);
187         else 
188             PeGraph->ExtractAndDeliverLocalMsgs(MyPe);
189     }
190     else 
191         ProcManyMsg(id, NULL);
192 }
193
194 void GraphRouter:: ProcManyMsg(comID id, char *m)
195 {
196     int stage = nstages - 1;
197     if(m) {
198         PeGraph->UnpackAndInsert(m);
199         recvCount[stage] ++;
200     }
201
202     if(recvCount[stage] == recvExpected[stage]) {
203         ComlibPrintf("%d proc many msg %d\n", MyPe, stage);
204         stageComplete[stage] = 1;
205     }
206     else 
207         return;
208     
209     if(curStage != nstages -1)
210         return;
211
212     currentIteration ++;
213     recvCount[stage] = 0;
214     PeGraph->ExtractAndDeliverLocalMsgs(MyPe);
215     
216     PeGraph->Purge();
217     curStage = 0;
218     Done(id);
219 }
220
221 Router * newgraphobject(int n, int me)
222 {
223     ComlibPrintf("In create graph router \n");
224     Router *obj = new GraphRouter(n, me);
225     return(obj);
226 }
227
228 void GraphRouter :: SetMap(int *pes)
229 {
230     gpes=pes;
231
232 #if CMK_PERSISTENT_COMM
233 /*
234     numNeighbors=0;
235     neighborPeList = new int[NumPes];
236
237     tp->getNeighbors(numNeighbors, neighborPeList);
238     handlerArrayOdd = new PersistentHandle[numNeighbors];
239     handlerArrayEven = new PersistentHandle[numNeighbors];
240
241     //Persistent handlers for all the neighbors
242     int pcount = 0;
243     for (pcount = 0; pcount < numNeighbors; pcount++) {
244         int dest = neighborPeList[pcount];
245         gmap(dest);
246         ComlibPrintf("%d:Creating Persistent Buffer of size %d at %d\n", MyPe,
247                      PERSISTENT_BUFSIZE, dest);
248         handlerArrayOdd[pcount] = CmiCreatePersistent(dest, 
249                                                       PERSISTENT_BUFSIZE);
250         ComlibPrintf("%d:Creating Even Persistent Buffer of size %d at %d\n",
251                      MyPe, PERSISTENT_BUFSIZE, dest);
252         handlerArrayEven[pcount] = CmiCreatePersistent(dest, 
253                                                        PERSISTENT_BUFSIZE);
254     }
255 */
256 #endif
257 }