Program: 31746
Project: 31746
Buggy source:
@Override public DTWorkerParams doCompute(WorkerContext<DTMasterParams,DTWorkerParams> context){
  if (context.isFirstIteration()) {
    return new DTWorkerParams();
  }
  DTMasterParams lastMasterResult=context.getLastMasterResult();
  final List<TreeNode> trees=lastMasterResult.getTrees();
  final Map<Integer,TreeNode> todoNodes=lastMasterResult.getTodoNodes();
  if (todoNodes == null) {
    return new DTWorkerParams();
  }
  Map<Integer,NodeStats> statistics=initTodoNodeStats(todoNodes);
  double trainError=0d, validationError=0d;
  double weightedTrainCount=0d, weightedValidationCount=0d;
  if (this.isGBDT && !this.gbdtSampleWithReplacement && lastMasterResult.isSwitchToNextTree()) {
    this.baggingRandomMap=new HashMap<Integer,Random>();
  }
  long start=System.nanoTime();
  for (  Data data : this.trainingData) {
    if (this.isRF) {
      for (      TreeNode treeNode : trees) {
        if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
          continue;
        }
        Node predictNode=predictNodeIndex(treeNode.getNode(),data,true);
        if (predictNode.getPredict() != null) {
          float weight=data.subsampleWeights[treeNode.getTreeId()];
          if (Float.compare(weight,0f) == 0) {
            validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedValidationCount+=data.significance;
          }
 else {
            trainError+=weight * data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedTrainCount+=weight * data.significance;
          }
        }
      }
    }
    if (this.isGBDT) {
      if (this.isContinuousEnabled && lastMasterResult.isContinuousRunningStart()) {
        recoverGBTData(context,data.output,data.predict,data,false);
        trainError+=data.significance * loss.computeError(data.predict,data.label);
        weightedTrainCount+=data.significance;
      }
 else {
        if (isNeedRecoverGBDTPredict) {
          if (this.recoverTrees == null) {
            this.recoverTrees=recoverCurrentTrees();
          }
          recoverGBTData(context,data.output,data.predict,data,true);
        }
        int currTreeIndex=trees.size() - 1;
        if (lastMasterResult.isSwitchToNextTree()) {
          if (currTreeIndex >= 1) {
            Node node=trees.get(currTreeIndex - 1).getNode();
            Node predictNode=predictNodeIndex(node,data,false);
            if (predictNode.getPredict() != null) {
              double predict=predictNode.getPredict().getPredict();
              if (context.getLastMasterResult().isFirstTree()) {
                data.predict=(float)predict;
              }
 else {
                boolean drop=(this.dropOutRate > 0.0 && rand.nextDouble() < this.dropOutRate);
                if (!drop) {
                  data.predict+=(float)(this.learningRate * predict);
                }
              }
              data.output=-1f * loss.computeGradient(data.predict,data.label);
            }
            if (!this.gbdtSampleWithReplacement) {
              Random random=null;
              int classValue=(int)(data.label + 0.01f);
              if (this.isStratifiedSampling) {
                random=baggingRandomMap.get(classValue);
                if (random == null) {
                  random=new Random();
                  baggingRandomMap.put(classValue,random);
                }
              }
 else {
                random=baggingRandomMap.get(0);
                if (random == null) {
                  random=new Random();
                  baggingRandomMap.put(0,random);
                }
              }
              if (random.nextDouble() <= modelConfig.getTrain().getBaggingSampleRate()) {
                data.subsampleWeights[currTreeIndex % data.subsampleWeights.length]=1f;
              }
 else {
                data.subsampleWeights[currTreeIndex % data.subsampleWeights.length]=0f;
              }
            }
          }
        }
        if (context.getLastMasterResult().isFirstTree() && !lastMasterResult.isSwitchToNextTree()) {
          Node currTree=trees.get(currTreeIndex).getNode();
          Node predictNode=predictNodeIndex(currTree,data,true);
          if (predictNode.getPredict() != null) {
            trainError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedTrainCount+=data.significance;
          }
        }
 else {
          trainError+=data.significance * loss.computeError(data.predict,data.label);
          weightedTrainCount+=data.significance;
        }
      }
    }
  }
  LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  if (validationData != null) {
    start=System.nanoTime();
    for (    Data data : this.validationData) {
      if (this.isRF) {
        for (        TreeNode treeNode : trees) {
          if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
            continue;
          }
          Node predictNode=predictNodeIndex(treeNode.getNode(),data,true);
          if (predictNode.getPredict() != null) {
            validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedValidationCount+=data.significance;
          }
        }
      }
      if (this.isGBDT) {
        if (this.isContinuousEnabled && lastMasterResult.isContinuousRunningStart()) {
          recoverGBTData(context,data.output,data.predict,data,false);
          validationError+=data.significance * loss.computeError(data.predict,data.label);
          weightedValidationCount+=data.significance;
        }
 else {
          if (isNeedRecoverGBDTPredict) {
            if (this.recoverTrees == null) {
              this.recoverTrees=recoverCurrentTrees();
            }
            recoverGBTData(context,data.output,data.predict,data,true);
          }
          int currTreeIndex=trees.size() - 1;
          if (lastMasterResult.isSwitchToNextTree()) {
            if (currTreeIndex >= 1) {
              Node node=trees.get(currTreeIndex - 1).getNode();
              Node predictNode=predictNodeIndex(node,data,false);
              if (predictNode.getPredict() != null) {
                double predict=predictNode.getPredict().getPredict();
                if (context.getLastMasterResult().isFirstTree()) {
                  data.predict=(float)predict;
                }
 else {
                  data.predict+=(float)(this.learningRate * predict);
                }
                data.output=-1f * loss.computeGradient(data.predict,data.label);
              }
            }
          }
          if (context.getLastMasterResult().isFirstTree() && !lastMasterResult.isSwitchToNextTree()) {
            Node predictNode=predictNodeIndex(trees.get(currTreeIndex).getNode(),data,true);
            if (predictNode.getPredict() != null) {
              validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
              weightedValidationCount+=data.significance;
            }
          }
 else {
            validationError+=data.significance * loss.computeError(data.predict,data.label);
            weightedValidationCount+=data.significance;
          }
        }
      }
    }
    LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  }
  if (this.isGBDT) {
    this.recoverTrees=null;
    if (this.isNeedRecoverGBDTPredict) {
      this.isNeedRecoverGBDTPredict=false;
    }
  }
  if (this.isGBDT && this.isNeedRecoverGBDTPredict) {
    this.isNeedRecoverGBDTPredict=false;
  }
  start=System.nanoTime();
  CompletionService<Map<Integer,NodeStats>> completionService=new ExecutorCompletionService<Map<Integer,NodeStats>>(this.threadPool);
  Set<Entry<Integer,TreeNode>> treeNodeEntrySet=todoNodes.entrySet();
  Iterator<Entry<Integer,TreeNode>> treeNodeIterator=treeNodeEntrySet.iterator();
  int roundNodeNumer=treeNodeEntrySet.size() / this.workerThreadCount;
  int modeNodeNumber=treeNodeEntrySet.size() % this.workerThreadCount;
  int realThreadCount=0;
  LOG.debug("String_Node_Str",todoNodes.size());
  for (int i=0; i < this.workerThreadCount; i++) {
    final Map<Integer,TreeNode> localTodoNodes=new HashMap<Integer,TreeNode>();
    final Map<Integer,NodeStats> localStatistics=new HashMap<Integer,DTWorkerParams.NodeStats>();
    for (int j=0; j < roundNodeNumer; j++) {
      Entry<Integer,TreeNode> tmpTreeNode=treeNodeIterator.next();
      localTodoNodes.put(tmpTreeNode.getKey(),tmpTreeNode.getValue());
      localStatistics.put(tmpTreeNode.getKey(),statistics.get(tmpTreeNode.getKey()));
    }
    if (modeNodeNumber > 0) {
      Entry<Integer,TreeNode> tmpTreeNode=treeNodeIterator.next();
      localTodoNodes.put(tmpTreeNode.getKey(),tmpTreeNode.getValue());
      localStatistics.put(tmpTreeNode.getKey(),statistics.get(tmpTreeNode.getKey()));
      modeNodeNumber-=1;
    }
    LOG.info("String_Node_Str",i,localTodoNodes.size(),localStatistics.size());
    if (localTodoNodes.size() == 0) {
      continue;
    }
    realThreadCount+=1;
    completionService.submit(new Callable<Map<Integer,NodeStats>>(){
      @Override public Map<Integer,NodeStats> call() throws Exception {
        List<Integer> nodeIndexes=new ArrayList<Integer>(trees.size());
        for (        Data data : DTWorker.this.trainingData) {
          nodeIndexes.clear();
          if (DTWorker.this.isRF) {
            for (            TreeNode treeNode : trees) {
              if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
                nodeIndexes.add(Node.INVALID_INDEX);
              }
 else {
                Node predictNode=predictNodeIndex(treeNode.getNode(),data,false);
                nodeIndexes.add(predictNode.getId());
              }
            }
          }
          if (DTWorker.this.isGBDT) {
            int currTreeIndex=trees.size() - 1;
            Node predictNode=predictNodeIndex(trees.get(currTreeIndex).getNode(),data,false);
            nodeIndexes.add(predictNode.getId());
          }
          for (          Map.Entry<Integer,TreeNode> entry : localTodoNodes.entrySet()) {
            Node todoNode=entry.getValue().getNode();
            int treeId=entry.getValue().getTreeId();
            int currPredictIndex=0;
            if (DTWorker.this.isRF) {
              currPredictIndex=nodeIndexes.get(entry.getValue().getTreeId());
            }
            if (DTWorker.this.isGBDT) {
              currPredictIndex=nodeIndexes.get(0);
            }
            if (todoNode.getId() == currPredictIndex) {
              List<Integer> features=entry.getValue().getFeatures();
              if (features.isEmpty()) {
                features=getAllValidFeatures();
              }
              for (              Integer columnNum : features) {
                double[] featuerStatistic=localStatistics.get(entry.getKey()).getFeatureStatistics().get(columnNum);
                float weight=data.subsampleWeights[treeId % data.subsampleWeights.length];
                if (Float.compare(weight,0f) != 0) {
                  short binIndex=data.inputs[DTWorker.this.inputIndexMap.get(columnNum)];
                  DTWorker.this.impurity.featureUpdate(featuerStatistic,binIndex,data.output,data.significance,weight);
                }
              }
            }
          }
        }
        return localStatistics;
      }
    }
);
  }
  int rCnt=0;
  while (rCnt < realThreadCount) {
    try {
      statistics.putAll(completionService.take().get());
    }
 catch (    ExecutionException e) {
      throw new RuntimeException(e);
    }
catch (    InterruptedException e) {
      Thread.currentThread().interrupt();
    }
    rCnt+=1;
  }
  LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  LOG.info("String_Node_Str",count,trainError,statistics.size(),weightedTrainCount,weightedValidationCount,trainError,validationError);
  return new DTWorkerParams(weightedTrainCount,weightedValidationCount,trainError,validationError,statistics);
}

