-
Notifications
You must be signed in to change notification settings - Fork 1
/
AtomMap.C
275 lines (245 loc) · 6.84 KB
/
AtomMap.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
*** Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by
*** The Board of Trustees of the University of Illinois.
*** All rights reserved.
**/
/*
Tracks location of Atoms on node. Singleton.
*/
#include "ProcessorPrivate.h"
#include "AtomMap.h"
#define MIN_DEBUG_LEVEL 4
// #define DEBUGM
#include "Debug.h"
#ifdef MEM_OPT_VERSION
#define MAXBITS 20
#define MAXNUMATOMS (1<<MAXBITS)
#endif
void AtomMapper::registerIDsCompAtomExt(const CompAtomExt *begin, const CompAtomExt *end) {
if ( mapped ) return;
mapped = 1;
#ifdef MEM_OPT_VERSION
if ( ! map->onlyUseTbl ) {
int n = end - begin;
entries.resize(n);
AtomMapEntry *e = entries.begin();
for ( int i=0; i<n; ++i, ++e ) {
e->pid = pid;
e->index = i;
AtomID aid = begin[i].id;
short aid_upper = aid >> MAXBITS;
e->aid_upper = aid_upper;
int aid_hash = aid & (MAXNUMATOMS-1);
AtomMapEntry **me = map->entries + aid_hash;
while ( *me && (*me)->aid_upper < aid_upper ) me = &((*me)->next);
e->next = *me;
*me = e;
}
} else
#endif
if ( map->registerIDsCompAtomExt(pid, begin, end) ) NAMD_bug("atom map failed");
}
void AtomMapper::registerIDsFullAtom(const FullAtom *begin, const FullAtom *end) {
if ( mapped ) return;
mapped = 1;
#ifdef MEM_OPT_VERSION
if ( ! map->onlyUseTbl ) {
int n = end - begin;
entries.resize(n);
AtomMapEntry *e = entries.begin();
for ( int i=0; i<n; ++i, ++e ) {
e->pid = pid;
e->index = i;
AtomID aid = begin[i].id;
short aid_upper = aid >> MAXBITS;
e->aid_upper = aid_upper;
int aid_hash = aid & (MAXNUMATOMS-1);
AtomMapEntry **me = map->entries + aid_hash;
while ( *me && (*me)->aid_upper < aid_upper ) me = &((*me)->next);
e->next = *me;
*me = e;
}
} else
#endif
if ( map->registerIDsFullAtom(pid, begin, end) ) NAMD_bug("atom map failed");
}
void AtomMapper::unregisterIDsCompAtomExt(const CompAtomExt *begin, const CompAtomExt *end) {
if ( ! mapped ) return;
mapped = 0;
#ifdef MEM_OPT_VERSION
if ( ! map->onlyUseTbl ) {
int n = end - begin;
if ( entries.size() != n ) {
CkPrintf("AtomMapper entries.size() %d != %d\n", entries.size(), n);
NAMD_bug("AtomMapper::unregisterIDsCompAtomExt size mismatch");
}
AtomMapEntry *e = entries.begin();
for ( int i=0; i<n; ++i, ++e ) {
AtomID aid = begin[i].id;
int aid_hash = aid & (MAXNUMATOMS-1);
AtomMapEntry **me = map->entries + aid_hash;
while ( *me != e ) me = &((*me)->next);
*me = e->next;
}
} else
#endif
if ( map->unregisterIDsCompAtomExt(pid, begin, end) ) NAMD_bug("atom map failed");
}
void AtomMapper::unregisterIDsFullAtom(const FullAtom *begin, const FullAtom *end) {
if ( ! mapped ) return;
mapped = 0;
#ifdef MEM_OPT_VERSION
if ( ! map->onlyUseTbl ) {
int n = end - begin;
if ( entries.size() != n ) {
CkPrintf("AtomMapper entries.size() %d != %d\n", entries.size(), n);
NAMD_bug("AtomMapper::unregisterIDsFullAtom size mismatch");
}
AtomMapEntry *e = entries.begin();
for ( int i=0; i<n; ++i, ++e ) {
AtomID aid = begin[i].id;
int aid_hash = aid & (MAXNUMATOMS-1);
AtomMapEntry **me = map->entries + aid_hash;
while ( *me != e ) me = &((*me)->next);
*me = e->next;
}
} else
#endif
if ( map->unregisterIDsFullAtom(pid, begin, end) ) NAMD_bug("atom map failed");
}
// Singleton method
AtomMap *AtomMap::Instance() {
if (CkpvAccess(AtomMap_instance) == 0) {
CkpvAccess(AtomMap_instance) = new AtomMap; // this is never deleted!
}
return CkpvAccess(AtomMap_instance);
}
//----------------------------------------------------------------------
AtomMap::AtomMap(void)
{
localIDTable = NULL;
#ifdef MEM_OPT_VERSION
entries = NULL;
onlyUseTbl = false;
#endif
}
void
AtomMap::checkMap(void)
{ }
//----------------------------------------------------------------------
AtomMap::~AtomMap(void)
{
delete [] localIDTable; // Delete on a NULL pointer should be ok
#ifdef MEM_OPT_VERSION
delete [] entries;
#endif
}
//----------------------------------------------------------------------
// Creates fixed size table
void AtomMap::allocateMap(int nAtomIds)
{
#ifdef MEM_OPT_VERSION
if ( nAtomIds > MAXNUMATOMS ) {
entries = new AtomMapEntry*[MAXNUMATOMS];
memset(entries,0,MAXNUMATOMS*sizeof(AtomMapEntry*));
return;
} // else use non-memopt strategy
onlyUseTbl = true;
#endif
localIDTable = new LocalID[nAtomIds];
tableSz = nAtomIds;
for(int i=0; i < nAtomIds; i++)
localIDTable[i].pid = localIDTable[i].index = notUsed;
}
//
int AtomMap::unregisterIDsCompAtomExt(PatchID pid, const CompAtomExt *begin, const CompAtomExt *end)
{
if (localIDTable == NULL)
return -1;
else
{
for(const CompAtomExt *a = begin; a != end; ++a)
{
unsigned int ali = a->id;
if (localIDTable[ali].pid == pid) {
localIDTable[ali].pid = notUsed;
localIDTable[ali].index = notUsed;
}
}
return 0;
}
}
//----------------------------------------------------------------------
int AtomMap::unregisterIDsFullAtom(PatchID pid, const FullAtom *begin, const FullAtom *end)
{
if (localIDTable == NULL)
return -1;
else
{
for(const FullAtom *a = begin; a != end; ++a)
{
unsigned int ali = a->id;
if (localIDTable[ali].pid == pid) {
localIDTable[ali].pid = notUsed;
localIDTable[ali].index = notUsed;
}
}
return 0;
}
}
//It's possible to register the same atom for a new patch before it is moved
//from the old patch on the same processor!
//----------------------------------------------------------------------
int AtomMap::registerIDsCompAtomExt(PatchID pid, const CompAtomExt *begin, const CompAtomExt *end)
{
if (localIDTable == NULL)
return -1;
else
{
for(const CompAtomExt *a = begin; a != end; ++a)
{
unsigned int ali = a->id;
localIDTable[ali].pid = pid;
localIDTable[ali].index = a - begin;
}
return 0;
}
}
//----------------------------------------------------------------------
int AtomMap::registerIDsFullAtom(PatchID pid, const FullAtom *begin, const FullAtom *end)
{
if (localIDTable == NULL)
return -1;
else
{
for(const FullAtom *a = begin; a != end; ++a)
{
unsigned int ali = a->id;
localIDTable[ali].pid = pid;
localIDTable[ali].index = a - begin;
}
return 0;
}
}
#ifdef MEM_OPT_VERSION
LocalID AtomMap::localID(AtomID id)
{
if(onlyUseTbl){
return localIDTable[id];
}else{
short aid_upper = id >> MAXBITS;
int aid_hash = id & (MAXNUMATOMS-1);
AtomMapEntry *me = entries[aid_hash];
while ( me && me->aid_upper < aid_upper ) me = me->next;
LocalID rval;
if ( me && me->aid_upper == aid_upper ) {
rval.pid = me->pid;
rval.index = me->index;
} else {
rval.pid = notUsed;
rval.index = notUsed;
}
return rval;
}
}
#endif