hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From millec...@apache.org
Subject svn commit: r1527466 [1/2] - in /hama/trunk: ./ c++/src/ c++/src/main/native/examples/ c++/src/main/native/examples/conf/ c++/src/main/native/examples/impl/ c++/src/main/native/examples/input/ c++/src/main/native/pipes/impl/ c++/src/main/native/utils/i...
Date Mon, 30 Sep 2013 07:26:16 GMT
Author: millecker
Date: Mon Sep 30 07:26:15 2013
New Revision: 1527466

URL: http://svn.apache.org/r1527466
Log:
HAMA-800: Hama Pipes Examples

Added:
    hama/trunk/c++/src/main/native/examples/
    hama/trunk/c++/src/main/native/examples/README.txt
    hama/trunk/c++/src/main/native/examples/conf/
    hama/trunk/c++/src/main/native/examples/conf/matrixmultiplication.xml
    hama/trunk/c++/src/main/native/examples/conf/piestimator.xml
    hama/trunk/c++/src/main/native/examples/conf/summation.xml
    hama/trunk/c++/src/main/native/examples/impl/
    hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.cc
    hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.hh
    hama/trunk/c++/src/main/native/examples/impl/matrixmultiplication.cc
    hama/trunk/c++/src/main/native/examples/impl/piestimator.cc
    hama/trunk/c++/src/main/native/examples/impl/summation.cc
    hama/trunk/c++/src/main/native/examples/input/
    hama/trunk/c++/src/main/native/examples/input/MatrixA.seq   (with props)
    hama/trunk/c++/src/main/native/examples/input/MatrixB_transposed.seq   (with props)
    hama/trunk/c++/src/main/native/utils/impl/Splitter.cc
Modified:
    hama/trunk/CHANGES.txt
    hama/trunk/c++/src/CMakeLists.txt
    hama/trunk/c++/src/main/native/pipes/impl/HamaPipes.cc
    hama/trunk/core/src/main/java/org/apache/hama/pipes/PipesApplication.java
    hama/trunk/core/src/main/java/org/apache/hama/pipes/protocol/StreamingProtocol.java
    hama/trunk/core/src/main/java/org/apache/hama/pipes/protocol/UplinkReader.java

Modified: hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hama/trunk/CHANGES.txt?rev=1527466&r1=1527465&r2=1527466&view=diff
==============================================================================
--- hama/trunk/CHANGES.txt (original)
+++ hama/trunk/CHANGES.txt Mon Sep 30 07:26:15 2013
@@ -4,6 +4,7 @@ Release 0.6.3 (unreleased changes)
 
   NEW FEATURES
   
+   HAMA-800: Hama Pipes Examples (Martin Illecker)
    HAMA-804: Create NeuralNetwork Example (Yexi Jiang)
    HAMA-795: Implement Autoencoder based on NeuralNetwork (Yexi Jiang)
    HAMA-767: Add vertex addition/removal APIs (Anastasis Andronidis via edwardyoon)

Modified: hama/trunk/c++/src/CMakeLists.txt
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/CMakeLists.txt?rev=1527466&r1=1527465&r2=1527466&view=diff
==============================================================================
--- hama/trunk/c++/src/CMakeLists.txt (original)
+++ hama/trunk/c++/src/CMakeLists.txt Mon Sep 30 07:26:15 2013
@@ -45,24 +45,25 @@ include_directories(
 )
 
 # Example programs
-# add_executable(wordcount-simple main/native/examples/impl/wordcount-simple.cc)
-# target_link_libraries(wordcount-simple hadooppipes hadooputils)
-# output_directory(wordcount-simple examples)
-
-# add_executable(wordcount-part main/native/examples/impl/wordcount-part.cc)
-# target_link_libraries(wordcount-part hadooppipes hadooputils)
-# output_directory(wordcount-part examples)
-
-# add_executable(wordcount-nopipe main/native/examples/impl/wordcount-nopipe.cc)
-# target_link_libraries(wordcount-nopipe hadooppipes hadooputils)
-# output_directory(wordcount-nopipe examples)
-
-# add_executable(pipes-sort main/native/examples/impl/sort.cc)
-# target_link_libraries(pipes-sort hadooppipes hadooputils)
-# output_directory(pipes-sort examples)
+add_executable(summation main/native/examples/impl/summation.cc)
+target_link_libraries(summation hamapipes hadooputils)
+output_directory(summation examples)
+
+add_executable(piestimator main/native/examples/impl/piestimator.cc)
+target_link_libraries(piestimator hamapipes hadooputils)
+output_directory(piestimator examples)
+
+add_executable(matrixmultiplication main/native/examples/impl/matrixmultiplication.cc)
+target_link_libraries(matrixmultiplication DenseDoubleVector hamapipes hadooputils)
+output_directory(matrixmultiplication examples)
+
+add_library(DenseDoubleVector 
+    main/native/examples/impl/DenseDoubleVector.cc
+)
 
 add_library(hadooputils STATIC
     main/native/utils/impl/StringUtils.cc
+    main/native/utils/impl/Splitter.cc
     main/native/utils/impl/SerialUtils.cc
 )
 

