1/*2* Licensed to the Apache Software Foundation (ASF) under one3* or more contributor license agreements. See the NOTICE file4* distributed with this work for additional information5* regarding copyright ownership. The ASF licenses this file6* to you under the Apache License, Version 2.0 (the7* "License"); you may not use this file except in compliance8* with the License. You may obtain a copy of the License at9*10* http://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* 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 and16* limitations under the License.17*/18packageorg.apache.giraph.worker; 19 20importjava.util.ArrayList; 21importjava.util.List; 22 23importcom.google.common.base.Preconditions; 24 25importit.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; 26 27/**28* Information about all workers, their WorkerInfo values, and indices.29*/30publicclassAllWorkersInfo { 31/**List of workers in current superstep, sorted by task id */32privatefinalList<WorkerInfo> workerInfos; 33/**My worker index */34privatefinalintmyWorkerIndex; 35/**Map of taskId to worker index */36privatefinalInt2IntOpenHashMap taskIdToIndex; 37 38/**39* Constructor40* @param workers Ordered list of workers41* @param myWorker My worker42*/43publicAllWorkersInfo(List<WorkerInfo> workers, WorkerInfo myWorker) { 44 workerInfos =newArrayList<>(workers); 45 46 taskIdToIndex =newInt2IntOpenHashMap(workerInfos.size()); 47 taskIdToIndex.defaultReturnValue(-1); 48for(inti = 0; i < workerInfos.size(); i++) { 49inttask = workerInfos.get(i).getTaskId(); 50if(i > 0) { 51 Preconditions.checkState( 52 task > workerInfos.get(i - 1).getTaskId(), "Tasks not ordered"); 53 } 54 Preconditions.checkState(task >= 0, "Task not specified, %d", task); 55intold = taskIdToIndex.put(task, i); 56 Preconditions.checkState(old == -1, 57 "Task with %d id found twice (positions %d and %d)", 58 task, i, old); 59 } 60 61 myWorkerIndex = getWorkerIndex(myWorker); 62 } 63 64/**65* List of WorkerInfos66*67* @return List of WorkerInfos68*/69publicList<WorkerInfo> getWorkerList() { 70returnworkerInfos; 71 } 72 73/**74* Get number of workers75*76* @return Number of workers77*/78publicintgetWorkerCount() { 79returnworkerInfos.size(); 80 } 81 82/**83* Get index for this worker84*85* @return Index of this worker86*/87publicintgetMyWorkerIndex() { 88returnmyWorkerIndex; 89 } 90 91/**92* For every worker this method returns unique number93* between 0 and N, where N is the total number of workers.94* This number stays the same throughout the computation.95* TaskID may be different from this number and task ID96* is not necessarily continuous97* @param workerInfo worker info object98* @return worker number99*/100publicintgetWorkerIndex(WorkerInfo workerInfo) { 101returntaskIdToIndex.get(workerInfo.getTaskId()); 102 } 103 }