Fixing bug in the router strategy. MyPe was not initialized.
[charm.git] / src / conv-com / routerstrategy.C
1
2 #include "routerstrategy.h"
3
4 CkpvDeclare(int, RecvHandle);
5 CkpvDeclare(int, ProcHandle);
6 CkpvDeclare(int, DummyHandle);
7
8 //Handlers that call the entry funtions of routers 
9 //Refer to router.h for details on these entry functions
10
11 //Correspods to Router::ProcManyMsg
12 void procManyCombinedMsg(char *msg)
13 {
14     //comID id;
15     int instance_id;
16
17     ComlibPrintf("In Recv combined message at %d\n", CkMyPe());
18     //memcpy(&id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
19
20     //Comid specific
21     memcpy(&instance_id, (char*) msg + CmiReservedHeaderSize + 2*sizeof(int)
22            , sizeof(int));
23
24     Strategy *s = ConvComlibGetStrategy(instance_id);
25     ((RouterStrategy *)s)->ProcManyMsg(msg);
26 }
27
28 //Correspods to Router::DummyEP
29 void dummyEP(DummyMsg *m)
30 {
31     Strategy *s = ConvComlibGetStrategy(m->id.instanceID);
32     
33     ((RouterStrategy *)s)->DummyEP(m);
34 }
35
36 //Correspods to Router::RecvManyMsg
37 void recvManyCombinedMsg(char *msg)
38 {
39     //comID id;
40     int instance_id;
41     ComlibPrintf("In Recv combined message at %d\n", CkMyPe());
42     //memcpy(&id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
43     
44     //Comid specific
45     memcpy(&instance_id, (char*) msg + CmiReservedHeaderSize + 2*sizeof(int)
46            , sizeof(int));
47
48     Strategy *s = ConvComlibGetStrategy(instance_id);
49     ((RouterStrategy *)s)->RecvManyMsg(msg);
50 }
51
52
53 void doneHandler(DummyMsg *m){
54     Strategy *s = ConvComlibGetStrategy(m->id.instanceID);
55     
56     ((RouterStrategy *)s)->Done(m);
57 }
58
59 void RouterStrategy::setReverseMap(){
60     int pcount;
61     for(pcount = 0; pcount < CkNumPes(); pcount++)
62         procMap[pcount] = -1;
63
64     //All processors not in the domain will point to -1
65     for(pcount = 0; pcount < npes; pcount++) {
66         if (pelist[pcount] == CkMyPe())
67             myPe = pcount;
68
69         procMap[pelist[pcount]] = pcount;
70     }
71 }
72
73 RouterStrategy::RouterStrategy(int stratid, int handle, int _npes, 
74                                int *_pelist) 
75     : Strategy(){
76     
77     setType(CONVERSE_STRATEGY);
78
79     CkpvInitialize(int, RecvHandle);
80     CkpvInitialize(int, ProcHandle);
81     CkpvInitialize(int, DummyHandle);
82
83     id.instanceID = 0; //Set later in doneInserting
84     
85     id.isAllToAll = 0;
86     id.refno = 0;
87
88     CkpvAccess(RecvHandle) =
89         CkRegisterHandler((CmiHandler)recvManyCombinedMsg);
90     CkpvAccess(ProcHandle) =
91         CkRegisterHandler((CmiHandler)procManyCombinedMsg);
92     CkpvAccess(DummyHandle) = 
93         CkRegisterHandler((CmiHandler)dummyEP);    
94
95     myDoneHandle = CkRegisterHandler((CmiHandler)doneHandler);    
96
97     //Array strategy done handle
98     doneHandle = handle;
99
100     routerID = stratid;
101
102     npes = _npes;
103     //pelist = new int[npes];
104     pelist = _pelist;
105     //memcpy(pelist, _pelist, sizeof(int) * npes);    
106
107     myPe = -1;
108     procMap = new int[CkNumPes()];    
109     setReverseMap();
110
111     bcast_pemap = NULL;
112
113     ComlibPrintf("Router Strategy : %d, MYPE = %d, NUMPES = %d \n", stratid, 
114                  myPe, npes);
115
116     if(myPe < 0) {
117         //I am not part of this strategy
118         
119         doneFlag = 0;
120         router = NULL;
121         bufferedDoneInserting = 0;
122         return;        
123     }
124
125     //Start with all iterations done
126     doneFlag = 1;
127     
128     //No Buffered doneInserting at the begining
129     bufferedDoneInserting = 0;
130
131     switch(stratid) {
132     case USE_TREE: 
133         router = new TreeRouter(npes, myPe);
134         break;
135         
136     case USE_MESH:
137         router = new GridRouter(npes, myPe);
138         break;
139         
140     case USE_HYPERCUBE:
141         router = new DimexRouter(npes, myPe);
142         break;
143         
144     case USE_GRID:
145         router = new D3GridRouter(npes, myPe);
146         break;
147
148     case USE_DIRECT: router = NULL;
149         break;
150         
151     default: CmiAbort("Unknown Strategy\n");
152         break;
153     }
154
155     if(router) {
156         router->SetMap(pelist);
157         router->setDoneHandle(myDoneHandle);
158         //router->SetID(id);
159     }
160 }
161
162
163 RouterStrategy::~RouterStrategy() {
164     //delete [] pelist;
165
166     if(bcast_pemap)
167         delete [] bcast_pemap;
168     
169     delete [] procMap;
170     if(router)
171         delete router;
172 }
173
174
175 void RouterStrategy::insertMessage(MessageHolder *cmsg){
176
177     if(myPe < 0)
178         CmiAbort("insertMessage: mype < 0\n");
179
180     int count = 0;
181     if(routerID == USE_DIRECT) {
182         if(cmsg->dest_proc == IS_BROADCAST) {
183             for(count = 0; count < cmsg->npes-1; count ++)
184                 CmiSyncSend(cmsg->pelist[count], cmsg->size, 
185                             cmsg->getMessage());
186             if(cmsg->npes > 0)
187                 CmiSyncSendAndFree(cmsg->pelist[cmsg->npes-1], cmsg->size, 
188                                    cmsg->getMessage());
189         }
190         else
191             CmiSyncSendAndFree(cmsg->dest_proc, cmsg->size, 
192                                cmsg->getMessage());
193         delete cmsg;
194     }
195     else {
196         if(cmsg->dest_proc >= 0) {
197             cmsg->pelist = &procMap[cmsg->dest_proc];
198             cmsg->npes = 1;
199         }
200         else if (cmsg->dest_proc == IS_BROADCAST){
201
202             if(bcast_pemap == NULL) {
203                 bcast_pemap = new int[npes];
204                 for(count = 0; count < npes; count ++) {
205                     bcast_pemap[count] = count;
206                 }
207             }
208
209             cmsg->pelist = bcast_pemap;
210             cmsg->npes = npes;
211         }
212         
213         msgQ.push(cmsg);
214     }
215 }
216
217 void RouterStrategy::doneInserting(){
218     
219     if(myPe < 0)
220         CmiAbort("insertMessage: mype < 0\n");
221
222     id.instanceID = getInstance();
223
224     //ComlibPrintf("Instance ID = %d\n", getInstance());
225     ComlibPrintf("%d: DoneInserting %d \n", CkMyPe(), msgQ.length());
226     
227     if(doneFlag == 0) {
228         ComlibPrintf("%d:Waiting for previous iteration to Finish\n", 
229                      CkMyPe());
230         bufferedDoneInserting = 1;
231         return;
232     }
233     
234     if(routerID == USE_DIRECT) {
235         DummyMsg *m = (DummyMsg *)CmiAlloc(sizeof(DummyMsg));
236         memset((char *)m, 0, sizeof(DummyMsg)); 
237         m->id.instanceID = getInstance();
238         
239         Done(m);
240         return;
241     }
242
243     doneFlag = 0;
244     bufferedDoneInserting = 0;
245
246     id.refno ++;
247
248     if(msgQ.length() == 0) {
249         if(routerID == USE_DIRECT)
250             return;
251
252         DummyMsg * dummymsg = (DummyMsg *)CmiAlloc(sizeof(DummyMsg));
253         ComlibPrintf("[%d] Creating a dummy message\n", CkMyPe());
254         CmiSetHandler(dummymsg, CkpvAccess(RecvdummyHandle));
255         
256         MessageHolder *cmsg = new MessageHolder((char *)dummymsg, 
257                                                      myPe, 
258                                                      sizeof(DummyMsg));
259         cmsg->isDummy = 1;
260         cmsg->pelist = &myPe;
261         cmsg->npes = 1;
262         msgQ.push(cmsg);
263     }
264
265     router->EachToManyMulticastQ(id, msgQ);
266
267     while(!recvQ.isEmpty()) {
268         char *msg = recvQ.deq();
269         RecvManyMsg(msg);
270     }
271
272     while(!procQ.isEmpty()) {
273         char *msg = procQ.deq();
274         ProcManyMsg(msg);
275     }
276
277     while(!dummyQ.isEmpty() > 0) {
278         DummyMsg *m = dummyQ.deq();
279         router->DummyEP(m->id, m->magic);
280         CmiFree(m);
281     }
282 }
283
284 void RouterStrategy::Done(DummyMsg *m){
285
286     ComlibPrintf("%d: Finished iteration\n", CkMyPe());
287
288     if(doneHandle > 0) {
289         CmiSetHandler(m, doneHandle);
290         CmiSyncSendAndFree(CkMyPe(), sizeof(DummyMsg), (char*)m);
291     }
292     else
293         CmiFree(m);
294
295     doneFlag = 1;
296
297     if(bufferedDoneInserting)
298         doneInserting();
299 }
300
301
302 //Implement it later while implementing checkpointing of Comlib
303 void RouterStrategy::pup(PUP::er &p){}
304
305 PUPable_def(RouterStrategy);