00001
00007
00008
00009
00010
00011 #include "ProcessorPrivate.h"
00012 #include "AtomMap.h"
00013
00014 #define MIN_DEBUG_LEVEL 4
00015
00016 #include "Debug.h"
00017
00018 #ifdef MEM_OPT_VERSION
00019 #define MAXBITS 20
00020 #define MAXNUMATOMS (1<<MAXBITS)
00021 #endif
00022
00023
00024 void AtomMapper::registerIDsCompAtomExt(const CompAtomExt *begin, const CompAtomExt *end) {
00025 if ( mapped ) return;
00026 mapped = 1;
00027 #ifdef MEM_OPT_VERSION
00028 if ( ! map->onlyUseTbl ) {
00029 int n = end - begin;
00030 entries.resize(n);
00031 AtomMapEntry *e = entries.begin();
00032 for ( int i=0; i<n; ++i, ++e ) {
00033 e->pid = pid;
00034 e->index = i;
00035 AtomID aid = begin[i].id;
00036 short aid_upper = aid >> MAXBITS;
00037 e->aid_upper = aid_upper;
00038 int aid_hash = aid & (MAXNUMATOMS-1);
00039 AtomMapEntry **me = map->entries + aid_hash;
00040 while ( *me && (*me)->aid_upper < aid_upper ) me = &((*me)->next);
00041 e->next = *me;
00042 *me = e;
00043 }
00044 } else
00045 #endif
00046 if ( map->registerIDsCompAtomExt(pid, begin, end) ) NAMD_bug("atom map failed");
00047 }
00048
00049
00050 void AtomMapper::registerIDsFullAtom(const FullAtom *begin, const FullAtom *end) {
00051 if ( mapped ) return;
00052 mapped = 1;
00053 #ifdef MEM_OPT_VERSION
00054 if ( ! map->onlyUseTbl ) {
00055 int n = end - begin;
00056 entries.resize(n);
00057 AtomMapEntry *e = entries.begin();
00058 for ( int i=0; i<n; ++i, ++e ) {
00059 e->pid = pid;
00060 e->index = i;
00061 AtomID aid = begin[i].id;
00062 short aid_upper = aid >> MAXBITS;
00063 e->aid_upper = aid_upper;
00064 int aid_hash = aid & (MAXNUMATOMS-1);
00065 AtomMapEntry **me = map->entries + aid_hash;
00066 while ( *me && (*me)->aid_upper < aid_upper ) me = &((*me)->next);
00067 e->next = *me;
00068 *me = e;
00069 }
00070 } else
00071 #endif
00072 if ( map->registerIDsFullAtom(pid, begin, end) ) NAMD_bug("atom map failed");
00073 }
00074
00075
00076 void AtomMapper::unregisterIDsCompAtomExt(const CompAtomExt *begin, const CompAtomExt *end) {
00077 if ( ! mapped ) return;
00078 mapped = 0;
00079 #ifdef MEM_OPT_VERSION
00080 if ( ! map->onlyUseTbl ) {
00081 int n = end - begin;
00082 AtomMapEntry *e = entries.begin();
00083 for ( int i=0; i<n; ++i, ++e ) {
00084 AtomID aid = begin[i].id;
00085 int aid_hash = aid & (MAXNUMATOMS-1);
00086 AtomMapEntry **me = map->entries + aid_hash;
00087 while ( *me != e ) me = &((*me)->next);
00088 *me = e->next;
00089 }
00090 } else
00091 #endif
00092 if ( map->unregisterIDsCompAtomExt(pid, begin, end) ) NAMD_bug("atom map failed");
00093 }
00094
00095
00096 void AtomMapper::unregisterIDsFullAtom(const FullAtom *begin, const FullAtom *end) {
00097 if ( ! mapped ) return;
00098 mapped = 0;
00099 #ifdef MEM_OPT_VERSION
00100 if ( ! map->onlyUseTbl ) {
00101 int n = end - begin;
00102 AtomMapEntry *e = entries.begin();
00103 for ( int i=0; i<n; ++i, ++e ) {
00104 AtomID aid = begin[i].id;
00105 int aid_hash = aid & (MAXNUMATOMS-1);
00106 AtomMapEntry **me = map->entries + aid_hash;
00107 while ( *me != e ) me = &((*me)->next);
00108 *me = e->next;
00109 }
00110 } else
00111 #endif
00112 if ( map->unregisterIDsFullAtom(pid, begin, end) ) NAMD_bug("atom map failed");
00113 }
00114
00115
00116
00117 AtomMap *AtomMap::Instance() {
00118 if (CkpvAccess(AtomMap_instance) == 0) {
00119 CkpvAccess(AtomMap_instance) = new AtomMap;
00120 }
00121 return CkpvAccess(AtomMap_instance);
00122 }
00123
00124
00125 AtomMap::AtomMap(void)
00126 {
00127 localIDTable = NULL;
00128
00129 #ifdef MEM_OPT_VERSION
00130 entries = NULL;
00131 onlyUseTbl = false;
00132 #endif
00133 }
00134
00135 void
00136 AtomMap::checkMap(void)
00137 { }
00138
00139
00140
00141 AtomMap::~AtomMap(void)
00142 {
00143 delete [] localIDTable;
00144
00145 #ifdef MEM_OPT_VERSION
00146 delete [] entries;
00147 #endif
00148 }
00149
00150
00151
00152 void AtomMap::allocateMap(int nAtomIds)
00153 {
00154 #ifdef MEM_OPT_VERSION
00155 if ( nAtomIds > MAXNUMATOMS ) {
00156 entries = new AtomMapEntry*[MAXNUMATOMS];
00157 memset(entries,0,MAXNUMATOMS*sizeof(AtomMapEntry*));
00158 return;
00159 }
00160 onlyUseTbl = true;
00161 #endif
00162 localIDTable = new LocalID[nAtomIds];
00163 tableSz = nAtomIds;
00164 for(int i=0; i < nAtomIds; i++)
00165 localIDTable[i].pid = localIDTable[i].index = notUsed;
00166 }
00167
00168
00169 int AtomMap::unregisterIDsCompAtomExt(PatchID pid, const CompAtomExt *begin, const CompAtomExt *end)
00170 {
00171 if (localIDTable == NULL)
00172 return -1;
00173 else
00174 {
00175 for(const CompAtomExt *a = begin; a != end; ++a)
00176 {
00177 unsigned int ali = a->id;
00178 if (localIDTable[ali].pid == pid) {
00179 localIDTable[ali].pid = notUsed;
00180 localIDTable[ali].index = notUsed;
00181 }
00182 }
00183 return 0;
00184 }
00185 }
00186
00187
00188 int AtomMap::unregisterIDsFullAtom(PatchID pid, const FullAtom *begin, const FullAtom *end)
00189 {
00190 if (localIDTable == NULL)
00191 return -1;
00192 else
00193 {
00194 for(const FullAtom *a = begin; a != end; ++a)
00195 {
00196 unsigned int ali = a->id;
00197 if (localIDTable[ali].pid == pid) {
00198 localIDTable[ali].pid = notUsed;
00199 localIDTable[ali].index = notUsed;
00200 }
00201 }
00202 return 0;
00203 }
00204 }
00205
00206
00207
00208
00209
00210 int AtomMap::registerIDsCompAtomExt(PatchID pid, const CompAtomExt *begin, const CompAtomExt *end)
00211 {
00212 if (localIDTable == NULL)
00213 return -1;
00214 else
00215 {
00216 for(const CompAtomExt *a = begin; a != end; ++a)
00217 {
00218 unsigned int ali = a->id;
00219 localIDTable[ali].pid = pid;
00220 localIDTable[ali].index = a - begin;
00221 }
00222 return 0;
00223 }
00224 }
00225
00226
00227 int AtomMap::registerIDsFullAtom(PatchID pid, const FullAtom *begin, const FullAtom *end)
00228 {
00229 if (localIDTable == NULL)
00230 return -1;
00231 else
00232 {
00233 for(const FullAtom *a = begin; a != end; ++a)
00234 {
00235 unsigned int ali = a->id;
00236 localIDTable[ali].pid = pid;
00237 localIDTable[ali].index = a - begin;
00238 }
00239 return 0;
00240 }
00241 }
00242
00243
00244 #ifdef MEM_OPT_VERSION
00245 LocalID AtomMap::localID(AtomID id)
00246 {
00247 if(onlyUseTbl){
00248 return localIDTable[id];
00249 }else{
00250
00251 short aid_upper = id >> MAXBITS;
00252 int aid_hash = id & (MAXNUMATOMS-1);
00253 AtomMapEntry *me = entries[aid_hash];
00254 while ( me && me->aid_upper < aid_upper ) me = me->next;
00255 LocalID rval;
00256 if ( me && me->aid_upper == aid_upper ) {
00257 rval.pid = me->pid;
00258 rval.index = me->index;
00259 } else {
00260 rval.pid = notUsed;
00261 rval.index = notUsed;
00262 }
00263 return rval;
00264 }
00265 }
00266 #endif
00267