NAMD
CompressPsf.h
Go to the documentation of this file.
1 #ifndef COMPRESSPSF_H
2 #define COMPRESSPSF_H
3 
4 #include "structures.h"
5 #include <string>
6 #include <deque>
7 #include <ckhashtable.h>
8 
9 #define COMPRESSED_PSF_VER 1.72
10 
11 //used to detemine big-endian or little-endian for
12 //the per-atom binary file
13 #define COMPRESSED_PSF_MAGICNUM 1234
14 
15 class Molecule;
16 class Parameters;
17 class SimParameters;
18 class ConfigList;
19 
20 //if the compiler supports anonymous struct, then sSet, iSet
21 //and fSet could be omitted for simplicity. -Chao Mei
23  struct shortVals{
24  short segNameIdx;
25  short resNameIdx;
26  short atomNameIdx;
27  short atomTypeIdx;
28  short chargeIdx;
29  short massIdx;
30  short vdw_type;
31  }sSet;
32 
33  struct integerVals{
36  int resID;
39  int GPID;
41  int MPID;
42  }iSet;
43 
44  struct floatVals{
46  }fSet;
47 
48  void flip();
49 };
50 
51 void compress_molecule_info(Molecule *mol, char *psfFileName, Parameters *param, SimParameters *simParam, ConfigList* cfgList);
52 
53 void flipNum(char *elem, int elemSize, int numElems);
54 
55 template <typename T>
56 int lookupCstPool(const std::vector<T>& pool, const T& val)
57 {
58  for(int i=0; i<pool.size(); i++)
59  {
60  if(pool[i]==val)
61  return i;
62  }
63  return -1;
64 }
65 
66 // Adapt the function prototype for ckhashtable keys to the
67 // NAMD XXXSig classes
68 template <class T> class HashPoolAdaptorT {
69  T val;
70 public:
71  HashPoolAdaptorT<T>(const T &v):val(v) { }
74  operator T & () { return val; }
75  operator const T & () const { return val; }
76 
77  inline CkHashCode hash(void) const {
78  const int hash=val.hash();
79  return CkHashFunction_int(&hash,sizeof(int));
80  }
81 
82  static CkHashCode staticHash(const void *k,size_t) {
83  return ((HashPoolAdaptorT<T> *)k)->hash();
84  }
85 
86  inline int compare(const HashPoolAdaptorT<T> &t) const {
87  return val==t.val;
88  }
89 
90  static int staticCompare(const void *a,const void *b,size_t) {
91  return ((HashPoolAdaptorT<T> *)a)->compare(*(HashPoolAdaptorT<T> *)b);
92  }
93 
94  inline T& getVal() { return val; }
95 
96  // PUPer not tested
97  void pup(PUP::er &p){
98  p | *val;
99  }
100 };
101 
102 template <typename T>
103 class HashPool {
104 public:
106  clear();
107  }
108 
109  void clear() {
110  // Delete the pool entries
111  for ( int i=0; i < pool.size(); i++)
112  delete pool[i];
113  // Clear the pool and hash table
114  pool.clear();
115  index_table.empty();
116  }
117 
118  int lookupCstPool(const T& val)
119  {
120  HashPoolAdaptorT<T> hx(val);
121  // Ugly: Can't store zeros in the table, so add 1 to indices on insert and
122  // subtract 1 to get real index when retrieving
123  int loc = index_table.get(hx) - 1;
124 #if 0
125  int i;
126  for(i=0; i < pool.size(); i++)
127  {
128  if (pool[i]->getVal() == val) {
129  if (i != loc) {
130  CmiPrintf("Get[%d] returned %d, actual is %d\n",hx.hash(),loc,i);
131  dump_tables();
132  loc = i;
133  }
134  break;
135  }
136  }
137  if (loc != -1 && i == pool.size()) {
138  CmiPrintf("Get returned %d, actual not found\n",loc);
139  }
140 #endif
141  return loc;
142  }
143 
144  void push_back(const T& x)
145  {
146  // Behave like a STL vector, but also store the indexing info in
147  // the hashtable index
148 
149  // Add to vector
151  pool.push_back(val);
152  // Also add to hash table. Make sure pointer doesn't change
153  int* index = &(index_table.put(*val));
154  // Ugly: Can't store zeros in the table, so add one to all entries
155  *index = pool.size(); // pool.size() - 1 + 1
156 
157  // CmiPrintf("Adding hx=%p hash=%d index[%p]=%d\n",&val,val->hash(),index,*index);
158  // dump_tables();
159  }
160 
161  void dump_tables();
162 
163  T& operator[](int i) const { return pool[i]->getVal(); }
164 
165  int size() { return pool.size(); }
166 
167 private:
168  CkHashtableT<HashPoolAdaptorT<T>,int> index_table;
169  std::vector<HashPoolAdaptorT<T>*> pool;
170 };
171 
172 #endif
int size()
Definition: CompressPsf.h:165
CkHashCode hash(void) const
Definition: CompressPsf.h:77
void flipNum(char *elem, int elemSize, int numElems)
Definition: CompressPsf.C:406
static CkHashCode staticHash(const void *k, size_t)
Definition: CompressPsf.h:82
float Real
Definition: common.h:107
int lookupCstPool(const T &val)
Definition: CompressPsf.h:118
int lookupCstPool(const std::vector< T > &pool, const T &val)
Definition: CompressPsf.h:56
void clear()
Definition: CompressPsf.h:109
void pup(PUP::er &p)
Definition: CompressPsf.h:97
void compress_molecule_info(Molecule *mol, char *psfFileName, Parameters *param, SimParameters *simParam, ConfigList *cfgList)
Definition: CompressPsf.C:436
struct OutputAtomRecord::floatVals fSet
struct OutputAtomRecord::integerVals iSet
void dump_tables()
Definition: CompressPsf.C:1386
int compare(const HashPoolAdaptorT< T > &t) const
Definition: CompressPsf.h:86
void push_back(const T &x)
Definition: CompressPsf.h:144
gridSize x
struct OutputAtomRecord::shortVals sSet
static int staticCompare(const void *a, const void *b, size_t)
Definition: CompressPsf.h:90
T & operator[](int i) const
Definition: CompressPsf.h:163