inefficiency fixed
[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     procMap = new int[CkNumPes()];
105     
106     memcpy(pelist, _pelist, sizeof(int) * npes);    
107     setReverseMap();
108
109     ComlibPrintf("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                                                      myPe, 
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                 
215                 ComlibPrintf("%d: Insert Pers. Message to %d\n", CkMyPe(), procMap[cmsg->dest_proc]);
216                              router->EachToManyMulticast(id, cmsg->size, msg, 1,
217                                             &procMap[cmsg->dest_proc],
218                                             numToDeposit > 1);
219             }            
220         }   
221         else
222             router->EachToManyMulticast(id, cmsg->size, msg, 1, &myPe, 
223                                         numToDeposit > 1);
224         
225         numToDeposit --;
226         delete cmsg;        
227     }
228
229     while(!recvQ.isEmpty()) {
230         char *msg = recvQ.deq();
231         RecvManyMsg(msg);
232     }
233
234     while(!procQ.isEmpty()) {
235         char *msg = procQ.deq();
236         ProcManyMsg(msg);
237     }
238
239     while(!dummyQ.isEmpty() > 0) {
240         DummyMsg *m = dummyQ.deq();
241         router->DummyEP(m->id, m->magic);
242         CmiFree(m);
243     }
244 }
245
246 void RouterStrategy::Done(DummyMsg *m){
247
248     ComlibPrintf("%d: Finished iteration\n", CkMyPe());
249
250     if(doneHandle > 0) {
251         CmiSetHandler(m, doneHandle);
252         CmiSyncSendAndFree(CkMyPe(), sizeof(DummyMsg), (char*)m);
253     }
254     else
255         CmiFree(m);
256
257     doneFlag = 1;
258
259     if(bufferedDoneInserting)
260         doneInserting();
261 }
262
263
264 //Implement it later while implementing checkpointing of Comlib
265 void RouterStrategy::pup(PUP::er &p){}
266
267 //Call the router functions
268 void RouterStrategy::RecvManyMsg(char *msg) {
269
270     //comID new_id;
271     int new_refno =0;
272
273     //FOO BAR when structure of comid changes this will break !!!!!
274     ComlibPrintf("In RecvManyMsg at %d\n", CkMyPe());
275     //memcpy(&new_id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
276     //ComlibPrintf("REFNO = %d, %d\n", new_id.refno, id.refno);
277     
278     //First int in comid is refno
279     memcpy(&new_refno, (char*) msg + CmiReservedHeaderSize + sizeof(int), 
280            sizeof(int)); 
281
282     if(new_refno != id.refno)
283         recvQ.push(msg);
284     else
285         router->RecvManyMsg(id, msg);
286 }
287
288 void RouterStrategy::ProcManyMsg(char *msg) {    
289
290     //comID new_id;
291     int new_refno =0;
292     ComlibPrintf("In ProcManyMsg at %d\n", CkMyPe());
293     //memcpy(&new_id,(msg+CmiReservedHeaderSize+sizeof(int)), sizeof(comID));
294     //First int in comid is refno
295     memcpy(&new_refno, (char*) msg + CmiReservedHeaderSize + sizeof(int), 
296            sizeof(int)); 
297     
298     if(new_refno != id.refno)
299         procQ.push(msg);
300     else
301         router->ProcManyMsg(id, msg);
302 }
303
304 void RouterStrategy::DummyEP(DummyMsg *m) {
305
306     if(id.refno != m->id.refno)
307         dummyQ.push(m);
308     else {
309         router->DummyEP(m->id, m->magic);
310         CmiFree(m);
311     }
312 }
313
314 PUPable_def(RouterStrategy);