try to work on memory usage...have large test case running now
authorbdemsky <bdemsky>
Wed, 21 Oct 2009 04:29:07 +0000 (04:29 +0000)
committerbdemsky <bdemsky>
Wed, 21 Oct 2009 04:29:07 +0000 (04:29 +0000)
12 files changed:
Robust/src/Benchmarks/SingleTM/Bayes/Adtree.java
Robust/src/Benchmarks/SingleTM/Bayes/AdtreeNode.java
Robust/src/Benchmarks/SingleTM/Bayes/AdtreeVary.java
Robust/src/Benchmarks/SingleTM/Bayes/Bayes.java
Robust/src/Benchmarks/SingleTM/Bayes/Data.java
Robust/src/Benchmarks/SingleTM/Bayes/Learner.java
Robust/src/Benchmarks/SingleTM/Bayes/Net.java
Robust/src/Benchmarks/SingleTM/Bayes/QuickSort.java
Robust/src/Benchmarks/SingleTM/Bayes/Random.java
Robust/src/Benchmarks/SingleTM/Bayes/Sort.java
Robust/src/Benchmarks/SingleTM/Bayes/Vector_t.java [new file with mode: 0644]
Robust/src/Benchmarks/SingleTM/Bayes/makefile

index c124d59355b9555cebbe3665458bbfff21975247..f70fca103ba4d77b29034035a7ef2b54e82d3c3d 100644 (file)
@@ -83,155 +83,69 @@ public class Adtree {
   int numRecord;
   AdtreeNode rootNodePtr;
 
-  public Adtree() {
-
-  }
-
-  /* =============================================================================
-   * freeNode
-   * =============================================================================
-   */
-  public void
-    freeNode (AdtreeNode nodePtr)
-    {
-      nodePtr.varyVectorPtr.vector_free();
-      nodePtr = null;
-    }
-
-
-
-  /* =============================================================================
-   * freeVary
-   * =============================================================================
-   */
-  public void
-    freeVary (AdtreeVary varyPtr)
-    {
-      varyPtr = null;
-    }
-
-
   /* =============================================================================
    * adtree_alloc
    * =============================================================================
    */
-  public static Adtree adtree_alloc ()
-    {
-      Adtree adtreePtr = new Adtree();
-      if (adtreePtr != null) {
-        adtreePtr.numVar = -1;
-        adtreePtr.numRecord = -1;
-        adtreePtr.rootNodePtr = null;
-      }
-
-      return adtreePtr;
-    }
-
-
-  /* =============================================================================
-   * freeNodes
-   * =============================================================================
-   */
-  public void
-    freeNodes (AdtreeNode nodePtr)
-    {
-      if (nodePtr != null) {
-        Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
-        int numVary = varyVectorPtr.vector_getSize();
-        for (int v = 0; v < numVary; v++) {
-          AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at(v));
-          freeNodes(varyPtr.zeroNodePtr);
-          freeNodes(varyPtr.oneNodePtr);
-          freeVary(varyPtr);
-        }
-        freeNode(nodePtr);
-      }
-    }
-
+  public Adtree() {
+    numVar = -1;
+    numRecord = -1;
+    rootNodePtr = null;
+  }
 
-  /* =============================================================================
-   * adtree_free
-   * =============================================================================
-   */
-  public void
-    adtree_free ()
-    {
-      freeNodes(rootNodePtr);
-    }
 
   /* =============================================================================
    * makeVary
    * =============================================================================
    */
