Author: edwardyoon
Date: Sun Sep 14 03:41:04 2008
New Revision: 695186
URL: http://svn.apache.org/viewvc?rev=695186&view=rev
Log:
Add setRow(int row, Vector vector) method to matrix inteface
Modified:
incubator/hama/trunk/CHANGES.txt
incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
Modified: incubator/hama/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=695186&r1=695185&r2=695186&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Sun Sep 14 03:41:04 2008
@@ -22,6 +22,7 @@
IMPROVEMENTS
+ HAMA-56: Add setRow(int row, Vector vector) method to matrix inteface
(edwardyoon)
HAMA-39: IO operations should throws an IOException (edwardyoon)
HAMA-52: Fixture setup (edwardyoon)
HAMA-42: Replace ImmutableBytesWritable to IntWritable (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=695186&r1=695185&r2=695186&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Sun Sep 14
03:41:04 2008
@@ -1,205 +1,222 @@
-/**
- * Copyright 2007 The Apache Software Foundation
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hama;
-
-import java.io.IOException;
-
-import org.apache.hadoop.hbase.HColumnDescriptor;
-import org.apache.hadoop.hbase.HConstants;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.client.Scanner;
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hama.algebra.AdditionMap;
-import org.apache.hama.algebra.AdditionReduce;
-import org.apache.hama.algebra.MultiplicationMap;
-import org.apache.hama.algebra.MultiplicationReduce;
-import org.apache.hama.io.VectorEntry;
-import org.apache.hama.io.VectorMapWritable;
-import org.apache.hama.mapred.MatrixReduce;
-import org.apache.hama.util.Numeric;
-import org.apache.hama.util.RandomVariable;
-
-public class DenseMatrix extends AbstractMatrix implements Matrix {
-
- /**
- * Construct
- *
- * @param conf configuration object
- */
- public DenseMatrix(HamaConfiguration conf) {
- setConfiguration(conf);
- }
-
- /**
- * Construct an matrix
- *
- * @param conf configuration object
- * @param matrixName the name of the matrix
- */
- public DenseMatrix(HamaConfiguration conf, String matrixName) {
- try {
- setConfiguration(conf);
- this.matrixName = matrixName;
-
- if (!admin.tableExists(matrixName)) {
- tableDesc = new HTableDescriptor(matrixName);
- tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
- create();
- }
-
- table = new HTable(config, matrixName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Construct an m-by-n constant matrix.
- *
- * @param conf configuration object
- * @param m the number of rows.
- * @param n the number of columns.
- * @param s fill the matrix with this scalar value.
- */
- public DenseMatrix(HamaConfiguration conf, int m, int n, double s) {
- try {
- setConfiguration(conf);
- matrixName = RandomVariable.randMatrixName();
-
- if (!admin.tableExists(matrixName)) {
- tableDesc = new HTableDescriptor(matrixName);
- tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
- create();
- }
-
- table = new HTable(config, matrixName);
-
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- set(i, j, s);
- }
- }
-
- setDimension(m, n);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Generate matrix with random elements
- *
- * @param conf configuration object
- * @param m the number of rows.
- * @param n the number of columns.
- * @return an m-by-n matrix with uniformly distributed random elements.
- * @throws IOException
- */
- public static Matrix random(HamaConfiguration conf, int m, int n)
- throws IOException {
- String name = RandomVariable.randMatrixName();
- Matrix rand = new DenseMatrix(conf, name);
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- rand.set(i, j, RandomVariable.rand());
- }
- }
-
- rand.setDimension(m, n);
- LOG.info("Create the " + m + " * " + n + " random matrix : " + name);
- return rand;
- }
-
- public Matrix add(Matrix B) throws IOException {
- String output = RandomVariable.randMatrixName();
- Matrix result = new DenseMatrix(config, output);
-
- JobConf jobConf = new JobConf(config);
- jobConf.setJobName("addition MR job" + result.getName());
-
- AdditionMap.initJob(this.getName(), B.getName(), AdditionMap.class,
- IntWritable.class, DenseVector.class, jobConf);
- MatrixReduce.initJob(result.getName(), AdditionReduce.class, jobConf);
-
- execute(jobConf, result);
- return result;
- }
-
- public Matrix add(double alpha, Matrix B) throws IOException {
- // TODO Auto-generated method stub
- return null;
- }
-
- public DenseVector getRow(int row) throws IOException {
- return new DenseVector(row, table.getRow(String.valueOf(row)));
- }
-
- public Vector getColumn(int column) throws IOException {
- byte[] columnKey = Numeric.getColumnIndex(column);
- byte[][] c = { columnKey };
- Scanner scan = table.getScanner(c, HConstants.EMPTY_START_ROW);
-
- VectorMapWritable<Integer, VectorEntry> trunk = new
VectorMapWritable<Integer, VectorEntry>();
-
- for (RowResult row : scan) {
- trunk.put(Numeric.bytesToInt(row.getRow()), new VectorEntry(row
- .get(columnKey)));
- }
-
- return new DenseVector(column, trunk);
- }
-
- public Matrix mult(Matrix B) throws IOException {
- String output = RandomVariable.randMatrixName();
- Matrix result = new DenseMatrix(config, output);
-
- JobConf jobConf = new JobConf(config);
- jobConf.setJobName("multiplication MR job : " + result.getName());
-
- MultiplicationMap.initJob(this.getName(), B.getName(),
- MultiplicationMap.class, IntWritable.class, DenseVector.class,
jobConf);
- MatrixReduce.initJob(result.getName(), MultiplicationReduce.class,
jobConf);
- execute(jobConf, result);
- return result;
- }
-
- public Matrix multAdd(double alpha, Matrix B, Matrix C) throws IOException {
- // TODO Auto-generated method stub
- return null;
- }
-
- public double norm(Norm type) throws IOException {
- // TODO Auto-generated method stub
- return 0;
- }
-
- public Matrix set(double alpha, Matrix B) throws IOException {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Matrix set(Matrix B) throws IOException {
- // TODO Auto-generated method stub
- return null;
- }
-}
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Scanner;
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hama.algebra.AdditionMap;
+import org.apache.hama.algebra.AdditionReduce;
+import org.apache.hama.algebra.MultiplicationMap;
+import org.apache.hama.algebra.MultiplicationReduce;
+import org.apache.hama.io.VectorEntry;
+import org.apache.hama.io.VectorMapWritable;
+import org.apache.hama.mapred.MatrixReduce;
+import org.apache.hama.util.Numeric;
+import org.apache.hama.util.RandomVariable;
+
+public class DenseMatrix extends AbstractMatrix implements Matrix {
+
+ /**
+ * Construct
+ *
+ * @param conf configuration object
+ */
+ public DenseMatrix(HamaConfiguration conf) {
+ setConfiguration(conf);
+ }
+
+ /**
+ * Construct an matrix
+ *
+ * @param conf configuration object
+ * @param matrixName the name of the matrix
+ */
+ public DenseMatrix(HamaConfiguration conf, String matrixName) {
+ try {
+ setConfiguration(conf);
+ this.matrixName = matrixName;
+
+ if (!admin.tableExists(matrixName)) {
+ tableDesc = new HTableDescriptor(matrixName);
+ tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
+ create();
+ }
+
+ table = new HTable(config, matrixName);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Construct an m-by-n constant matrix.
+ *
+ * @param conf configuration object
+ * @param m the number of rows.
+ * @param n the number of columns.
+ * @param s fill the matrix with this scalar value.
+ */
+ public DenseMatrix(HamaConfiguration conf, int m, int n, double s) {
+ try {
+ setConfiguration(conf);
+ matrixName = RandomVariable.randMatrixName();
+
+ if (!admin.tableExists(matrixName)) {
+ tableDesc = new HTableDescriptor(matrixName);
+ tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
+ create();
+ }
+
+ table = new HTable(config, matrixName);
+
+ for (int i = 0; i < m; i++) {
+ for (int j = 0; j < n; j++) {
+ set(i, j, s);
+ }
+ }
+
+ setDimension(m, n);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Generate matrix with random elements
+ *
+ * @param conf configuration object
+ * @param m the number of rows.
+ * @param n the number of columns.
+ * @return an m-by-n matrix with uniformly distributed random elements.
+ * @throws IOException
+ */
+ public static Matrix random(HamaConfiguration conf, int m, int n)
+ throws IOException {
+ String name = RandomVariable.randMatrixName();
+ Matrix rand = new DenseMatrix(conf, name);
+ for (int i = 0; i < m; i++) {
+ for (int j = 0; j < n; j++) {
+ rand.set(i, j, RandomVariable.rand());
+ }
+ }
+
+ rand.setDimension(m, n);
+ LOG.info("Create the " + m + " * " + n + " random matrix : " + name);
+ return rand;
+ }
+
+ public Matrix add(Matrix B) throws IOException {
+ String output = RandomVariable.randMatrixName();
+ Matrix result = new DenseMatrix(config, output);
+
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("addition MR job" + result.getName());
+
+ AdditionMap.initJob(this.getName(), B.getName(), AdditionMap.class,
+ IntWritable.class, DenseVector.class, jobConf);
+ MatrixReduce.initJob(result.getName(), AdditionReduce.class, jobConf);
+
+ execute(jobConf, result);
+ return result;
+ }
+
+ public Matrix add(double alpha, Matrix B) throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public DenseVector getRow(int row) throws IOException {
+ return new DenseVector(row, table.getRow(String.valueOf(row)));
+ }
+
+ public Vector getColumn(int column) throws IOException {
+ byte[] columnKey = Numeric.getColumnIndex(column);
+ byte[][] c = { columnKey };
+ Scanner scan = table.getScanner(c, HConstants.EMPTY_START_ROW);
+
+ VectorMapWritable<Integer, VectorEntry> trunk = new
VectorMapWritable<Integer, VectorEntry>();
+
+ for (RowResult row : scan) {
+ trunk.put(Numeric.bytesToInt(row.getRow()), new VectorEntry(row
+ .get(columnKey)));
+ }
+
+ return new DenseVector(column, trunk);
+ }
+
+ public Matrix mult(Matrix B) throws IOException {
+ String output = RandomVariable.randMatrixName();
+ Matrix result = new DenseMatrix(config, output);
+
+ JobConf jobConf = new JobConf(config);
+ jobConf.setJobName("multiplication MR job : " + result.getName());
+
+ MultiplicationMap.initJob(this.getName(), B.getName(),
+ MultiplicationMap.class, IntWritable.class, DenseVector.class,
jobConf);
+ MatrixReduce.initJob(result.getName(), MultiplicationReduce.class,
jobConf);
+ execute(jobConf, result);
+ return result;
+ }
+
+ public Matrix multAdd(double alpha, Matrix B, Matrix C) throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public double norm(Norm type) throws IOException {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public Matrix set(double alpha, Matrix B) throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Matrix set(Matrix B) throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void setRow(int row, Vector vector) throws IOException {
+ BatchUpdate b = new BatchUpdate(Numeric.intToBytes(row));
+ for (Map.Entry<Integer, VectorEntry> e : ((DenseVector) vector)
+ .getEntries().entrySet()) {
+ b.put(Numeric.getColumnIndex(e.getKey()), Numeric.doubleToBytes(e
+ .getValue().getValue()));
+ }
+
+ table.commit(b);
+ }
+
+ public void setColumn(int column, Vector vector) throws IOException {
+ // TODO Auto-generated method stub
+ }
+}
Modified: incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Matrix.java?rev=695186&r1=695185&r2=695186&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Sun Sep 14
03:41:04 2008
@@ -100,6 +100,24 @@
public Matrix set(Matrix B) throws IOException;
/**
+ * Set the row of a matrix to a given vector
+ *
+ * @param row
+ * @param vector
+ * @throws IOException
+ */
+ public void setRow(int row, Vector vector) throws IOException;
+
+ /**
+ * Set the column of a matrix to a given vector
+ *
+ * @param column
+ * @param vector
+ * @throws IOException
+ */
+ public void setColumn(int column, Vector vector) throws IOException;
+
+ /**
* Sets the dimension of matrix
*
* @param rows the number of rows
Modified: incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java?rev=695186&r1=695185&r2=695186&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
(original)
+++ incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java Sun
Sep 14 03:41:04 2008
@@ -138,7 +138,6 @@
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("row=");
- sb.append(String.valueOf(this.row));
sb.append(", cells={");
boolean moreThanOne = false;
for (Map.Entry<Integer, VectorEntry> e : this.entries.entrySet()) {
Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
URL:
http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java?rev=695186&r1=695185&r2=695186&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
(original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java Sun Sep
14 03:41:04 2008
@@ -104,6 +104,25 @@
verifyMultResult(m1, m2, result);
}
+ public void testSetRow() throws IOException {
+ Vector v = new DenseVector();
+ double[] entries = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
+
+ for (int i = 0; i < SIZE; i++) {
+ v.set(i, entries[i]);
+ }
+
+ m1.setRow(SIZE + 1, v);
+ Iterator<VectorEntry> it = m1.getRow(SIZE + 1).iterator();
+
+ // We should remove the timestamp and row attribute from the vector
+ int i = 0;
+ while (it.hasNext()) {
+ assertEquals(entries[i], it.next().getValue());
+ i++;
+ }
+ }
+
/**
* Verifying multiplication result
*
@@ -126,8 +145,8 @@
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
- assertEquals(String.valueOf(result.get(i, j)).substring(0, 14),
- String.valueOf(C[i][j]).substring(0, 14));
+ assertEquals(String.valueOf(result.get(i, j)).substring(0, 14), String
+ .valueOf(C[i][j]).substring(0, 14));
}
}
}