Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LongSparseMatrixSumAggregator |
|
| 1.6;1.6 |
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.sparse; | |
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 long matrix aggregator is used to register and aggregate long matrices. | |
28 | */ | |
29 | public class LongSparseMatrixSumAggregator extends MatrixSumAggregator { | |
30 | /** sparse vector with single entry */ | |
31 | 0 | private LongSparseVector singletonVector = new LongSparseVector(); |
32 | ||
33 | /** | |
34 | * Create a new matrix aggregator with the given prefix name for the vector | |
35 | * aggregators. | |
36 | * | |
37 | * @param name the prefix for the row vector aggregators | |
38 | */ | |
39 | public LongSparseMatrixSumAggregator(String name) { | |
40 | 0 | super(name); |
41 | 0 | } |
42 | ||
43 | /** | |
44 | * Register the long vector aggregators, one for each row of the matrix. | |
45 | * | |
46 | * @param numRows the number of rows | |
47 | * @param master the master to register the aggregators | |
48 | */ | |
49 | public void register(int numRows, MasterAggregatorUsage master) | |
50 | throws InstantiationException, IllegalAccessException { | |
51 | 0 | for (int i = 0; i < numRows; ++i) { |
52 | 0 | master.registerAggregator(getRowAggregatorName(i), |
53 | LongSparseVectorSumAggregator.class); | |
54 | } | |
55 | 0 | } |
56 | ||
57 | /** | |
58 | * Add the given value to the entry specified. | |
59 | * | |
60 | * @param i the row | |
61 | * @param j the column | |
62 | * @param v the value | |
63 | * @param worker the worker to aggregate | |
64 | */ | |
65 | public void aggregate(int i, int j, long v, WorkerAggregatorUsage worker) { | |
66 | 0 | singletonVector.clear(); |
67 | 0 | singletonVector.set(j, v); |
68 | 0 | worker.aggregate(getRowAggregatorName(i), singletonVector); |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * Set the values of the matrix to the master specified. This is typically | |
73 | * used in the master, to build an external LongMatrix and only set it at | |
74 | * the end. | |
75 | * | |
76 | * @param matrix the matrix to set the values | |
77 | * @param master the master | |
78 | */ | |
79 | public void setMatrix(LongSparseMatrix matrix, | |
80 | MasterAggregatorUsage master) { | |
81 | 0 | int numRows = matrix.getNumRows(); |
82 | 0 | for (int i = 0; i < numRows; ++i) { |
83 | 0 | master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i)); |
84 | } | |
85 | 0 | } |
86 | ||
87 | /** | |
88 | * Read the aggregated values of the matrix. | |
89 | * | |
90 | * @param numRows the number of rows | |
91 | * @param aggUser the master or worker | |
92 | * @return the long matrix | |
93 | */ | |
94 | public LongSparseMatrix getMatrix(int numRows, AggregatorUsage aggUser) { | |
95 | 0 | LongSparseMatrix matrix = new LongSparseMatrix(numRows); |
96 | 0 | for (int i = 0; i < numRows; ++i) { |
97 | 0 | LongSparseVector vec = aggUser.getAggregatedValue( |
98 | 0 | getRowAggregatorName(i)); |
99 | 0 | matrix.setRow(i, vec); |
100 | } | |
101 | 0 | return matrix; |
102 | } | |
103 | } |