Added: hama/trunk/c++/src/main/native/examples/README.txt
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/README.txt?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/README.txt (added)
+++ hama/trunk/c++/src/main/native/examples/README.txt Mon Sep 30 07:26:15 2013
@@ -0,0 +1,113 @@
+####################################################################
+# Hama Pipes Examples                                              #
+####################################################################
+# - Summation                                                      #
+# - PiEstimator                                                    #
+# - MatrixMultiplication                                           #
+####################################################################
+
+To run the examples, first compile them:
+
+% mvn install 
+
+and then copy the binaries to dfs:
+
+% hadoop fs -put c++/target/native/examples/summation \
+ /examples/bin/summation
+
+create an input directory with text files:
+
+% hadoop fs -put my-data in-dir
+
+and run the word count example:
+
+% hama pipes \
+ -conf c++/src/main/native/examples/conf/summation.xml \
+ -input in-dir -output out-dir
+
+####################################################################
+
+# Summation Example
+
+% hadoop fs -put c++/target/native/examples/summation \
+ /examples/bin/summation
+
+% echo -e "key1\t1.0\nkey2\t2.0\nkey3\t3.0\nkey4\t4.0\n\
+key5\t5.0\nkey6\t6.0\nkey7\t7.0\nkey8\t8.0\nkey9\t9.0\n\
+key10\t10.0" > summation.txt && hadoop fs -put summation.txt \
+ /examples/input/summation/input.txt && rm summation.txt
+
+% hama pipes \
+ -conf c++/src/main/native/examples/conf/summation.xml \
+ -input /examples/input/summation \
+ -output /examples/output/summation
+
+% hadoop fs -cat /examples/input/summation/input.txt
+% hadoop fs -cat /examples/output/summation/part-00000
+
+% hadoop fs -rmr /examples/input/summation
+% hadoop fs -rmr /examples/output/summation
+
+####################################################################
+
+# PiEstimator Example
+
+% hadoop fs -put c++/target/native/examples/piestimator \
+ /examples/bin/piestimator
+
+% hama pipes \
+ -conf c++/src/main/native/examples/conf/piestimator.xml \
+ -output /examples/output/piestimator
+
+% hadoop fs -cat /examples/output/piestimator/part-00001
+
+% hadoop fs -rmr /examples/output/piestimator
+
+####################################################################
+
+# MatrixMultiplication Example
+
+% hadoop fs -put c++/target/native/examples/matrixmultiplication \
+ /examples/bin/matrixmultiplication
+
+% hadoop fs -put c++/src/main/native/examples/input/MatrixA.seq \
+ /examples/input/matrixmultiplication/MatrixA.seq
+
+% hadoop fs -put \
+ c++/src/main/native/examples/input/MatrixB_transposed.seq \
+ /examples/input/matrixmultiplication/MatrixB_transposed.seq
+
+% hama pipes \
+ -conf c++/src/main/native/examples/conf/matrixmultiplication.xml \
+ -output /examples/output/matrixmultiplication
+
+% hama seqdumper \
+ -seqFile /examples/input/matrixmultiplication/MatrixA.seq
+
+% hama seqdumper \
+ -seqFile /examples/input/matrixmultiplication/MatrixB_transposed.seq
+
+% hadoop fs -cat /examples/output/matrixmultiplication/part-00000
+
+# Matrix A
+#    9     4     1     9
+#    1     8     6     3
+#    8     3     3     9
+#    7     1     9     6
+
+# Matrix B (not transposed)
+#    2     1     6     5
+#    7     8     9     5
+#    2     1     5     8
+#    7     4     4     9
+
+# Resulting Matrix C
+#  111.0 78.0 131.0 154.0
+#   91.0 83.0 120.0 120.0
+#  106.0 71.0 126.0 160.0
+#   81.0 48.0 120.0 166.0
+
+% hadoop fs -rmr /examples/output/matrixmultiplication
+
+####################################################################
+

