hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sam...@apache.org
Subject svn commit: r725655 - in /incubator/hama/trunk: CHANGES.txt src/java/org/apache/hama/DenseMatrix.java src/java/org/apache/hama/SubMatrix.java src/java/org/apache/hama/io/BlockID.java
Date Thu, 11 Dec 2008 11:00:21 GMT
Author: samuel
Date: Thu Dec 11 03:00:20 2008
New Revision: 725655

URL: http://svn.apache.org/viewvc?rev=725655&view=rev
Log:
HAMA-134:We don't need to fill C with zeros

Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=725655&r1=725654&r2=725655&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Thu Dec 11 03:00:20 2008
@@ -33,6 +33,7 @@
     
   IMPROVEMENTS
     
+    HAMA-134: We don't need to fill C with zeros (edwardyoon)
     HAMA-131: Add argument for the number of blocks (edwardyoon)
     HAMA-113: Random matrix generator on map/reduce (edwardyoon)
     HAMA-118: Add getting started page link (edwardyoon)

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=725655&r1=725654&r2=725655&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Thu Dec 11 03:00:20 2008
@@ -30,7 +30,6 @@
 import org.apache.hadoop.hbase.io.BatchUpdate;
 import org.apache.hadoop.hbase.io.Cell;
 import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.io.BooleanWritable;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.LongWritable;
@@ -47,6 +46,7 @@
 import org.apache.hama.algebra.RowCyclicAdditionReduce;
 import org.apache.hama.algebra.SIMDMultiplyMap;
 import org.apache.hama.algebra.SIMDMultiplyReduce;
+import org.apache.hama.io.BlockID;
 import org.apache.hama.io.BlockWritable;
 import org.apache.hama.io.DoubleEntry;
 import org.apache.hama.io.MapWritable;
@@ -515,7 +515,7 @@
         if (endColumn >= this.getColumns())
           endColumn = this.getColumns() - 1;
 
-        BatchUpdate update = new BatchUpdate(getBlockKey(i, j));
+        BatchUpdate update = new BatchUpdate(new BlockID(i, j).getBytes());
         update.put(Constants.BLOCK_STARTROW, BytesUtil.intToBytes(startRow));
         update.put(Constants.BLOCK_ENDROW, BytesUtil.intToBytes(endRow));
         update.put(Constants.BLOCK_STARTCOLUMN, BytesUtil
@@ -531,22 +531,17 @@
   }
 
   protected int[] getBlockPosition(int i, int j) throws IOException {
+    RowResult rs = table.getRow(new BlockID(i, j).getBytes());
     int[] result = new int[4];
-    result[0] = BytesUtil.bytesToInt(table.get(getBlockKey(i, j),
-        Constants.BLOCK_STARTROW).getValue());
-    result[1] = BytesUtil.bytesToInt(table.get(getBlockKey(i, j),
-        Constants.BLOCK_ENDROW).getValue());
-    result[2] = BytesUtil.bytesToInt(table.get(getBlockKey(i, j),
-        Constants.BLOCK_STARTCOLUMN).getValue());
-    result[3] = BytesUtil.bytesToInt(table.get(getBlockKey(i, j),
-        Constants.BLOCK_ENDCOLUMN).getValue());
+    
+    result[0] = BytesUtil.bytesToInt(rs.get(Constants.BLOCK_STARTROW).getValue());
+    result[1] = BytesUtil.bytesToInt(rs.get(Constants.BLOCK_ENDROW).getValue());
+    result[2] = BytesUtil.bytesToInt(rs.get(Constants.BLOCK_STARTCOLUMN).getValue());
+    result[3] = BytesUtil.bytesToInt(rs.get(Constants.BLOCK_ENDCOLUMN).getValue());
+    
     return result;
   }
 
