00001 #ifndef _INIT_H
00002 #define _INIT_H
00003
00004 #include "charm.h"
00005 #include "charm-api.h"
00006 #include <new>
00007 #include "ckhashtable.h"
00008 #include <vector>
00009
00010 typedef CkQ<void *> PtrQ;
00011 class envelope;
00012 typedef std::vector<CkZeroPtr<envelope> > PtrVec;
00013
00014 class IrrGroup;
00015 class TableEntry {
00016 IrrGroup *obj;
00017 PtrQ *pending;
00018 int cIdx;
00019
00020 public:
00021 TableEntry(int ignored=0) { (void)ignored; obj=0; pending=0; cIdx=-1; }
00022 inline IrrGroup* getObj(void) { return obj; }
00023 inline void setObj(void *_obj) { obj=(IrrGroup *)_obj; }
00024 PtrQ* getPending(void) { return pending; }
00025 inline void clearPending(void) { delete pending; pending = NULL; }
00026 void enqMsg(void *msg) {
00027 if (pending==0)
00028 pending=new PtrQ();
00029 pending->enq(msg);
00030 }
00031 void setcIdx(int cIdx_){
00032 cIdx = cIdx_;
00033 }
00034 inline int getcIdx(void) const { return cIdx; }
00035 };
00036
00037 template <class dtype>
00038 class GroupIdxArray {
00039
00040 enum {INIT_BINS_PE0=32};
00041
00042 dtype *tab;
00043 CkHashtable_c hashTab;
00044 int max;
00045
00046
00047
00048 dtype& nonInlineFind(CkGroupID n) {
00049 #if CMK_ERROR_CHECKING
00050 if (n.idx==0) {
00051 CkAbort("Group ID is zero-- invalid!\n");
00052 return *(new dtype);
00053 } else
00054 #endif
00055 if (n.idx>=max) {
00056 dtype *oldtab=tab;
00057 int i, oldmax=max;
00058 max=2*n.idx+1;
00059 tab=new dtype[max];
00060 for (i=0;i<oldmax;i++) tab[i]=oldtab[i];
00061 for (i=oldmax;i<max;i++) tab[i]=dtype(0);
00062 delete [] oldtab;
00063 return tab[n.idx];
00064 }
00065 else
00066 {
00067 if(hashTab == NULL)
00068 hashTab = CkCreateHashtable_int(sizeof(dtype),17);
00069
00070 dtype *ret = (dtype *)CkHashtableGet(hashTab,&(n.idx));
00071
00072 if(ret == NULL)
00073 {
00074 ret = (dtype *)CkHashtablePut(hashTab,&(n.idx));
00075 new (ret) dtype(0);
00076 }
00077 return *ret;
00078 }
00079 }
00080
00081 public:
00082 GroupIdxArray() {tab=NULL;max=0;hashTab=NULL;}
00083 ~GroupIdxArray() {delete[] tab; if (hashTab!=NULL) CkDeleteHashtable(hashTab);}
00084 void init(void) {
00085 max = INIT_BINS_PE0;
00086 tab = new dtype[max];
00087 for(int i=0;i<max;i++)
00088 tab[i]=dtype(0);
00089 hashTab=NULL;
00090 }
00091
00092 inline dtype& find(CkGroupID n) {
00093 if(n.idx>0 && n.idx<max)
00094 return tab[n.idx];
00095 else
00096 return nonInlineFind(n);
00097 }
00098 };
00099
00100 typedef GroupIdxArray<TableEntry> GroupTable;
00101 typedef std::vector<CkGroupID> GroupIDTable;
00102
00103 typedef void (*CkInitCallFn)(void);
00104 class InitCallTable
00105 {
00106 public:
00107 CkQ<CkInitCallFn> initNodeCalls;
00108 CkQ<CkInitCallFn> initProcCalls;
00109 public:
00110 void enumerateInitCalls();
00111 };
00112 void _registerInitCall(CkInitCallFn fn, int isNodeCall);
00113
00114
00120 extern unsigned int _printCS;
00121 extern unsigned int _printSS;
00122
00123 extern int _infoIdx;
00124 extern int _charmHandlerIdx;
00125 extern int _roRestartHandlerIdx;
00126 #if CMK_SHRINK_EXPAND
00127 extern int _ROGroupRestartHandlerIdx;
00128 #endif
00129 extern int _bocHandlerIdx;
00130 extern int _qdHandlerIdx;
00131 extern unsigned int _numInitMsgs;
00132
00133 CksvExtern(unsigned int, _numInitNodeMsgs);
00134 CksvExtern(CmiNodeLock, _nodeLock);
00135 CksvExtern(GroupTable*, _nodeGroupTable);
00136 CksvExtern(GroupIDTable, _nodeGroupIDTable);
00137 CksvExtern(CmiImmediateLockType, _nodeGroupTableImmLock);
00138 CksvExtern(unsigned int, _numNodeGroups);
00139
00140 CkpvExtern(int, _charmEpoch);
00141
00142 CkpvExtern(CkGroupID,_currentGroup);
00143 CkpvExtern(void*, _currentNodeGroupObj);
00144 CkpvExtern(CkGroupID, _currentGroupRednMgr);
00145
00146 CkpvExtern(GroupTable*, _groupTable);
00147 CkpvExtern(GroupIDTable*, _groupIDTable);
00148 CkpvExtern(CmiImmediateLockType, _groupTableImmLock);
00149 CkpvExtern(unsigned int, _numGroups);
00150
00151 CkpvExtern(bool, _destroyingNodeGroup);
00152
00153 CkpvExtern(char **,Ck_argv);
00154
00155 static inline IrrGroup *_localBranch(CkGroupID gID)
00156 {
00157 return CkpvAccess(_groupTable)->find(gID).getObj();
00158 }
00159
00160 extern void _registerCommandLineOpt(const char* opt);
00161 extern void _initCharm(int argc, char **argv);
00162 extern void _sendReadonlies();
00163
00164 CLINKAGE int charm_main(int argc, char **argv);
00165 FLINKAGE void FTN_NAME(CHARM_MAIN_FORTRAN_WRAPPER, charm_main_fortran_wrapper)(int *argc, char **argv);
00166
00170 CLINKAGE void CkRegisterMainModule(void);
00171
00172 typedef void (*CkExitFn) (void);
00173
00174 extern CkQ<CkExitFn> _CkExitFnVec;
00175 extern void registerExitFn(CkExitFn);
00176
00177
00178 extern void CkContinueExit();
00179
00180 void EmergencyExit(void);
00181
00184 #endif
00185
00186