hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r782540 - in /incubator/hama/trunk/src: examples/org/apache/hama/examples/ java/org/apache/hama/ java/org/apache/hama/mapred/ java/org/apache/hama/util/ test/org/apache/hama/mapred/ test/org/apache/hama/util/
Date Mon, 08 Jun 2009 06:31:15 GMT
Author: edwardyoon
Date: Mon Jun  8 06:31:10 2009
New Revision: 782540

URL: http://svn.apache.org/viewvc?rev=782540&view=rev
Log:
Fix random matrix bug

Modified:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/SparseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/mapred/RandomMatrixMap.java
    incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java
    incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java
    incubator/hama/trunk/src/test/org/apache/hama/util/TestRandomVariable.java

Modified: incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java (original)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java Mon Jun 
8 06:31:10 2009
@@ -32,7 +32,7 @@
       System.out
           .println("rand [-m maps] [-r reduces] <rows> <columns> <density>
<matrix_name>");
       System.out
-      .println("ex) rand -m 10 -r 10 2000 2000 30% matrixA");
+      .println("ex) rand -m 10 -r 10 2000 2000 30.5% matrixA");
       System.exit(-1);
     } else {
       parseArgs(args);
@@ -40,7 +40,7 @@
 
     int row = Integer.parseInt(ARGS.get(0));
     int column = Integer.parseInt(ARGS.get(1));
-    int percent = Integer.parseInt(ARGS.get(2).substring(0, ARGS.get(2).length()-1));
+    double percent = Double.parseDouble(ARGS.get(2).substring(0, ARGS.get(2).length()-1));
     
     Matrix a;
     if(percent == 100)

Modified: incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Mon Jun  8 06:31:10 2009
@@ -245,7 +245,7 @@
     jobConf.setSpeculativeExecution(false);
     jobConf.setInt("matrix.column", n);
     jobConf.set("matrix.type", TABLE_PREFIX);
-    jobConf.setInt("matrix.density", 100);
+    jobConf.set("matrix.density", "100");
     
     jobConf.setInputFormat(SequenceFileInputFormat.class);
     final FileSystem fs = FileSystem.get(jobConf);

Modified: incubator/hama/trunk/src/java/org/apache/hama/SparseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/SparseMatrix.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/SparseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/SparseMatrix.java Mon Jun  8 06:31:10 2009
@@ -109,7 +109,7 @@
     return rand;
   }
   
-  public static SparseMatrix random_mapred(HamaConfiguration conf, int m, int n, int percent)
throws IOException {
+  public static SparseMatrix random_mapred(HamaConfiguration conf, int m, int n, double percent)
throws IOException {
     SparseMatrix rand = new SparseMatrix(conf, m, n);
     LOG.info("Create the " + m + " * " + n + " random matrix : "
         + rand.getPath());
@@ -131,7 +131,7 @@
     jobConf.setSpeculativeExecution(false);
     jobConf.setInt("matrix.column", n);
     jobConf.set("matrix.type", TABLE_PREFIX);
-    jobConf.setInt("matrix.density", percent);
+    jobConf.set("matrix.density", String.valueOf(percent));
 
     jobConf.setInputFormat(SequenceFileInputFormat.class);
     final FileSystem fs = FileSystem.get(jobConf);

Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/RandomMatrixMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/RandomMatrixMap.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/RandomMatrixMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/RandomMatrixMap.java Mon Jun  8 06:31:10
2009
@@ -40,7 +40,9 @@
 public class RandomMatrixMap extends MapReduceBase implements
     Mapper<IntWritable, IntWritable, IntWritable, MapWritable> {
   static final Logger LOG = Logger.getLogger(RandomMatrixMap.class);
-  protected int column, density;
+  protected int column;
+  protected double density;
+  protected int minNums;
   protected String type;
   protected Vector vector = new DenseVector();
 
@@ -49,16 +51,16 @@
       OutputCollector<IntWritable, MapWritable> output, Reporter report)
       throws IOException {
     if (type.equals("SparseMatrix")) {
-      ((SparseVector) vector).clear();
       for (int i = key.get(); i <= value.get(); i++) {
-        for (int j = 0; j < column; j++) {
-            ((SparseVector) vector).set(j, RandomVariable.rand(density));
+        ((SparseVector) vector).clear();
+        for (int j = 0; j < minNums; j++) {
+          ((SparseVector) vector).set(RandomVariable.randInt(0, column - 1), RandomVariable.rand());
         }
         output.collect(new IntWritable(i), vector.getEntries());
       }
     } else {
-      ((DenseVector) vector).clear();
       for (int i = key.get(); i <= value.get(); i++) {
+        ((DenseVector) vector).clear();
         for (int j = 0; j < column; j++) {
           ((DenseVector) vector).set(j, RandomVariable.rand());
         }
@@ -69,7 +71,13 @@
 
   public void configure(JobConf job) {
     column = job.getInt("matrix.column", 0);
-    density = job.getInt("matrix.density", 100);
+    density = Double.parseDouble(job.get("matrix.density"));
+    
+    double vv = (column/100.0) * density;
+    minNums = Math.round((float) vv);
+    if(minNums == 0)
+      minNums = 1;
+    
     type = job.get("matrix.type");
     if (type.equals("SparseMatrix"))
       vector = new SparseVector();

Modified: incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java Mon Jun  8 06:31:10
2009
@@ -38,23 +38,6 @@
   }
 
   /**
-   * Generate a random number
-   * 
-   * @param density percentage of the non-zero
-   * @return a double 0 or between 0 and 1
-   */
-  public static double rand(int density) {
-    int[] prob = new int[100];
-    double x = Math.random();
-    
-    for(int i = 0; i < density; i++) {
-      prob[i] = 1;
-    }
-
-    return (prob[randInt(0, 99)] == 0) ? 0 : x;
-  }
-
-  /**
    * Generate a random integer.
    * 
    * @param i0 min of the random variable.

Modified: incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/mapred/TestRandomMatrixMapReduce.java Mon
Jun  8 06:31:10 2009
@@ -39,7 +39,7 @@
         assertTrue(rand.get(i, j) > -1);
       }
     }
-    
+
     rand.close();
     
     SparseMatrix rand2 = SparseMatrix.random_mapred(conf, 20, 20, 30);

Modified: incubator/hama/trunk/src/test/org/apache/hama/util/TestRandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/util/TestRandomVariable.java?rev=782540&r1=782539&r2=782540&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/util/TestRandomVariable.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/util/TestRandomVariable.java Mon Jun  8
06:31:10 2009
@@ -40,13 +40,6 @@
       double result = RandomVariable.rand();
       assertTrue(result >= 0.0d && result <= 1.0);
     }
-    
-    int nonZero = 0;
-    for (int i = 0; i < COUNT; i++) {
-      if(RandomVariable.rand(70) > 0)
-        nonZero++;
-    }
-    assertTrue((COUNT/2) < nonZero);
   }
 
   /**



Mime
View raw message