Added: hama/trunk/c++/src/main/native/examples/conf/matrixmultiplication.xml
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/conf/matrixmultiplication.xml?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/conf/matrixmultiplication.xml (added)
+++ hama/trunk/c++/src/main/native/examples/conf/matrixmultiplication.xml Mon Sep 30 07:26:15
2013
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<!--
+   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.
+-->
+
+<configuration>
+  <property>
+    <name>hama.pipes.executable</name>
+    <value>hdfs:/examples/bin/matrixmultiplication</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordreader</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordwriter</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>bsp.input.format.class</name>
+    <value>org.apache.hama.bsp.SequenceFileInputFormat</value>
+  </property>
+  <property>
+    <name>bsp.input.dir</name>
+    <value>/examples/input/matrixmultiplication/MatrixA.seq</value>         
                                  
+  </property>
+  <property>
+    <name>hama.mat.mult.B.path</name>
+    <value>/examples/input/matrixmultiplication/MatrixB_transposed.seq</value>
+  </property>
+  <property>
+    <name>hama.pipes.logging</name>
+    <value>false</value>
+  </property>
+  <property>
+    <name>hama.messenger.queue.class</name>
+    <value>org.apache.hama.bsp.message.queue.SortedMessageQueue</value>     
                                      
+  </property>
+  <property>
+    <name>bsp.input.partitioner.class</name>
+    <value>org.apache.hama.pipes.PipesPartitioner</value>                   
                        
+  </property>
+</configuration>

Added: hama/trunk/c++/src/main/native/examples/conf/piestimator.xml
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/conf/piestimator.xml?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/conf/piestimator.xml (added)
+++ hama/trunk/c++/src/main/native/examples/conf/piestimator.xml Mon Sep 30 07:26:15 2013
@@ -0,0 +1,48 @@
+<?xml version="1.0"?>
+<!--
+   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.
+-->
+
+<configuration>
+  <property>
+    <name>hama.pipes.executable</name>
+    <value>hdfs:/examples/bin/piestimator</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordreader</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordwriter</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>bsp.input.format.class</name>
+    <value>org.apache.hama.bsp.NullInputFormat</value>
+  </property>
+  <property>
+    <name>bsp.output.format.class</name>
+    <value>org.apache.hama.bsp.TextOutputFormat</value>
+  </property>
+  <property>
+    <name>hama.pipes.logging</name>
+    <value>false</value>
+  </property>
+  <property>
+    <name>bsp.peers.num</name>
+    <value>3</value>                                            
+  </property>
+</configuration>

Added: hama/trunk/c++/src/main/native/examples/conf/summation.xml
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/conf/summation.xml?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/conf/summation.xml (added)
+++ hama/trunk/c++/src/main/native/examples/conf/summation.xml Mon Sep 30 07:26:15 2013
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+<!--
+   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.
+-->
+
+<configuration>
+  <property>
+    <name>hama.pipes.executable</name>
+    <value>hdfs:/examples/bin/summation</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordreader</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>hama.pipes.java.recordwriter</name>
+    <value>true</value>
+  </property>
+  <property>
+    <name>bsp.input.format.class</name>
+    <value>org.apache.hama.bsp.KeyValueTextInputFormat</value>
+  </property>
+  <property>
+    <name>bsp.output.format.class</name>
+    <value>org.apache.hama.bsp.TextOutputFormat</value>
+  </property>
+  <property>
+    <name>hama.pipes.logging</name>
+    <value>false</value>
+  </property>
+</configuration>

