change benchmark
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / dsm / Em3d2.java
index 22bda0a3b0e159a3c84ab6f2e1fb03ce68e02aaf..b233e560389be8a4d219e01ef7e9b1593b1928de 100644 (file)
@@ -13,8 +13,7 @@
  * 1993, pages 262-273.
  * </cite>
  **/
-public class Em3d extends Thread
-{
+public class Em3d extends Thread {
 
   /**
    * The number of nodes (E and H) 
@@ -32,78 +31,105 @@ public class Em3d extends Thread
    * Should we print the results and other runtime messages
    **/
   private boolean printResult;
-  /**
-   * Print information messages?
-   **/
+    /**
+     * Print information messages?
+     **/
   private boolean printMsgs;
 
+    int threadindex;
+    int numThreads;
+
   BiGraph bg;
   int upperlimit;
   int lowerlimit;
-  Barrier mybarr;
-
-  public Em3d() {
-    numNodes = 0;
-    numDegree = 0;
-    numIter = 1;
-    printResult = false;
-    printMsgs = false;
-  }
+    public Em3d() {
+    }
 
-  public Em3d(BiGraph bg, int lowerlimit, int upperlimit, int numIter, Barrier mybarr) {
+    public Em3d(BiGraph bg, int lowerlimit, int upperlimit, int numIter, int numDegree, int threadindex) {
     this.bg = bg;
     this.lowerlimit = lowerlimit;
     this.upperlimit = upperlimit;
     this.numIter = numIter;
-    this.mybarr = mybarr;
+    this.numDegree = numDegree;
+    this.threadindex=threadindex;
   }
 
   public void run() {
     int iteration;
     Barrier barr;
-    Node enodebase;
-    Node hnodebase;
+    int degree;
+    Random random;
+    String hname;
 
+    barr = new Barrier("128.195.175.79");
     atomic {
-      iteration = numIter;
-      barr=mybarr;
+    System.printString("Inside atomic 1\n");
+       iteration = numIter;
+       degree = numDegree;
+       random = new Random(lowerlimit);
     }
+
     atomic {
-       enodebase=bg.eNodes;
-       hnodebase=bg.hNodes;
-       for(int j = 0; j<lowerlimit; j++){
-           enodebase = enodebase.next;
-           hnodebase = hnodebase.next;
-       }
+       //This is going to conflict badly...Minimize work here
+    System.printString("Inside atomic 2\n");
+       bg.allocateNodes ( lowerlimit, upperlimit, threadindex);
     }
+    Barrier.enterBarrier(barr);
+    System.clearPrefetchCache();
 
+    atomic {
+       //initialize the eNodes
+    System.printString("Inside atomic 3\n");
+       bg.initializeNodes(bg.eNodes, bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, degree, random, threadindex);
+    }
+    Barrier.enterBarrier(barr);
+
+    atomic {
+       //initialize the hNodes
+    System.printString("Inside atomic 4\n");
+       bg.initializeNodes(bg.hNodes, bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, degree, random, threadindex);
+    }
+    Barrier.enterBarrier(barr);
+
+    atomic {
+    System.printString("Inside atomic 5\n");
+       bg.makeFromNodes(bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, random);
+    }
+    Barrier.enterBarrier(barr);
+
+    atomic {
+    System.printString("Inside atomic 6\n");
+       bg.makeFromNodes(bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, random);
+    }
+    Barrier.enterBarrier(barr);
+
+    //Do the computation
     for (int i = 0; i < iteration; i++) {
-      /* for  eNodes */
+       /* for  eNodes */
        atomic {
-           Node n = enodebase;
+    System.printString("Inside atomic 7\n");
            for(int j = lowerlimit; j<upperlimit; j++) {
+               Node n = bg.eNodes[j];
+               
                for (int k = 0; k < n.fromCount; k++) {
                    n.value -= n.coeffs[k] * n.fromNodes[k].value;
                }
-               n = n.next;
            }
        }
        
        Barrier.enterBarrier(barr);
-       System.clearPrefetchCache();
-
+       
        /* for  hNodes */
        atomic {
-           Node n = hnodebase;
+    System.printString("Inside atomic 8\n");
            for(int j = lowerlimit; j<upperlimit; j++) {
+               Node n = bg.hNodes[j];
                for (int k = 0; k < n.fromCount; k++) {
                    n.value -= n.coeffs[k] * n.fromNodes[k].value;
                }
-               n=n.next;
            }
        }
        Barrier.enterBarrier(barr);
-       System.clearPrefetchCache();
     }
   }
 
@@ -119,65 +145,62 @@ public class Em3d extends Thread
     if (em.printMsgs) 
       System.printString("Initializing em3d random graph...\n");
     long start0 = System.currentTimeMillis();
-    int numThreads = 2;
-    int[] mid = new int[numThreads];
-    mid[0] = (128<<24)|(195<<16)|(175<<8)|78;
-    mid[1] = (128<<24)|(195<<16)|(175<<8)|79;
+    int numThreads = em.numThreads;
+    int[] mid = new int[4];
+    mid[0] = (128<<24)|(195<<16)|(175<<8)|79;//dw-1
+    mid[1] = (128<<24)|(195<<16)|(175<<8)|73;//dw-2
+    mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
+    mid[3] = (128<<24)|(195<<16)|(175<<8)|69;
+
     System.printString("DEBUG -> numThreads = " + numThreads+"\n");
-    Barrier mybarr;
-    atomic {
-      mybarr = global new Barrier(numThreads);
-    }
+    BarrierServer mybarr;
     BiGraph graph;
-    Random rand = new Random(783);
+
+    
+    // initialization step 1: allocate BiGraph
+   // System.printString( "Allocating BiGraph.\n" );
+
     atomic {
-      graph =  BiGraph.create(em.numNodes, em.numDegree, em.printResult, rand);
+      mybarr = global new BarrierServer(numThreads);
+      graph =  BiGraph.create(em.numNodes, em.numDegree, numThreads);
     }
+    mybarr.start(mid[0]);
 
-    long end0 = System.currentTimeMillis();
 
-    // compute a single iteration of electro-magnetic propagation
-    if (em.printMsgs) 
-      System.printString("Propagating field values for " + em.numIter + 
-          " iteration(s)...\n");
-    long start1 = System.currentTimeMillis();
-    Em3d[] em3d;
+    Em3dWrap[] em3d=new Em3dWrap[numThreads];    
+    int increment = em.numNodes/numThreads;
+
+
+    // initialization step 2: divide work of allocating nodes
+    // System.printString( "Launching distributed allocation of nodes.\n" );
+    
     atomic {
-      em3d = global new Em3d[numThreads];
-      em3d[0] = global new Em3d(graph, 0, em.numNodes/2, em.numIter, mybarr);
-      em3d[1] = global new Em3d(graph, (em.numNodes/2) + 1, em.numNodes, em.numIter, mybarr);
+      int base=0;
+      for(int i=0;i<numThreads;i++) {
+         Em3d tmp;
+         if ((i+1)==numThreads)
+             tmp = global new Em3d(graph, base, em.numNodes, em.numIter, em.numDegree, i);
+         else
+             tmp = global new Em3d(graph, base, base+increment, em.numIter, em.numDegree, i);
+         em3d[i]=new Em3dWrap(tmp);
+         base+=increment;
+      }
     }
 
-    Em3d tmp;
-    for(int i = 0; i<numThreads; i++) {
-      atomic {
-        tmp = em3d[i];
-      }
-      tmp.start(mid[i]);
+    boolean waitfordone=true;
+    while(waitfordone) {
+       atomic {
+           if (mybarr.done)
+               waitfordone=false;
+       }
     }
 
+    //System.printString("Starting Barrier run\n");
     for(int i = 0; i<numThreads; i++) {
-      atomic { 
-        tmp = em3d[i];
-      }
-      tmp.join();
-    }
-    long end1 = System.currentTimeMillis();
-
-    // print current field values
-    if (em.printResult) {
-      StringBuffer retval = new StringBuffer();
-      double dvalue;
-      atomic {
-        dvalue = graph.hNodes.value;
-      }
-      int intvalue = (int)dvalue;
+      em3d[i].em3d.start(mid[i]);
     }
-
-    if (em.printMsgs) {
-      System.printString("EM3D build time "+ (long)((end0 - start0)/1000.0) + "\n");
-      System.printString("EM3D compute time " + (long)((end1 - start1)/1000.0) + "\n");
-      System.printString("EM3D total time " + (long)((end1 - start0)/1000.0) + "\n");
+    for(int i = 0; i<numThreads; i++) {
+      em3d[i].em3d.join();
     }
     System.printString("Done!"+ "\n");
   }
@@ -201,6 +224,10 @@ public class Em3d extends Thread
         if (i < args.length) {
                em.numNodes = new Integer(args[i++]).intValue();
         }
+      } else if (arg.equals("-T")) {
+        if (i < args.length) {
+               em.numThreads = new Integer(args[i++]).intValue();
+        }
       } else if (arg.equals("-d")) {
         if (i < args.length) {
                em.numDegree = new Integer(args[i++]).intValue();
@@ -227,8 +254,9 @@ public class Em3d extends Thread
    **/
   public void usage()
   {
-    System.printString("usage: java Em3d -N <nodes> -d <degree> [-p] [-m] [-h]\n");
+    System.printString("usage: java Em3d -T <threads> -N <nodes> -d <degree> [-p] [-m] [-h]\n");
     System.printString("    -N the number of nodes\n");
+    System.printString("    -T the number of threads\n");
     System.printString("    -d the out-degree of each node\n");
     System.printString("    -i the number of iterations\n");
     System.printString("    -p (print detailed results\n)");