Fixed version (ground truth):
@Override public DTWorkerParams doCompute(WorkerContext<DTMasterParams,DTWorkerParams> context){
  if (context.isFirstIteration()) {
    return new DTWorkerParams();
  }
  DTMasterParams lastMasterResult=context.getLastMasterResult();
  final List<TreeNode> trees=lastMasterResult.getTrees();
  final Map<Integer,TreeNode> todoNodes=lastMasterResult.getTodoNodes();
  if (todoNodes == null) {
    return new DTWorkerParams();
  }
  Map<Integer,NodeStats> statistics=initTodoNodeStats(todoNodes);
  double trainError=0d, validationError=0d;
  double weightedTrainCount=0d, weightedValidationCount=0d;
  if (this.isGBDT && !this.gbdtSampleWithReplacement && lastMasterResult.isSwitchToNextTree()) {
    this.baggingRandomMap=new HashMap<Integer,Random>();
  }
  long start=System.nanoTime();
  for (  Data data : this.trainingData) {
    if (this.isRF) {
      for (      TreeNode treeNode : trees) {
        if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
          continue;
        }
        Node predictNode=predictNodeIndex(treeNode.getNode(),data,true);
        if (predictNode.getPredict() != null) {
          float weight=data.subsampleWeights[treeNode.getTreeId()];
          if (Float.compare(weight,0f) == 0) {
            validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedValidationCount+=data.significance;
          }
 else {
            trainError+=weight * data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedTrainCount+=weight * data.significance;
          }
        }
      }
    }
    if (this.isGBDT) {
      if (this.isContinuousEnabled && lastMasterResult.isContinuousRunningStart()) {
        recoverGBTData(context,data.output,data.predict,data,false);
        trainError+=data.significance * loss.computeError(data.predict,data.label);
        weightedTrainCount+=data.significance;
      }
 else {
        if (isNeedRecoverGBDTPredict) {
          if (this.recoverTrees == null) {
            this.recoverTrees=recoverCurrentTrees();
          }
          recoverGBTData(context,data.output,data.predict,data,true);
        }
        int currTreeIndex=trees.size() - 1;
        if (lastMasterResult.isSwitchToNextTree()) {
          if (currTreeIndex >= 1) {
            Node node=trees.get(currTreeIndex - 1).getNode();
            Node predictNode=predictNodeIndex(node,data,false);
            if (predictNode.getPredict() != null) {
              double predict=predictNode.getPredict().getPredict();
              if (context.getLastMasterResult().isFirstTree()) {
                data.predict=(float)predict;
              }
 else {
                boolean drop=(this.dropOutRate > 0.0 && dropOutRandom.nextDouble() < this.dropOutRate);
                if (!drop) {
                  data.predict+=(float)(this.learningRate * predict);
                }
              }
              data.output=-1f * loss.computeGradient(data.predict,data.label);
            }
            if (!this.gbdtSampleWithReplacement) {
              Random random=null;
              int classValue=(int)(data.label + 0.01f);
              if (this.isStratifiedSampling) {
                random=baggingRandomMap.get(classValue);
                if (random == null) {
                  random=new Random();
                  baggingRandomMap.put(classValue,random);
                }
              }
 else {
                random=baggingRandomMap.get(0);
                if (random == null) {
                  random=new Random();
                  baggingRandomMap.put(0,random);
                }
              }
              if (random.nextDouble() <= modelConfig.getTrain().getBaggingSampleRate()) {
                data.subsampleWeights[currTreeIndex % data.subsampleWeights.length]=1f;
              }
 else {
                data.subsampleWeights[currTreeIndex % data.subsampleWeights.length]=0f;
              }
            }
          }
        }
        if (context.getLastMasterResult().isFirstTree() && !lastMasterResult.isSwitchToNextTree()) {
          Node currTree=trees.get(currTreeIndex).getNode();
          Node predictNode=predictNodeIndex(currTree,data,true);
          if (predictNode.getPredict() != null) {
            trainError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedTrainCount+=data.significance;
          }
        }
 else {
          trainError+=data.significance * loss.computeError(data.predict,data.label);
          weightedTrainCount+=data.significance;
        }
      }
    }
  }
  LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  if (validationData != null) {
    start=System.nanoTime();
    for (    Data data : this.validationData) {
      if (this.isRF) {
        for (        TreeNode treeNode : trees) {
          if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
            continue;
          }
          Node predictNode=predictNodeIndex(treeNode.getNode(),data,true);
          if (predictNode.getPredict() != null) {
            validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
            weightedValidationCount+=data.significance;
          }
        }
      }
      if (this.isGBDT) {
        if (this.isContinuousEnabled && lastMasterResult.isContinuousRunningStart()) {
          recoverGBTData(context,data.output,data.predict,data,false);
          validationError+=data.significance * loss.computeError(data.predict,data.label);
          weightedValidationCount+=data.significance;
        }
 else {
          if (isNeedRecoverGBDTPredict) {
            if (this.recoverTrees == null) {
              this.recoverTrees=recoverCurrentTrees();
            }
            recoverGBTData(context,data.output,data.predict,data,true);
          }
          int currTreeIndex=trees.size() - 1;
          if (lastMasterResult.isSwitchToNextTree()) {
            if (currTreeIndex >= 1) {
              Node node=trees.get(currTreeIndex - 1).getNode();
              Node predictNode=predictNodeIndex(node,data,false);
              if (predictNode.getPredict() != null) {
                double predict=predictNode.getPredict().getPredict();
                if (context.getLastMasterResult().isFirstTree()) {
                  data.predict=(float)predict;
                }
 else {
                  data.predict+=(float)(this.learningRate * predict);
                }
                data.output=-1f * loss.computeGradient(data.predict,data.label);
              }
            }
          }
          if (context.getLastMasterResult().isFirstTree() && !lastMasterResult.isSwitchToNextTree()) {
            Node predictNode=predictNodeIndex(trees.get(currTreeIndex).getNode(),data,true);
            if (predictNode.getPredict() != null) {
              validationError+=data.significance * loss.computeError((float)(predictNode.getPredict().getPredict()),data.label);
              weightedValidationCount+=data.significance;
            }
          }
 else {
            validationError+=data.significance * loss.computeError(data.predict,data.label);
            weightedValidationCount+=data.significance;
          }
        }
      }
    }
    LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  }
  if (this.isGBDT) {
    this.recoverTrees=null;
    if (this.isNeedRecoverGBDTPredict) {
      this.isNeedRecoverGBDTPredict=false;
    }
  }
  if (this.isGBDT && this.isNeedRecoverGBDTPredict) {
    this.isNeedRecoverGBDTPredict=false;
  }
  start=System.nanoTime();
  CompletionService<Map<Integer,NodeStats>> completionService=new ExecutorCompletionService<Map<Integer,NodeStats>>(this.threadPool);
  Set<Entry<Integer,TreeNode>> treeNodeEntrySet=todoNodes.entrySet();
  Iterator<Entry<Integer,TreeNode>> treeNodeIterator=treeNodeEntrySet.iterator();
  int roundNodeNumer=treeNodeEntrySet.size() / this.workerThreadCount;
  int modeNodeNumber=treeNodeEntrySet.size() % this.workerThreadCount;
  int realThreadCount=0;
  LOG.debug("String_Node_Str",todoNodes.size());
  for (int i=0; i < this.workerThreadCount; i++) {
    final Map<Integer,TreeNode> localTodoNodes=new HashMap<Integer,TreeNode>();
    final Map<Integer,NodeStats> localStatistics=new HashMap<Integer,DTWorkerParams.NodeStats>();
    for (int j=0; j < roundNodeNumer; j++) {
      Entry<Integer,TreeNode> tmpTreeNode=treeNodeIterator.next();
      localTodoNodes.put(tmpTreeNode.getKey(),tmpTreeNode.getValue());
      localStatistics.put(tmpTreeNode.getKey(),statistics.get(tmpTreeNode.getKey()));
    }
    if (modeNodeNumber > 0) {
      Entry<Integer,TreeNode> tmpTreeNode=treeNodeIterator.next();
      localTodoNodes.put(tmpTreeNode.getKey(),tmpTreeNode.getValue());
      localStatistics.put(tmpTreeNode.getKey(),statistics.get(tmpTreeNode.getKey()));
      modeNodeNumber-=1;
    }
    LOG.info("String_Node_Str",i,localTodoNodes.size(),localStatistics.size());
    if (localTodoNodes.size() == 0) {
      continue;
    }
    realThreadCount+=1;
    completionService.submit(new Callable<Map<Integer,NodeStats>>(){
      @Override public Map<Integer,NodeStats> call() throws Exception {
        List<Integer> nodeIndexes=new ArrayList<Integer>(trees.size());
        for (        Data data : DTWorker.this.trainingData) {
          nodeIndexes.clear();
          if (DTWorker.this.isRF) {
            for (            TreeNode treeNode : trees) {
              if (treeNode.getNode().getId() == Node.INVALID_INDEX) {
                nodeIndexes.add(Node.INVALID_INDEX);
              }
 else {
                Node predictNode=predictNodeIndex(treeNode.getNode(),data,false);
                nodeIndexes.add(predictNode.getId());
              }
            }
          }
          if (DTWorker.this.isGBDT) {
            int currTreeIndex=trees.size() - 1;
            Node predictNode=predictNodeIndex(trees.get(currTreeIndex).getNode(),data,false);
            nodeIndexes.add(predictNode.getId());
          }
          for (          Map.Entry<Integer,TreeNode> entry : localTodoNodes.entrySet()) {
            Node todoNode=entry.getValue().getNode();
            int treeId=entry.getValue().getTreeId();
            int currPredictIndex=0;
            if (DTWorker.this.isRF) {
              currPredictIndex=nodeIndexes.get(entry.getValue().getTreeId());
            }
            if (DTWorker.this.isGBDT) {
              currPredictIndex=nodeIndexes.get(0);
            }
            if (todoNode.getId() == currPredictIndex) {
              List<Integer> features=entry.getValue().getFeatures();
              if (features.isEmpty()) {
                features=getAllValidFeatures();
              }
              for (              Integer columnNum : features) {
                double[] featuerStatistic=localStatistics.get(entry.getKey()).getFeatureStatistics().get(columnNum);
                float weight=data.subsampleWeights[treeId % data.subsampleWeights.length];
                if (Float.compare(weight,0f) != 0) {
                  short binIndex=data.inputs[DTWorker.this.inputIndexMap.get(columnNum)];
                  DTWorker.this.impurity.featureUpdate(featuerStatistic,binIndex,data.output,data.significance,weight);
                }
              }
            }
          }
        }
        return localStatistics;
      }
    }
);
  }
  int rCnt=0;
  while (rCnt < realThreadCount) {
    try {
      statistics.putAll(completionService.take().get());
    }
 catch (    ExecutionException e) {
      throw new RuntimeException(e);
    }
catch (    InterruptedException e) {
      Thread.currentThread().interrupt();
    }
    rCnt+=1;
  }
  LOG.debug("String_Node_Str",TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
  LOG.info("String_Node_Str",count,trainError,statistics.size(),weightedTrainCount,weightedValidationCount,trainError,validationError);
  return new DTWorkerParams(weightedTrainCount,weightedValidationCount,trainError,validationError,statistics);
}