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 float matrix aggregator is used to register and aggregate float matrices.28 */29publicclassFloatSparseMatrixSumAggregatorextendsMatrixSumAggregator {
30/** sparse vector with single entry */31privateFloatSparseVector singletonVector = newFloatSparseVector();
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 */39publicFloatSparseMatrixSumAggregator(String name) {
40super(name);
41 }
4243/**44 * Register the float 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 FloatSparseVectorSumAggregator.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, float 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 FloatMatrix and only set it at74 * the end.75 *76 * @param matrix the matrix to set the values77 * @param master the master78 */79publicvoid setMatrix(FloatSparseMatrix matrix,
80MasterAggregatorUsage master) {
81int numRows = matrix.getNumRows();
82for (int i = 0; i < numRows; ++i) {
83 master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i));
84 }
85 }
8687/**88 * Read the aggregated values of the matrix.89 *90 * @param numRows the number of rows91 * @param aggUser the master or worker92 * @return the float matrix93 */94publicFloatSparseMatrix getMatrix(int numRows, AggregatorUsage aggUser) {
95FloatSparseMatrix matrix = newFloatSparseMatrix(numRows);
96for (int i = 0; i < numRows; ++i) {
97FloatSparseVector vec = aggUser.getAggregatedValue(
98 getRowAggregatorName(i));
99 matrix.setRow(i, vec);
100 }
101return matrix;
102 }
103 }