LRTS Comm Thread Tracing in message recieve
[charm.git] / src / ck-core / init.h
blob4362fb2866a5d29d9754719ec65664b5a0597ddf
1 #ifndef _INIT_H
2 #define _INIT_H
4 #include "charm.h" // For CkNumPes
5 #include <new> // for in-place new operator
6 #include "ckhashtable.h"
8 typedef CkQ<void *> PtrQ;
9 class envelope;
10 typedef CkVec<CkZeroPtr<envelope> > PtrVec;
12 class IrrGroup;
13 class TableEntry {
14 IrrGroup *obj;
15 PtrQ *pending; //Buffers msgs recv'd before group is created
16 int cIdx;
18 public:
19 TableEntry(int ignored=0) { obj=0; pending=0; cIdx=-1; }
20 inline IrrGroup* getObj(void) { return obj; }
21 inline void setObj(void *_obj) { obj=(IrrGroup *)_obj; }
22 PtrQ* getPending(void) { return pending; }
23 inline void clearPending(void) { delete pending; pending = NULL; }
24 void enqMsg(void *msg) {
25 if (pending==0)
26 pending=new PtrQ();
27 pending->enq(msg);
29 void setcIdx(int cIdx_){
30 cIdx = cIdx_;
32 inline int getcIdx(void) const { return cIdx; }
35 template <class dtype>
36 class GroupIdxArray {
37 // The initial size of the table for groups created on PE 0:
38 enum {INIT_BINS_PE0=32};
40 dtype *tab; // direct table for groups created on processor 0
41 CkHashtable_c hashTab; // hashtable for groups created on processors >0
42 int max; // Size of "tab"
44 //This non-inline version of "find", below, allows the (much simpler)
45 // common case to be inlined.
46 dtype& nonInlineFind(CkGroupID n) {
47 #if CMK_ERROR_CHECKING
48 if (n.idx==0) {
49 CkAbort("Group ID is zero-- invalid!\n");
50 return *(new dtype);
51 } else
52 #endif
53 if (n.idx>=max) { /* Extend processor 0's group table */
54 dtype *oldtab=tab;
55 int i, oldmax=max;
56 max=2*n.idx+1;
57 tab=new dtype[max];
58 for (i=0;i<oldmax;i++) tab[i]=oldtab[i];
59 for (i=oldmax;i<max;i++) tab[i]=dtype(0);
60 delete [] oldtab;
61 return tab[n.idx];
63 else /*n.idx < 0*/
64 { /*Groups created on processors >0 go into a hashtable:*/
65 if(hashTab == NULL)
66 hashTab = CkCreateHashtable_int(sizeof(dtype),17);
68 dtype *ret = (dtype *)CkHashtableGet(hashTab,&(n.idx));
70 if(ret == NULL) // insert new entry into the table
72 ret = (dtype *)CkHashtablePut(hashTab,&(n.idx));
73 new (ret) dtype(0); //Call dtype's constructor (ICK!)
75 return *ret;
79 public:
80 GroupIdxArray() {tab=NULL;max=0;hashTab=NULL;}
81 ~GroupIdxArray() {delete[] tab; if (hashTab!=NULL) CkDeleteHashtable(hashTab);}
82 void init(void) {
83 max = INIT_BINS_PE0;
84 tab = new dtype[max];
85 for(int i=0;i<max;i++)
86 tab[i]=dtype(0);
87 hashTab=NULL;
90 inline dtype& find(CkGroupID n) {
91 if(n.idx>0 && n.idx<max)
92 return tab[n.idx];
93 else
94 return nonInlineFind(n);
98 typedef GroupIdxArray<TableEntry> GroupTable;
99 typedef CkVec<CkGroupID> GroupIDTable;
101 typedef void (*CkInitCallFn)(void);
102 class InitCallTable
104 public:
105 CkQ<CkInitCallFn> initNodeCalls;
106 CkQ<CkInitCallFn> initProcCalls;
107 public:
108 void enumerateInitCalls();
110 void _registerInitCall(CkInitCallFn fn, int isNodeCall);
112 /*********************************************************/
114 \addtogroup CkInit
115 These are implemented in init.C.
117 /*@{*/
118 extern unsigned int _printCS;
119 extern unsigned int _printSS;
121 extern int _infoIdx;
122 extern int _charmHandlerIdx;
123 extern int _roRestartHandlerIdx; /* for checkpoint/restart */
124 extern int _bocHandlerIdx;
125 extern int _qdHandlerIdx;
126 extern unsigned int _numInitMsgs;
128 CksvExtern(unsigned int, _numInitNodeMsgs);
129 CksvExtern(CmiNodeLock, _nodeLock);
130 CksvExtern(GroupTable*, _nodeGroupTable);
131 CksvExtern(GroupIDTable, _nodeGroupIDTable);
132 CksvExtern(CmiImmediateLockType, _nodeGroupTableImmLock);
133 CksvExtern(unsigned int, _numNodeGroups);
135 CkpvExtern(int, _charmEpoch);
137 CkpvExtern(CkGroupID,_currentGroup);
138 CkpvExtern(void*, _currentNodeGroupObj);
139 CkpvExtern(CkGroupID, _currentGroupRednMgr);
141 CkpvExtern(GroupTable*, _groupTable);
142 CkpvExtern(GroupIDTable*, _groupIDTable);
143 CkpvExtern(CmiImmediateLockType, _groupTableImmLock);
144 CkpvExtern(unsigned int, _numGroups);
146 CkpvExtern(bool, _destroyingNodeGroup);
148 CkpvExtern(char **,Ck_argv);
150 static inline IrrGroup *_localBranch(CkGroupID gID)
152 return CkpvAccess(_groupTable)->find(gID).getObj();
155 extern void _initCharm(int argc, char **argv);
157 /** This routine registers the user's main module. It is normally
158 generated by the translator, but for FEM and AMPI may actually be
159 the "fallback" version in compat_regmm.c. */
160 extern "C" void CkRegisterMainModule(void);
162 typedef void (*CkExitFn) (void);
164 extern CkQ<CkExitFn> _CkExitFnVec;
165 extern void registerExitFn(CkExitFn);
167 extern "C" void EmergencyExit(void);
169 /*@}*/
171 #endif