View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.aggregators.matrix.dense;
20  
21  import org.apache.giraph.aggregators.AggregatorUsage;
22  import org.apache.giraph.aggregators.matrix.MatrixSumAggregator;
23  import org.apache.giraph.master.MasterAggregatorUsage;
24  import org.apache.giraph.worker.WorkerAggregatorUsage;
25  
26  /**
27   * The double dense matrix aggregator is used to register and aggregate double
28   * dense matrices.
29   */
30  public class DoubleDenseMatrixSumAggregator extends MatrixSumAggregator {
31    /** Dense vector with a single entry */
32    private DoubleDenseVector singletonVector = new DoubleDenseVector();
33  
34    /**
35     * Create a new matrix aggregator with the given prefix name for the vector
36     * aggregators.
37     *
38     * @param name the prefix for the row vector aggregators
39     */
40    public DoubleDenseMatrixSumAggregator(String name) {
41      super(name);
42    }
43  
44    /**
45     * Register the double vector aggregators, one for each row of the matrix.
46     *
47     * @param numRows the number of rows
48     * @param master the master to register the aggregators
49     */
50    public void register(int numRows, MasterAggregatorUsage master)
51      throws InstantiationException, IllegalAccessException {
52      for (int i = 0; i < numRows; ++i) {
53        boolean success = master.registerAggregator(getRowAggregatorName(i),
54            DoubleDenseVectorSumAggregator.class);
55        if (!success) {
56          throw new RuntimeException("Aggregator already registered");
57        }
58      }
59    }
60  
61    /**
62     * Add the given value to the entry specified.
63     *
64     * @param i the row
65     * @param j the column
66     * @param v the value
67     * @param worker the worker to aggregate
68     */
69    public void aggregate(int i, int j, double v, WorkerAggregatorUsage worker) {
70      singletonVector.setSingleton(j, v);
71      worker.aggregate(getRowAggregatorName(i), singletonVector);
72    }
73  
74    /**
75     * Set the values of the matrix to the master specified. This is typically
76     * used in the master, to build an external DoubleMatrix and only set it at
77     * the end.
78     *
79     * @param matrix the matrix to set the values
80     * @param master the master
81     */
82    public void setMatrix(DoubleDenseMatrix matrix,
83        MasterAggregatorUsage master) {
84      int numRows = matrix.getNumRows();
85      for (int i = 0; i < numRows; ++i) {
86        master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i));
87      }
88    }
89  
90    /**
91     * Read the aggregated values of the matrix.
92     *
93     * @param numRows the number of rows
94     * @param aggUser the master or worker
95     * @return the double matrix
96     */
97    public DoubleDenseMatrix getMatrix(int numRows, AggregatorUsage aggUser) {
98      DoubleDenseMatrix matrix = new DoubleDenseMatrix(numRows, 1);
99      for (int i = 0; i < numRows; ++i) {
100       DoubleDenseVector vec = aggUser.getAggregatedValue(
101           getRowAggregatorName(i));
102       matrix.addRow(vec);
103     }
104     return matrix;
105   }
106 }