changes to use floats
authorbdemsky <bdemsky>
Fri, 15 May 2009 09:29:13 +0000 (09:29 +0000)
committerbdemsky <bdemsky>
Fri, 15 May 2009 09:29:13 +0000 (09:29 +0000)
Robust/src/Benchmarks/SingleTM/KMeans/Cluster.java
Robust/src/Benchmarks/SingleTM/KMeans/Common.java
Robust/src/Benchmarks/SingleTM/KMeans/KMeans.java
Robust/src/Benchmarks/SingleTM/KMeans/Normal.java
Robust/src/Benchmarks/SingleTM/KMeans/Random.java
Robust/src/Benchmarks/SingleTM/KMeans/makefile
Robust/src/ClassLibrary/String.java
Robust/src/Runtime/runtime.c

index 037dc779a0a72a58b4b62bf7416a546dacf46872..90912c29ec69c7d78e2eb3ba0ab131804e5982fd 100644 (file)
@@ -83,20 +83,21 @@ public class Cluster {
    * extractMoments
    * =============================================================================
    */
-  public float[]
+  public static float[]
     extractMoments (float []data, int num_elts, int num_moments)
     {
       float[] moments = new float[num_moments];
 
+      float mzero=0.0f;
       for (int i = 0; i < num_elts; i++) {
-        moments[0] += data[i];
+        mzero += data[i];
       }
 
-      moments[0] = moments[0] / num_elts;
+      moments[0] = mzero / num_elts;
       for (int j = 1; j < num_moments; j++) {
         moments[j] = 0;
         for (int i = 0; i < num_elts; i++) {
-          moments[j] += Math.pow((data[i]-moments[0]), j+1);
+          moments[j] += (float) Math.pow((data[i]-moments[0]), j+1);
         }
         moments[j] = moments[j] / num_elts;
       }
@@ -108,7 +109,7 @@ public class Cluster {
    * zscoreTransform
    * =============================================================================
    */
-  public void
+  public static void
     zscoreTransform (float[][] data, /* in & out: [numObjects][numAttributes] */
         int     numObjects,
         int     numAttributes)
@@ -132,7 +133,7 @@ public class Cluster {
    * cluster_exec
    * =============================================================================
    */
-  public void
+  public static void
     cluster_exec (
         int      nthreads,               /* in: number of threads*/
         int      numObjects,             /* number of input objects */
@@ -145,11 +146,11 @@ public class Cluster {
       int itime;
       int nclusters;
 
-      float[][] tmp_cluster_centres = null;
+      float[][] tmp_cluster_centres;
       int[] membership = new int[numObjects];
 
       Random randomPtr = new Random();
-      randomPtr = randomPtr.random_alloc(randomPtr);
+      randomPtr.random_alloc();
 
       if (kms.use_zscore_transform == 1) {
         zscoreTransform(attributes, numObjects, numAttributes);
@@ -162,7 +163,7 @@ public class Cluster {
        */
       for (nclusters = kms.min_nclusters; nclusters <= kms.max_nclusters; nclusters++) {
 
-        randomPtr.random_seed(randomPtr, 7);
+        randomPtr.random_seed(7);
         args.nclusters = nclusters;
 
         Normal norm = new Normal();
@@ -184,8 +185,6 @@ public class Cluster {
 
         itime++;
       } /* nclusters */
-
-      randomPtr = null;
     }
 }
 
index 83fc44b22aadf2e2d8410444a86fd1eb519efd68..bd372e23407d725d17b8a3fbf116ccc6d56aa65d 100644 (file)
@@ -78,7 +78,7 @@ public class Common {
     common_euclidDist2 (float[] pt1, float[] pt2, int numdims)
     {
       int i;
-      float ans = 0.0;
+      float ans = 0.0f;
 
       for (i = 0; i < numdims; i++) {
         ans += (pt1[i] - pt2[i]) * (pt1[i] - pt2[i]);
@@ -101,8 +101,8 @@ public class Common {
       int index = -1;
       int i;
       //double max_dist = FLT_MAX;
-      float max_dist = 3.40282347e+38;
-      float limit = 0.99999;
+      float max_dist = (float)3.40282347e+38;
+      float limit = (float) 0.99999;
 
       /* Find the cluster center id with min distance to pt */
       for (i = 0; i < npts; i++) {
index 44fdffaff5e19c0d3bb4d9c2be24de14fd25108f..7462512cbfb02d9911246ddfb57daaf411a962fd 100644 (file)
@@ -141,7 +141,7 @@ public class KMeans extends Thread {
     min_nclusters = 4;
     isBinaryFile = 0;
     use_zscore_transform = 1;
-    threshold = 0.001;
+    threshold = (float) 0.001;
     best_nclusters = 0;
   }
 
@@ -264,8 +264,7 @@ public class KMeans extends Thread {
         }
       }
 
-      Cluster clus = new Cluster();
-      clus.cluster_exec(nthreads,
+      Cluster.cluster_exec(nthreads,
           numObjects,
           numAttributes,
           attributes,             // [numObjects][numAttributes] 
@@ -378,8 +377,9 @@ public class KMeans extends Thread {
        for(int ii=0;ii<x;ii++)
          newbytes[ii+oldbytes.length]=b[ii];
        x++; //skip past space
-       if (j>=0)
-         buf[i][j]=ByteToFloat(newbytes, 0, newbytes.length);
+       if (j>=0) {
+         buf[i][j]=(float)Double.parseDouble(new String(newbytes, 0, newbytes.length));
+       }
        j++;
        oldbytes=null;
       }
@@ -404,8 +404,10 @@ public class KMeans extends Thread {
        }
        
        //otherwise x is beginning of character string, y is end
-       if (j>=0)
-         buf[i][j]= ByteToFloat(b, x, y-x);
+       if (j>=0) {
+
+         buf[i][j]=(float)Double.parseDouble(new String(b,x,y-x));
+       }
        x=y;//skip to end of number
        x++;//skip past space
        j++;
@@ -413,27 +415,6 @@ public class KMeans extends Thread {
     }
     inputFile.close();
   }
-
-  /**
-   * Convert a string into float
-   **/
-  public static float ByteToFloat (byte[] str, int offset, int length) {
-    float left=0.0d;
-    float right=0.0d;
-    int i;
-    for(i=0;i<length;i++) {
-      if (str[i+offset]=='.')
-       break;
-      left=left*10+(str[i+offset]-'0');
-    }
-    i++; //skip past decimal point
-    float multiplier=0.1d;
-    for(;i<length;i++) {
-      right+=multiplier*(str[i+offset]-'0');
-      multiplier*=0.1d;
-    }
-    return left+right;
-  }
 }
 
 /* =============================================================================
index 8f9e23d214c943d13041f91d36d7c97a2a62b2cf..4856ff368e52d3db070cf337a32ffb3785da16f7 100644 (file)
@@ -106,7 +106,7 @@ public class Normal {
     float[][] clusters = args.clusters;
     intwrapper[] new_centers_len = args.new_centers_len;
     float[][] new_centers = args.new_centers;
-    float delta = 0.0;
+    float delta = 0.0f;
     int index, start, stop;
 
     start = myId * CHUNK;
@@ -125,7 +125,7 @@ public class Normal {
          * membership[i] cannot be changed by other threads
          */
         if (membership[i] != index) {
-          delta += 1.0;
+          delta += 1.0f;
         }
 
         /* Assign the membership to object i */
@@ -180,7 +180,7 @@ public class Normal {
 
     /* Randomly pick cluster centers */
     for (int i = 0; i < nclusters; i++) {
-      int n = (int)(randomPtr.random_generate(randomPtr) % npoints);
+      int n = (int)(randomPtr.random_generate() % npoints);
       for (int j = 0; j < nfeatures; j++) {
         clusters[i][j] = feature[n][j];
       }
@@ -200,7 +200,7 @@ public class Normal {
 
     int loop = 0;
     do {
-      delta = 0.0d;
+      delta = (float) 0.0;
 
       args.feature         = feature;
       args.nfeatures       = nfeatures;
@@ -225,7 +225,7 @@ public class Normal {
           if (new_centers_len[i] > 0) {
             clusters[i][j] = new_centers[i][j] / new_centers_len[i];
           }
-          new_centers[i][j] = 0.0;   /* set back to 0 */
+          new_centers[i][j] = (float)0.0;   /* set back to 0 */
         }
         new_centers_len[i] = 0;   /* set back to 0 */
       }
@@ -242,7 +242,7 @@ public class Normal {
   /**
    * Work done by primary thread in parallel with other threads
    **/
-  thread_work(GlobalArgs args) {
+  void thread_work(GlobalArgs args) {
    Barrier.enterBarrier();
    Normal.work(0, args); //threadId = 0 because primary thread
    Barrier.enterBarrier();
index 819abe265a6a45514b823aeda9b7141398964c73..2c601f48176362ad79ec88a09926fa924536dad2 100644 (file)
@@ -14,74 +14,71 @@ public class Random {
     N = 624;
     M = 397;
     mt = new long[N];
-    mti = 0;
+    mti = N;
     MATRIX_A = 0x9908b0dfL;   /* constant vector a */
     UPPER_MASK = 0x80000000L; /* most significant w-r bits */
     LOWER_MASK = 0x7fffffffL; /* least significant r bits */
   }
 
-  public Random random_alloc(Random rand) {
-    init_genrand(rand, rand.RANDOM_DEFAULT_SEED);
-    return rand;
+  public void random_alloc() {
+    init_genrand(this.RANDOM_DEFAULT_SEED);
   }
 
   /* initializes mt[N] with a seed */
-  public void init_genrand(Random rand, long s) {
+  public void init_genrand(long s) {
     int mti;
-
-    rand.mt[0]= s & 0xFFFFFFFFL;
-    for (mti=1; mti<rand.N; mti++) {
-     rand.mt[mti] = (1812433253L * (rand.mt[mti-1] ^ (rand.mt[mti-1] >> 30)) + mti);
+    mt[0]= s & 0xFFFFFFFFL;
+    for (mti=1; mti<N; mti++) {
+     mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + 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[].                        */
       /* 2002/01/09 modified by Makoto Matsumoto             */
-      rand.mt[mti] &= 0xFFFFFFFFL;
+      mt[mti] &= 0xFFFFFFFFL;
       /* for >32 bit machines */
     }
-  
-    rand.mti = mti;
+    this.mti=mti;
   }
 
-  public void random_seed(Random rand, long seed) {
-    init_genrand(rand, seed);
+  public void random_seed(long seed) {
+    init_genrand(seed);
   }
 
-  public long random_generate(Random rand) {
-    return genrand_int32(rand);
+  public long random_generate() {
+    return genrand_int32();
   }
 
   //public static long genrand_int32(long[] mt, long mtiPtr) {
-  public long genrand_int32(Random rand) {
+  public long genrand_int32() {
     long y;
     long[] mag01= new long[2];
     mag01[0] = 0x0L;
-    mag01[1] = rand.MATRIX_A;
-    int mti = rand.mti;
+    mag01[1] = MATRIX_A;
+    int mti = this.mti;
 
     /* mag01[x] = x * MATRIX_A  for x=0,1 */
 
-    if (mti >= rand.N) { /* generate N words at one time */
+    if (mti >= N) { /* generate N words at one time */
       int kk;
 
-      if (mti == rand.N+1)   /* if init_genrand() has not been called, */
-        init_genrand(rand, 5489L); /* a default initial seed is used */
+      if (mti == N+1)   /* if init_genrand() has not been called, */
+        init_genrand(5489L); /* a default initial seed is used */
 
-      for (kk=0;kk<rand.N-rand.M;kk++) {
-        y = (rand.mt[kk]&rand.UPPER_MASK)|(rand.mt[kk+1]&LOWER_MASK);
-        rand.mt[kk] = rand.mt[kk+M] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      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)];
       }
-      for (;kk<rand.N-1;kk++) {
-        y = (rand.mt[kk]&rand.UPPER_MASK)|(rand.mt[kk+1]&LOWER_MASK);
-        rand.mt[kk] = rand.mt[kk+(M-N)] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      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)];
       }
-      y = (rand.mt[N-1]&rand.UPPER_MASK)|(rand.mt[0]&LOWER_MASK);
-      rand.mt[N-1] = rand.mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+      y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
+      mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
 
       mti = 0;
     }
 
-    y = rand.mt[mti++];
+    y = mt[mti++];
 
     /* Tempering */
     y ^= (y >> 11);
@@ -89,7 +86,7 @@ public class Random {
     y ^= (y << 15) & 0xefc60000L;
     y ^= (y >> 18);
 
-    rand.mti = mti;
+    this.mti = mti;
 
     return y;
   }
index a27712041219005cd6d4e17e40527f8b8b2c4c89..7fa5bb352cc96bc10147df91bd386fc1221eea3d 100644 (file)
@@ -7,7 +7,7 @@ SRC=${MAINCLASS}.java \
     GlobalArgs.java \
     ../../../ClassLibrary/JavaSTM/Barrier.java
 FLAGS=-mainclass ${MAINCLASS} -singleTM -optimize -debug -dcopts -joptimize -stmstats -fastmemcpy
-FLAGS2=-mainclass ${MAINCLASS} -singleTM -optimize -debug -joptimize -dcopts -fastmemcpy
+FLAGS2=-mainclass ${MAINCLASS} -singleTM -optimize -debug -joptimize -dcopts -fastmemcpy -abcclose
 FLAGS3=-mainclass ${MAINCLASS} -optimize -debug -joptimize -thread
 
 
index b1ebb8fc23ab4ef165a0d32ca30e691c27ada790..037e3cb6462685238eb3e88700d8eaefcf2da827 100644 (file)
@@ -289,59 +289,15 @@ public class String {
   }
 
   public static String valueOf(double val) {
-    int i = 0, j = 0, k = 0;
-    long nodecimal = 0;
-    double decimal = 1.0d, valueA = 0.0d;
-    StringBuffer output = new StringBuffer();
-
-    for(i = 0; decimal != nodecimal; i++) {
-      long basePower = 1;
-      for(int x=0; x<i; x++) {
-       basePower*= 10;
-      }
-      nodecimal = (long) (val*basePower);
-      decimal = val*basePower;
-    } //i = place counted from right that decimal point appears
-
-    valueA = nodecimal; //valueA = val with no decimal point (val*10^i)
-
-    for(j = 0; decimal >= 0; j++) {
-      long basePower = 1;
-      for(int x=0; x<j; x++) {
-       basePower*= 10;
-      }
-      nodecimal = (long) (valueA - basePower);
-      decimal = (double) nodecimal;
-    } //j-1 = number of digits
-
-    i--;
-    j--;
-    decimal = 0;
-
-    for(k = j; k > 0; k--) {
-      if(k == i) { //if a decimal point was previously found
-       //insert it where its meant to be
-       output.append((char)46);
-      }
-      long basePower = 1;
-      for(int x=0; x<(k-1); x++) {
-       basePower*= 10;
-      }
-      nodecimal = ((long) (valueA - decimal) / basePower);
-      decimal += nodecimal*basePower;
-      output.append((char)(48 + nodecimal));
-    }
-
-    return output.toString();
-  }
-
-  public static long basePower(int x, int y) {
-    long t = 1;
-    for(int i=0; i<y; i++) {
-      t *= x;
-    }
-    return t;
-  }
+    char[] chararray=new char[10];
+    String s=new String();
+    s.offset=0;
+    s.count=convertdoubletochar(val, chararray);
+    s.value=chararray;
+    return s;
+  }
+  
+  public static native int convertdoubletochar(double val, char [] chararray);
 
   public static String valueOf(long x) {
     int length=0;
index c8161aa494f9c9355e6c6a97c466abcc9598c483..c175242faf4de4d6beb6b940a4b3cfcf725f36b2 100644 (file)
@@ -109,6 +109,38 @@ void injectinstructionfailure() {
 #endif
 }
 
+#ifdef D___Double______nativeparsedouble____L___String___
+double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
+  int length=VAR(___str___)->___count___;
+  int maxlength=(length>60)?60:length;
+  char str[maxlength+1];
+  struct ArrayObject * chararray=VAR(___str___)->___value___;
+  int i;
+  int offset=VAR(___str___)->___offset___;
+  for(i=0; i<maxlength; i++) {
+    str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
+  }
+  str[i]=0;
+  double d=atof(str);
+  return d;
+}
+#endif
+
+#ifdef D___String______convertdoubletochar____D__AR_C
+int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
+  int length=VAR(___chararray___)->___length___;
+  char str[length];
+  int i;
+  int num=snprintf(str, length, "%f",___val___);
+  if (num>=length)
+    num=length-1;
+  for(i=0; i<length; i++) {
+    ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
+  }
+  return num;
+}
+#endif
+
 void CALL11(___System______exit____I,int ___status___, int ___status___) {
 #ifdef TRANSSTATS
   printf("numTransCommit = %d\n", numTransCommit);
@@ -369,7 +401,6 @@ __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int len
 #endif
 #endif
 
-
 /* Converts C character arrays into Java strings */
 #ifdef PRECISE_GC
 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {