From 051ca9ef431cc4d61e27e7ece4717ed73e61a951 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 15 May 2009 09:29:13 +0000 Subject: [PATCH] changes to use floats --- .../Benchmarks/SingleTM/KMeans/Cluster.java | 21 +++---- .../Benchmarks/SingleTM/KMeans/Common.java | 6 +- .../Benchmarks/SingleTM/KMeans/KMeans.java | 37 +++-------- .../Benchmarks/SingleTM/KMeans/Normal.java | 12 ++-- .../Benchmarks/SingleTM/KMeans/Random.java | 61 +++++++++--------- .../src/Benchmarks/SingleTM/KMeans/makefile | 2 +- Robust/src/ClassLibrary/String.java | 62 +++---------------- Robust/src/Runtime/runtime.c | 33 +++++++++- 8 files changed, 99 insertions(+), 135 deletions(-) diff --git a/Robust/src/Benchmarks/SingleTM/KMeans/Cluster.java b/Robust/src/Benchmarks/SingleTM/KMeans/Cluster.java index 037dc779..90912c29 100644 --- a/Robust/src/Benchmarks/SingleTM/KMeans/Cluster.java +++ b/Robust/src/Benchmarks/SingleTM/KMeans/Cluster.java @@ -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; } } diff --git a/Robust/src/Benchmarks/SingleTM/KMeans/Common.java b/Robust/src/Benchmarks/SingleTM/KMeans/Common.java index 83fc44b2..bd372e23 100644 --- a/Robust/src/Benchmarks/SingleTM/KMeans/Common.java +++ b/Robust/src/Benchmarks/SingleTM/KMeans/Common.java @@ -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++) { diff --git a/Robust/src/Benchmarks/SingleTM/KMeans/KMeans.java b/Robust/src/Benchmarks/SingleTM/KMeans/KMeans.java index 44fdffaf..7462512c 100644 --- a/Robust/src/Benchmarks/SingleTM/KMeans/KMeans.java +++ b/Robust/src/Benchmarks/SingleTM/KMeans/KMeans.java @@ -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=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 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(); diff --git a/Robust/src/Benchmarks/SingleTM/KMeans/Random.java b/Robust/src/Benchmarks/SingleTM/KMeans/Random.java index 819abe26..2c601f48 100644 --- a/Robust/src/Benchmarks/SingleTM/KMeans/Random.java +++ b/Robust/src/Benchmarks/SingleTM/KMeans/Random.java @@ -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> 30)) + mti); + mt[0]= s & 0xFFFFFFFFL; + for (mti=1; mti> 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> 1) ^ mag01[(int)(y & 0x1L)]; + for (kk=0;kk> 1) ^ mag01[(int)(y & 0x1L)]; } - for (;kk> 1) ^ mag01[(int)(y & 0x1L)]; + for (;kk> 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; } diff --git a/Robust/src/Benchmarks/SingleTM/KMeans/makefile b/Robust/src/Benchmarks/SingleTM/KMeans/makefile index a2771204..7fa5bb35 100644 --- a/Robust/src/Benchmarks/SingleTM/KMeans/makefile +++ b/Robust/src/Benchmarks/SingleTM/KMeans/makefile @@ -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 diff --git a/Robust/src/ClassLibrary/String.java b/Robust/src/ClassLibrary/String.java index b1ebb8fc..037e3cb6 100644 --- a/Robust/src/ClassLibrary/String.java +++ b/Robust/src/ClassLibrary/String.java @@ -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= 0; j++) { - long basePower = 1; - for(int x=0; x 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___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___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___)+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) { -- 2.34.1