This project has retired. For details please refer to its Attic page.
CollectShardedReducerHandle 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.block_app.reducers.collect;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.giraph.block_app.framework.api.CreateReducersApi;
24  import org.apache.giraph.block_app.framework.piece.global_comm.BroadcastHandle;
25  import org.apache.giraph.block_app.framework.piece.global_comm.array.BroadcastArrayHandle;
26  import org.apache.giraph.master.MasterGlobalCommUsage;
27  import org.apache.giraph.reducers.ReduceOperation;
28  import org.apache.giraph.worker.WorkerBroadcastUsage;
29  import org.apache.giraph.writable.kryo.KryoWritableWrapper;
30  
31  /**
32   * ShardedReducerHandle where we keep a list of reduced values
33   *
34   * @param <S> Single value type
35   */
36  public class CollectShardedReducerHandle<S>
37      extends ShardedReducerHandle<S, List<S>> {
38    public CollectShardedReducerHandle(CreateReducersApi reduceApi) {
39      register(reduceApi);
40    }
41  
42    @Override
43    public ReduceOperation<S, KryoWritableWrapper<List<S>>>
44    createReduceOperation() {
45      return new CollectReduceOperation<>();
46    }
47  
48    @Override
49    public List<S> createReduceResult(MasterGlobalCommUsage master) {
50      int size = 0;
51      for (int i = 0; i < REDUCER_COUNT; i++) {
52        size += reducers.get(i).getReducedValue(master).get().size();
53      }
54      return createList(size);
55    }
56  
57    public List<S> createList(int size) {
58      return new ArrayList<S>(size);
59    }
60  
61    @Override
62    public BroadcastHandle<List<S>> createBroadcastHandle(
63        BroadcastArrayHandle<KryoWritableWrapper<List<S>>> broadcasts) {
64      return new CollectShardedBroadcastHandle(broadcasts);
65    }
66  
67    /**
68     * BroadcastHandle for CollectShardedReducerHandle
69     */
70    public class CollectShardedBroadcastHandle extends ShardedBroadcastHandle {
71      public CollectShardedBroadcastHandle(
72          BroadcastArrayHandle<KryoWritableWrapper<List<S>>> broadcasts) {
73        super(broadcasts);
74      }
75  
76      @Override
77      public List<S> createBroadcastResult(WorkerBroadcastUsage worker) {
78        int size = 0;
79        for (int i = 0; i < REDUCER_COUNT; i++) {
80          size += broadcasts.get(i).getBroadcast(worker).get().size();
81        }
82        return createList(size);
83      }
84    }
85  }