incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r753231 - in /incubator/hama/trunk: ./ src/java/org/apache/hama/ src/java/org/apache/hama/algebra/ src/java/org/apache/hama/graph/ src/test/org/apache/hama/
Date Fri, 13 Mar 2009 13:13:17 GMT
Author: edwardyoon
Date: Fri Mar 13 13:13:16 2009
New Revision: 753231

URL: http://svn.apache.org/viewvc?rev=753231&view=rev
Log:
Vector.add(Vector v) throw the Index out of bounds exception when they are different

Added:
    incubator/hama/trunk/src/java/org/apache/hama/graph/package.html
Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
    incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java
    incubator/hama/trunk/src/java/org/apache/hama/Vector.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Fri Mar 13 13:13:16 2009
@@ -105,6 +105,8 @@
 
   BUG FIXES
 
+    HAMA-172: Vector.add(Vector v) throw the Index out of bounds 
+                exception when they are different (edwardyoon)
     HAMA-169: Vector's first location bug fixed,
               getNormInf() and getNorm2Robust() are implemented (edwardyoon)
     HAMA-155: When out of index, should throw exception (edwardyoon)

Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractVector.java Fri Mar 13 13:13:16 2009
@@ -1,86 +1,96 @@
-/**
- * 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.util.Iterator;
-import java.util.Map;
-
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.MapWritable;
-import org.apache.hadoop.io.Text;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.io.DoubleEntry;
-import org.apache.hama.util.BytesUtil;
-
-/**
- * Methods of the vector classes
- */
-public abstract class AbstractVector {
-  protected MapWritable entries;
-
-  public void initMap(RowResult row) {
-    this.entries = new MapWritable();
-    for (Map.Entry<byte[], Cell> f : row.entrySet()) {
-      this.entries.put(new IntWritable(BytesUtil.getColumnIndex(f.getKey())),
-          new DoubleEntry(f.getValue()));
-    }    
-  }
-  
-  /**
-   * Returns an Iterator.
-   * 
-   * @return iterator
-   */
-  public Iterator<Writable> iterator() {
-    return this.entries.values().iterator();
-  }
-
-  /**
-   * Returns a size of vector. If vector is sparse, returns the number of only
-   * non-zero elements.
-   * 
-   * @return a size of vector
-   */
-  public int size() {
-    int x = 0;
-    if (this.entries != null && this.entries.containsKey(new Text("row")))
-      x = 1;
-
-    return (this.entries != null) ? this.entries.size() - x : 0;
-  }
-
-  /**
-   * Returns the {@link org.apache.hadoop.io.MapWritable}
-   * 
-   * @return the entries of vector
-   */
-  public MapWritable getEntries() {
-    return this.entries;
-  }
-
-  /**
-   * Clears the entries.
-   */
-  public void clear() {
-    this.entries = 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.util.Iterator;
+import java.util.Map;
+
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.io.DoubleEntry;
+import org.apache.hama.util.BytesUtil;
+
+/**
+ * Methods of the vector classes
+ */
+public abstract class AbstractVector {
+  protected MapWritable entries;
+
+  public void initMap(RowResult row) {
+    this.entries = new MapWritable();
+    for (Map.Entry<byte[], Cell> f : row.entrySet()) {
+      this.entries.put(new IntWritable(BytesUtil.getColumnIndex(f.getKey())),
+          new DoubleEntry(f.getValue()));
+    }
+  }
+
+  /**
+   * Returns an Iterator.
+   * 
+   * @return iterator
+   */
+  public Iterator<Writable> iterator() {
+    return this.entries.values().iterator();
+  }
+
+  /**
+   * Returns a size of vector. If vector is sparse, returns the number of only
+   * non-zero elements.
+   * 
+   * @return a size of vector
+   */
+  public int size() {
+    int x = 0;
+    if (this.entries != null && this.entries.containsKey(new Text("row")))
+      x = 1;
+
+    return (this.entries != null) ? this.entries.size() - x : 0;
+  }
+
+  /**
+   * Returns the {@link org.apache.hadoop.io.MapWritable}
+   * 
+   * @return the entries of vector
+   */
+  public MapWritable getEntries() {
+    return this.entries;
+  }
+
+  /**
+   * Checks for conformant sizes
+   */
+  protected void checkComformantSize(Vector v2) {
+    if (this.size() != v2.size()) {
+      throw new IndexOutOfBoundsException("v1.size != v2.size (" + this.size()
+          + " != " + v2.size() + ")");
+    }
+  }
+
+  /**
+   * Clears the entries.
+   */
+  public void clear() {
+    this.entries = null;
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Fri Mar 13 13:13:16 2009
@@ -1,288 +1,305 @@
-/**
- * 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.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.MapWritable;
-import org.apache.hadoop.io.Text;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.io.DoubleEntry;
-import org.apache.log4j.Logger;
-
-/**
- * This class represents a dense vector.
- */
-public class DenseVector extends AbstractVector implements Vector {
-  static final Logger LOG = Logger.getLogger(DenseVector.class);
-
-  public DenseVector() {
-    this(new MapWritable());
-  }
-
-  public DenseVector(MapWritable m) {
-    this.entries = m;
-  }
-
-  public DenseVector(RowResult row) {
-    this.initMap(row);
-  }
-
-  public DenseVector(int row, MapWritable m) {
-    this.entries = m;
-    this.entries.put(new Text("row"), new IntWritable(row));
-  }
-
-  /**
-   * Sets the vector
-   * 
-   * @param v
-   * @return x = v
-   */
-  public DenseVector set(Vector v) {
-    return new DenseVector(v.getEntries());
-  }
-
-  /**
-   * Sets the value of index
-   * 
-   * @param index
-   * @param value
-   */
-  public void set(int index, double value) {
-    // If entries are null, create new object
-    if (this.entries == null) {
-      this.entries = new MapWritable();
-    }
-
-    this.entries.put(new IntWritable(index), new DoubleEntry(value));
-  }
-  
-  public void setRow(int row) {
-    this.entries.put(new Text("row"), new IntWritable(row));
-  }
-
-  /**
-   * Gets the value of index
-   * 
-   * @param index
-   * @return the value of v(index)
-   * @throws IOException
-   */
-  public double get(int index) {
-    double value;
-    try {
-      value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
-          .getValue();
-    } catch (NullPointerException e) {
-      throw new NullPointerException("Unexpected null value : " + e.toString());
-    }
-
-    return value;
-  }
-  
-  public int getRow() {
-    return ((IntWritable) this.entries.get(new Text("row"))).get();
-  }
-
-  /**
-   * Adds the value to v(index)
-   * 
-   * @param index
-   * @param value
-   */
-  public void add(int index, double value) {
-    set(index, get(index) + value);
-  }
-  
-  /**
-   * x = alpha*v + x
-   * 
-   * @param alpha
-   * @param v
-   * @return x = alpha*v + x
-   */
-  public DenseVector add(double alpha, Vector v) {
-    if (alpha == 0)
-      return this;
-
-    for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
-      int key = ((IntWritable) e.getKey()).get();
-      this.add(key, alpha * v.get(key));
-    }
-
-    return this;
-  }
-
-  /**
-   * x = v + x
-   * 
-   * @param v2
-   * @return x = v + x
-   */
-  public DenseVector add(Vector v2) {
-    if (this.size() == 0) {
-      DenseVector trunk = (DenseVector) v2;
-      this.entries = trunk.entries;
-      return this;
-    }
-
-    for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
-      int key = ((IntWritable) e.getKey()).get();
-      this.add(key, v2.get(key));
-    }
-
-    return this;
-  }
-
-  /**
-   * x dot v
-   * 
-   * @param v
-   * @return x dot v
-   */
-  public double dot(Vector v) {
-    double cosine = 0.0;
-    double q_i, d_i;
-    for (int i = 0; i < Math.min(this.size(), v.size()); i++) {
-      q_i = v.get(i);
-      d_i = this.get(i);
-      cosine += q_i * d_i;
-    }
-    return cosine / (this.getNorm2() * ((DenseVector) v).getNorm2());
-  }
-
-  /**
-   * v = alpha*v
-   * 
-   * @param alpha
-   * @return v = alpha*v
-   */
-  public DenseVector scale(double alpha) {
-    for (Map.Entry<Writable, Writable> e : this.entries.entrySet()) {
-      this.entries.put(e.getKey(), new DoubleEntry(((DoubleEntry) e.getValue())
-          .getValue()
-          * alpha));
-    }
-    return this;
-  }
-
-  /**
-   * Computes the given norm of the vector
-   * 
-   * @param type
-   * @return norm of the vector
-   */
-  public double norm(Norm type) {
-    if (type == Norm.One)
-      return getNorm1();
-    else if (type == Norm.Two)
-      return getNorm2();
-    else if (type == Norm.TwoRobust)
-      return getNorm2Robust();
-    else
-      return getNormInf();
-  }
-
-  protected double getNorm1() {
-    double sum = 0.0;
-
-    Set<Writable> keySet = this.entries.keySet();
-    Iterator<Writable> it = keySet.iterator();
-
-    while (it.hasNext()) {
-      sum += get(((IntWritable) it.next()).get());
-    }
-
-    return sum;
-  }
-
-  protected double getNorm2() {
-    double square_sum = 0.0;
-
-    Set<Writable> keySet = entries.keySet();
-    Iterator<Writable> it = keySet.iterator();
-
-    while (it.hasNext()) {
-      double value = get(((IntWritable) it.next()).get());
-      square_sum += value * value;
-    }
-
-    return Math.sqrt(square_sum);
-  }
-
-  /**
-   * Returns the robust norm of the vector
-   * 
-   * @return the robust norm of the vector
-   */
-  protected double getNorm2Robust() {
-    double scale = 0, ssq = 1;
-    for (int i = 0; i < this.size(); i++) {
-      double val = get(i);
-      if (val != 0) {
-        double absxi = Math.abs(val);
-        if (scale < absxi) {
-          ssq = 1 + ssq * Math.pow(scale / absxi, 2);
-          scale = absxi;
-        } else
-          ssq = ssq + Math.pow(absxi / scale, 2);
-      }
-    }
-    return scale * Math.sqrt(ssq);
-  }
-
-  /**
-   * Returns the infinity norm of the vector
-   * 
-   * @return the infinity norm of the vector
-   */
-  protected double getNormInf() {
-    double max = 0.0;
-    for (int i = 0; i < this.size(); i++) {
-      max = Math.max(max, Math.abs(get(i)));
-    }
-    return max;
-  }
-
-  /**
-   * Returns a sub-vector.
-   * 
-   * @param i0 the index of the first element
-   * @param i1 the index of the last element
-   * @return v[i0:i1]
-   */
-  public DenseVector subVector(int i0, int i1) {
-    DenseVector res = new DenseVector();
-    if (this.entries.containsKey(new Text("row")))
-      res.setRow(this.getRow());
-
-    for (int i = i0; i <= i1; i++) {
-      res.set(i, get(i));
-    }
-
-    return res;
-  }
-}
+/**
+ * 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.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.io.DoubleEntry;
+import org.apache.log4j.Logger;
+
+/**
+ * This class represents a dense vector.
+ */
+public class DenseVector extends AbstractVector implements Vector {
+  static final Logger LOG = Logger.getLogger(DenseVector.class);
+
+  public DenseVector() {
+    this(new MapWritable());
+  }
+
+  public DenseVector(MapWritable m) {
+    this.entries = m;
+  }
+
+  public DenseVector(RowResult row) {
+    this.initMap(row);
+  }
+
+  public DenseVector(int row, MapWritable m) {
+    this.entries = m;
+    this.entries.put(new Text("row"), new IntWritable(row));
+  }
+
+  /**
+   * Sets the value of index
+   * 
+   * @param index
+   * @param value
+   */
+  public void set(int index, double value) {
+    // If entries are null, create new object
+    if (this.entries == null) {
+      this.entries = new MapWritable();
+    }
+
+    this.entries.put(new IntWritable(index), new DoubleEntry(value));
+  }
+  
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public DenseVector set(Vector v) {
+    this.set(1, v);
+    return this;
+  }
+
+  public Vector set(double alpha, Vector v) {
+    checkComformantSize(v);
+    boolean zeroFill = false;
+    if(alpha == 0) 
+      zeroFill = true;
+    
+    for (Map.Entry<Writable, Writable> e : v.getEntries().entrySet()) {
+      int key = ((IntWritable) e.getKey()).get();
+      if(zeroFill)
+        this.set(key, 0);
+      else
+        this.set(key, alpha * ((DoubleEntry) e.getValue()).getValue());
+    }
+    
+    return this;
+  }
+  
+  public void setRow(int row) {
+    this.entries.put(new Text("row"), new IntWritable(row));
+  }
+
+  /**
+   * Gets the value of index
+   * 
+   * @param index
+   * @return the value of v(index)
+   * @throws IOException
+   */
+  public double get(int index) {
+    double value;
+    try {
+      value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
+          .getValue();
+    } catch (NullPointerException e) {
+      throw new NullPointerException("Unexpected null value : " + e.toString());
+    }
+
+    return value;
+  }
+  
+  public int getRow() {
+    return ((IntWritable) this.entries.get(new Text("row"))).get();
+  }
+
+  /**
+   * Adds the value to v(index)
+   * 
+   * @param index
+   * @param value
+   */
+  public void add(int index, double value) {
+    set(index, get(index) + value);
+  }
+  
+  /**
+   * x = alpha*v + x
+   * 
+   * @param alpha
+   * @param v
+   * @return x = alpha*v + x
+   */
+  public DenseVector add(double alpha, Vector v) {
+    checkComformantSize(v);
+    if (alpha == 0)
+      return this;
+
+    for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
+      int key = ((IntWritable) e.getKey()).get();
+      this.add(key, alpha * v.get(key));
+    }
+
+    return this;
+  }
+
+  /**
+   * x = v + x
+   * 
+   * @param v2
+   * @return x = v + x
+   */
+  public DenseVector add(Vector v2) {
+    checkComformantSize(v2);
+
+    for (Map.Entry<Writable, Writable> e : this.getEntries().entrySet()) {
+      int key = ((IntWritable) e.getKey()).get();
+      this.add(key, v2.get(key));
+    }
+
+    return this;
+  }
+
+  /**
+   * x dot v
+   * 
+   * @param v
+   * @return x dot v
+   */
+  public double dot(Vector v) {
+    checkComformantSize(v);
+    
+    double cosine = 0.0;
+    double q_i, d_i;
+    for (int i = 0; i < Math.min(this.size(), v.size()); i++) {
+      q_i = v.get(i);
+      d_i = this.get(i);
+      cosine += q_i * d_i;
+    }
+    return cosine / (this.getNorm2() * ((DenseVector) v).getNorm2());
+  }
+
+  /**
+   * v = alpha*v
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public DenseVector scale(double alpha) {
+    for (Map.Entry<Writable, Writable> e : this.entries.entrySet()) {
+      this.entries.put(e.getKey(), new DoubleEntry(((DoubleEntry) e.getValue())
+          .getValue()
+          * alpha));
+    }
+    return this;
+  }
+
+  /**
+   * Computes the given norm of the vector
+   * 
+   * @param type
+   * @return norm of the vector
+   */
+  public double norm(Norm type) {
+    if (type == Norm.One)
+      return getNorm1();
+    else if (type == Norm.Two)
+      return getNorm2();
+    else if (type == Norm.TwoRobust)
+      return getNorm2Robust();
+    else
+      return getNormInf();
+  }
+
+  protected double getNorm1() {
+    double sum = 0.0;
+
+    Set<Writable> keySet = this.entries.keySet();
+    Iterator<Writable> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      sum += get(((IntWritable) it.next()).get());
+    }
+
+    return sum;
+  }
+
+  protected double getNorm2() {
+    double square_sum = 0.0;
+
+    Set<Writable> keySet = entries.keySet();
+    Iterator<Writable> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      double value = get(((IntWritable) it.next()).get());
+      square_sum += value * value;
+    }
+
+    return Math.sqrt(square_sum);
+  }
+
+  /**
+   * Returns the robust norm of the vector
+   * 
+   * @return the robust norm of the vector
+   */
+  protected double getNorm2Robust() {
+    double scale = 0, ssq = 1;
+    for (int i = 0; i < this.size(); i++) {
+      double val = get(i);
+      if (val != 0) {
+        double absxi = Math.abs(val);
+        if (scale < absxi) {
+          ssq = 1 + ssq * Math.pow(scale / absxi, 2);
+          scale = absxi;
+        } else
+          ssq = ssq + Math.pow(absxi / scale, 2);
+      }
+    }
+    return scale * Math.sqrt(ssq);
+  }
+
+  /**
+   * Returns the infinity norm of the vector
+   * 
+   * @return the infinity norm of the vector
+   */
+  protected double getNormInf() {
+    double max = 0.0;
+    for (int i = 0; i < this.size(); i++) {
+      max = Math.max(max, Math.abs(get(i)));
+    }
+    return max;
+  }
+
+  /**
+   * Returns a sub-vector.
+   * 
+   * @param i0 the index of the first element
+   * @param i1 the index of the last element
+   * @return v[i0:i1]
+   */
+  public DenseVector subVector(int i0, int i1) {
+    DenseVector res = new DenseVector();
+    if (this.entries.containsKey(new Text("row")))
+      res.setRow(this.getRow());
+
+    for (int i = i0; i <= i1; i++) {
+      res.set(i, get(i));
+    }
+
+    return res;
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/SparseVector.java Fri Mar 13 13:13:16 2009
@@ -1,187 +1,193 @@
-/**
- * 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.io.RowResult;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.MapWritable;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.io.DoubleEntry;
-import org.apache.log4j.Logger;
-
-/**
- * This class represents a sparse vector.
- */
-public class SparseVector extends AbstractVector implements Vector {
-  static final Logger LOG = Logger.getLogger(SparseVector.class);
-
-  public SparseVector() {
-    this(new MapWritable());
-  }
-
-  public SparseVector(MapWritable m) {
-    this.entries = m;
-  }
-
-  public SparseVector(RowResult row) {
-    this.initMap(row);
-  }
-
-  @Override
-  public Vector add(double alpha, Vector v) {
-    if (alpha == 0)
-      return this;
-
-    for (Map.Entry<Writable, Writable> e : v.getEntries().entrySet()) {
-      if (this.entries.containsKey(e.getKey())) {
-        // add
-        double value = alpha * ((DoubleEntry) e.getValue()).getValue()
-            + this.get(((IntWritable) e.getKey()).get());
-        this.entries.put(e.getKey(), new DoubleEntry(value));
-      } else {
-        // put
-        double value = alpha * ((DoubleEntry) e.getValue()).getValue();
-        this.entries.put(e.getKey(), new DoubleEntry(value));
-      }
-    }
-
-    return this;
-  }
-
-  /**
-   * x = v + x
-   * 
-   * @param v2
-   * @return x = v + x
-   */
-  public SparseVector add(Vector v2) {
-    if (this.size() == 0) {
-      SparseVector trunk = (SparseVector) v2;
-      this.entries = trunk.entries;
-      return this;
-    }
-
-    for (Map.Entry<Writable, Writable> e : v2.getEntries().entrySet()) {
-      if (this.entries.containsKey(e.getKey())) {
-        // add
-        double value = ((DoubleEntry) e.getValue()).getValue()
-            + this.get(((IntWritable) e.getKey()).get());
-        this.entries.put(e.getKey(), new DoubleEntry(value));
-      } else {
-        // put
-        this.entries.put(e.getKey(), e.getValue());
-      }
-    }
-
-    return this;
-  }
-
-  @Override
-  public double dot(Vector v) {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-
-  @Override
-  public double norm(Norm type) {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-
-  /**
-   * v = alpha*v
-   * 
-   * @param alpha
-   * @return v = alpha*v
-   */
-  public SparseVector scale(double alpha) {
-    for (Map.Entry<Writable, Writable> e : this.entries.entrySet()) {
-      this.entries.put(e.getKey(), new DoubleEntry(((DoubleEntry) e.getValue())
-          .getValue()
-          * alpha));
-    }
-    return this;
-  }
-
-  /**
-   * Gets the value of index
-   * 
-   * @param index
-   * @return the value of v(index)
-   * @throws IOException
-   */
-  public double get(int index) {
-    double value;
-    try {
-      value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
-          .getValue();
-    } catch (NullPointerException e) { // returns zero if there is no value
-      return 0;
-    }
-
-    return value;
-  }
-
-  /**
-   * Sets the value of index
-   * 
-   * @param index
-   * @param value
-   */
-  public void set(int index, double value) {
-    // If entries are null, create new object
-    if (this.entries == null) {
-      this.entries = new MapWritable();
-    }
-
-    if (value != 0) // only stores non-zero element
-      this.entries.put(new IntWritable(index), new DoubleEntry(value));
-  }
-
-  /**
-   * Adds the value to v(index)
-   * 
-   * @param index
-   * @param value
-   */
-  public void add(int index, double value) {
-    set(index, get(index) + value);
-  }
-
-  /**
-   * Sets the vector
-   * 
-   * @param v
-   * @return x = v
-   */
-  public SparseVector set(Vector v) {
-    return new SparseVector(v.getEntries());
-  }
-
-  @Override
-  public Vector subVector(int i0, int i1) {
-    // 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.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.io.DoubleEntry;
+import org.apache.log4j.Logger;
+
+/**
+ * This class represents a sparse vector.
+ */
+public class SparseVector extends AbstractVector implements Vector {
+  static final Logger LOG = Logger.getLogger(SparseVector.class);
+
+  public SparseVector() {
+    this(new MapWritable());
+  }
+
+  public SparseVector(MapWritable m) {
+    this.entries = m;
+  }
+
+  public SparseVector(RowResult row) {
+    this.initMap(row);
+  }
+
+  @Override
+  public Vector add(double alpha, Vector v) {
+    if (alpha == 0)
+      return this;
+
+    for (Map.Entry<Writable, Writable> e : v.getEntries().entrySet()) {
+      if (this.entries.containsKey(e.getKey())) {
+        // add
+        double value = alpha * ((DoubleEntry) e.getValue()).getValue()
+            + this.get(((IntWritable) e.getKey()).get());
+        this.entries.put(e.getKey(), new DoubleEntry(value));
+      } else {
+        // put
+        double value = alpha * ((DoubleEntry) e.getValue()).getValue();
+        this.entries.put(e.getKey(), new DoubleEntry(value));
+      }
+    }
+
+    return this;
+  }
+
+  /**
+   * x = v + x
+   * 
+   * @param v2
+   * @return x = v + x
+   */
+  public SparseVector add(Vector v2) {
+    if (this.size() == 0) {
+      SparseVector trunk = (SparseVector) v2;
+      this.entries = trunk.entries;
+      return this;
+    }
+
+    for (Map.Entry<Writable, Writable> e : v2.getEntries().entrySet()) {
+      if (this.entries.containsKey(e.getKey())) {
+        // add
+        double value = ((DoubleEntry) e.getValue()).getValue()
+            + this.get(((IntWritable) e.getKey()).get());
+        this.entries.put(e.getKey(), new DoubleEntry(value));
+      } else {
+        // put
+        this.entries.put(e.getKey(), e.getValue());
+      }
+    }
+
+    return this;
+  }
+
+  @Override
+  public double dot(Vector v) {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+
+  @Override
+  public double norm(Norm type) {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+
+  /**
+   * v = alpha*v
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public SparseVector scale(double alpha) {
+    for (Map.Entry<Writable, Writable> e : this.entries.entrySet()) {
+      this.entries.put(e.getKey(), new DoubleEntry(((DoubleEntry) e.getValue())
+          .getValue()
+          * alpha));
+    }
+    return this;
+  }
+
+  /**
+   * Gets the value of index
+   * 
+   * @param index
+   * @return the value of v(index)
+   * @throws IOException
+   */
+  public double get(int index) {
+    double value;
+    try {
+      value = ((DoubleEntry) this.entries.get(new IntWritable(index)))
+          .getValue();
+    } catch (NullPointerException e) { // returns zero if there is no value
+      return 0;
+    }
+
+    return value;
+  }
+
+  /**
+   * Sets the value of index
+   * 
+   * @param index
+   * @param value
+   */
+  public void set(int index, double value) {
+    // If entries are null, create new object
+    if (this.entries == null) {
+      this.entries = new MapWritable();
+    }
+
+    if (value != 0) // only stores non-zero element
+      this.entries.put(new IntWritable(index), new DoubleEntry(value));
+  }
+
+  /**
+   * Adds the value to v(index)
+   * 
+   * @param index
+   * @param value
+   */
+  public void add(int index, double value) {
+    set(index, get(index) + value);
+  }
+
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public SparseVector set(Vector v) {
+    return new SparseVector(v.getEntries());
+  }
+
+  @Override
+  public Vector subVector(int i0, int i1) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public Vector set(double alpha, Vector v) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/Vector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Vector.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Fri Mar 13 13:13:16 2009
@@ -1,155 +1,164 @@
-/**
- * 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.util.Iterator;
-
-import org.apache.hadoop.io.MapWritable;
-import org.apache.hadoop.io.Writable;
-
-/**
- * Basic vector interface.
- */
-public interface Vector {
-
-  /**
-   * Size of the vector
-   * 
-   * @return size of the vector
-   */
-  public int size();
-
-  /**
-   * Gets the value of index
-   * 
-   * @param index
-   * @return v(index)
-   */
-  public double get(int index);
-
-  /**
-   * Sets the value of index
-   * 
-   * @param index
-   * @param value
-   */
-  public void set(int index, double value);
-
-  /**
-   * Sets the vector
-   * 
-   * @param v
-   * @return x = v
-   */
-  public Vector set(Vector v);
-
-  /**
-   * Adds the value to v(index)
-   * 
-   * @param index
-   * @param value
-   */
-  public void add(int index, double value);
-
-  /**
-   * x = alpha*v + x
-   * 
-   * @param alpha
-   * @param v
-   * @return x = alpha*v + x
-   */
-  public Vector add(double alpha, Vector v);
-
-  /**
-   * x = v + x
-   * 
-   * @param v
-   * @return x = v + x
-   */
-  public Vector add(Vector v);
-
-  /**
-   * x dot v
-   * 
-   * @param v
-   * @return x dot v
-   */
-  public double dot(Vector v);
-
-  /**
-   * v = alpha*v 
-   * 
-   * @param alpha
-   * @return v = alpha*v
-   */
-  public Vector scale(double alpha);
-  
-  /**
-   * Returns a sub-vector.
-   * 
-   * @param i0 the index of the first element
-   * @param i1 the index of the last element
-   * @return v[i0:i1]
-   */
-  public Vector subVector( int i0, int i1 ); 
-  
-  /**
-   * Computes the given norm of the vector
-   * 
-   * @param type
-   * @return norm of the vector
-   */
-  public double norm(Norm type);
-
-  /**
-   * Supported vector-norms.
-   */
-  enum Norm {
-
-    /** Sum of the absolute values of the entries */
-    One,
-
-    /** The root of sum of squares */
-    Two,
-
-    /**
-     * As the 2 norm may overflow, an overflow resistant version is also
-     * available. Note that it may be slower.
-     */
-    TwoRobust,
-
-    /** Largest entry in absolute value */
-    Infinity
-  }
-
-  /**
-   * Returns an iterator
-   * 
-   * @return iterator
-   */
-  public Iterator<Writable> iterator();
-  
-  /**
-   * Returns the {@link org.apache.hadoop.io.MapWritable}
-   * 
-   * @return the entries of vector
-   */
-  public MapWritable getEntries();
-}
+/**
+ * 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.util.Iterator;
+
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Writable;
+
+/**
+ * Basic vector interface.
+ */
+public interface Vector {
+
+  /**
+   * Size of the vector
+   * 
+   * @return size of the vector
+   */
+  public int size();
+
+  /**
+   * Gets the value of index
+   * 
+   * @param index
+   * @return v(index)
+   */
+  public double get(int index);
+
+  /**
+   * Sets the value of index
+   * 
+   * @param index
+   * @param value
+   */
+  public void set(int index, double value);
+
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public Vector set(Vector v);
+
+  /**
+   * x = alpha * v
+   * 
+   * @param alpha
+   * @param v
+   * @return x = alpha * v
+   */
+  public Vector set(double alpha, Vector v);
+  
+  /**
+   * Adds the value to v(index)
+   * 
+   * @param index
+   * @param value
+   */
+  public void add(int index, double value);
+
+  /**
+   * x = alpha*v + x
+   * 
+   * @param alpha
+   * @param v
+   * @return x = alpha*v + x
+   */
+  public Vector add(double alpha, Vector v);
+
+  /**
+   * x = v + x
+   * 
+   * @param v
+   * @return x = v + x
+   */
+  public Vector add(Vector v);
+
+  /**
+   * x dot v
+   * 
+   * @param v
+   * @return x dot v
+   */
+  public double dot(Vector v);
+
+  /**
+   * v = alpha*v 
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public Vector scale(double alpha);
+  
+  /**
+   * Returns a sub-vector.
+   * 
+   * @param i0 the index of the first element
+   * @param i1 the index of the last element
+   * @return v[i0:i1]
+   */
+  public Vector subVector( int i0, int i1 ); 
+  
+  /**
+   * Computes the given norm of the vector
+   * 
+   * @param type
+   * @return norm of the vector
+   */
+  public double norm(Norm type);
+
+  /**
+   * Supported vector-norms.
+   */
+  enum Norm {
+
+    /** Sum of the absolute values of the entries */
+    One,
+
+    /** The root of sum of squares */
+    Two,
+
+    /**
+     * As the 2 norm may overflow, an overflow resistant version is also
+     * available. Note that it may be slower.
+     */
+    TwoRobust,
+
+    /** Largest entry in absolute value */
+    Infinity
+  }
+
+  /**
+   * Returns an iterator
+   * 
+   * @return iterator
+   */
+  public Iterator<Writable> iterator();
+  
+  /**
+   * Returns the {@link org.apache.hadoop.io.MapWritable}
+   * 
+   * @return the entries of vector
+   */
+  public MapWritable getEntries();
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/SIMDMultiplyMap.java Fri Mar 13 13:13:16 2009
@@ -1,110 +1,120 @@
-/**
- * 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.algebra;
-
-import java.io.IOException;
-
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.MapWritable;
-import org.apache.hadoop.mapred.FileInputFormat;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.Mapper;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.Constants;
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.DenseVector;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-import org.apache.hama.SparseMatrix;
-import org.apache.hama.SparseVector;
-import org.apache.hama.Vector;
-import org.apache.hama.mapred.VectorInputFormat;
-import org.apache.log4j.Logger;
-
-/**
- * SIMD version
- */
-public class SIMDMultiplyMap extends MapReduceBase implements
-Mapper<IntWritable, MapWritable, IntWritable, MapWritable> {
-  static final Logger LOG = Logger.getLogger(SIMDMultiplyMap.class);
-  protected Matrix matrix_b;
-  protected Vector sum;
-  protected String type;
-  public static final String MATRIX_B = "hama.multiplication.matrix.b";
-  public static final String MATRIX_TYPE = "hama.multiplication.matrix.type";
-  
-  public void configure(JobConf job) {
-    try {
-      type = job.get(MATRIX_TYPE, "");
-      if(type.equals("SparseMatrix")) {
-        matrix_b = new SparseMatrix(new HamaConfiguration(job), job.get(MATRIX_B, ""));
-        sum = new SparseVector();
-      } else {
-        matrix_b = new DenseMatrix(new HamaConfiguration(job), job.get(MATRIX_B, ""));
-        sum = new DenseVector();
-      }
-    } catch (IOException e) {
-      LOG.warn("Load matrix_b failed : " + e.getMessage());
-    }
-  }
-
-  public static void initJob(String matrix_a, String matrix_b, String type, 
-      Class<SIMDMultiplyMap> map, Class<IntWritable> outputKeyClass,
-      Class<MapWritable> outputValueClass, JobConf jobConf) {
-
-    jobConf.setMapOutputValueClass(outputValueClass);
-    jobConf.setMapOutputKeyClass(outputKeyClass);
-    jobConf.setMapperClass(map);
-    jobConf.set(MATRIX_B, matrix_b);
-    jobConf.set(MATRIX_TYPE, type);
-
-    jobConf.setInputFormat(VectorInputFormat.class);
-    FileInputFormat.addInputPaths(jobConf, matrix_a);
-    jobConf.set(VectorInputFormat.COLUMN_LIST, Constants.COLUMN);
-  }
-
-  @Override
-  public void map(IntWritable key, MapWritable value,
-      OutputCollector<IntWritable, MapWritable> output, Reporter reporter)
-      throws IOException {
-    if(type.equals("SparseMatrix")) {
-      ((SparseVector) sum).clear();
-      SparseVector currVector = new SparseVector(value);
-      
-      for(int i = 0; i < matrix_b.getColumns(); i++) {
-        ((SparseVector) sum).add(((SparseMatrix) matrix_b).getRow(i).scale(
-            currVector.get(i)));
-      }
-      
-      output.collect(key, ((SparseVector) sum).getEntries());
-    } else {
-      ((DenseVector) sum).clear();
-      DenseVector currVector = new DenseVector(value);
-      for(int i = 0; i < value.size(); i++) {
-        ((DenseVector) sum).add(((DenseMatrix) matrix_b).getRow(i).scale(
-            currVector.get(i)));
-      }
-      output.collect(key, ((DenseVector) sum).getEntries());
-    }
-    
-  }
-}
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.MapWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.DenseVector;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.SparseMatrix;
+import org.apache.hama.SparseVector;
+import org.apache.hama.Vector;
+import org.apache.hama.mapred.VectorInputFormat;
+import org.apache.log4j.Logger;
+
+/**
+ * SIMD version
+ */
+public class SIMDMultiplyMap extends MapReduceBase implements
+Mapper<IntWritable, MapWritable, IntWritable, MapWritable> {
+  static final Logger LOG = Logger.getLogger(SIMDMultiplyMap.class);
+  protected Matrix matrix_b;
+  protected Vector sum;
+  protected String type;
+  public static final String MATRIX_B = "hama.multiplication.matrix.b";
+  public static final String MATRIX_TYPE = "hama.multiplication.matrix.type";
+  
+  public void configure(JobConf job) {
+    try {
+      type = job.get(MATRIX_TYPE, "");
+      if(type.equals("SparseMatrix")) {
+        matrix_b = new SparseMatrix(new HamaConfiguration(job), job.get(MATRIX_B, ""));
+        sum = new SparseVector();
+      } else {
+        matrix_b = new DenseMatrix(new HamaConfiguration(job), job.get(MATRIX_B, ""));
+        sum = new DenseVector();
+      }
+    } catch (IOException e) {
+      LOG.warn("Load matrix_b failed : " + e.getMessage());
+    }
+  }
+
+  public static void initJob(String matrix_a, String matrix_b, String type, 
+      Class<SIMDMultiplyMap> map, Class<IntWritable> outputKeyClass,
+      Class<MapWritable> outputValueClass, JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+    jobConf.set(MATRIX_B, matrix_b);
+    jobConf.set(MATRIX_TYPE, type);
+
+    jobConf.setInputFormat(VectorInputFormat.class);
+    FileInputFormat.addInputPaths(jobConf, matrix_a);
+    jobConf.set(VectorInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  @Override
+  public void map(IntWritable key, MapWritable value,
+      OutputCollector<IntWritable, MapWritable> output, Reporter reporter)
+      throws IOException {
+    if(type.equals("SparseMatrix")) {
+      ((SparseVector) sum).clear();
+      SparseVector currVector = new SparseVector(value);
+      
+      for(int i = 0; i < matrix_b.getColumns(); i++) {
+        SparseVector scaled = ((SparseMatrix) matrix_b).getRow(i).scale(currVector.get(i));
+        ((SparseVector) sum).add(scaled);
+      }
+      
+      output.collect(key, ((SparseVector) sum).getEntries());
+    } else {
+      ((DenseVector) sum).clear();
+      DenseVector currVector = new DenseVector(value);
+      for(int i = 0; i < value.size(); i++) {
+        DenseVector scaled = ((DenseMatrix) matrix_b).getRow(i).scale(currVector.get(i));
+        
+        // Should be initialized for adding vectors.
+        if(sum.size() == 0) {
+          for(Map.Entry<Writable, Writable> e: scaled.getEntries().entrySet()) {
+            sum.set(((IntWritable) e.getKey()).get(), 0);
+          }
+        }
+        
+        ((DenseVector) sum).add(scaled);
+      }
+      output.collect(key, ((DenseVector) sum).getEntries());
+    }
+    
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/graph/package.html
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/graph/package.html?rev=753231&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/graph/package.html (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/graph/package.html Fri Mar 13 13:13:16 2009
@@ -0,0 +1,23 @@
+<html>
+
+<!--
+   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.
+-->
+
+<body>
+A graph package
+</body>
+</html>

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java?rev=753231&r1=753230&r2=753231&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.java Fri Mar 13 13:13:16 2009
@@ -1,180 +1,226 @@
-/**
- * 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.Iterator;
-
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.io.DoubleEntry;
-
-public class TestDenseVector extends TestCase {
-  final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
-  
-  private static final double cosine = 0.6978227007909176;
-  private static final double norm1 = 12.0;
-  private static final double norm2 = 6.782329983125268;
-  private static final double normInf = 5.0;
-  private static final double norm2Robust = 6.782329983125269;
-  private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
-  private static DenseMatrix m1;
-  private static DenseVector v1;
-  private static DenseVector v2;
-
-  public static Test suite() {
-    TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) {
-      protected void setUp() throws Exception {
-        HCluster hCluster = new HCluster();
-        hCluster.setUp();
-
-        m1 = new DenseMatrix(hCluster.getConf());
-
-        for (int i = 0; i < 2; i++)
-          for (int j = 0; j < 4; j++)
-            m1.set(i, j, values[i][j]);
-
-        v1 = m1.getRow(0);
-        v2 = m1.getRow(1);
-      }
-
-      protected void tearDown() {
-        LOG.info("tearDown()");
-      }
-    };
-    return setup;
-  }
-
-  /**
-   * Test |a| dot |b|
-   */
-  public void testDot() {
-    double cos = v1.dot(v2);
-    assertEquals(cos, cosine);
-  }
-
-  public void testSubVector() {
-    int start = 2;
-    Vector subVector = v1.subVector(start, v1.size() - 1);
-    Iterator<Writable> it = subVector.iterator();
-
-    int i = start;
-    while (it.hasNext()) {
-      assertEquals(v1.get(i), ((DoubleEntry) it.next()).getValue());
-      i++;
-    }
-  }
-
-  /**
-   * Test norm one
-   */
-  public void testNom1() {
-    assertEquals(norm1, v1.norm(Vector.Norm.One));
-  }
-
-  /**
-   * Test norm two
-   */
-  public void testNom2() {
-    assertEquals(norm2, v1.norm(Vector.Norm.Two));
-  }
-
-  /**
-   * Test infinity norm
-   */
-  public void testNormInf() {
-    assertEquals(normInf, v1.norm(Vector.Norm.Infinity));
-  }
-  
-  /**
-   * Test infinity norm
-   */
-  public void testNorm2Robust() {
-    assertEquals(norm2Robust, v1.norm(Vector.Norm.TwoRobust));
-  }
-  
-  /**
-   * Test scaling
-   */
-  public void scalingTest() {
-    v2.scale(0.5);
-
-    for (int i = 0; i < v2.size(); i++) {
-      assertEquals(values[1][i] * 0.5, v2.get(i));
-    }
-  }
-
-  /**
-   * Test get/set methods
-   * @throws IOException 
-   */
-  public void testGetSet() throws IOException {
-    assertEquals(v1.get(0), values[0][0]);
-    boolean ex = false;
-    try {
-      v1.get(5);
-    } catch (NullPointerException e) {
-      ex = true;
-    }
-    assertTrue(ex);
-    assertEquals(m1.getColumn(0).size(), 2);
-  }
-
-  /**
-   * Test add()
-   */
-  public void testAdd() {
-    v1.add(v2);
-    int i = 0;
-    Iterator<Writable> it = v1.iterator();
-    while (it.hasNext()) {
-      DoubleEntry c = (DoubleEntry) it.next();
-      assertEquals(c.getValue(), values[0][i] + values[1][i]);
-      i++;
-    }
-
-    v1.add(0.5, v2);
-    int j = 0;
-    Iterator<Writable> itt = v1.iterator();
-    while (itt.hasNext()) {
-      DoubleEntry c = (DoubleEntry) itt.next();
-      assertEquals(c.getValue(), (values[0][j] + values[1][j]) + (0.5 * values[1][j]));
-      j++;
-    }
-    
-    double old = v1.get(0);
-    v1.add(0, norm1);
-    assertEquals(v1.get(0), old + norm1);
-  }
-  
-  /**
-   * Clear test
-   */
-  public void testClear() {
-    ((DenseVector) v1).clear();
-    assertEquals(v1.size(), 0);
-  }
-}
+/**
+ * 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.Iterator;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.io.DoubleEntry;
+
+public class TestDenseVector extends TestCase {
+  final static Log LOG = LogFactory.getLog(TestDenseVector.class.getName());
+  
+  private static final double cosine = 0.6978227007909176;
+  private static final double norm1 = 12.0;
+  private static final double norm2 = 6.782329983125268;
+  private static final double normInf = 5.0;
+  private static final double norm2Robust = 6.782329983125269;
+  private static double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
+  private static DenseMatrix m1;
+  private static DenseVector v1;
+  private static DenseVector v2;
+  private static DenseVector smallSize = new DenseVector();
+  
+  public static Test suite() {
+    TestSetup setup = new TestSetup(new TestSuite(TestDenseVector.class)) {
+      protected void setUp() throws Exception {
+        HCluster hCluster = new HCluster();
+        hCluster.setUp();
+
+        m1 = new DenseMatrix(hCluster.getConf());
+
+        for (int i = 0; i < 2; i++)
+          for (int j = 0; j < 4; j++)
+            m1.set(i, j, values[i][j]);
+
+        v1 = m1.getRow(0);
+        v2 = m1.getRow(1);
+        smallSize.set(0, 0.5);
+      }
+
+      protected void tearDown() {
+        LOG.info("tearDown()");
+      }
+    };
+    return setup;
+  }
+
+  /**
+   * Test |a| dot |b|
+   */
+  public void testDot() {
+    double cos = v1.dot(v2);
+    assertEquals(cos, cosine);
+    
+    boolean except = false;
+    try {
+      v1.dot(smallSize);
+    } catch (IndexOutOfBoundsException e) {
+      except = true;
+    }
+    
+    assertTrue(except);
+  }
+
+  public void testSubVector() {
+    int start = 2;
+    Vector subVector = v1.subVector(start, v1.size() - 1);
+    Iterator<Writable> it = subVector.iterator();
+
+    int i = start;
+    while (it.hasNext()) {
+      assertEquals(v1.get(i), ((DoubleEntry) it.next()).getValue());
+      i++;
+    }
+  }
+
+  /**
+   * Test norm one
+   */
+  public void testNom1() {
+    assertEquals(norm1, v1.norm(Vector.Norm.One));
+  }
+
+  /**
+   * Test norm two
+   */
+  public void testNom2() {
+    assertEquals(norm2, v1.norm(Vector.Norm.Two));
+  }
+
+  /**
+   * Test infinity norm
+   */
+  public void testNormInf() {
+    assertEquals(normInf, v1.norm(Vector.Norm.Infinity));
+  }
+  
+  /**
+   * Test infinity norm
+   */
+  public void testNorm2Robust() {
+    assertEquals(norm2Robust, v1.norm(Vector.Norm.TwoRobust));
+  }
+  
+  /**
+   * Test scaling
+   */
+  public void scalingTest() {
+    v2.scale(0.5);
+
+    for (int i = 0; i < v2.size(); i++) {
+      assertEquals(values[1][i] * 0.5, v2.get(i));
+    }
+  }
+
+  /**
+   * Test get/set methods
+   * @throws IOException 
+   */
+  public void testGetSet() throws IOException {
+    assertEquals(v1.get(0), values[0][0]);
+    boolean ex = false;
+    try {
+      v1.get(5);
+    } catch (NullPointerException e) {
+      ex = true;
+    }
+    assertTrue(ex);
+    assertEquals(m1.getColumn(0).size(), 2);
+  }
+
+  /**
+   * Test add()
+   */
+  public void testAdd() {
+    v1.add(v2);
+    int i = 0;
+    Iterator<Writable> it = v1.iterator();
+    while (it.hasNext()) {
+      DoubleEntry c = (DoubleEntry) it.next();
+      assertEquals(c.getValue(), values[0][i] + values[1][i]);
+      i++;
+    }
+
+    v1.add(0.5, v2);
+    int j = 0;
+    Iterator<Writable> itt = v1.iterator();
+    while (itt.hasNext()) {
+      DoubleEntry c = (DoubleEntry) itt.next();
+      assertEquals(c.getValue(), (values[0][j] + values[1][j]) + (0.5 * values[1][j]));
+      j++;
+    }
+    
+    double old = v1.get(0);
+    v1.add(0, norm1);
+    assertEquals(v1.get(0), old + norm1);
+    
+    boolean except = false;
+    try {
+      v1.add(smallSize);
+    } catch (IndexOutOfBoundsException e) {
+      except = true;
+    }
+    
+    assertTrue(except);
+    
+    except = false;
+    try {
+      v1.add(0.6, smallSize);
+    } catch (IndexOutOfBoundsException e) {
+      except = true;
+    }
+    
+    assertTrue(except);
+  }
+  
+  public void testSet() {
+    v1.set(v2);
+    
+    for(int i = 0; i < v1.size(); i ++) {
+      assertEquals(v2.get(i), v1.get(i));
+    }
+    
+    boolean except = false;
+    try {
+      v1.set(0.6, smallSize);
+    } catch (IndexOutOfBoundsException e) {
+      except = true;
+    }
+    
+    assertTrue(except);
+  }
+  
+  /**
+   * Clear test
+   */
+  public void testClear() {
+    ((DenseVector) v1).clear();
+    assertEquals(v1.size(), 0);
+  }
+}



Mime
View raw message