Added: hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.cc
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/impl/DenseDoubleVector.cc?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.cc (added)
+++ hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.cc Mon Sep 30 07:26:15
2013
@@ -0,0 +1,248 @@
+/**
+ * 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.
+ */
+
+#include "hadoop/StringUtils.hh"
+#include "hadoop/Splitter.hh"
+#include "DenseDoubleVector.hh"
+
+#include <limits>
+#include <vector>
+#include <stdlib.h>
+#include <string>
+#include <iostream>
+
+using std::string;
+using std::cout;
+using HadoopUtils::Splitter;
+
+namespace math {
+  
+  DenseDoubleVector::DenseDoubleVector(int len) : size(len), vector(new double[len]) {
+  }
+  
+  DenseDoubleVector::DenseDoubleVector(int len, double val) : size(len), vector(new double[len])
{
+    for (int i=0; i<len; i++)
+      vector[i] = val;
+  }
+  
+  DenseDoubleVector::DenseDoubleVector(double arr[]) : vector(arr) {
+  }
+  
+  DenseDoubleVector::DenseDoubleVector(const string values) {
+    
+    Splitter split ( values, "," );
+    size = split.size();
+    
+    vector = new double[size];
+    for ( Splitter::size_type i = 0; i < split.size(); i++ )
+      vector[i] = HadoopUtils::toDouble(split[i]);
+  }
+  
+  DenseDoubleVector::~DenseDoubleVector() {
+    free(vector);
+  }
+  
+  int DenseDoubleVector::getLength() {
+    return size;
+  }
+  
+  int DenseDoubleVector::getDimension() {
+    return getLength();
+  }
+  
+  void DenseDoubleVector::set(int index, double value) {
+    vector[index] = value;
+  }
+  
+  double DenseDoubleVector::get(int index) {
+    return vector[index];
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::add(DenseDoubleVector *v) {
+    DenseDoubleVector *newv = new DenseDoubleVector(v->getLength());
+    for (int i = 0; i < v->getLength(); i++) {
+      newv->set(i, this->get(i) + v->get(i));
+    }
+    return newv;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::add(double scalar) {
+    DenseDoubleVector *newv = new DenseDoubleVector(this->getLength());
+    for (int i = 0; i < this->getLength(); i++) {
+      newv->set(i, this->get(i) + scalar);
+    }
+    return newv;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::subtract(DenseDoubleVector *v) {
+    DenseDoubleVector *newv = new DenseDoubleVector(v->getLength());
+    for (int i = 0; i < v->getLength(); i++) {
+      newv->set(i, this->get(i) - v->get(i));
+    }
+    return newv;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::subtract(double v) {
+    DenseDoubleVector *newv = new DenseDoubleVector(size);
+    for (int i = 0; i < size; i++) {
+      newv->set(i, vector[i] - v);
+    }
+    return newv;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::subtractFrom(double v) {
+    DenseDoubleVector *newv = new DenseDoubleVector(size);
+    for (int i = 0; i < size; i++) {
+      newv->set(i, v - vector[i]);
+    }
+    return newv;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::multiply(double scalar) {
+    DenseDoubleVector *v = new DenseDoubleVector(size);
+    for (int i = 0; i < size; i++) {
+      v->set(i, this->get(i) * scalar);
+    }
+    return v;
+  }
+  
+  DenseDoubleVector* DenseDoubleVector::divide(double scalar) {
+    DenseDoubleVector *v = new DenseDoubleVector(size);
+    for (int i = 0; i < size; i++) {
+      v->set(i, this->get(i) / scalar);
+    }
+    return v;
+  }
+  
+  /*
+   DenseDoubleVector* pow(int x) {
+   DenseDoubleVector *v = new DenseDoubleVector(size);
+   for (int i = 0; i < size; i++) {
+   double value = 0.0;
+   // it is faster to multiply when we having ^2
+   if (x == 2) {
+   value = vector[i] * vector[i];
+   }
+   else {
+   value = pow(vector[i], x);
+   }
+   v->set(i, value);
+   }
+   return v;
+   }
+   
+   DenseDoubleVector* sqrt() {
+   DenseDoubleVector *v = new DenseDoubleVector(size);
+   for (int i = 0; i < size; i++) {
+   v->set(i, sqrt(vector[i]));
+   }
+   return v;
+   }
+   */
+  
+  double DenseDoubleVector::sum() {
+    double sum = 0.0;
+    for (int i = 0; i < size; i++) {
+      sum +=  vector[i];
+    }
+    return sum;
+  }
+  
+  /*
+   DenseDoubleVector* abs() {
+   DenseDoubleVector *v = new DenseDoubleVector(size);
+   for (int i = 0; i < size; i++) {
+   v->set(i, abs(vector[i]));
+   }
+   return v;
+   }
+   */
+  
+  double DenseDoubleVector::dot(DenseDoubleVector *s) {
+    double dotProduct = 0.0;
+    for (int i = 0; i < size; i++) {
+      dotProduct += this->get(i) * s->get(i);
+    }
+    return dotProduct;
+  }
+  
+  double DenseDoubleVector::max() {
+    double max = std::numeric_limits<double>::min();
+    for (int i = 0; i < size; i++) {
+      double d = vector[i];
+      if (d > max) {
+        max = d;
+      }
+    }
+    return max;
+  }
+  
+  int DenseDoubleVector::maxIndex() {
+    double max = std::numeric_limits<double>::min();
+    int maxIndex = 0;
+    for (int i = 0; i < size; i++) {
+      double d = vector[i];
+      if (d > max) {
+        max = d;
+        maxIndex = i;
+      }
+    }
+    return maxIndex;
+  }
+  
+  double DenseDoubleVector::min() {
+    double min = std::numeric_limits<double>::max();
+    for (int i = 0; i < size; i++) {
+      double d = vector[i];
+      if (d < min) {
+        min = d;
+      }
+    }
+    return min;
+  }
+  
+  int DenseDoubleVector::minIndex() {
+    double min = std::numeric_limits<double>::max();
+    int minIndex = 0;
+    for (int i = 0; i < size; i++) {
+      double d = vector[i];
+      if (d < min) {
+        min = d;
+        minIndex = i;
+      }
+    }
+    return minIndex;
+  }
+  
+  double* DenseDoubleVector::toArray() {
+    return vector;
+  }
+  
+  string DenseDoubleVector::toString() {
+    string str;
+    string delimiter = ",";
+    for (int i = 0; i < size; i++)
+      if (i==0)
+        str += HadoopUtils::toString(vector[i]);
+      else
+        str += delimiter + HadoopUtils::toString(vector[i]);
+    
+    return str;
+  }
+}
+

Added: hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.hh
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/impl/DenseDoubleVector.hh?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.hh (added)
+++ hama/trunk/c++/src/main/native/examples/impl/DenseDoubleVector.hh Mon Sep 30 07:26:15
2013
@@ -0,0 +1,70 @@
+/**
+ * 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.
+ */
+
+#include <string>
+
+using std::string;
+
+namespace math {
+  
+  class DenseDoubleVector {
+  private:
+    double *vector;
+    int size;
+  public:
+    DenseDoubleVector(); // Default-Constructor
+    DenseDoubleVector(int length);
+    /// Creates a new vector with the given length and default value.
+    DenseDoubleVector(int length, double val);
+    // Creates a new vector with the given array.
+    DenseDoubleVector(double arr[]);
+    DenseDoubleVector(const string values);
+    ~DenseDoubleVector();  // Destructor
+    
+    int getLength();
+    int getDimension();
+    void set(int index, double value);
+    double get(int index);
+    
+    DenseDoubleVector *add(DenseDoubleVector *v);
+    DenseDoubleVector *add(double scalar);
+    DenseDoubleVector *subtract(DenseDoubleVector *v);
+    DenseDoubleVector *subtract(double v);
+    DenseDoubleVector *subtractFrom(double v);
+    
+    DenseDoubleVector *multiply(double scalar);
+    DenseDoubleVector *divide(double scalar);
+    /*
+     DenseDoubleVector *pow(int x);
+     DenseDoubleVector *sqrt();
+     */
+    double sum();
+    
+    //DenseDoubleVector *abs();
+    double dot(DenseDoubleVector *s);
+    
+    double max();
+    int maxIndex();
+    double min();
+    int minIndex();
+    
+    double *toArray();
+    virtual string toString();
+  };
+}
+

Added: hama/trunk/c++/src/main/native/examples/impl/matrixmultiplication.cc
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/impl/matrixmultiplication.cc?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/impl/matrixmultiplication.cc (added)
+++ hama/trunk/c++/src/main/native/examples/impl/matrixmultiplication.cc Mon Sep 30 07:26:15
2013
@@ -0,0 +1,142 @@
+/**
+ * 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.
+ */
+
+#include "hama/Pipes.hh"
+#include "hama/TemplateFactory.hh"
+#include "hadoop/StringUtils.hh"
+#include "DenseDoubleVector.hh"
+
+#include <time.h>
+#include <math.h>
+#include <string>
+#include <string>
+#include <iostream>
+#include <sstream>
+
+using std::string;
+using std::cout;
+
+using HamaPipes::BSP;
+using HamaPipes::BSPJob;
+using HamaPipes::Partitioner;
+using HamaPipes::BSPContext;
+using namespace HadoopUtils;
+
+using math::DenseDoubleVector;
+
+class MatrixMultiplicationBSP: public BSP {
+private:
+  string masterTask;
+  int seqFileID;
+  string HAMA_MAT_MULT_B_PATH;
+public:
+  MatrixMultiplicationBSP(BSPContext& context) {
+    seqFileID = 0;
+    HAMA_MAT_MULT_B_PATH = "hama.mat.mult.B.path";
+  }
+  
+  void setup(BSPContext& context) {
+    // Choose one as a master
+    masterTask = context.getPeerName(context.getNumPeers() / 2);
+    
+    reopenMatrixB(context);
+  }
+  
+  void bsp(BSPContext& context) {
+    
+    string aRowKey;
+    string aRowVectorStr;
+    // while for each row of matrix A
+    while(context.readNext(aRowKey, aRowVectorStr)) {
+      
+      DenseDoubleVector *aRowVector = new DenseDoubleVector(aRowVectorStr);
+      DenseDoubleVector *colValues = NULL;
+      
+      string bColKey;
+      string bColVectorStr;
+      
+      // while for each col of matrix B
+      while (context.sequenceFileReadNext(seqFileID,bColKey,bColVectorStr)) {
+        
+        DenseDoubleVector *bColVector = new DenseDoubleVector(bColVectorStr);
+        
+        if (colValues == NULL)
+          colValues = new DenseDoubleVector(bColVector->getDimension());
+        
+        double dot = aRowVector->dot(bColVector);
+        
+        colValues->set(toInt(bColKey), dot);
+      }
+      
+      // Submit one calculated row
+      std::stringstream message;
+      message << aRowKey << ":" << colValues->toString();
+      context.sendMessage(masterTask, message.str());
+      
+      reopenMatrixB(context);
+    }
+    
+    context.sequenceFileClose(seqFileID);
+    context.sync();
+  }
+  
+  void cleanup(BSPContext& context) {
+    if (context.getPeerName().compare(masterTask)==0) {
+      
+      int msgCount = context.getNumCurrentMessages();
+      
+      for (int i=0; i<msgCount; i++) {
+        
+        string received = context.getCurrentMessage();
+        //key:value1,value2,value3
+        int pos = (int)received.find(":");
+        string key = received.substr(0,pos);
+        string values = received.substr(pos+1,received.length());
+        
+        context.write(key, values);
+      }
+    }
+  }
+  
+  void reopenMatrixB(BSPContext& context) {
+    if (seqFileID!=0) {
+      context.sequenceFileClose(seqFileID);
+    }
+    
+    const BSPJob* job = context.getBSPJob();
+    string path = job->get(HAMA_MAT_MULT_B_PATH);
+    
+    seqFileID = context.sequenceFileOpen(path,"r",
+                                         "org.apache.hadoop.io.IntWritable",
+                                         "org.apache.hama.ml.writable.VectorWritable");
+  }
+  
+};
+
+class MatrixRowPartitioner: public Partitioner {
+public:
+  MatrixRowPartitioner(BSPContext& context) { }
+  
+  int partition(const string& key,const string& value, int32_t numTasks) {
+    return toInt(key) % numTasks;
+  }
+};
+
+int main(int argc, char *argv[]) {
+  return HamaPipes::runTask(HamaPipes::TemplateFactory<MatrixMultiplicationBSP,MatrixRowPartitioner>());
+}

Added: hama/trunk/c++/src/main/native/examples/impl/piestimator.cc
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/impl/piestimator.cc?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/impl/piestimator.cc (added)
+++ hama/trunk/c++/src/main/native/examples/impl/piestimator.cc Mon Sep 30 07:26:15 2013
@@ -0,0 +1,92 @@
+/**
+ * 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.
+ */
+
+#include "hama/Pipes.hh"
+#include "hama/TemplateFactory.hh"
+#include "hadoop/StringUtils.hh"
+
+#include <time.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string>
+#include <iostream>
+
+using std::string;
+using std::cout;
+
+using HamaPipes::BSP;
+using HamaPipes::BSPContext;
+using namespace HadoopUtils;
+
+class PiEstimatorBSP: public BSP {
+private:
+  string masterTask;
+  long iterations; // iterations_per_bsp_task
+public:
+  PiEstimatorBSP(BSPContext& context) {
+    iterations = 1000000L;
+  }
+  
+  inline double closed_interval_rand(double x0, double x1) {
+    return x0 + (x1 - x0) * rand() / ((double) RAND_MAX);
+  }
+  
+  void setup(BSPContext& context) {
+    // Choose one as a master
+    masterTask = context.getPeerName(context.getNumPeers() / 2);
+  }
+  
+  void bsp(BSPContext& context) {
+    
+    /* initialize random seed */
+    srand(time(NULL));
+    
+    int in = 0;
+    for (long i = 0; i < iterations; i++) {
+      double x = 2.0 * closed_interval_rand(0, 1) - 1.0;
+      double y = 2.0 * closed_interval_rand(0, 1) - 1.0;
+      if (sqrt(x * x + y * y) < 1.0) {
+        in++;
+      }
+    }
+    
+    context.sendMessage(masterTask, toString(in));
+    context.sync();
+  }
+  
+  void cleanup(BSPContext& context) {
+    if (context.getPeerName().compare(masterTask)==0) {
+      
+      long totalHits = 0;
+      int msgCount = context.getNumCurrentMessages();
+      string received;
+      for (int i=0; i<msgCount; i++) {
+        string received = context.getCurrentMessage();
+        totalHits += toInt(received);
+      }
+      
+      double pi = 4.0 * totalHits / (msgCount * iterations);
+      context.write("Estimated value of PI is", toString(pi));
+    }
+  }
+};
+
+int main(int argc, char *argv[]) {
+  return HamaPipes::runTask(HamaPipes::TemplateFactory<PiEstimatorBSP>());
+}
+

Added: hama/trunk/c++/src/main/native/examples/impl/summation.cc
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/impl/summation.cc?rev=1527466&view=auto
==============================================================================
--- hama/trunk/c++/src/main/native/examples/impl/summation.cc (added)
+++ hama/trunk/c++/src/main/native/examples/impl/summation.cc Mon Sep 30 07:26:15 2013
@@ -0,0 +1,74 @@
+/**
+ * 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.
+ */
+
+#include "hama/Pipes.hh"
+#include "hama/TemplateFactory.hh"
+#include "hadoop/StringUtils.hh"
+
+#include <stdlib.h>
+#include <string>
+#include <iostream>
+
+using std::string;
+
+using HamaPipes::BSP;
+using HamaPipes::BSPContext;
+
+class SummationBSP: public BSP {
+private:
+  string masterTask;
+public:
+  SummationBSP(BSPContext& context) {  }
+  
+  void setup(BSPContext& context) {
+    // Choose one as a master
+    masterTask = context.getPeerName(context.getNumPeers() / 2);
+  }
+  
+  void bsp(BSPContext& context) {
+    
+    double intermediateSum = 0.0;
+    string key;
+    string value;
+    
+    while(context.readNext(key,value)) {
+      intermediateSum += HadoopUtils::toDouble(value);
+    }
+    
+    context.sendMessage(masterTask, HadoopUtils::toString(intermediateSum));
+    context.sync();
+  }
+  
+  void cleanup(BSPContext& context) {
+    if (context.getPeerName().compare(masterTask)==0) {
+      
+      double sum = 0.0;
+      int msgCount = context.getNumCurrentMessages();
+      for (int i=0; i<msgCount; i++) {
+        string received = context.getCurrentMessage();
+        sum += HadoopUtils::toDouble(received);
+      }
+      context.write("Sum", HadoopUtils::toString(sum));
+    }
+  }
+};
+
+int main(int argc, char *argv[]) {
+  return HamaPipes::runTask(HamaPipes::TemplateFactory<SummationBSP>());
+}
+

Added: hama/trunk/c++/src/main/native/examples/input/MatrixA.seq
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/input/MatrixA.seq?rev=1527466&view=auto
==============================================================================
Binary file - no diff available.

Propchange: hama/trunk/c++/src/main/native/examples/input/MatrixA.seq
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: hama/trunk/c++/src/main/native/examples/input/MatrixB_transposed.seq
URL: http://svn.apache.org/viewvc/hama/trunk/c%2B%2B/src/main/native/examples/input/MatrixB_transposed.seq?rev=1527466&view=auto
==============================================================================
Binary file - no diff available.

Propchange: hama/trunk/c++/src/main/native/examples/input/MatrixB_transposed.seq
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message