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 int matrix aggregator is used to register and aggregate int matrices.28 */29publicclassIntSparseMatrixSumAggregatorextendsMatrixSumAggregator {
30/** sparse vector with single entry */31privateIntSparseVector singletonVector = newIntSparseVector();
3233/**34 * Create a new matrix aggregator with the given prefix name for the vector35 * aggregators.36 *37 * @param name the prefix for the row vector aggregators38 */39publicIntSparseMatrixSumAggregator(String name) {
40super(name);
41 }
4243/**44 * Register the int vector aggregators, one for each row of the matrix.45 *46 * @param numRows the number of rows47 * @param master the master to register the aggregators48 */49publicvoid register(int numRows, MasterAggregatorUsage master)
50throws InstantiationException, IllegalAccessException {
51for (int i = 0; i < numRows; ++i) {
52 master.registerAggregator(getRowAggregatorName(i),
53 IntSparseVectorSumAggregator.class);
54 }
55 }
5657/**58 * Add the given value to the entry specified.59 *60 * @param i the row61 * @param j the column62 * @param v the value63 * @param worker the worker to aggregate64 */65publicvoid aggregate(int i, int j, int v, WorkerAggregatorUsage worker) {
66 singletonVector.clear();
67 singletonVector.set(j, v);
68 worker.aggregate(getRowAggregatorName(i), singletonVector);
69 }
7071/**72 * Set the values of the matrix to the master specified. This is typically73 * used in the master, to build an external IntMatrix and only set it at74 * the end.75 *76 * @param matrix the matrix to set the values77 * @param master the master78 */79publicvoid setMatrix(IntSparseMatrix matrix, MasterAggregatorUsage master) {
80int numRows = matrix.getNumRows();
81for (int i = 0; i < numRows; ++i) {
82 master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i));
83 }
84 }
8586/**87 * Read the aggregated values of the matrix.88 *89 * @param numRows the number of rows90 * @param aggUser the master or worker91 * @return the int matrix92 */93publicIntSparseMatrix getMatrix(int numRows, AggregatorUsage aggUser) {
94IntSparseMatrix matrix = newIntSparseMatrix(numRows);
95for (int i = 0; i < numRows; ++i) {
96IntSparseVector vec = aggUser.getAggregatedValue(
97 getRowAggregatorName(i));
98 matrix.setRow(i, vec);
99 }
100return matrix;
101 }
102 }