modified the registration of the converse handlers for propagate and propagate_frag.
[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     int count = 0;
152     if(routerID == USE_DIRECT) {
153         if(cmsg->dest_proc == IS_MULTICAST) {
154             for(count = 0; count < cmsg->npes-1; count ++)
155                 CmiSyncSend(cmsg->pelist[count], cmsg->size, 
156                             cmsg->getMessage());
157             if(cmsg->npes > 0)
158                 CmiSyncSendAndFree(cmsg->pelist[cmsg->npes-1], cmsg->size, 
159                                    cmsg->getMessage());
160         }
161         else
162             CmiSyncSendAndFree(cmsg->dest_proc, cmsg->size, 
163                                cmsg->getMessage());
164         delete cmsg;
165     }
166     else {
167         if(cmsg->dest_proc != IS_MULTICAST) {
168             cmsg->pelist = &procMap[cmsg->dest_proc];
169             cmsg->npes = 1;
170         }
171         else {
172             for(count = 0; count < cmsg->npes; count ++) {
173                 cmsg->pelist[count] = procMap[cmsg->pelist[count]];
174             }
175         }
176         
177         msgQ.push(cmsg);
178     }
179 }
180
181 void RouterStrategy::doneInserting(){
182     
183     id.instanceID = getInstance();
184
185     ComlibPrintf("Instance ID = %d\n", getInstance());
186
187     if(routerID == USE_DIRECT)
188         return;
189
190     ComlibPrintf("%d: DoneInserting \n", CkMyPe());
191     
192     if(doneFlag == 0) {
193         ComlibPrintf("%d:Waiting for previous iteration to Finish\n", 
194                      CkMyPe());
195         bufferedDoneInserting = 1;
196         return;
197     }
198     
199     doneFlag = 0;
200     bufferedDoneInserting = 0;
201
202     id.refno ++;
203
204     if(msgQ.length() == 0) {
205         if(routerID == USE_DIRECT)
206             return;
207
208         DummyMsg * dummymsg = (DummyMsg *)CmiAlloc(sizeof(DummyMsg));
209         ComlibPrintf("[%d] Creating a dummy message\n", CkMyPe());
210         CmiSetHandler(dummymsg, CkpvAccess(RecvdummyHandle));
211         
212         MessageHolder *cmsg = new MessageHolder((char *)dummymsg, 
213                                                      myPe, 
214                                                      sizeof(DummyMsg));
215         cmsg->isDummy = 1;
216         cmsg->pelist = &myPe;
217         cmsg->npes = 1;
218         msgQ.push(cmsg);
219     }
220
221     /*
222       int numToDeposit = msgQ.length();
223       
224       while(!msgQ.isEmpty()) {
225       MessageHolder *cmsg = msgQ.deq();
226       char *msg = cmsg->getMessage();
227       
228       if(!cmsg->isDummy)  {            
229       //Assuming list of processors to multicast to is in the
230       //order of relative processors numbering and NOT absolute
231       //processor numbering
232       
233       if(cmsg->dest_proc == IS_MULTICAST) {  
234       router->EachToManyMulticast(id, cmsg->size, msg, cmsg->npes, 
235       cmsg->pelist, 
236       numToDeposit > 1);
237       }            
238       else {                                
239       
240       ComlibPrintf("%d: Insert Pers. Message to %d\n", CkMyPe(), procMap[cmsg->dest_proc]);
241       router->EachToManyMulticast(id, cmsg->size, msg, 1,
242       &procMap[cmsg->dest_proc],
243       numToDeposit > 1);
244       }            
245       }   
246       else
247       router->EachToManyMulticast(id, cmsg->size, msg, 1, &myPe, 
248       numToDeposit > 1);
249       
250       numToDeposit --;
251       delete cmsg;        
252       }
253     */
254     
255     router->EachToManyMulticastQ(id, msgQ);
256
257     while(!recvQ.isEmpty()) {
258         char *msg = recvQ.deq();
259         RecvManyMsg(msg);
260     }
261
262     while(!procQ.isEmpty()) {
263         char *msg = procQ.deq();
264         ProcManyMsg(msg);
265     }
266
267     while(!dummyQ.isEmpty() > 0) {
268         DummyMsg *m = dummyQ.deq();
269         router->DummyEP(m->id, m->magic);
270         CmiFree(m);
271     }
272 }
273
274 void RouterStrategy::Done(DummyMsg *m){
275
276     ComlibPrintf("%d: Finished iteration\n", CkMyPe());
277
278     if(doneHandle > 0) {
279         CmiSetHandler(m, doneHandle);
280         CmiSyncSendAndFree(CkMyPe(), sizeof(DummyMsg), (char*)m);
281     }
282     else
283         CmiFree(m);
284
285     doneFlag = 1;
286
287     if(bufferedDoneInserting)
288         doneInserting();
289 }
290
291
292 //Implement it later while implementing checkpointing of Comlib
293 void RouterStrategy::pup(PUP::er &p){}
294
295 PUPable_def(RouterStrategy);