remove unnecessary include <string> .
[charm.git] / src / ck-com / KDirectMulticastStrategy.C
1 // #ifdef filippo
2
3 // #include "KDirectMulticastStrategy.h"
4
5 // //Group Constructor
6 // KDirectMulticastStrategy::KDirectMulticastStrategy(int kf, 
7 //                                                    int ndest, int *pelist) 
8 //     : DirectMulticastStrategy(ndest, pelist), kfactor(kf) {
9 //     //FIXME: verify the list is sorted
10 //     commonKDirectInit();
11 // }
12
13 // //Array Constructor
14 // KDirectMulticastStrategy::KDirectMulticastStrategy(int kf, 
15 //                                                    CkArrayID dest_aid)
16 //     : DirectMulticastStrategy(dest_aid), kfactor(kf){
17 //     commonKDirectInit();    
18 // }
19
20 // void KDirectMulticastStrategy::commonKDirectInit(){
21 //     //sort list and create a reverse map
22 // }
23
24 // extern int _charmHandlerIdx;
25 // void KDirectMulticastStrategy::doneInserting(){
26 //     ComlibPrintf("%d: DoneInserting \n", CkMyPe());
27     
28 //     if(messageBuf->length() == 0) {
29 //         return;
30 //     }
31     
32 //     while(!messageBuf->isEmpty()) {
33 //      CharmMessageHolder *cmsg = messageBuf->deq();
34 //         char *msg = cmsg->getCharmMessage();
35 //         register envelope* env = UsrToEnv(msg);
36
37 //         ComlibPrintf("[%d] Calling KDirect %d %d %d\n", CkMyPe(),
38 //                      env->getTotalsize(), ndestpes, cmsg->dest_proc);
39                 
40 //         if(cmsg->dest_proc == IS_MULTICAST) {      
41 //             CmiSetHandler(env, handlerId);
42             
43 //             int *cur_pelist = NULL;
44 //             int cur_npes = 0;
45             
46 //             if(cmsg->sec_id == NULL) {
47 //                 cur_pelist = kdestpelist;
48 //                 cur_npes = kfactor;
49 //             }
50 //             else {                
51 //                 cur_npes = (kfactor <= cmsg->sid.npes)?kfactor : 
52 //                     cmsg->sid.npes;
53 //                 cur_pelist = cmsg->sid.pe_list;
54 //             }
55             
56 //             ComlibPrintf("[%d] Sending Message to %d\n", CkMyPe(), cur_npes);
57 //             CmiSyncListSendAndFree(cur_npes, cur_pelist, 
58 //                                    UsrToEnv(msg)->getTotalsize(), 
59 //                                    UsrToEnv(msg));
60 //         }
61 //         else {
62 //             CmiSyncSendAndFree(cmsg->dest_proc, 
63 //                                UsrToEnv(msg)->getTotalsize(), 
64 //                                (char *)UsrToEnv(msg));
65 //         }        
66         
67 //         delete cmsg; 
68 //     }
69 // }
70
71 // void KDirectMulticastStrategy::pup(PUP::er &p){
72 //     DirectMulticastStrategy::pup(p);
73
74 //     p | kfactor;
75 // }
76
77 // void KDirectMulticastStrategy::beginProcessing(int  nelements){
78
79 //     DirectMulticastStrategy::beginProcessing(nelements);
80
81 //     kndestpelist = new int[kfactor]; 
82
83 //     int next_pe = 0, count = 0;
84 //     //Assuming the destination pe list is sorted.
85 //     for(count = 0; count < ndestpes; count++)        
86 //         if(destpelist[count] > CkMyPe()) {
87 //             next_pe = count;
88 //             break;
89 //         }
90
91 //     int kpos = 0;
92 //     for(count = next_pe; count < next_pe + kfactor; count++){
93 //         int pe = destpelist[count % ndestpes];
94 //         kdestpelist[kpos ++] = pe;
95 //     }
96 // }
97
98 // void KDirectMulticastStrategy::handleMulticastMessage(void *msg){
99 //     register envelope *env = (envelope *)msg;
100     
101 //     CkMcastBaseMsg *cbmsg = (CkMcastBaseMsg *)EnvToUsr(env);
102 //     int src_pe = cbmsg->_cookie.pe;
103 //     if(isDestinationGroup){               
104 //         CmiSetHandler(env, _charmHandlerIdx);
105 //         CmiSyncSend(CkMyPe(), env->getTotalsize(), (char *)env);
106         
107 //         int nmsgs = getNumMessagesToSend(src_pe, CkMyPe, CkNumPes());
108 //         if(nmsgs > 0){            
109 //             CmiSetHandler(env, handlerId);            
110 //             CmiSyncListSendAndFree(nmsgs, kdestpelist, 
111 //                                    env->getTotalsize(), env);
112 //         }        
113 //         return;
114 //     }
115
116 //     int status = cbmsg->_cookie.sInfo.cInfo.status;
117 //     ComlibPrintf("[%d] In handle multicast message %d\n", CkMyPe(), status);
118
119 //     if(status == COMLIB_MULTICAST_ALL) {                        
120 //         int nmsgs = getNumMessagesToSend(src_pe. CkMyPe(), CkNumPes());
121 //         if(nmsgs > 0){ //Have to forward the messages           
122 //             void *msg = EnvToUsr(env);
123 //             void *newmsg = CkCopyMsg(&msg);
124 //             envelope *newenv = UsrToEnv(newmsg);        
125 //             CmiSyncListSendAndFree(nmsgs, kdestpelist, 
126 //                                    newenv->getTotalsize(), newenv);
127 //         }
128
129 //         //Multicast to all destination elements on current processor        
130 //         ComlibPrintf("[%d] Local multicast sending all %d\n", CkMyPe(), 
131 //                      localDestIndices.size());
132         
133 //         localMulticast(&localDestIndices, env);
134 //     }   
135 //     else if(status == COMLIB_MULTICAST_NEW_SECTION){        
136 //         CkUnpackMessage(&env);
137 //         ComlibPrintf("[%d] Received message for new section src=%d\n", 
138 //                      CkMyPe(), cbmsg->_cookie.pe);
139
140 //         ComlibMulticastMsg *ccmsg = (ComlibMulticastMsg *)cbmsg;
141         
142 //         KDirectHashObject *kobj = 
143 //             createHashObject(ccmsg->nIndices, ccmsg->indices);
144         
145 //         envelope *usrenv = (envelope *) ccmsg->usrMsg;
146         
147 //         envelope *newenv = (envelope *)CmiAlloc(usrenv->getTotalsize());
148 //         memcpy(newenv, usrenv, usrenv->getTotalsize());
149
150 //         localMulticast(&kobj->indices, newenv);
151
152 //         ComlibSectionHashKey key(cbmsg->_cookie.pe, 
153 //                                  cbmsg->_cookie.sInfo.cInfo.id);
154
155 //         KDirectHashObject *old_kobj = 
156 //             (KDirectHashObject*)sec_ht.get(key);
157 //         if(old_kobj != NULL)
158 //             delete old_kobj;
159         
160 //         sec_ht.put(key) = kobj;
161
162 //         if(kobj->npes > 0) {
163 //             ComlibPrintf("[%d] Forwarding Message of %d to %d pes\n", 
164 //                          CkMyPe(), cbmsg->_cookie.pe, kobj->npes);
165 //             CkPackMessage(&env);
166 //             CmiSyncListSendAndFree(kpbj->npes, kobj->pelist, 
167 //                                    env->getTotalsize(), env);
168 //         }
169 //         else
170 //             CmiFree(env);       
171 //     }
172 //     else {
173 //         //status == COMLIB_MULTICAST_OLD_SECTION, use the cached section id
174 //         ComlibSectionHashKey key(cbmsg->_cookie.pe, 
175 //                                  cbmsg->_cookie.sInfo.cInfo.id);    
176 //         KDirectHashObject *kobj = (KDirectHashObject *)sec_ht.get(key);
177         
178 //         if(kobj == NULL)
179 //             CkAbort("Destination indices is NULL\n");
180         
181 //         if(kobj->npes > 0){
182 //             void *msg = EnvToUsr(env);
183 //             void *newmsg = CkCopyMsg(&msg);
184 //             envelope *newenv = UsrToEnv(newmsg);        
185 //             CmiSyncListSendAndFree(kpbj->npes, kobj->pelist, 
186 //                                    newenv->getTotalsize(), newenv);
187
188 //         }
189         
190 //         localMulticast(&kobj->indices, env);
191 //     }
192 // }
193
194 // void KDirectMulticastStrategy::initSectionID(CkSectionID *sid){
195
196 //     ComlibPrintf("KDirect Init section ID\n");
197 //     sid->pelist = NULL;
198 //     sid->npes = 0;
199
200 //     int *pelist = new int[kfactor];
201 //     int npes;
202 //     getPeList(sid->_nElem,  sid->_elems, pelist, npes);
203     
204 //     sid->destpelist = pelist;
205 //     sid->ndestpes = npes;    
206 // }
207
208 // KDirectHashObject *KDirectMulticastStrategy::createHashObject(int nelements, CkArrayIndexMax *elements){
209
210 //     KDirectHashObject *kobj = new KDirectHashObject;
211 //     kobj->pelist = new int[kfactor];
212 //     getPeList(nelements,  elements, kobj->pelist, kobj->npes);
213
214 //     return kobj;
215 // }
216
217
218 // void KDirectMulticastStrategy::getPeList(int nelements, 
219 //                                          CkArrayIndexMax *elements, 
220 //                                          int *pelist, int &npes, 
221 //                                          int src_pe){
222     
223 //     npes = 0;
224     
225 //     int *tmp_pelist = new int[CkNumPes()];
226 //     int num_pes;
227     
228 //     //make this a reusable function call later.
229 //     int count = 0, acount = 0;
230 //     for(acount = 0; acount < nelements; acount++){
231 //         int p = CkArrayID::CkLocalBranch(destArrayID)->
232 //             lastKnown(elements[acount]);
233         
234 //         for(count = 0; count < num_pes; count ++)
235 //             if(tmp_pelist[count] == p)
236 //                 break;
237         
238 //         if(count == num_pes) {
239 //             tmp_pelist[num_pes ++] = p;
240 //         }
241 //     }
242
243 //     if(num_pes == 0) {
244 //         delete [] tmp_pelist;
245 //         return;
246 //     }
247
248 //     qsort(tmp_pelist, num_pes, sizeof(int), intCompare);
249     
250 //     int pdiff = 0;
251 //     int my_pos = 0;
252 //     int src_pos = 0;
253
254 //     int count;
255 //     for(count = 0; count < num_pes; count ++) {
256 //         if(tmp_pelist[count] == CkMyPe()){
257 //             my_pos = count;
258 //         }
259
260 //         if(tmp_pelist[count] == src_pos){
261 //             src_pos = count;
262 //         }        
263 //     }            
264
265 //     int n_tosend = getNumMessagesToSend(src_pos, my_pos, num_pes);
266 //     for(count = 0; count < n_tosend; count ++) {
267 //         pelist[npes ++] = tmp_pelist[(src_pos + count)%num_pes];
268 //     }    
269
270 //     delete [] tmp_pelist;    
271 // }
272
273 // int KDirectMulticastStrategy::getNumMessagesToSend(int src_pe, int my_pe, 
274 //                                                    int num_pes){
275     
276 //     if(src_pe == my_pe) {
277 //         retutn 0;
278 //     }
279
280 //     int nToSend = 0;
281
282 //     int pdiff = my_pe - src_pe;
283     
284 //     if(pdiff < 0)
285 //         pdiff += num_pes;
286     
287 //     if(pdiff % kfactor != 0)
288 //         return 0;
289     
290 //     return (num_pes - pdiff > kfactor)? kfactor : num_pes - pdiff;
291 // }
292
293 // #endif