Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FloatDenseMatrixSumAggregator |
|
| 2.0;2 |
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 float dense matrix aggregator is used to register and aggregate float | |
28 | * dense matrices. | |
29 | */ | |
30 | public class FloatDenseMatrixSumAggregator extends MatrixSumAggregator { | |
31 | /** Dense vector with a single entry */ | |
32 | 0 | private FloatDenseVector singletonVector = new FloatDenseVector(); |
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 FloatDenseMatrixSumAggregator(String name) { | |
41 | 0 | super(name); |
42 | 0 | } |
43 | ||
44 | /** | |
45 | * Register the float 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 | * @throws InstantiationException | |
50 | * @throws IllegalAccessException | |
51 | */ | |
52 | public void register(int numRows, MasterAggregatorUsage master) | |
53 | throws InstantiationException, IllegalAccessException { | |
54 | 0 | for (int i = 0; i < numRows; ++i) { |
55 | 0 | boolean success = master.registerAggregator(getRowAggregatorName(i), |
56 | FloatDenseVectorSumAggregator.class); | |
57 | 0 | if (!success) { |
58 | 0 | throw new RuntimeException("Aggregator already registered"); |
59 | } | |
60 | } | |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Add the given value to the entry specified. | |
65 | * | |
66 | * @param i the row | |
67 | * @param j the column | |
68 | * @param v the value | |
69 | * @param worker the worker to aggregate | |
70 | */ | |
71 | public void aggregate(int i, int j, float v, WorkerAggregatorUsage worker) { | |
72 | 0 | singletonVector.setSingleton(j, v); |
73 | 0 | worker.aggregate(getRowAggregatorName(i), singletonVector); |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * Set the values of the matrix to the master specified. This is typically | |
78 | * used in the master, to build an external FloatMatrix and only set it at | |
79 | * the end. | |
80 | * | |
81 | * @param matrix the matrix to set the values | |
82 | * @param master the master | |
83 | */ | |
84 | public void setMatrix(FloatDenseMatrix matrix, MasterAggregatorUsage master) { | |
85 | 0 | int numRows = matrix.getNumRows(); |
86 | 0 | for (int i = 0; i < numRows; ++i) { |
87 | 0 | master.setAggregatedValue(getRowAggregatorName(i), matrix.getRow(i)); |
88 | } | |
89 | 0 | } |
90 | ||
91 | /** | |
92 | * Read the aggregated values of the matrix. | |
93 | * | |
94 | * @param numRows the number of rows | |
95 | * @param aggUser the master or worker | |
96 | * @return the float matrix | |
97 | */ | |
98 | public FloatDenseMatrix getMatrix(int numRows, AggregatorUsage aggUser) { | |
99 | 0 | FloatDenseMatrix matrix = new FloatDenseMatrix(numRows, 1); |
100 | 0 | for (int i = 0; i < numRows; ++i) { |
101 | 0 | FloatDenseVector vec = aggUser.getAggregatedValue( |
102 | 0 | getRowAggregatorName(i)); |
103 | 0 | matrix.addRow(vec); |
104 | } | |
105 | 0 | return matrix; |
106 | } | |
107 | } |