check in Java code
authorbdemsky <bdemsky>
Tue, 10 Mar 2009 19:29:19 +0000 (19:29 +0000)
committerbdemsky <bdemsky>
Tue, 10 Mar 2009 19:29:19 +0000 (19:29 +0000)
Robust/src/Benchmarks/Distributed/LookUpService/dsm2/DistributedHashMap.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/LookUpService/dsm2/LookUpService.java [new file with mode: 0644]

diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/dsm2/DistributedHashMap.java b/Robust/src/Benchmarks/Distributed/LookUpService/dsm2/DistributedHashMap.java
new file mode 100644 (file)
index 0000000..0f147e5
--- /dev/null
@@ -0,0 +1,146 @@
+public class DistributedHashMap {
+  DistributedHashEntry[] table;
+  float loadFactor;
+
+  public DistributedHashMap(int initialCapacity, float loadFactor) {
+    init(initialCapacity, loadFactor);
+  }
+
+  private void init(int initialCapacity, float loadFactor) {
+    table=global new DistributedHashEntry[initialCapacity];
+    this.loadFactor=loadFactor;
+  }
+
+  private static int hash1(int hashcode, int length) {
+    int value=hashcode%length;
+    if (value<0)
+      return -value;
+    else
+      return value;
+  }
+
+  Object remove(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return null;
+    DHashEntry ptr=dhe.array;
+
+    if (ptr!=null) {
+      if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
+       dhe.array=ptr.next;
+       dhe.count--;
+       return ptr.value;
+      }
+      while(ptr.next!=null) {
+       if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
+         Object oldvalue=ptr.value;
+         ptr.next=ptr.next.next;
+         dhe.count--;
+         return oldvalue;
+       }
+       ptr=ptr.next;
+      }
+    }
+    return null;
+  }
+
+  Object get(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return null;
+
+    /****** Add Manual Prefetch *****/
+    //dhe.array.next(5).key
+    Object obj1 = dhe.array;
+    short[] offsets1 = new short[4];
+    offsets1[0] = getoffset {DHashEntry, next};
+    offsets1[1] = (short) 2;
+    offsets1[2] = getoffset {DHashEntry, key};
+    offsets1[3] = (short) 0;
+    System.rangePrefetch(obj1, offsets1);
+    /********************************/
+
+    DHashEntry ptr=dhe.array;
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode
+          &&ptr.key.equals(key)) {
+       return ptr.value;
+      }
+      ptr=ptr.next;
+    }
+    return null;
+  }
+
+  boolean containsKey(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return false;
+
+    DHashEntry ptr=dhe.array;
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode
+          &&ptr.key.equals(key)) {
+       return true;
+      }
+      ptr=ptr.next;
+    }
+    return false;
+  }
+
+  Object put(Object key, Object value) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null) {
+       dhe=global new DistributedHashEntry();
+       table[index1]=dhe;
+    }
+    DHashEntry ptr=dhe.array;
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
+       Object oldvalue=ptr.value;
+       ptr.value=value;
+       return oldvalue;
+      }
+      ptr=ptr.next;
+    }
+
+    DHashEntry he=global new DHashEntry();
+    he.value=value;
+    he.key=key;
+    he.hashval=hashcode;
+    he.next=dhe.array;
+    dhe.array=he;
+
+    dhe.count++;
+    return null;
+  }
+}
+
+
+class DistributedHashEntry {
+  public DistributedHashEntry() {
+  }
+  int count;
+  DHashEntry array;
+}
+
+
+class DHashEntry {
+  public DHashEntry() {
+  }
+  int hashval;
+  Object key;
+  Object value;
+  DHashEntry next;
+}
diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/dsm2/LookUpService.java b/Robust/src/Benchmarks/Distributed/LookUpService/dsm2/LookUpService.java
new file mode 100644 (file)
index 0000000..fceba44
--- /dev/null
@@ -0,0 +1,176 @@
+public class LookUpService extends Thread {
+  DistributedHashMap mydhmap;
+  /**
+   * The thread id involved 
+   **/
+  private int threadid;
+  /**
+   * The total number of threads
+   **/
+  private int numthreads;
+
+  /**
+   * The total number of transactions 
+   **/
+  private int numtrans;
+
+  /**
+   * The total number of objects created
+   **/
+  private int nobjs;
+
+  /**
+   * The probability of initiating a look up
+   * the read probability % between 0-99
+   **/
+  private int rdprob;
+
+  /**
+   * The number of look up operations
+   **/
+  private int nLookUp;
+
+  public LookUpService() {
+  }
+
+  public LookUpService(DistributedHashMap dmap, int threadid, int numthreads, int nobjs, int numtrans, int rdprob, int nLookUp) {
+    mydhmap = dmap;
+    this.threadid = threadid;
+    this.numthreads = numthreads;
+    this.nobjs = nobjs;
+    this.numtrans = numtrans;
+    this.rdprob = rdprob;
+    this.nLookUp = nLookUp;
+  }
+
+  public void run() {
+    int ntrans;
+    atomic {
+      ntrans = numtrans;
+    }
+
+    // Do read/writes
+    Random rand = new Random(0);
+      
+    for (int i = 0; i < ntrans; i++) {
+      atomic {
+        for(int j = 0; j < nLookUp; j++) {
+          int rdwr = rand.nextInt(100);
+          int rwkey = rand.nextInt(nobjs);
+          Integer key = global new Integer(rwkey);
+          if (rdwr < rdprob) {
+            Object o3 = mydhmap.get(key); //Read
+          } else {
+            Integer val = global new Integer(j);
+            mydhmap.put(key, val); //Modify 
+          }
+        }
+      }
+    }
+  }
+
+  public static void main(String[] args) {
+    LookUpService ls = new LookUpService();
+    LookUpService.parseCmdLine(args,ls);
+
+    int nthreads = ls.numthreads;
+    int[] mid = new int[8];
+    mid[0] = (128<<24)|(195<<16)|(136<<8)|162;//dc-1
+    mid[1] = (128<<24)|(195<<16)|(136<<8)|163;//dc-2
+    mid[2] = (128<<24)|(195<<16)|(136<<8)|164;//dc-3
+    mid[3] = (128<<24)|(195<<16)|(136<<8)|165;//dc-4
+    mid[4] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5
+    mid[5] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6
+    mid[6] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7
+    mid[7] = (128<<24)|(195<<16)|(136<<8)|169;//dc-8
+
+    LookUpService[] lus;
+    DistributedHashMap dhmap;
+
+    atomic {
+      dhmap = global new DistributedHashMap(500, 0.75f);
+      //Add to the hash map
+      for(int i = 0; i < ls.nobjs; i++) {
+        Integer key = global new Integer(i);
+        Integer val = global new Integer(i*i);
+        Object o1 = key;
+        Object o2 = val;
+        dhmap.put(o1, o2);
+      }
+      lus = global new LookUpService[nthreads];
+      for(int i = 0; i<nthreads; i++) {
+        lus[i] = global new LookUpService(dhmap, i, ls.numthreads, ls.nobjs, ls.numtrans, ls.rdprob, ls.nLookUp);
+      }
+    }
+
+    LookUpService tmp;
+    /* Start threads */
+    for(int i = 0; i<nthreads; i++) {
+      atomic {
+        tmp = lus[i];
+      }
+      tmp.start(mid[i]);
+    }
+
+    /* Join threads */
+    for(int i = 0; i<nthreads; i++) {
+      atomic {
+        tmp = lus[i];
+      }
+      tmp.join();
+    }
+
+    System.printString("Finished\n");
+  }
+
+  /**
+   * Parse the command line options.
+   **/
+  public static void parseCmdLine(String args[], LookUpService lus) {
+    int i = 0;
+    String arg;
+    while(i < args.length && args[i].startsWith("-")) {
+      arg = args[i++];
+      //check options
+      if(arg.equals("-N")) {
+        if(i < args.length) {
+          lus.numthreads = new Integer(args[i++]).intValue();
+        }
+      } else if(arg.equals("-nEntry")) {
+        if(i < args.length) {
+          lus.nobjs = new Integer(args[i++]).intValue();
+        }
+      } else if (arg.equals("-nTrans")) {
+        if(i < args.length) {
+          lus.numtrans =  new Integer(args[i++]).intValue();
+        }
+      } else if(arg.equals("-probRead")) {
+        if(i < args.length) {
+          lus.rdprob = new Integer(args[i++]).intValue();
+        }
+      } else if(arg.equals("-nLookUp")) {
+        if(i < args.length) {
+          lus.nLookUp = new Integer(args[i++]).intValue();
+        }
+      } else if(arg.equals("-h")) {
+        lus.usage();
+      }
+    }
+
+    if(lus.nobjs == 0  || lus.numtrans == 0)
+      lus.usage();
+  }
+
+  /**
+   * The usage routine which describes the program options.
+   **/
+  public void usage() {
+    System.printString("usage: ./LookUpServiceN.bin master -N <threads> -nEntry <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> -nLookUp <number of lookups>\n");
+    System.printString("    -N the number of threads\n");
+    System.printString("    -nEntry the number of objects to be inserted into distributed hashmap\n");
+    System.printString("    -nTrans the number of transactions to run\n");
+    System.printString("    -probRead the probability of read given a transaction\n");
+    System.printString("    -nLookUp the number of lookups per transaction\n");
+    System.printString("    -h help with usage\n");
+  }
+}