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/12/04 09:51:44 UTC

svn commit: r723246 - in /incubator/hama/trunk/src/examples/org/apache/hama/examples: AbstractExample.java ExampleDriver.java MatrixAddition.java MatrixMultiplication.java RandomMatrix.java

Author: edwardyoon
Date: Thu Dec  4 00:51:44 2008
New Revision: 723246

URL: http://svn.apache.org/viewvc?rev=723246&view=rev
Log:
Add RandomMatrix generation example

Added:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/AbstractExample.java
    incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java
Modified:
    incubator/hama/trunk/src/examples/org/apache/hama/examples/ExampleDriver.java
    incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java
    incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java

Added: incubator/hama/trunk/src/examples/org/apache/hama/examples/AbstractExample.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/AbstractExample.java?rev=723246&view=auto
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/AbstractExample.java (added)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/AbstractExample.java Thu Dec  4 00:51:44 2008
@@ -0,0 +1,52 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hama.HamaConfiguration;
+
+public abstract class AbstractExample {
+  public static final HamaConfiguration conf = new HamaConfiguration();
+  protected static List<String> ARGS;
+  
+  public static void parseArgs(String[] args) {
+    List<String> other_args = new ArrayList<String>();
+    for (int i = 0; i < args.length; ++i) {
+      try {
+        if ("-m".equals(args[i])) {
+          conf.setNumMapTasks(Integer.parseInt(args[++i]));
+        } else if ("-r".equals(args[i])) {
+          conf.setNumReduceTasks(Integer.parseInt(args[++i]));
+        } else {
+          other_args.add(args[i]);
+        }
+      } catch (NumberFormatException except) {
+        System.out.println("ERROR: Integer expected instead of " + args[i]);
+      } catch (ArrayIndexOutOfBoundsException except) {
+        System.out.println("ERROR: Required parameter missing from "
+            + args[i - 1]);
+      }
+    }
+
+    ARGS = other_args;
+  }
+}

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=723246&r1=723245&r2=723246&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 Thu Dec  4 00:51:44 2008
@@ -22,11 +22,13 @@
 import org.apache.hadoop.util.ProgramDriver;
 
 public class ExampleDriver {
+  
   public static void main(String[] args) {
     ProgramDriver pgd = new ProgramDriver();
     try {
-      pgd.addClass("add", MatrixAddition.class, "matrix-matrix addition.");
-      pgd.addClass("mult", MatrixMultiplication.class, "matrix-matrix multiplication.");
+      pgd.addClass("random", RandomMatrix.class, "Generate matrix with random elements.");
+      pgd.addClass("add", MatrixAddition.class, "Mat-Mat addition.");
+      pgd.addClass("mult", MatrixMultiplication.class, "Mat-Mat multiplication.");
       pgd.driver(args);
     } catch (Throwable e) {
       e.printStackTrace();

Modified: 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=723246&r1=723245&r2=723246&view=diff
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java (original)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java Thu Dec  4 00:51:44 2008
@@ -20,18 +20,11 @@
 package org.apache.hama.examples;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.hama.DenseMatrix;
-import org.apache.hama.HamaConfiguration;
 import org.apache.hama.Matrix;
 
-public class MatrixAddition {
-  private static HamaConfiguration conf = new HamaConfiguration();
-  private static int row;
-  private static int column;
-
+public class MatrixAddition extends AbstractExample {
   public static void main(String[] args) throws IOException {
     if (args.length < 2) {
       System.out.println("add  [-m maps] [-r reduces] <row_m> <column_n>");
@@ -40,36 +33,13 @@
       parseArgs(args);
     }
 
-    DenseMatrix a = DenseMatrix.random_mapred(conf, row, column);
-    DenseMatrix b = DenseMatrix.random_mapred(conf, row, column);
+    String matrixA = ARGS.get(0);
+    String matrixB = ARGS.get(1);
+    
+    DenseMatrix a = new DenseMatrix(conf, matrixA, false);
+    DenseMatrix b = new DenseMatrix(conf, matrixB, false);
 
     Matrix c = a.add(b);
-
-    a.close();
-    b.close();
     c.close();
   }
-
-  private static void parseArgs(String[] args) {
-    List<String> other_args = new ArrayList<String>();
-    for (int i = 0; i < args.length; ++i) {
-      try {
-        if ("-m".equals(args[i])) {
-          conf.setNumMapTasks(Integer.parseInt(args[++i]));
-        } else if ("-r".equals(args[i])) {
-          conf.setNumReduceTasks(Integer.parseInt(args[++i]));
-        } else {
-          other_args.add(args[i]);
-        }
-      } catch (NumberFormatException except) {
-        System.out.println("ERROR: Integer expected instead of " + args[i]);
-      } catch (ArrayIndexOutOfBoundsException except) {
-        System.out.println("ERROR: Required parameter missing from "
-            + args[i - 1]);
-      }
-    }
-
-    row = Integer.parseInt(other_args.get(0));
-    column = Integer.parseInt(other_args.get(1));
-  }
 }

Modified: incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java?rev=723246&r1=723245&r2=723246&view=diff
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java (original)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixMultiplication.java Thu Dec  4 00:51:44 2008
@@ -20,28 +20,24 @@
 package org.apache.hama.examples;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.hama.DenseMatrix;
-import org.apache.hama.HamaConfiguration;
 import org.apache.hama.Matrix;
 
-public class MatrixMultiplication {
-  private static HamaConfiguration conf = new HamaConfiguration();
-  private static int row;
-  private static int column;
-
+public class MatrixMultiplication extends AbstractExample {
   public static void main(String[] args) throws IOException {
     if (args.length < 2) {
-      System.out.println("mult  [-m maps] [-r reduces] <row_m> <column_n>");
+      System.out.println("mult  [-m maps] [-r reduces] <matrix_A> <matrix_B>");
       System.exit(-1);
     } else {
       parseArgs(args);
     }
 
-    DenseMatrix a = DenseMatrix.random_mapred(conf, row, column);
-    DenseMatrix b = DenseMatrix.random_mapred(conf, row, column);
+    String matrixA = ARGS.get(0);
+    String matrixB = ARGS.get(1);
+    
+    DenseMatrix a = new DenseMatrix(conf, matrixA, false);
+    DenseMatrix b = new DenseMatrix(conf, matrixB, false);
     
     if (!a.isBlocked())
       a.blocking_mapred(conf.getNumMapTasks());
@@ -49,32 +45,6 @@
       b.blocking_mapred(conf.getNumMapTasks());
 
     Matrix c = a.mult(b);
-
-    a.close();
-    b.close();
     c.close();
   }
-
-  private static void parseArgs(String[] args) {
-    List<String> other_args = new ArrayList<String>();
-    for (int i = 0; i < args.length; ++i) {
-      try {
-        if ("-m".equals(args[i])) {
-          conf.setNumMapTasks(Integer.parseInt(args[++i]));
-        } else if ("-r".equals(args[i])) {
-          conf.setNumReduceTasks(Integer.parseInt(args[++i]));
-        } else {
-          other_args.add(args[i]);
-        }
-      } catch (NumberFormatException except) {
-        System.out.println("ERROR: Integer expected instead of " + args[i]);
-      } catch (ArrayIndexOutOfBoundsException except) {
-        System.out.println("ERROR: Required parameter missing from "
-            + args[i - 1]);
-      }
-    }
-
-    row = Integer.parseInt(other_args.get(0));
-    column = Integer.parseInt(other_args.get(1));
-  }
 }

Added: 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=723246&view=auto
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java (added)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/RandomMatrix.java Thu Dec  4 00:51:44 2008
@@ -0,0 +1,43 @@
+/**
+ * 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 java.io.IOException;
+
+import org.apache.hama.DenseMatrix;
+
+public class RandomMatrix extends AbstractExample {
+
+  public static void main(String[] args) throws IOException {
+    if (args.length < 3) {
+      System.out
+          .println("add  [-m maps] [-r reduces] <rows> <columns> <matrix_name>");
+      System.exit(-1);
+    } else {
+      parseArgs(args);
+    }
+
+    int row = Integer.parseInt(ARGS.get(0));
+    int column = Integer.parseInt(ARGS.get(1));
+
+    DenseMatrix a = DenseMatrix.random_mapred(conf, row, column);
+    a.save(ARGS.get(2));
+  }
+}