-  public AdtreeVary
-    makeVary (int parentIndex,
-        int index,
-        int start,
-        int numRecord,
-        Data dataPtr)
-    {
-      AdtreeVary varyPtr = AdtreeVary.allocVary(index);
-
-      if ((parentIndex + 1 != index) && (numRecord > 1)) {
-        dataPtr.data_sort(start, numRecord, index);
-      }
-
-      int num0 = dataPtr.data_findSplit(start, numRecord, index);
-      int num1 = numRecord - num0;
-
-      int mostCommonValue = ((num0 >= num1) ? 0 : 1);
-      varyPtr.mostCommonValue = mostCommonValue;
-
-      if (num0 == 0 || mostCommonValue == 0) {
-        varyPtr.zeroNodePtr = null;
-      } else {
-        varyPtr.zeroNodePtr =
-          makeNode(index, index, start, num0, dataPtr);
-        varyPtr.zeroNodePtr.value = 0;
-      }
-
-      if (num1 == 0 || mostCommonValue == 1) {
-        varyPtr.oneNodePtr = null;
-      } else {
-        varyPtr.oneNodePtr =
-          makeNode(index, index, (start + num0), num1, dataPtr);
-        varyPtr.oneNodePtr.value = 1;
-      }
+  public AdtreeVary makeVary (int parentIndex, int index, int start, int numRecord, Data dataPtr) {
+    AdtreeVary varyPtr = new AdtreeVary(index);
 
-      return varyPtr;
+    if ((parentIndex + 1 != index) && (numRecord > 1)) {
+      dataPtr.data_sort(start, numRecord, index);
     }
+    
+    int num0 = dataPtr.data_findSplit(start, numRecord, index);
+    int num1 = numRecord - num0;
+    
+    int mostCommonValue = ((num0 >= num1) ? 0 : 1);
+    varyPtr.mostCommonValue = mostCommonValue;
+    
+    if (num0 == 0 || mostCommonValue == 0) {
+    } else {
+      varyPtr.zeroNodePtr = makeNode(index, index, start, num0, dataPtr);
+      varyPtr.zeroNodePtr.value = 0;
+    }
+    
+    if (num1 == 0 || mostCommonValue == 1) {
+    } else {
+      varyPtr.oneNodePtr = makeNode(index, index, (start + num0), num1, dataPtr);
+      varyPtr.oneNodePtr.value = 1;
+    }
+    
+    return varyPtr;
+  }
 
 
   /* =============================================================================
    * makeNode
    * =============================================================================
    */
-  public AdtreeNode
-    makeNode (int parentIndex,
-        int index,
-        int start,
-        int numRecord,
-        Data dataPtr)
-    {
-      AdtreeNode nodePtr = AdtreeNode.allocNode(index);
-
-      nodePtr.count = numRecord;
-
-      Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
-
-      int numVar = dataPtr.numVar;
-      for (int v = (index + 1); v < numVar; v++) {
-        AdtreeVary varyPtr =
-          makeVary(parentIndex, v, start, numRecord, dataPtr);
-        boolean status;
-        if((status = varyVectorPtr.vector_pushBack(varyPtr)) != true) {
-          System.out.println("Assert failed: varyVectorPtr.vector_pushBack != true");
-          System.exit(0);
-        }
-      }
-
-      return nodePtr;
+  public AdtreeNode makeNode (int parentIndex, int index, int start, int numRecord, Data dataPtr) {
+    int numVar = dataPtr.numVar;
+    AdtreeNode nodePtr = new AdtreeNode(index, numVar-index-1);
+    nodePtr.count = numRecord;
+    
+    AdtreeVary varyVectorPtr[] = nodePtr.varyVectorPtr;
+    int i=0;
+
+    for (int v = (index + 1); v < numVar; v++) {
+      AdtreeVary varyPtr = makeVary(parentIndex, v, start, numRecord, dataPtr);
+      varyVectorPtr[i++]=varyPtr;
     }
+    
+    return nodePtr;
+  }
 
 
   /* =============================================================================
@@ -239,14 +153,12 @@ public class Adtree {
    * -- Records in dataPtr will get rearranged
    * =============================================================================
    */
-  public void
-    adtree_make (Data dataPtr)
-    {
-      numVar = dataPtr.numVar;
-      numRecord = dataPtr.numRecord;
-      dataPtr.data_sort(0, numRecord, 0);
-      rootNodePtr = makeNode(-1, -1, 0, numRecord, dataPtr);
-    }
+  public void adtree_make (Data dataPtr) {
+    numVar = dataPtr.numVar;
+    numRecord = dataPtr.numRecord;
+    dataPtr.data_sort(0, numRecord, 0);
+    rootNodePtr = makeNode(-1, -1, 0, numRecord, dataPtr);
+  }
 
 
   /* =============================================================================
@@ -282,8 +194,7 @@ public class Adtree {
         System.exit(0);
       }
 
-      Vector_t varyVectorPtr = nodePtr.varyVectorPtr;
-      AdtreeVary varyPtr = (AdtreeVary)(varyVectorPtr.vector_at((queryIndex - nodeIndex - 1)));
+      AdtreeVary varyPtr = nodePtr.varyVectorPtr[queryIndex - nodeIndex - 1];
 
       int queryValue = queryPtr.value;
 
@@ -296,18 +207,16 @@ public class Adtree {
          * query with the current toggled (invertCount).
          */
         int numQuery = queryVectorPtr.vector_getSize();
-        Vector_t superQueryVectorPtr = Vector_t.vector_alloc(numQuery - 1);
+        Vector_t superQueryVectorPtr = new Vector_t(numQuery - 1);
 
         for (int qq = 0; qq < numQuery; qq++) {
           if (qq != q) {
-            boolean status = superQueryVectorPtr.vector_pushBack(
-                queryVectorPtr.vector_at(qq));
+            boolean status = superQueryVectorPtr.vector_pushBack(queryVectorPtr.vector_at(qq));
           }
         }
 
         int superCount = adtree_getCount(superQueryVectorPtr);
-
-        superQueryVectorPtr.vector_free();
+        superQueryVectorPtr.clear();
 
         int invertCount;
         if (queryValue == 0) {
index e33220774ec86604d5ec64ca73d4e779b7e01838..e84dedfc67c32c8b4b2fe50eea80527a3d488496 100644 (file)
@@ -9,7 +9,7 @@ public class AdtreeNode {
   int index;
   int value;
   int count;
-  Vector_t varyVectorPtr;
+  AdtreeVary varyVectorPtr[];
 
   public AdtreeNode() {
 
@@ -19,22 +19,10 @@ public class AdtreeNode {
    * allocNode
    * =============================================================================
    */
-  public static AdtreeNode
-    allocNode (int index)
-    {
-      AdtreeNode nodePtr = new AdtreeNode();
-
-      if (nodePtr != null) {
-        nodePtr.varyVectorPtr = Vector_t.vector_alloc(1);
-        if (nodePtr.varyVectorPtr == null) {
-          nodePtr = null;
-          return null;
-        }
-        nodePtr.index = index;
-        nodePtr.value = -1;
-        nodePtr.count = -1;
-      }
-
-      return nodePtr;
-    }
+  public AdtreeNode(int index, int vecsize) {
+    this.varyVectorPtr = new AdtreeVary[vecsize];
+    this.index = index;
+    this.value = -1;
+    this.count = -1;
+  }
 }
index e7c24925ef82bca9302c6f4d2e9fbee8a94b7a84..1edd43543010e0029db3e5a3ca697192e4dc88b1 100644 (file)
@@ -13,26 +13,19 @@ public class AdtreeVary {
   AdtreeNode oneNodePtr;
 
   public AdtreeVary() {
-
   }
 
   /* =============================================================================
    * allocVary
    * =============================================================================
    */
-  public AdtreeVary
-    allocVary (int index)
-    {
-      AdtreeVary varyPtr= new AdtreeVary();
-
-      if (varyPtr != null) {
-        varyPtr.index = index;
-        varyPtr.mostCommonValue = -1;
-        varyPtr.zeroNodePtr = null;
-        varyPtr.oneNodePtr = null;
-      }
-
-      return varyPtr;
-    }
+  public AdtreeVary(int index) {
+    this.index = index;
+    mostCommonValue = -1;
+  }
 
+  public void free_vary() {
+    zeroNodePtr=null;
+    oneNodePtr=null;
+  }
 }
index 89663d3fce8f929dc170c757db06a55d4c79d41e..08597cae05c075d365f1d6e169856b81a8e2b99a 100644 (file)
@@ -199,28 +199,27 @@ public class Bayes extends Thread {
    * score
    * =============================================================================
    */
-  public float
-    score (Net netPtr, Adtree adtreePtr)
-    {
-      /*
-       * Create dummy data structures to conform to learner_score assumptions
-       */
-
-      Data dataPtr = Data.data_alloc(1, 1, null);
-
-      Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, 1, global_insertPenalty, global_maxNumEdgeLearned, global_operationQualityFactor);
-
-      Net tmpNetPtr = learnerPtr.netPtr;
-      learnerPtr.netPtr = netPtr;
+  public float score (Net netPtr, Adtree adtreePtr) {
+    /*
+     * Create dummy data structures to conform to learner_score assumptions
+     */
+    
+    Data dataPtr = new Data(1, 1, null);
+    
+    Learner learnerPtr = new Learner(dataPtr, adtreePtr, 1, global_insertPenalty, global_maxNumEdgeLearned, global_operationQualityFactor);
+    
+    Net tmpNetPtr = learnerPtr.netPtr;
+    learnerPtr.netPtr = netPtr;
+    
+    float score = learnerPtr.learner_score();
+    learnerPtr.netPtr = tmpNetPtr;
+    learnerPtr.learner_free();
+    dataPtr.data_free();
 
-      float score = learnerPtr.learner_score();
 
-      learnerPtr.netPtr = tmpNetPtr;
-      learnerPtr.learner_free();
-      dataPtr.data_free();
 
-      return score;
-    }
+    return score;
+  }
 
 
   /**
@@ -282,7 +281,7 @@ public class Bayes extends Thread {
     randomPtr.random_alloc();
     randomPtr.random_seed(randomSeed);
 
-    Data dataPtr = Data.data_alloc(numVar, numRecord, randomPtr); 
+    Data dataPtr = new Data(numVar, numRecord, randomPtr); 
 
     Net netPtr = dataPtr.data_generate(-1, maxNumParent, percentParent);
     System.out.println("done.");
@@ -291,11 +290,12 @@ public class Bayes extends Thread {
      * Generate adtree
      */
 
-    Adtree adtreePtr = Adtree.adtree_alloc();
+    Adtree adtreePtr = new Adtree();
 
     System.out.print("Generating adtree... ");
 
     adtreePtr.adtree_make(dataPtr);
+    dataPtr.data_free();
 
     System.out.println("done.");
 
@@ -310,9 +310,7 @@ public class Bayes extends Thread {
      * Learn structure of Bayesian network
      */
 
-    Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, numThread, b.global_insertPenalty, b.global_maxNumEdgeLearned, b.global_operationQualityFactor);
-
-    dataPtr.data_free(); /* save memory */
+    Learner learnerPtr = new Learner(dataPtr, adtreePtr, numThread, b.global_insertPenalty, b.global_maxNumEdgeLearned, b.global_operationQualityFactor);
 
     System.out.print("Learning structure...");
 
@@ -359,14 +357,6 @@ public class Bayes extends Thread {
     /*
      * Clean up
      */
-
-#ifndef SIMULATOR
-    adtreePtr.adtree_free();
-#  if 0    
-    learnerPtr.learner_free();
-#  endif    
-#endif
-
   }
 }
 /* =============================================================================
index 7a9b3a9784bcc3cb8ba0943747eecf0db1b561fa..a376db2518b00722721ffb270fe3b60be004fbf4 100644 (file)
@@ -60,42 +60,29 @@ public class Data {
   int numRecord;
   byte[] records; /* coordination of all records */
   Random randomPtr;
-
-  public Data() {
-  }
+  Sort sort;
 
   /* =============================================================================
    * data_alloc
    * =============================================================================
    */
-  public static Data data_alloc (int numVar, int numRecord, Random randomPtr)
+  public Data(int numVar, int numRecord, Random randomPtr)
   {
-    Data dataPtr = new Data();
-
-    if (dataPtr != null) {
-      int numDatum = numVar * numRecord;
-      dataPtr.records = new byte[numDatum];
-      for(int i = 0; i<numDatum; i++)
-        dataPtr.records[i] = (byte)DATA_INIT;
-
-      dataPtr.numVar = numVar;
-      dataPtr.numRecord = numRecord;
-      dataPtr.randomPtr = randomPtr;
-    }
-
-    return dataPtr;
+    int numDatum = numVar * numRecord;
+    records = new byte[numDatum];
+    for(int i = 0; i<numDatum; i++)
+      this.records[i] = (byte)DATA_INIT;
+    
+    this.numVar = numVar;
+    this.numRecord = numRecord;
+    this.randomPtr = randomPtr;
+    this.sort=new Sort();
   }
 
-
-  /* =============================================================================
-   * data_free
-   * =============================================================================
-   */
-  void
-    data_free ()
-    {
-      records = null;
-    }
+  public void data_free() {
+    records=null;
+    randomPtr=null;
+  }
 
   /* =============================================================================
    * data_generate
@@ -114,7 +101,7 @@ public class Data {
      * Generate random Bayesian network
      */
 
-    Net netPtr = Net.net_alloc(numVar);
+    Net netPtr = new Net(numVar);
     netPtr.net_generateRandomEdges(maxNumParent, percentParent, randomPtr);
 
     /*
@@ -250,22 +237,6 @@ public class Data {
       }
     }
 
-    /*
-     * Clean up
-     */
-
-    doneBitmapPtr.bitmap_free();
-    orderedBitmapPtr.bitmap_free();
-    dependencyVectorPtr.vector_free();
-    workQueuePtr.queue_free();
-    order = null;
-
-    for (v = 0; v < numVar; v++) {
-      thresholdsTable[v] = null;
-    }
-
-    thresholdsTable = null;
-
     return netPtr;
   }
 
@@ -281,11 +252,7 @@ public class Data {
       int numDstDatum = dstPtr.numVar * dstPtr.numRecord;
       int numSrcDatum = srcPtr.numVar * srcPtr.numRecord;
       if (numDstDatum != numSrcDatum) {
-        dstPtr.records = null;
         dstPtr.records = new byte[numSrcDatum];
-        if (dstPtr.records == null) {
-          return false;
-        }
       }
 
       dstPtr.numVar    = srcPtr.numVar;
@@ -319,7 +286,7 @@ public class Data {
         System.exit(0);
       }
 
-      Sort.sort(records, 
+      sort.sort(records, 
           start * numVar,
           num,
           numVar,
index 870b152e9ba6b3a9758b8ddab5b232b1e1387b5c..f10105d40711b1fb8a51dbf75562bdc698535c5c 100644 (file)
@@ -132,47 +132,33 @@ public class Learner {
    * learner_alloc
    * =============================================================================
    */
-  public static Learner
-    learner_alloc (Data dataPtr, 
-        Adtree adtreePtr, 
-        int numThread, 
-        int global_insertPenalty,
-        int global_maxNumEdgeLearned,
-        float global_operationQualityFactor)
-    {
-      Learner learnerPtr = new Learner();
-
-      if (learnerPtr != null) {
-        learnerPtr.adtreePtr = adtreePtr;
-        learnerPtr.netPtr = Net.net_alloc(dataPtr.numVar);
-        learnerPtr.localBaseLogLikelihoods = new float[dataPtr.numVar];
-        learnerPtr.baseLogLikelihood = 0.0f;
-        learnerPtr.tasks = new LearnerTask[dataPtr.numVar];
-        learnerPtr.taskListPtr = List.list_alloc();
-        learnerPtr.numTotalParent = 0;
+  public Learner(Data dataPtr, 
+                Adtree adtreePtr, 
+                int numThread, 
+                int global_insertPenalty,
+                int global_maxNumEdgeLearned,
+                float global_operationQualityFactor) {
+    this.adtreePtr = adtreePtr;
+    this.netPtr = new Net(dataPtr.numVar);
+    this.localBaseLogLikelihoods = new float[dataPtr.numVar];
+    this.baseLogLikelihood = 0.0f;
+    this.tasks = new LearnerTask[dataPtr.numVar];
+    this.taskListPtr = List.list_alloc();
+    this.numTotalParent = 0;
 #ifndef TEST_LEARNER
-        learnerPtr.global_insertPenalty = global_insertPenalty;
-        learnerPtr.global_maxNumEdgeLearned = global_maxNumEdgeLearned;
-        learnerPtr.global_operationQualityFactor = global_operationQualityFactor;
+    this.global_insertPenalty = global_insertPenalty;
+    this.global_maxNumEdgeLearned = global_maxNumEdgeLearned;
+    this.global_operationQualityFactor = global_operationQualityFactor;
 #endif
-      }
-
-      return learnerPtr;
-    }
-
+  }
 
-  /* =============================================================================
-   * learner_free
-   * =============================================================================
-   */
-  public void
-    learner_free ()
-    {
-      taskListPtr.list_free();
-      tasks = null;
-      localBaseLogLikelihoods = null;
-      netPtr.net_free();
-    }
+  public void learner_free() {
+    adtreePtr=null;
+    netPtr=null;
+    localBaseLogLikelihoods=null;
+    tasks=null;
+    taskListPtr=null;
+  }
 
 
   /* =============================================================================
@@ -238,11 +224,7 @@ public class Learner {
       queries[0] = new Query();
       queries[1] = new Query();
 
-      Vector_t queryVectorPtr = Vector_t.vector_alloc(2);
-      if(queryVectorPtr == null) {
-        System.out.println("Assert failed: cannot allocate vector");
-        System.exit(0);
-      }
+      Vector_t queryVectorPtr = new Vector_t(2);
 
       if((status = queryVectorPtr.vector_pushBack(queries[0])) == false) {
         System.out.println("Assert failed: status = "+ status + "vector_pushBack failed in createTaskList()");
@@ -250,12 +232,7 @@ public class Learner {
       }
 
       Query parentQuery = new Query();
-      Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1); 
-
-      if(parentQueryVectorPtr == null) {
-        System.out.println("Assert failed: for vector_alloc at createTaskList()");
-        System.exit(0);
-      }
+      Vector_t parentQueryVectorPtr = new Vector_t(1); 
 
       int numVar = learnerPtr.adtreePtr.numVar;
       int numRecord = learnerPtr.adtreePtr.numRecord;
@@ -401,8 +378,8 @@ public class Learner {
       } // for each variable 
 
 
-      queryVectorPtr.vector_free();
-      parentQueryVectorPtr.vector_free();
+      queryVectorPtr.clear();
+      parentQueryVectorPtr.clear();
 
 #ifdef TEST_LEARNER
       ListNode it = learnerPtr.taskListPtr.head;
@@ -1137,19 +1114,10 @@ public class Learner {
       }
 
       Queue workQueuePtr = Queue.queue_alloc(-1);
-      if(workQueuePtr == null) {
-        System.out.println("Assert failed: for vector alloc in learnStructure()");
-        System.exit(0);
-      }
 
       int numVar = learnerPtr.adtreePtr.numVar;
       Query[] queries = new Query[numVar];
 
-      if(queries == null) {
-        System.out.println("Assert failed: for queries alloc in learnStructure()");
-        System.exit(0);
-      }
-
       for (int v = 0; v < numVar; v++) {
         queries[v] = new Query();
         queries[v].index = v;
@@ -1158,30 +1126,10 @@ public class Learner {
 
       float basePenalty = (float)(-0.5 * Math.log((double)numRecord));
 
-      Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
-      if(queryVectorPtr == null) {
-        System.out.println("Assert failed: for vector_alloc in learnStructure()");
-        System.exit(0);
-      }
-
-      Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
-      if(parentQueryVectorPtr == null) {
-        System.out.println("Assert failed: for vector_alloc in learnStructure()");
-        System.exit(0);
-      }
-
-      Vector_t aQueryVectorPtr = Vector_t.vector_alloc(1);
-      if(aQueryVectorPtr == null) {
-        System.out.println("Assert failed: for vector_alloc in learnStructure()");
-        System.exit(0);
-      }
-
-      Vector_t bQueryVectorPtr = Vector_t.vector_alloc(1);
-      if(bQueryVectorPtr == null) {
-        System.out.println("Assert failed: for vector_alloc in learnStructure()");
-        System.exit(0);
-      }
-
+      Vector_t queryVectorPtr = new Vector_t(1);
+      Vector_t parentQueryVectorPtr = new Vector_t(1);
+      Vector_t aQueryVectorPtr = new Vector_t(1);
+      Vector_t bQueryVectorPtr = new Vector_t(1);
 
       FindBestTaskArg arg = new FindBestTaskArg();
       arg.learnerPtr           = learnerPtr;
@@ -1454,10 +1402,10 @@ public class Learner {
 
       visitedBitmapPtr.bitmap_free();
       workQueuePtr.queue_free();
-      bQueryVectorPtr.vector_free();
-      aQueryVectorPtr.vector_free();
-      queryVectorPtr.vector_free();
-      parentQueryVectorPtr.vector_free();
+      bQueryVectorPtr.clear();
+      aQueryVectorPtr.clear();
+      queryVectorPtr.clear();
+      parentQueryVectorPtr.clear();
       queries = null;
     }
 
@@ -1488,8 +1436,8 @@ public class Learner {
     learner_score ()
     {
 
-      Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
-      Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
+      Vector_t queryVectorPtr = new Vector_t(1);
+      Vector_t parentQueryVectorPtr = new Vector_t(1);
 
       int numVar = adtreePtr.numVar;
       Query[] queries = new Query[numVar];
@@ -1522,8 +1470,8 @@ public class Learner {
         logLikelihood += localLogLikelihood;
       }
 
-      queryVectorPtr.vector_free();
-      parentQueryVectorPtr.vector_free();
+      queryVectorPtr.clear();
+      parentQueryVectorPtr.clear();
       queries = null;
 
 
index 817d7b5c947d60e0bfc14514589ace8fe5512f40..dfbd7859483e680e43fcc7fa565d4baec77c2baa 100644 (file)
@@ -74,8 +74,8 @@ public class Net {
    * allocNode
    * =============================================================================
    */
-  public static NetNode allocNode (int id)
-  {
+
+  public static NetNode allocNode (int id) {
     NetNode nodePtr = new NetNode();
 
     if (nodePtr != null) {
@@ -101,34 +101,14 @@ public class Net {
    * net_alloc
    * =============================================================================
    */
-  public static Net net_alloc (int numNode)
-  {
-    Net netPtr = new Net();
-    if (netPtr != null) {
-      Vector_t nodeVectorPtr = Vector_t.vector_alloc(numNode);
-      if (nodeVectorPtr == null) {
-        netPtr = null;
-        return null;
-      }
-
-      for (int i = 0; i < numNode; i++) {
-        NetNode nodePtr = allocNode(i);
-        if (nodePtr == null) {
-          for (int j = 0; j < i; j++) {
-            nodePtr = (NetNode)(nodeVectorPtr.vector_at(j));
-            nodePtr.freeNode();
-          }
-          nodeVectorPtr.vector_free();
-          netPtr = null;
-          return null;
-        }
+  public Net(int numNode) {
+    Vector_t nodeVectorPtr = new Vector_t(numNode);
 
-        boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
-      }
-      netPtr.nodeVectorPtr = nodeVectorPtr;
+    for (int i = 0; i < numNode; i++) {
+      NetNode nodePtr = allocNode(i);
+      boolean status = nodeVectorPtr.vector_pushBack(nodePtr);
     }
-
-    return netPtr;
+    this.nodeVectorPtr = nodeVectorPtr;
   }
 
 
@@ -136,17 +116,10 @@ public class Net {
    * net_free
    * =============================================================================
    */
-  public void
-    net_free ()
-    {
-      int numNode = nodeVectorPtr.vector_getSize();
-      for (int i = 0; i < numNode; i++) {
-        NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(i));
-        nodePtr.freeNode();
-      }
-      nodeVectorPtr.vector_free();
-    }
-
+  public void net_free () {
+    nn=null;
+    nodeVectorPtr=null;
+  }
 
   /* =============================================================================
    * insertEdge
index 51f20a704cf11db22f38a8c24af7e366fa404ade..acb73c9848afe9327b2f954a59c0389da5d3707f 100644 (file)
@@ -13,17 +13,17 @@ public class QuickSort {
    *
    * @param a the array to sort
    */
-  public void sort(Object[] a)
+  public static void sort(Object[] a)
   {
     qsort(a, 0, a.length);
   }
 
-  public void sort(Object[] a, int fromIndex, int toIndex)
+  public static void sort(Object[] a, int fromIndex, int toIndex)
   {
     qsort(a, fromIndex, toIndex);
   }
 
-  private int med3(int a, int b, int c, Object[]d)
+  private static int med3(int a, int b, int c, Object[]d)
   {
     if(less(d[a], d[b])) {
       if(less(d[b], d[c])) {
@@ -46,14 +46,14 @@ public class QuickSort {
     }
   }
 
-  private void swap(int i, int j, Object[] a)
+  private static void swap(int i, int j, Object[] a)
   {
     Object c = a[i];
     a[i] = a[j];
     a[j] = c;
   }
 
-  private void qsort(Object[] a, int start, int n)
+  private static void qsort(Object[] a, int start, int n)
   {
     // use an insertion sort on small arrays
     if (n <= 7)
@@ -126,7 +126,7 @@ public class QuickSort {
       qsort(a, pn - s, s);
   }
 
-  private void vecswap(int i, int j, int n, Object[] a)
+  private static void vecswap(int i, int j, int n, Object[] a)
   {
     for (; n > 0; i++, j++, n--)
       swap(i, j, a);
@@ -138,7 +138,7 @@ public class QuickSort {
    * -- For vector_sort
    * ===========================================
    */
-  public boolean less(Object x, Object y) {
+  public static boolean less(Object x, Object y) {
     Query aQueryPtr = (Query) x;
     Query bQueryPtr = (Query) y;
     if(aQueryPtr.index < bQueryPtr.index)
@@ -146,7 +146,7 @@ public class QuickSort {
     return false;
   }
 
-  public int diff(Object x, Object y) {
+  public static int diff(Object x, Object y) {
     Query aQueryPtr = (Query) x;
     Query bQueryPtr = (Query) y;
     return (aQueryPtr.index - bQueryPtr.index);
index 2c601f48176362ad79ec88a09926fa924536dad2..1862c3b4ad2b9f482f12f5b5e7d4dbc29da8ca7c 100644 (file)
@@ -1,23 +1,13 @@
 public class Random {
   long[] mt; 
   int mti;
-  long RANDOM_DEFAULT_SEED;
+  int RANDOM_DEFAULT_SEED;
   /* period parameter */
-  int N;
-  int M;
-  long MATRIX_A;
-  long UPPER_MASK;
-  long LOWER_MASK;
+
 
   public Random() {
-    RANDOM_DEFAULT_SEED = 0L;
-    N = 624;
-    M = 397;
-    mt = new long[N];
-    mti = N;
-    MATRIX_A = 0x9908b0dfL;   /* constant vector a */
-    UPPER_MASK = 0x80000000L; /* most significant w-r bits */
-    LOWER_MASK = 0x7fffffffL; /* least significant r bits */
+    RANDOM_DEFAULT_SEED = 0;
+    mt = new long[624];
   }
 
   public void random_alloc() {
@@ -25,11 +15,10 @@ public class Random {
   }
 
   /* initializes mt[N] with a seed */
-  public void init_genrand(long s) {
-    int mti;
-    mt[0]= s & 0xFFFFFFFFL;
-    for (mti=1; mti<N; mti++) {
-     mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+  public void init_genrand(int s) {
+    mt[0]= ((long)s) & 0xFFFFFFFFL;
+    for (int mti=1; mti<624; mti++) {
+      mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + ((long)mti));
       /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
       /* In the previous versions, MSBs of the seed affect   */
       /* only MSBs of the array mt[].                        */
@@ -37,43 +26,48 @@ public class Random {
       mt[mti] &= 0xFFFFFFFFL;
       /* for >32 bit machines */
     }
-    this.mti=mti;
+    this.mti=624;
   }
 
-  public void random_seed(long seed) {
+  public void random_seed(int seed) {
     init_genrand(seed);
   }
 
   public long random_generate() {
-    return genrand_int32();
+    long x= genrand_int32()&0xFFFFFFFFL;
+    return x;
+  }
+
+  public long posrandom_generate() {
+    long r=genrand_int32();
+    if (r>0)
+      return r;
+    else 
+      return -r;
   }
 
-  //public static long genrand_int32(long[] mt, long mtiPtr) {
   public long genrand_int32() {
     long y;
-    long[] mag01= new long[2];
-    mag01[0] = 0x0L;
-    mag01[1] = MATRIX_A;
     int mti = this.mti;
+    long[] mt = this.mt;
 
-    /* mag01[x] = x * MATRIX_A  for x=0,1 */
-
-    if (mti >= N) { /* generate N words at one time */
+    if (mti >= 624) { /* generate N words at one time */
       int kk;
 
-      if (mti == N+1)   /* if init_genrand() has not been called, */
-        init_genrand(5489L); /* a default initial seed is used */
-
-      for (kk=0;kk<N-M;kk++) {
-        y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
-        mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      if (mti == 624+1) {  /* if init_genrand() has not been called, */
+        init_genrand(5489); /* a default initial seed is used */
+       mti=this.mti;
+      }
+      for (kk=0;kk<(624-397);kk++) {
+        y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
+        mt[kk] = mt[kk+397] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
       }
-      for (;kk<N-1;kk++) {
-        y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
-        mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      for (;kk<(624-1);kk++) {
+        y = (mt[kk]&0x80000000L)|(mt[kk+1]&0x7fffffffL);
+        mt[kk] = mt[kk+(397-624)] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
       }
-      y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
-      mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      y = (mt[624-1]&0x80000000L)|(mt[0]&0x7fffffffL);
+      mt[624-1] = mt[397-1] ^ (y >> 1) ^ ((y & 0x1)==0 ? 0L:0x9908b0dfL);
 
       mti = 0;
     }
index 572859dec3e4c78a22ab235f4abcc2934cbe0a72..6708a0e84fa89645fd50e264822bb7a02cc8e634 100644 (file)
 #define CUTOFF 8
 
 public class Sort {
+  int[] lostk;
+  int[] histk;
 
-  public Sort() {
 
+  public Sort() {
+    lostk= new int[30];
+    histk= new int[30];
   }
 
   /* =============================================================================
    * swap
    * =============================================================================
    */
-  public static void
-    swap (byte[] base, int a, int b, int width)
-    {
-      if (a != b ) {
-        while (width--) {
-          byte tmp = base[a];
-          base[a++] = base[b];
-          base[b++] = tmp;
-        }
+  public static void swap (byte[] base, int a, int b, int width) {
+    if (a != b ) {
+      while(width--) {
+       byte tmp = base[a];
+       base[a++] = base[b];
+       base[b++] = tmp;
       }
     }
+  }
 
 
   /* =============================================================================
@@ -134,106 +136,104 @@ public class Sort {
    * sort
    * =============================================================================
    */
-  public static void
-    sort (byte[] base,
-        int start,
-        int num,
-        int width,
-        int n,
-        int offset)
-    {
-      if (num < 2 || width == 0) {
-        return;
-      }
-
-      /**
-       * Pointers that keep track of
-       * where to start looking in 
-       * the base array
-       **/
-      int[] lostk= new int[30];
-      int[] histk= new int[30];
-
-      int stkptr = 0;
-
-      int lo = start;
-      int hi = start + (width * (num - 1));
-
-      int size = 0;
-
-      int pvlo = lo;
-      int pvhi = hi;
-      int pvwidth = width;
-      int pvn = n;
-      int pvmid;
-      int pvloguy;
-      int pvhiguy;
-      int typeflag;
-
-      while(true) {
-
-        size = (pvhi - pvlo) / pvwidth + 1;
-        if (size <= CUTOFF) {
-
-          shortsort(base, pvlo, pvhi, pvwidth, pvn, offset);
-
-        } else {
-
-          pvmid = pvlo + (size / 2) * pvwidth;
-          swap(base, pvmid, pvlo, pvwidth);
-
-          pvloguy = pvlo;
-          pvhiguy = pvhi + pvwidth;
-
-          while(true) {
-            do {
-              pvloguy += pvwidth;
-            } while (pvloguy <= pvhi && cmp(base, pvloguy, pvlo, pvn, offset) <= 0);
-            do {
-              pvhiguy -= pvwidth;
-            } while (pvhiguy > pvlo && cmp(base, pvhiguy, pvlo, pvn, offset) >= 0);
-            if (pvhiguy < pvloguy) {
-              break;
-            }
-            swap(base, pvloguy, pvhiguy, pvwidth);
-          }
-
-          swap(base, pvlo, pvhiguy, pvwidth);
-
-          if ((pvhiguy - 1 - pvlo) >= (pvhi - pvloguy)) {
-            if (pvlo + pvwidth < pvhiguy) {
-              lostk[stkptr] = pvlo;
-              histk[stkptr] = pvhiguy - pvwidth;
-              ++stkptr;
-            }
-
-            if (pvloguy < pvhi) {
-              pvlo = pvloguy;
-              continue;
-            }
-          } else {
-            if (pvloguy < pvhi) {
-              lostk[stkptr] = pvloguy;
-              histk[stkptr] = pvhi;
-              ++stkptr;
-            }
-            if (pvlo + pvwidth < pvhiguy) {
-              pvhi = pvhiguy - pvwidth;
-              continue;
+  public void sort (byte[] base,
+                   int start,
+                   int num,
+                   int width,
+                   int n,
+                   int offset) {
+    if (num < 2 || width == 0) {
+      return;
+    }
+    
+    /**
+     * Pointers that keep track of
+     * where to start looking in 
+     * the base array
+     **/
+    int[] lostk=this.lostk;
+    int[] histk=this.histk;
+    
+    int stkptr = 0;
+    
+    int lo = start;
+    int hi = start + (width * (num - 1));
+    
+    int size = 0;
+    
+    int pvlo = lo;
+    int pvhi = hi;
+    int pvwidth = width;
+    int pvn = n;
+    int pvmid;
+    int pvloguy;
+    int pvhiguy;
+    int typeflag;
+    
+    while(true) {
+      
+      size = (pvhi - pvlo) / pvwidth + 1;
+      
+      if (size <= CUTOFF) {
+       
+       shortsort(base, pvlo, pvhi, pvwidth, pvn, offset);
+       
+      } else {
+       
+       pvmid = pvlo + (size / 2) * pvwidth;
+       swap(base, pvmid, pvlo, pvwidth);
+       
+       pvloguy = pvlo;
+       pvhiguy = pvhi + pvwidth;
+       
+       while(true) {
+         do {
+           pvloguy += pvwidth;
+         } while (pvloguy <= pvhi && cmp(base, pvloguy, pvlo, pvn, offset) <= 0);
+         do {
+           pvhiguy -= pvwidth;
+         } while (pvhiguy > pvlo && cmp(base, pvhiguy, pvlo, pvn, offset) >= 0);
+         if (pvhiguy < pvloguy) {
+           break;
+         }
+         swap(base, pvloguy, pvhiguy, pvwidth);
+       }
+       
+       swap(base, pvlo, pvhiguy, pvwidth);
+       
+       if ((pvhiguy - 1 - pvlo) >= (pvhi - pvloguy)) {
+         if (pvlo + pvwidth < pvhiguy) {
+           lostk[stkptr] = pvlo;
+           histk[stkptr] = pvhiguy - pvwidth;
+           ++stkptr;
+         }
+         
+         if (pvloguy < pvhi) {
+           pvlo = pvloguy;
+           continue;
+         }
+       } else {
+         if (pvloguy < pvhi) {
+           lostk[stkptr] = pvloguy;
+           histk[stkptr] = pvhi;
+           ++stkptr;
+         }
+         if (pvlo + pvwidth < pvhiguy) {
+           pvhi = pvhiguy - pvwidth;
+           continue;
             }
-          }
-        }
-
-        --stkptr;
-        if (stkptr >= 0) {
-          pvlo = lostk[stkptr];
-          pvhi = histk[stkptr];
-          continue;
-        }
-       break;
+       }
       }
+      
+      --stkptr;
+      if (stkptr >= 0) {
+       pvlo = lostk[stkptr];
+       pvhi = histk[stkptr];
+       continue;
+      }
+      break;
     }
+  }
 
   /* =============================================================================
    * compareRecord
diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Vector_t.java b/Robust/src/Benchmarks/SingleTM/Bayes/Vector_t.java
new file mode 100644 (file)
index 0000000..b134600
--- /dev/null
@@ -0,0 +1,141 @@
+public class Vector_t {
+  int size;
+  int capacity;
+  Object[] elements;
+
+  public Vector_t() {
+  }
+
+  /* =============================================================================
+   * Vector_alloc
+   * -- Returns null if failed
+   * =============================================================================
+   */
+  public Vector_t(int initCapacity) {
+    int capacity = Math.imax(initCapacity, 1);
+    this.size = 0;
+    this.capacity = capacity;
+    this.elements = new Object[capacity];
+  }
+
+  /* =============================================================================
+   * Vector_free
+   * =============================================================================
+   */
+  public void clear() {
+    elements = null;
+  }
+
+  /* =============================================================================
+   * Vector_at
+   * -- Returns null if failed
+   * =============================================================================
+   */
+  public Object vector_at (int i) {
+    if ((i < 0) || (i >= size)) {
+      System.out.println("Illegal Vector.element\n");
+      return null;
+    }
+    return (elements[i]);
+  }
+
+
+  /* =============================================================================
+   * Vector_pushBack
+   * -- Returns false if fail, else true
+   * =============================================================================
+   */
+  public boolean vector_pushBack (Object dataPtr) {
+    if (size == capacity) {
+      int newCapacity = capacity * 2;
+      Object[] newElements = new Object[newCapacity];
+
+      if (newElements == null) {
+        return false;
+      }
+      capacity = newCapacity;
+      for (int i = 0; i < size; i++) {
+        newElements[i] = elements[i];
+      }
+      elements = null;
+      elements = newElements;
+    }
+
+    elements[size++] = dataPtr;
+
+    return true;
+  }
+
+  /* =============================================================================
+   * Vector_popBack
+   * -- Returns null if fail, else returns last element
+   * =============================================================================
+   */
+  public Object vector_popBack () {
+    if (size < 1) {
+      return null;
+    }
+    Object o=elements[--(size)];
+    elements[size]=null;
+    return o;
+  }
+
+  /* =============================================================================
+   * Vector_getSize
+   * =============================================================================
+   */
+  public int
+    vector_getSize ()
+    {
+      return (size);
+    }
+
+  /* =============================================================================
+   * Vector_clear
+   * =============================================================================
+   */
+  public void vector_clear () {
+    while(size>0)
+      elements[--size]=null;
+  }
+  
+  /* =============================================================================
+   * Vector_sort
+   * =============================================================================
+   */
+  public void
+    vector_sort ()
+    {
+      QuickSort.sort(elements, 0, size);
+    }
+
+  /* =============================================================================
+   * Vector_copy
+   * =============================================================================
+   */
+  public static boolean
+    vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
+    {
+      int dstCapacity = dstVectorPtr.capacity;
+      int srcSize = srcVectorPtr.size;
+      if (dstCapacity < srcSize) {
+        int srcCapacity = srcVectorPtr.capacity;
+        Object[] elements = new Object[srcCapacity];
+
+        if (elements == null) {
+          return false;
+        }
+        dstVectorPtr.elements = null;
+        dstVectorPtr.elements = elements;
+        dstVectorPtr.capacity = srcCapacity;
+      }
+
+      for(int i = 0; i< srcSize; i++) {
+        dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
+      }
+
+      dstVectorPtr.size = srcSize;
+
+      return true;
+    }
+}
index 18b57d0d0bed7336d7a5267282924eca29b6c319..e8b9d50d51cafab2e4f599aaaa1aca19010f6734 100644 (file)
@@ -16,7 +16,7 @@ SRC=tmp${MAINCLASS}.java \
        IntListNode.java \
        tmpQueue.java \
        Random.java \
-       ../common/Vector_t.java \
+       Vector_t.java \
        ListNode.java \
        tmpList.java \
        QuickSort.java \
@@ -24,9 +24,11 @@ SRC=tmp${MAINCLASS}.java \
     ../../../ClassLibrary/JavaSTM/Barrier.java \
     ../common/LocalStartStop.java
 
-FLAGS=-mainclass ${MAINCLASS} -singleTM -optimize -debug -dcopts -stmstats -fastmemcpy -transstats -abcclose
+include ../common/Makefile.flags
 
-default:
+include ../common/Makefile.builds
+
+prep:
        cpp Bayes.java > tmp1Bayes.java
        cpp Data.java > tmp1Data.java
        cpp Net.java > tmp1Net.java
@@ -37,7 +39,6 @@ default:
        cpp -DLIST_NO_DUPLICATES IntList.java > tmp1IntList.java
        cpp -DLIST_NO_DUPLICATES List.java > tmp1List.java
        ./extractLines
-       ../../../buildscript ${FLAGS} -o ${MAINCLASS} ${SRC}
 
 clean:
        rm tmp1Bayes.java