fixed compilation error
[charm.git] / src / ck-com / ComlibSectionInfo.C
1
2 #include "ComlibManager.h"
3 #include "ComlibSectionInfo.h"
4
5 ComlibMulticastMsg * ComlibSectionInfo::getNewMulticastMessage
6 (CharmMessageHolder *cmsg){
7     
8     if(cmsg->sec_id == NULL || cmsg->sec_id->_nElems == 0)
9         return NULL;
10
11     void *m = cmsg->getCharmMessage();
12     envelope *env = UsrToEnv(m);
13         
14     if(cmsg->sec_id->_cookie.sInfo.cInfo.id != 0) 
15         CmiAbort("In correct section\n");
16
17     initSectionID(cmsg->sec_id);   
18
19     CkPackMessage(&env);
20     int nRemotePes, nRemoteIndices;
21     ComlibMulticastIndexCount *indicesCount;
22     int *belongingList;
23     getRemotePeCount(cmsg->sec_id->_nElems, cmsg->sec_id->_elems, nRemotePes, nRemoteIndices, indicesCount, belongingList);
24     if (nRemotePes == 0) return NULL;
25
26     int sizes[3];
27     sizes[0] = nRemotePes;
28     sizes[1] = nRemoteIndices; // only those remote ///cmsg->sec_id->_nElems;
29     sizes[2] = env->getTotalsize();
30     
31     ComlibPrintf("Creating new comlib multicast message %d, %d %d\n", sizes[0], sizes[1], sizes[2]);
32     
33     ComlibMulticastMsg *msg = new(sizes, 0) ComlibMulticastMsg;
34     msg->nPes = nRemotePes;
35     msg->_cookie.sInfo.cInfo.instId = instanceID;
36     msg->_cookie.sInfo.cInfo.id = MaxSectionID - 1;
37     msg->_cookie.sInfo.cInfo.status = COMLIB_MULTICAST_NEW_SECTION;
38     msg->_cookie.type = COMLIB_MULTICAST_MESSAGE;
39     msg->_cookie.pe = CkMyPe();
40     
41     // fill in the three pointers of the ComlibMulticastMsg
42     memcpy(msg->indicesCount, indicesCount, sizes[0] * sizeof(ComlibMulticastIndexCount));
43     //memcpy(msg->indices, cmsg->sec_id->_elems, sizes[1] * sizeof(CkArrayIndexMax));
44     CkArrayIndexMax **indicesPe = (CkArrayIndexMax**)alloca(nRemotePes * sizeof(CkArrayIndexMax*));
45     indicesPe[0] = msg->indices;
46     for (int i=1; i<nRemotePes; ++i) indicesPe[i] = indicesPe[i-1] + indicesCount[i-1].count;
47     for (int i=0; i<cmsg->sec_id->_nElems; ++i) {
48       if (belongingList[i] >= 0) {
49         *indicesPe[belongingList[i]] = cmsg->sec_id->_elems[i];
50         indicesPe[belongingList[i]]++;
51       }
52     }
53     memcpy(msg->usrMsg, env, sizes[2] * sizeof(char));
54     envelope *newenv = UsrToEnv(msg);
55     delete [] indicesCount;
56     delete [] belongingList;
57
58     newenv->getsetArrayMgr() = env->getsetArrayMgr();
59     newenv->getsetArraySrcPe() = env->getsetArraySrcPe();
60     newenv->getsetArrayEp() = env->getsetArrayEp();
61     newenv->getsetArrayHops() = env->getsetArrayHops();
62     newenv->getsetArrayIndex() = env->getsetArrayIndex();
63
64     // for trace projections
65     newenv->setEvent(env->getEvent());
66     newenv->setSrcPe(env->getSrcPe());
67     
68     CkPackMessage(&newenv);        
69     return (ComlibMulticastMsg *)EnvToUsr(newenv);
70 }
71
72 void ComlibSectionInfo::unpack(envelope *cb_env,
73                                int &nLocalElems,
74                                CkArrayIndexMax *&dest_indices, 
75                                envelope *&env) {
76         
77     ComlibMulticastMsg *ccmsg = (ComlibMulticastMsg *)EnvToUsr(cb_env);
78     int i;
79
80     dest_indices = ccmsg->indices;
81     for (i=0; i<ccmsg->nPes; ++i) {
82       if (ccmsg->indicesCount[i].pe == CkMyPe()) break;
83       dest_indices += ccmsg->indicesCount[i].count;
84     }
85
86     CkAssert(i < ccmsg->nPes);
87     nLocalElems = ccmsg->indicesCount[i].count;
88
89     /*
90     for(int count = 0; count < ccmsg->nIndices; count++){
91         CkArrayIndexMax idx = ccmsg->indices[count];
92         
93         //This will work because. lastknown always knows if I have the
94         //element of not
95         int dest_proc = ComlibGetLastKnown(destArrayID, idx);
96         //CkArrayID::CkLocalBranch(destArrayID)->lastKnown(idx);
97         
98         //        if(dest_proc == CkMyPe())
99         dest_indices.insertAtEnd(idx);                        
100     }
101     */
102
103     envelope *usrenv = (envelope *) ccmsg->usrMsg;
104     env = (envelope *)CmiAlloc(usrenv->getTotalsize());
105     memcpy(env, ccmsg->usrMsg, usrenv->getTotalsize());
106 }
107
108
109 void ComlibSectionInfo::processOldSectionMessage(CharmMessageHolder *cmsg) {
110
111     ComlibPrintf("Process Old Section Message \n");
112
113     int cur_sec_id = ComlibSectionInfo::getSectionID(*cmsg->sec_id);
114
115     //Old section id, send the id with the message
116     CkMcastBaseMsg *cbmsg = (CkMcastBaseMsg *)cmsg->getCharmMessage();
117     cbmsg->_cookie.sInfo.cInfo.id = cur_sec_id;
118     cbmsg->_cookie.sInfo.cInfo.status = COMLIB_MULTICAST_OLD_SECTION;
119 }
120
121 void ComlibSectionInfo::getPeList(int _nElems, 
122                                   CkArrayIndexMax *_elems, 
123                                   int &npes, int *&pelist){
124     
125     int length = CkNumPes();
126     if(length > _nElems)    //There will not be more processors than
127                             //number of elements. This is wastage of
128                             //memory as there may be fewer
129                             //processors. Fix later.
130         length = _nElems;
131     
132     pelist = new int[length];
133     npes = 0;
134     
135     int count = 0, acount = 0;
136     
137     for(acount = 0; acount < _nElems; acount++){
138         
139         int p = ComlibGetLastKnown(destArrayID, _elems[acount]);
140         
141         if(p == -1) CkAbort("Invalid Section\n");        
142         for(count = 0; count < npes; count ++)
143             if(pelist[count] == p)
144                 break;
145         
146         if(count == npes) {
147             pelist[npes ++] = p;
148         }
149     }   
150
151     if(npes == 0) {
152         delete [] pelist;
153         pelist = NULL;
154     }
155 }
156
157
158 void ComlibSectionInfo::getRemotePeCount(int nindices, CkArrayIndexMax *idxlist, 
159                       int &npes, int &nidx,
160                       ComlibMulticastIndexCount *&counts, int *&belongs) {
161   int count = 0;
162   int i;
163     
164   int length = CkNumPes();
165
166   if(length > nindices) length = nindices;
167     
168   counts = new ComlibMulticastIndexCount[length];
169   belongs = new int[nindices];
170   npes = 0;
171   nidx = 0;
172
173   for(i=0; i<nindices; ++i){
174     int p = ComlibGetLastKnown(destArrayID, idxlist[i]);
175     if(p == CkMyPe()) {
176       belongs[i] = -1;
177       continue;
178     }
179     
180     if(p == -1) CkAbort("Invalid Section\n");        
181
182     ++nidx;
183     //Collect remote processors
184     for(count = 0; count < npes; count ++)
185       if(counts[count].pe == p)
186         break;
187     
188     if(count == npes) {
189       counts[npes].pe = p;
190       counts[npes].count = 0;
191       ++npes;
192     }
193
194     counts[count].count++;
195     belongs[i] = count;
196   }
197   
198   if(npes == 0) {
199     delete [] counts;
200     delete [] belongs;
201     counts = NULL;
202     belongs = NULL;
203   }
204 }
205
206
207 void ComlibSectionInfo::getRemotePelist(int nindices, 
208                                         CkArrayIndexMax *idxlist, 
209                                         int &npes, int *&pelist) {
210
211     int count = 0, acount = 0;
212     
213     int length = CkNumPes();
214
215     // HACK FOR DEBUGGING
216     /*pelist = new int[length-1];
217     npes = length-1;
218     for (acount=0; acount<length; acount++) {
219       if (acount == CkMyPe()) continue;
220       pelist[count]=acount;
221       count++;
222     }
223     return;*/
224     // END HACK
225
226     if(length > nindices)
227         length = nindices;
228     
229     pelist = new int[length+1];
230     npes = 0;
231
232     for(acount = 0; acount < nindices; acount++){
233         
234         int p = ComlibGetLastKnown(destArrayID, idxlist[acount]);
235         if(p == CkMyPe())
236             continue;
237         
238         if(p == -1) CkAbort("Invalid Section\n");        
239         
240         //Collect remote processors
241         for(count = 0; count < npes; count ++)
242             if(pelist[count] == p)
243                 break;
244         
245         if(count == npes) {
246             pelist[npes ++] = p;
247         }
248     }
249     
250     if(npes == 0) {
251         delete [] pelist;
252         pelist = NULL;
253     }
254 }
255
256
257 void ComlibSectionInfo::getLocalIndices(int nindices, 
258                                         CkArrayIndexMax *idxlist, 
259                                         CkVec<CkArrayIndexMax> &idx_vec){    
260     int count = 0, acount = 0;
261     idx_vec.resize(0);
262     
263     for(acount = 0; acount < nindices; acount++){
264         int p = ComlibGetLastKnown(destArrayID, idxlist[acount]);
265         if(p == CkMyPe()) 
266             idx_vec.insertAtEnd(idxlist[acount]);
267     }
268 }
269
270
271 void ComlibSectionInfo::localMulticast(envelope *env){
272     ComlibArrayInfo::localMulticast(&localDestIndexVec, env);
273 }