Fixed a memory leak and a added some other code optimizations.
[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 = pelist[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     procMap = new int[CkNumPes()];
105     
106     memcpy(pelist, _pelist, sizeof(int) * npes);    
107     setReverseMap();
108
109     //CkPrintf("Router Strategy : %d, MYPE = %d, NUMPES = %d \n", stratid, 
110     //       myPe, npes);
111
112     switch(stratid) {
113     case USE_TREE: 
114         router = new TreeRouter(npes, myPe);
115         break;
116         
117     case USE_MESH:
118         router = new GridRouter(npes, myPe);
119         break;
120         
121     case USE_HYPERCUBE:
122         router = new DimexRouter(npes, myPe);
123         break;
124         
125     case USE_GRID:
126         router = new D3GridRouter(npes, myPe);
127         break;
128
129     case USE_DIRECT: router = NULL;
130         break;
131         
132     default: CmiAbort("Unknown Strategy\n");
133         break;
134     }
135
136     if(router) {
137         router->SetMap(pelist);
138         router->setDoneHandle(myDoneHandle);
139         //router->SetID(id);
140     }
141
142     //Start with all iterations done
143     doneFlag = 1;
144
145     //No Buffered doneInserting at the begining
146     bufferedDoneInserting = 0;
147 }
148
149 void RouterStrategy::insertMessage(MessageHolder *cmsg){
150
151     if(routerID == USE_DIRECT) {
152         CmiSyncSendAndFree(cmsg->dest_proc, cmsg->size, cmsg->getMessage());
153         delete cmsg;
154     }
155     else
156         msgQ.push(cmsg);
157 }
158
159 void RouterStrategy::doneInserting(){
160     
161     id.instanceID = getInstance();
162
163     ComlibPrintf("Instance ID = %d\n", getInstance());
164
165     if(routerID == USE_DIRECT)
166         return;
167
168     ComlibPrintf("%d: DoneInserting \n", CkMyPe());
169     
170     if(doneFlag == 0) {
171         ComlibPrintf("%d:Waiting for previous iteration to Finish\n", 
172                      CkMyPe());
173         bufferedDoneInserting = 1;
174         return;
175     }
176     
177     doneFlag = 0;
178     bufferedDoneInserting = 0;
179
180     id.refno ++;
181
182     if(msgQ.length() == 0) {
183         if(routerID == USE_DIRECT)
184             return;
185
186         DummyMsg * dummymsg = (DummyMsg *)CmiAlloc(sizeof(DummyMsg));
187         ComlibPrintf("[%d] Creating a dummy message\n", CkMyPe());
188         CmiSetHandler(dummymsg, CkpvAccess(RecvdummyHandle));
189         
190         MessageHolder *cmsg = new MessageHolder((char *)dummymsg, 
191                                                      CkMyPe(), 
192                                                      sizeof(DummyMsg));
193         cmsg->isDummy = 1;
194         msgQ.push(cmsg);
195     }
196
197     int numToDeposit = msgQ.length();
198     
199     while(!msgQ.isEmpty()) {
200         MessageHolder *cmsg = msgQ.deq();
201         char *msg = cmsg->getMessage();
202         
203         if(!cmsg->isDummy)  {            
204             //Assuming list of processors to multicast to is in the
205             //order of relative processors numbering and NOT absolute
206             //processor numbering
207
208             if(cmsg->dest_proc == IS_MULTICAST) {  
209                 router->EachToManyMulticast(id, cmsg->size, msg, cmsg->npes, 
210                                             cmsg->pelist, 
211                                             numToDeposit > 1);
212             }            
213             else {                                
214                 router->EachToManyMulticast(id, cmsg->size, msg, 1,
215                                             &procMap[cmsg->dest_proc],
216                                             numToDeposit > 1);
217             }            
218         }   
219         else
220             router->EachToManyMulticast(id, cmsg->size, msg, 1, &myPe, 
221                                         numToDeposit > 1);
222         
223         numToDeposit --;
224         delete cmsg;        
225     }
226
227     while(!recvQ.isEmpty()) {
228         char *msg = recvQ.deq();
229         RecvManyMsg(msg);
230     }
231
232     while(!procQ.isEmpty()) {
233         char *msg = procQ.deq();
234         ProcManyMsg(msg);
235     }
236
237     while(!dummyQ.isEmpty() > 0) {
238         DummyMsg *m = dummyQ.deq();
239         router->DummyEP(m->id, m->magic);
240         CmiFree(m);
241     }
242 }
243
244 void RouterStrategy::Done(DummyMsg *m){
245
246     ComlibPrintf("%d: Finished iteration\n", CkMyPe());
247
248     if(doneHandle > 0) {
249         CmiSetHandler(m, doneHandle);
250         CmiSyncSendAndFree(CkMyPe(), sizeof(DummyMsg), (char*)m);
251     }
252     else
253         CmiFree(m);
254
255     doneFlag = 1;
256
257     if(bufferedDoneInserting)
258         doneInserting();
259 }
260
261
262 //Implement it later while implementing checkpointing of Comlib
263 void RouterStrategy::pup(PUP::er &p){}
264
265 //Call the router functions
266 void RouterStrategy::RecvManyMsg(char *msg) {
267
268     //comID new_id;
269     int new_refno =0;
270
271     //FOO BAR when structure of comid changes this will break !!!!!
272     ComlibPrintf("In RecvManyMsg at %d\n", CkMyPe());
273     //memcpy(&new_id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
274     //ComlibPrintf("REFNO = %d, %d\n", new_id.refno, id.refno);
275     
276     //First int in comid is refno
277     memcpy(&new_refno, (char*) msg + CmiReservedHeaderSize + sizeof(int), 
278            sizeof(int)); 
279
280     if(new_refno != id.refno)
281         recvQ.push(msg);
282     else
283         router->RecvManyMsg(id, msg);
284 }
285
286 void RouterStrategy::ProcManyMsg(char *msg) {    
287
288     //comID new_id;
289     int new_refno =0;
290     ComlibPrintf("In ProcManyMsg at %d\n", CkMyPe());
291     //memcpy(&new_id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
292     //First int in comid is refno
293     memcpy(&new_refno, (char*) msg + CmiReservedHeaderSize + sizeof(int), 
294            sizeof(int)); 
295     
296     if(new_refno != id.refno)
297         procQ.push(msg);
298     else
299         router->ProcManyMsg(id, msg);
300 }
301
302 void RouterStrategy::DummyEP(DummyMsg *m) {
303
304     if(id.refno != m->id.refno)
305         dummyQ.push(m);
306     else {
307         router->DummyEP(m->id, m->magic);
308         CmiFree(m);
309     }
310 }
311
312 PUPable_def(RouterStrategy);