This project has retired. For details please refer to its Attic page.
SumMessageCombiner xref
View Javadoc

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  package org.apache.giraph.combiner;
19  
20  import org.apache.giraph.types.ops.DoubleTypeOps;
21  import org.apache.giraph.types.ops.FloatTypeOps;
22  import org.apache.giraph.types.ops.IntTypeOps;
23  import org.apache.giraph.types.ops.LongTypeOps;
24  import org.apache.giraph.types.ops.NumericTypeOps;
25  import org.apache.hadoop.io.DoubleWritable;
26  import org.apache.hadoop.io.FloatWritable;
27  import org.apache.hadoop.io.IntWritable;
28  import org.apache.hadoop.io.LongWritable;
29  import org.apache.hadoop.io.Writable;
30  import org.apache.hadoop.io.WritableComparable;
31  
32  /**
33   * Message combiner which sums all messages.
34   *
35   * @param <M> Message type
36   */
37  public class SumMessageCombiner<M extends Writable>
38    implements MessageCombiner<WritableComparable, M> {
39    /** DoubleWritable specialization */
40    public static final SumMessageCombiner<DoubleWritable> DOUBLE =
41        new SumMessageCombiner<>(DoubleTypeOps.INSTANCE);
42    /** DoubleWritable specialization */
43    public static final SumMessageCombiner<FloatWritable> FLOAT =
44        new SumMessageCombiner<>(FloatTypeOps.INSTANCE);
45    /** LongWritable specialization */
46    public static final SumMessageCombiner<LongWritable> LONG =
47        new SumMessageCombiner<>(LongTypeOps.INSTANCE);
48    /** IntWritable specialization */
49    public static final SumMessageCombiner<IntWritable> INT =
50        new SumMessageCombiner<>(IntTypeOps.INSTANCE);
51  
52    /** Value type operations */
53    private final NumericTypeOps<M> typeOps;
54  
55    /**
56     * Constructor
57     * @param typeOps Value type operations
58     */
59    public SumMessageCombiner(NumericTypeOps<M> typeOps) {
60      this.typeOps = typeOps;
61    }
62  
63    @Override
64    public void combine(
65        WritableComparable vertexIndex, M originalMessage, M messageToCombine) {
66      typeOps.plusInto(originalMessage, messageToCombine);
67    }
68  
69    @Override
70    public M createInitialMessage() {
71      return typeOps.createZero();
72    }
73  }