Refactoring code for message logging protocols.
[charm.git] / src / ck-core / ckobjid.C
1 #include "charm.h"
2 #include "ck.h"
3 #include "ckobjid.h"
4
5 /**
6         The method for returning the actual object pointed to by an id 
7         If the object doesnot exist on the processor it returns NULL
8 **/
9
10 void* CkObjID::getObject(){
11         
12                 switch(type){
13                         case TypeChare: 
14                                 return CkLocalChare(&data.chare.id);
15                         case TypeMainChare:
16                                 return CkLocalChare(&data.chare.id);
17                         case TypeGroup:
18         
19                                 CkAssert(data.group.onPE == CkMyPe());
20                                 return CkLocalBranch(data.group.id);
21                         case TypeNodeGroup:
22                                 CkAssert(data.group.onPE == CkMyNode());
23                                 //CkLocalNodeBranch(data.group.id);
24                                 {
25                                         CmiImmediateLock(CksvAccess(_nodeGroupTableImmLock));
26                                   void *retval = CksvAccess(_nodeGroupTable)->find(data.group.id).getObj();
27                                   CmiImmediateUnlock(CksvAccess(_nodeGroupTableImmLock));                                       
28         
29                                         return retval;
30                                 }       
31                         case TypeArray:
32                                 {
33         
34         
35                                         CkArrayID aid(data.array.id);
36         
37                                         if(aid.ckLocalBranch() == NULL){ return NULL;}
38         
39                                         CProxyElement_ArrayBase aProxy(aid,data.array.idx.asChild());
40         
41                                         return aProxy.ckLocal();
42                                 }
43                         default:
44                                 CkAssert(0);
45                 }
46 }
47
48
49 int CkObjID::guessPE(){
50                 switch(type){
51                         case TypeChare:
52                         case TypeMainChare:
53                                 return data.chare.id.onPE;
54                         case TypeGroup:
55                         case TypeNodeGroup:
56                                 return data.group.onPE;
57                         case TypeArray:
58                                 {
59                                         CkArrayID aid(data.array.id);
60                                         if(aid.ckLocalBranch() == NULL){
61                                                 return -1;
62                                         }
63                                         return aid.ckLocalBranch()->lastKnown(data.array.idx.asChild());
64                                 }
65                         default:
66                                 CkAssert(0);
67                 }
68 };
69
70 char *CkObjID::toString(char *buf) const {
71         
72         switch(type){
73                 case TypeChare:
74                         sprintf(buf,"Chare %p PE %d \0",data.chare.id.objPtr,data.chare.id.onPE);
75                         break;
76                 case TypeMainChare:
77                         sprintf(buf,"Chare %p PE %d \0",data.chare.id.objPtr,data.chare.id.onPE);       
78                         break;
79                 case TypeGroup:
80                         sprintf(buf,"Group %d   PE %d \0",data.group.id.idx,data.group.onPE);
81                         break;
82                 case TypeNodeGroup:
83                         sprintf(buf,"NodeGroup %d       Node %d \0",data.group.id.idx,data.group.onPE);
84                         break;
85                 case TypeArray:
86                         {
87                                 const CkArrayIndexMax &idx = data.array.idx.asChild();
88                                 const int *indexData = idx.data();
89                                 sprintf(buf,"Array |%d %d %d| id %d \0",indexData[0],indexData[1],indexData[2],data.array.id.idx);
90                                 break;
91                         }
92                 default:
93                         CkAssert(0);
94         }
95         
96         return buf;
97 };
98
99 void CkObjID::updatePosition(int PE){
100         if(guessPE() == PE){
101                 return;
102         }
103         switch(type){
104                 case TypeArray:
105                         {
106                                         CkArrayID aid(data.array.id);
107                                         if(aid.ckLocalBranch() == NULL){
108                                                 
109                                         }else{
110                                                 char str[100];
111                                                 CkLocMgr *mgr = aid.ckLocalBranch()->getLocMgr();
112 //                                              CmiPrintf("[%d] location for object %s is %d\n",CmiMyPe(),toString(str),PE);
113                                                 CkLocRec *rec = mgr->elementNrec(data.array.idx.asChild());
114                                                 if(rec != NULL){
115                                                         if(rec->type() == CkLocRec::local){
116                                                                 CmiPrintf("[%d] local object %s can not exist on another processor %d\n",CmiMyPe(),str,PE);
117                                                                 return;
118                                                         }
119                                                 }
120                                                 mgr->inform(data.array.idx.asChild(),PE);
121                                         }       
122                                 }
123
124                         break;
125                 case TypeChare:
126                 case TypeMainChare:
127                         CkAssert(data.chare.id.onPE == PE);
128                         break;
129                 case TypeGroup:
130                 case TypeNodeGroup:
131                         CkAssert(data.group.onPE == PE);
132                         break;
133                 default:
134                         CkAssert(0);
135         }
136 }
137
138 // #endif    //CMK_MESSAGE_LOGGING