-  protected String getBlockKey(int i, int j) {
-    return i + "," + j;
-  }
-
   /**
    * Using a map/reduce job to block a dense matrix.
    * 
@@ -583,11 +578,9 @@
     Scanner scan = table.getScanner(columns);
 
     for (RowResult row : scan) {
-      String[] key = Bytes.toString(row.getRow()).split("[,]");
-      int blockR = Integer.parseInt(key[0]);
-      int blockC = Integer.parseInt(key[1]);
-      int[] pos = getBlockPosition(blockR, blockC);
-      setBlock(blockR, blockC, subMatrix(pos[0], pos[1], pos[2], pos[3]));
+      BlockID bID = new BlockID(row.getRow());
+      int[] pos = getBlockPosition(bID.getRow(), bID.getColumn());
+      setBlock(bID.getRow(), bID.getColumn(), subMatrix(pos[0], pos[1], pos[2], pos[3]));
     }
   }
 

Modified: incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java?rev=725655&r1=725654&r2=725655&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/SubMatrix.java Thu Dec 11 03:00:20 2008
@@ -24,7 +24,6 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.util.Arrays;
 
 import org.apache.log4j.Logger;
 
@@ -93,6 +92,10 @@
     return matrix[i][j];
   }
 
+  public void add(int row, int column, double value) {
+    matrix[row][column] = matrix[row][column] + value;
+  }
+
   /**
    * c = a+b
    * 
@@ -100,14 +103,15 @@
    * @return c
    */
   public SubMatrix add(SubMatrix b) {
-    double[][] C = new double[getRows()][getColumns()];
+    SubMatrix c = new SubMatrix(this.getRows(), this.getColumns());
+    
     for (int i = 0; i < this.getRows(); i++) {
       for (int j = 0; j < this.getColumns(); j++) {
-        C[i][j] = (this.get(i, j) + b.get(i, j));
+        c.set(i, j, (this.get(i, j) + b.get(i, j)));
       }
     }
 
-    return new SubMatrix(C);
+    return c;
   }
 
   /**
@@ -117,19 +121,17 @@
    * @return c
    */
   public SubMatrix mult(SubMatrix b) {
-    double[][] C = new double[this.getRows()][b.getColumns()];
-    for (int i = 0; i < this.getRows(); i++) {
-      Arrays.fill(C[i], 0);
-    }
+    SubMatrix c = new SubMatrix(this.getRows(), b.getColumns());
+    
     for (int i = 0; i < this.getRows(); i++) {
       for (int j = 0; j < b.getColumns(); j++) {
         for (int k = 0; k < this.getColumns(); k++) {
-          C[i][j] += this.get(i, k) * b.get(k, j);
+          c.add(i, j, this.get(i, k) * b.get(k, j));
         }
       }
     }
 
-    return new SubMatrix(C);
+    return c;
   }
 
   /**

Modified: incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java?rev=725655&r1=725654&r2=725655&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java Thu Dec 11 03:00:20 2008
@@ -19,15 +19,20 @@
  */
 package org.apache.hama.io;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 
 import org.apache.hadoop.io.WritableComparable;
 
 /** A WritableComparable for BlockIDs. */
 @SuppressWarnings("unchecked")
-public class BlockID implements WritableComparable {
+public class BlockID implements WritableComparable, java.io.Serializable {
+  private static final long serialVersionUID = 1L;
   private int row;
   private int column;
 
@@ -38,6 +43,21 @@
     set(row, column);
   }
 
+  public BlockID(byte[] bytes) throws IOException {
+    ByteArrayInputStream bos = new ByteArrayInputStream(bytes);
+    ObjectInputStream oos = new ObjectInputStream(bos);
+    Object obj = null;
+    try {
+      obj = oos.readObject();
+      this.row = ((BlockID)obj).getRow();
+      this.column = ((BlockID)obj).getColumn();
+    } catch (ClassNotFoundException e) {
+      e.printStackTrace();
+    }
+    oos.close();
+    bos.close();
+  }
+
   public void set(int row, int column) {
     this.row = row;
     this.column = column;
@@ -89,34 +109,14 @@
     }
   }
 
-  /**
-   * BlockID Comparator 
-   */
-  /* why we need a special Comparator
-  public static class Comparator extends WritableComparator {
-    protected Comparator() {
-      super(BlockID.class);
-    }
-
-    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
-      int thisRow = readInt(b1, s1);
-      int thatRow = readInt(b2, s2);
-      // why read from l1 & l2
-      // l1 means length 1, and l2 means length 2
-      int thisColumn = readInt(b1, l1);
-      int thatColumn = readInt(b2, l2);
-
-      if (thisRow != thatRow) {
-        return (thisRow < thatRow ? -1 : 1);
-      } else {
-        return (thisColumn < thatColumn ? -1 : (thisColumn == thatColumn ? 0
-            : 1));
-      }
-    }
-  }
-
-  static { // register this comparator
-    WritableComparator.define(BlockID.class, new Comparator());
+  public byte[] getBytes() throws IOException {
+    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    ObjectOutputStream oos = new ObjectOutputStream(bos);
+    oos.writeObject(this);
+    oos.flush();
+    oos.close();
+    bos.close();
+    byte[] data = bos.toByteArray();
+    return data;
   }
-  */
 }



Mime
View raw message