1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.aggregators.matrix.sparse;
2021import org.apache.giraph.aggregators.AggregatorUsage;
22import org.apache.giraph.aggregators.matrix.MatrixSumAggregator;
23import org.apache.giraph.master.MasterAggregatorUsage;
24import org.apache.giraph.worker.WorkerAggregatorUsage;
2526/**27 * The double matrix aggregator is used to register and aggregate double28 * matrices.29 */30publicclassDoubleSparseMatrixSumAggregatorextendsMatrixSumAggregator {
31/** sparse vector with single entry */32privateDoubleSparseVector singletonVector = newDoubleSparseVector();
3334/**35 * Create a new matrix aggregator with the given prefix name for the vector36 * aggregators.37 *38 * @param name the prefix for the row vector aggregators39 */40publicDoubleSparseMatrixSumAggregator(String name) {
41super(name);
42 }
4344/**45 * Register the double vector aggregators, one for each row of the matrix.46 *47 * @param numRows the number of rows48 * @param master the master to register the aggregators49 * @throws InstantiationException50 * @throws IllegalAccessException51 */52publicvoid register(int numRows, MasterAggregatorUsage master)
53throws InstantiationException, IllegalAccessException {
54for (int i = 0; i < numRows; ++i) {
55 master.registerAggregator(getRowAggregatorName(i),
56 DoubleSparseVectorSumAggregator.class);
57 }
58 }
5960/**61 * Add the given value to the entry specified.62 *63 * @param i the row64 * @param j the column65 * @param v the value66 * @param worker the worker to aggregate67 */68publicvoid aggregate(int i, int j, double v, WorkerAggregatorUsage worker) {
69 singletonVector.clear();
70 singletonVector.set(j, v);
71 worker.aggregate(getRowAggregatorName(i), singletonVector);
72 }
7374/**75 * Set the values of the matrix to the master specified. This is typically76 * used in the master, to build an external DoubleMatrix and only set it at77 * the end.78 *79 * @param matrix the matrix to set the values80 * @param master the master81 */82publicvoid setMatrix(DoubleSparseMatrix matrix,
83MasterAggregatorUsage master) {
84int numRows = matrix.getNumRows();
85for (int i = 0; i < numRows; ++i) {
86 master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i));
87 }
88 }
8990/**91 * Read the aggregated values of the matrix.92 *93 * @param numRows the number of rows94 * @param aggUser the master or worker95 * @return the double matrix96 */97publicDoubleSparseMatrix getMatrix(int numRows, AggregatorUsage aggUser) {
98DoubleSparseMatrix matrix = newDoubleSparseMatrix(numRows);
99for (int i = 0; i < numRows; ++i) {
100DoubleSparseVector vec = aggUser.getAggregatedValue(
101 getRowAggregatorName(i));
102 matrix.setRow(i, vec);
103 }
104return matrix;
105 }
106 }