This project has retired. For details please refer to its Attic page.
CollectShardedPrimitiveReducerHandle 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 org.apache.giraph.block_app.framework.api.CreateReducersApi;
21  import org.apache.giraph.block_app.framework.piece.global_comm.BroadcastHandle;
22  import org.apache.giraph.block_app.framework.piece.global_comm.array.BroadcastArrayHandle;
23  import org.apache.giraph.master.MasterGlobalCommUsage;
24  import org.apache.giraph.reducers.ReduceOperation;
25  import org.apache.giraph.types.ops.PrimitiveTypeOps;
26  import org.apache.giraph.types.ops.TypeOpsUtils;
27  import org.apache.giraph.types.ops.collections.array.WArrayList;
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   * when primitives are used
34   *
35   * @param <S> Single value type
36   */
37  public class CollectShardedPrimitiveReducerHandle<S>
38      extends ShardedReducerHandle<S, WArrayList<S>> {
39    /**
40     * Type ops if available, or null
41     */
42    private final PrimitiveTypeOps<S> typeOps;
43  
44    public CollectShardedPrimitiveReducerHandle(final CreateReducersApi reduceApi,
45        Class<S> valueClass) {
46      typeOps = TypeOpsUtils.getPrimitiveTypeOps(valueClass);
47      register(reduceApi);
48    }
49  
50    @Override
51    public ReduceOperation<S, KryoWritableWrapper<WArrayList<S>>>
52    createReduceOperation() {
53      return new CollectPrimitiveReduceOperation<>(typeOps);
54    }
55  
56    @Override
57    public WArrayList<S> createReduceResult(MasterGlobalCommUsage master) {
58      int size = 0;
59      for (int i = 0; i < REDUCER_COUNT; i++) {
60        size += reducers.get(i).getReducedValue(master).get().size();
61      }
62      return createList(size);
63    }
64  
65    public WArrayList<S> createList(int size) {
66      return typeOps.createArrayList(size);
67    }
68  
69    @Override
70    public BroadcastHandle<WArrayList<S>> createBroadcastHandle(
71        BroadcastArrayHandle<KryoWritableWrapper<WArrayList<S>>> broadcasts) {
72      return new CollectShardedPrimitiveBroadcastHandle(broadcasts);
73    }
74  
75    /**
76     * Broadcast handle for CollectShardedPrimitiveReducerHandle
77     */
78    public class CollectShardedPrimitiveBroadcastHandle
79        extends ShardedBroadcastHandle {
80      public CollectShardedPrimitiveBroadcastHandle(
81          BroadcastArrayHandle<KryoWritableWrapper<WArrayList<S>>>
82              broadcasts) {
83        super(broadcasts);
84      }
85  
86      @Override
87      public WArrayList<S> createBroadcastResult(
88          WorkerBroadcastUsage worker) {
89        int size = 0;
90        for (int i = 0; i < REDUCER_COUNT; i++) {
91          size += broadcasts.get(i).getBroadcast(worker).get().size();
92        }
93        return createList(size);
94      }
95    }
96  }