You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by ed...@apache.org on 2008/08/11 10:33:22 UTC

svn commit: r684669 - in /incubator/hama/trunk/src: examples/org/apache/hama/examples/ java/org/apache/hama/ java/org/apache/hama/mapred/ test/org/apache/hama/

Author: edwardyoon
Date: Mon Aug 11 01:33:21 2008
New Revision: 684669

URL: http://svn.apache.org/viewvc?rev=684669&view=rev
Log:
Add HamaConfiguration class

Added:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java
    incubator/hama/trunk/src/java/org/apache/hama/HamaConfiguration.java
Removed:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java
Modified:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java
    incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
    incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java
    incubator/hama/trunk/src/test/org/apache/hama/HamaTestCase.java

Modified: incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java?rev=684669&r1=684668&r2=684669&view=diff
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java (original)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java Mon Aug 11 01:33:21 2008
@@ -25,8 +25,7 @@
   public static void main(String[] args) {
     ProgramDriver pgd = new ProgramDriver();
     try {
-      pgd.addClass("multiplication", MatrixMultiplication.class,
-          "A map/reduce multiplication.");
+      pgd.addClass("addition", MatrixAddition.class, "A matrix addition.");
       pgd.driver(args);
     } catch (Throwable e) {
       e.printStackTrace();

Added: incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java?rev=684669&view=auto
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java (added)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java Mon Aug 11 01:33:21 2008
@@ -0,0 +1,57 @@
+/**
+ * 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.examples;
+
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+
+public class MatrixAddition {
+
+  public static void main(String[] args) {
+    if (args.length < 2) {
+      System.out.println("addition <row_m> <column_n>");
+      System.exit(-1);
+    }
+
+    int row = Integer.parseInt(args[0]);
+    int column = Integer.parseInt(args[1]);
+
+    HamaConfiguration conf = new HamaConfiguration();
+
+    Matrix a = Matrix.random(conf, row, column);
+    System.out.println("Create the " + row + " * " + column
+        + " random matrix A.");
+    Matrix b = Matrix.random(conf, row, column);
+    System.out.println("Create the " + row + " * " + column
+        + " random matrix B.");
+
+    long start = System.currentTimeMillis();
+    Matrix c = a.add(b);
+    long end = System.currentTimeMillis();
+    System.out.println(executeTime(start, end));
+
+    System.out.println(c.get(0, 0));
+  }
+
+  public static String executeTime(long start, long end) {
+    return "(" + String.format("%.2f", Double.valueOf((end - start) * 0.001))
+        + " sec)";
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java?rev=684669&r1=684668&r2=684669&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java Mon Aug 11 01:33:21 2008
@@ -21,8 +21,6 @@
 
 import java.io.IOException;
 
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.HColumnDescriptor;
 import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.MasterNotRunningException;
@@ -41,8 +39,8 @@
     MatrixInterface {
   static final Logger LOG = Logger.getLogger(AbstractMatrix.class);
 
-  /** Hbase Configuration */
-  protected HBaseConfiguration config;
+  /** Hama Configuration */
+  protected HamaConfiguration config;
   /** Hbase admin object */
   protected HBaseAdmin admin;
   /** The name of Matrix */
@@ -61,8 +59,8 @@
    * 
    * @param conf configuration object
    */
-  public void setConfiguration(Configuration conf) {
-    config = (HBaseConfiguration) conf;
+  public void setConfiguration(HamaConfiguration conf) {
+    config = (HamaConfiguration) conf;
     try {
       admin = new HBaseAdmin(config);
     } catch (MasterNotRunningException e) {

Added: incubator/hama/trunk/src/java/org/apache/hama/HamaConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/HamaConfiguration.java?rev=684669&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/HamaConfiguration.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/HamaConfiguration.java Mon Aug 11 01:33:21 2008
@@ -0,0 +1,32 @@
+package org.apache.hama;
+
+import java.util.Map.Entry;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+
+/**
+ * Adds Hama configuration files to a Configuration
+ */
+public class HamaConfiguration extends HBaseConfiguration {
+  /** constructor */
+  public HamaConfiguration() {
+    super();
+    addHbaseResources();
+  }
+  
+  /**
+   * Create a clone of passed configuration.
+   * @param c Configuration to clone.
+   */
+  public HamaConfiguration(final Configuration c) {
+    this();
+    for (Entry<String, String>e: c) {
+      set(e.getKey(), e.getValue());
+    }
+  }
+  
+  private void addHbaseResources() {
+    addResource("hama-site.xml");
+  }
+}

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=684669&r1=684668&r2=684669&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Mon Aug 11 01:33:21 2008
@@ -21,12 +21,17 @@
 
 import java.io.IOException;
 
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.HColumnDescriptor;
 import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hama.algebra.AdditionMap;
+import org.apache.hama.algebra.AdditionReduce;
+import org.apache.hama.mapred.MatrixMap;
+import org.apache.hama.mapred.MatrixReduce;
 
 /**
  * A library for mathematical operations on matrices of double.
@@ -38,7 +43,7 @@
    * 
    * @param conf configuration object
    */
-  public Matrix(Configuration conf) {
+  public Matrix(HamaConfiguration conf) {
     setConfiguration(conf);
   }
 
@@ -48,7 +53,7 @@
    * @param conf configuration object
    * @param matrixName the name of the matrix
    */
-  public Matrix(Configuration conf, Text matrixName) {
+  public Matrix(HamaConfiguration conf, Text matrixName) {
     try {
       setConfiguration(conf);
       this.matrixName = matrixName;
@@ -74,7 +79,7 @@
    * @param n the number of columns.
    * @param s fill the matrix with this scalar value.
    */
-  public Matrix(HBaseConfiguration conf, int m, int n, double s) {
+  public Matrix(HamaConfiguration conf, int m, int n, double s) {
     try {
       setConfiguration(conf);
       matrixName = RandomVariable.randMatrixName();
@@ -107,7 +112,7 @@
    * @param n the number of columns.
    * @return an m-by-n matrix with uniformly distributed random elements.
    */
-  public static Matrix random(Configuration conf, int m, int n) {
+  public static Matrix random(HamaConfiguration conf, int m, int n) {
     Matrix rand = new Matrix(conf, RandomVariable.randMatrixName());
     for (int i = 0; i < m; i++) {
       for (int j = 0; j < n; j++) {
@@ -120,10 +125,27 @@
   }
 
   public Matrix add(Matrix B) {
-    
-    
-    // TODO Auto-generated method stub
-    return null;
+    Text output = RandomVariable.randMatrixName();
+    Matrix C = new Matrix(config, output);
+
+    JobConf jobConf = new JobConf(config);
+    jobConf.setJobName("addition MR job");
+
+    MatrixMap.initJob(this.getName(), B.getName(), AdditionMap.class,
+        ImmutableBytesWritable.class, Vector.class, jobConf);
+    MatrixReduce.initJob(C.getName(), AdditionReduce.class, jobConf);
+
+    jobConf.setNumMapTasks(1);
+    jobConf.setNumReduceTasks(1);
+
+    try {
+      JobClient.runJob(jobConf);
+    } catch (IOException e) {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
+
+    return C;
   }
 
   public Matrix add(double alpha, Matrix B) {

Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java?rev=684669&r1=684668&r2=684669&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java Mon Aug 11 01:33:21 2008
@@ -2,7 +2,6 @@
 
 import java.io.IOException;
 
-import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
@@ -14,6 +13,7 @@
 import org.apache.hadoop.mapred.OutputCollector;
 import org.apache.hadoop.mapred.Reporter;
 import org.apache.hama.Constants;
+import org.apache.hama.HamaConfiguration;
 import org.apache.hama.Matrix;
 import org.apache.hama.Vector;
 
@@ -34,7 +34,7 @@
     job.setMapperClass(mapper);
     FileInputFormat.addInputPaths(job, matrixA);
 
-    B = new Matrix(new HBaseConfiguration(), new Text(matrixB));
+    B = new Matrix(new HamaConfiguration(), new Text(matrixB));
     job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN);
   }
 

Modified: incubator/hama/trunk/src/test/org/apache/hama/HamaTestCase.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/HamaTestCase.java?rev=684669&r1=684668&r2=684669&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/HamaTestCase.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/HamaTestCase.java Mon Aug 11 01:33:21 2008
@@ -30,6 +30,7 @@
   protected Matrix matrixA;
   protected Matrix matrixB;
   protected int SIZE = 5;
+  protected HamaConfiguration conf = new HamaConfiguration();
   
   /** constructor */
   public HamaTestCase() {