Move revised MSA-based hashtable into its own files
authorPhil Miller <mille121@illinois.edu>
Mon, 13 Oct 2008 22:16:42 +0000 (22:16 +0000)
committerPhil Miller <mille121@illinois.edu>
Mon, 13 Oct 2008 22:16:42 +0000 (22:16 +0000)
src/libs/ck-libs/ParFUM/MsaHashtable.C [new file with mode: 0644]
src/libs/ck-libs/ParFUM/MsaHashtable.h [new file with mode: 0644]

diff --git a/src/libs/ck-libs/ParFUM/MsaHashtable.C b/src/libs/ck-libs/ParFUM/MsaHashtable.C
new file mode 100644 (file)
index 0000000..b3dc21b
--- /dev/null
@@ -0,0 +1,80 @@
+#include "ParFUM.h"
+#include "ParFUM_internals.h"
+#include "MsaHashtable.h"
+
+MsaHashtable::Add& MsaHashtable::getInitialAdd()
+{
+       if(initHandleGiven)
+               throw MSA_InvalidHandle();
+       
+       Add *a = new Add(*this);
+       sync();
+       initHandleGiven = true;
+       return *a;
+}
+
+MsaHashtable::Add& MsaHashtable::syncToAdd(Read &r)
+{
+       r.checkInvalidate(this);
+       delete &r;
+       sync();
+       Add *a = new Add(*this);
+       return *a;
+}
+
+MsaHashtable::Read& MsaHashtable::syncToRead(Add &a)
+{
+       a.checkInvalidate(this);
+       delete &a;
+       sync();
+       Read *r = new Read(*this);
+       return *r;
+}
+
+void MsaHashtable::Read::print()
+{
+       unsigned nEntries = MSA1DHASH::Read::msa.length();
+       char str[100];
+       for(int i=0;i<nEntries;i++){
+               const Hashtuple &t = get(i);
+               for(int j=0;j<t.vec->size();j++){
+                       Hashnode &tuple = (*t.vec)[j];
+                       printf("ghost element chunk %d element %d index %d tuple < %s>\n", 
+                              tuple.chunk, tuple.elementNo, i, 
+                              tuple.nodes.toString(tuple.numnodes,str));
+               }
+       }
+}
+
+int MsaHashtable::Add::addTuple(int *tuple,int nodesPerTuple,int chunk,int elementNo)
+{
+       int slots = msa.length();
+
+       // sort the tuples to get a canonical form
+       // bubble sort should do just as well since the number
+       // of nodes is less than 10.
+       for(int i=0;i<nodesPerTuple-1;i++){
+               for(int j=i+1;j<nodesPerTuple;j++){
+                       if(tuple[j] < tuple[i]){
+                               int t = tuple[j];
+                               tuple[j] = tuple[i];
+                               tuple[i] = t;
+                       }
+               }
+       }
+
+       //find out the index
+       long long sum = 0;
+       for(int i=0;i<nodesPerTuple;i++){
+               sum = sum*slots + tuple[i];
+       }
+       int index = (int )(sum %(long )slots);
+       Hashnode entry(nodesPerTuple,chunk,elementNo,tuple);
+
+       Hashtuple &list=accumulate(index);
+       list.vec->push_back(entry);
+       char str[100];
+       DEBUG(printf("[%d] adding tuple %s element %d to index %d \n",chunk,entry.nodes.toString(nodesPerTuple,str),elementNo,index));
+       return index;
+}
+
diff --git a/src/libs/ck-libs/ParFUM/MsaHashtable.h b/src/libs/ck-libs/ParFUM/MsaHashtable.h
new file mode 100644 (file)
index 0000000..508e11f
--- /dev/null
@@ -0,0 +1,52 @@
+#ifndef MSAHASHTABLE_H
+#define MSAHASHTABLE_H
+
+#include "ParFUM.h"
+#include "ParFUM_internals.h"
+
+typedef UniqElemList<Hashnode> Hashtuple;
+typedef MSA1D<Hashtuple,DefaultListEntry<Hashtuple,true>,MSA_DEFAULT_ENTRIES_PER_PAGE> MSA1DHASH;
+
+class MsaHashtable : private MSA1DHASH
+{
+public:
+       class Read; class Add;
+       Read& syncToRead(Add&);
+       Add&  syncToAdd(Read&);
+       Add& getInitialAdd();
+       using MSA1DHASH::pup;
+       using MSA1DHASH::enroll;
+
+       class Read : private MSA1DHASH::Read
+       {
+       public:
+               using MSA1DHASH::Read::get;
+               friend Read &MsaHashtable::syncToRead(Add&);
+               friend Add& MsaHashtable::syncToAdd(Read&);
+               void print();
+
+       private:
+       Read(MsaHashtable &m) : MSA1DHASH::Read(m) { }
+       };
+
+       class Add : private MSA1DHASH::Accum
+       {
+               using MSA1DHASH::Accum::accumulate;
+               friend Add& MsaHashtable::syncToAdd(Read&);
+               friend Read &MsaHashtable::syncToRead(Add&);
+               friend Add& MsaHashtable::getInitialAdd();
+       Add(MsaHashtable &m) : MSA1DHASH::Accum(m) { }
+       public:
+               int addTuple(int *tuple, int nodesPerTuple, int chunk, int elementNo);
+
+       };
+
+
+MsaHashtable(int _numSlots,int numWorkers)
+       : MSA1DHASH(_numSlots, numWorkers) { }
+       MsaHashtable(){};
+};
+
+
+
+#endif // MSAHASHTABLE_H