Program: 26224
Project: 26224
Buggy source:
/** 
 * Calculate the geometric error between the source-target and target-source deformations. The corresponding coefficients are assumed to be at swxTargetToSource, swyTargetToSource, swxSourceToTarget and swySourceToTarget. Multi-thread version.
 * @param intervals Input: Number of intervals for the deformation
 * @param grad Output: Gradient of the function
 * @return geometric error between the source-target and target-source deformations.
 */
private double evaluateConsistencyMultiThread(final int intervals,double[] grad){
  double f_direct=0.0;
  double f_inverse=0.0;
  final int nproc=Runtime.getRuntime().availableProcessors();
  int block_height_target=this.targetCurrentHeight / nproc;
  if (this.targetCurrentHeight % 2 != 0)   block_height_target++;
  int block_height_source=this.sourceCurrentHeight / nproc;
  if (this.sourceCurrentHeight % 2 != 0)   block_height_source++;
  final int nThreads=nproc;
  Thread[] threads=new Thread[nThreads];
  Rectangle[] rect_target=new Rectangle[nThreads];
  Rectangle[] rect_source=new Rectangle[nThreads];
  final double[][] grad_direct=new double[nThreads][grad.length];
  final double[][] grad_inverse=new double[nThreads][grad.length];
  final double[][] result=new double[nThreads][4];
  int n_direct=0;
  int n_inverse=0;
  for (int i=0; i < nThreads; i++) {
    int y_start_target=i * block_height_target;
    int y_start_source=i * block_height_source;
    if (nThreads - 1 == i) {
      block_height_target=this.targetCurrentHeight - i * block_height_target;
      block_height_source=this.sourceCurrentHeight - i * block_height_source;
    }
    rect_target[i]=new Rectangle(0,y_start_target,this.targetCurrentHeight,block_height_target);
    rect_source[i]=new Rectangle(0,y_start_source,this.sourceCurrentHeight,block_height_source);
    threads[i]=new Thread(new EvaluateConsistencyTile(this,grad_direct[i],grad_inverse[i],result[i],rect_target[i],rect_source[i]));
    threads[i].start();
  }
  for (int i=0; i < nThreads; i++) {
    try {
      threads[i].join();
      threads[i]=null;
    }
 catch (    InterruptedException e) {
      e.printStackTrace();
    }
  }
  for (int i=0; i < nThreads; i++) {
    f_direct+=result[i][0];
    n_direct+=result[i][1];
    f_inverse+=result[i][2];
    n_inverse+=result[i][3];
  }
  f_direct/=n_direct;
  f_inverse/=n_inverse;
  for (int i=0; i < nThreads; i++) {
    for (int j=0; j < grad.length; j++)     grad[j]+=(grad_direct[i][j] / n_direct) + (grad_inverse[i][j] / n_inverse);
  }
  this.partialDirectConsitencyError=this.consistencyWeight * f_direct;
  this.partialInverseConsitencyError=this.consistencyWeight * f_inverse;
  double consistencyDirectError=(n_direct == 0) ? 1.0 / FLT_EPSILON : (this.consistencyWeight * f_direct);
  double consistencyInverseError=(n_inverse == 0) ? 1.0 / FLT_EPSILON : (this.consistencyWeight * f_inverse);
  if (showMarquardtOptim) {
    IJ.log("String_Node_Str" + consistencyDirectError);
    IJ.log("String_Node_Str" + consistencyInverseError);
  }
  if (n_direct == 0 || n_inverse == 0)   return 1 / FLT_EPSILON;
  return (this.consistencyWeight * (f_direct + f_inverse));
}

Fixed version (ground truth):
/** 
 * Calculate the geometric error between the source-target and target-source deformations. The corresponding coefficients are assumed to be at swxTargetToSource, swyTargetToSource, swxSourceToTarget and swySourceToTarget. Multi-thread version.
 * @param intervals Input: Number of intervals for the deformation
 * @param grad Output: Gradient of the function
 * @return geometric error between the source-target and target-source deformations.
 */
private double evaluateConsistencyMultiThread(final int intervals,double[] grad){
  double f_direct=0.0;
  double f_inverse=0.0;
  final int nproc=Runtime.getRuntime().availableProcessors();
  int block_height_target=this.targetCurrentHeight / nproc;
  int block_height_source=this.sourceCurrentHeight / nproc;
  final int nThreads=nproc;
  Thread[] threads=new Thread[nThreads];
  Rectangle[] rect_target=new Rectangle[nThreads];
  Rectangle[] rect_source=new Rectangle[nThreads];
  final double[][] grad_direct=new double[nThreads][grad.length];
  final double[][] grad_inverse=new double[nThreads][grad.length];
  final double[][] result=new double[nThreads][4];
  int n_direct=0;
  int n_inverse=0;
  for (int i=0; i < nThreads; i++) {
    int y_start_target=i * block_height_target;
    int y_start_source=i * block_height_source;
    if (nThreads - 1 == i) {
      block_height_target=this.targetCurrentHeight - i * block_height_target;
      block_height_source=this.sourceCurrentHeight - i * block_height_source;
    }
    rect_target[i]=new Rectangle(0,y_start_target,this.targetCurrentWidth,block_height_target);
    rect_source[i]=new Rectangle(0,y_start_source,this.sourceCurrentWidth,block_height_source);
    threads[i]=new Thread(new EvaluateConsistencyTile(this,grad_direct[i],grad_inverse[i],result[i],rect_target[i],rect_source[i]));
    threads[i].start();
  }
  for (int i=0; i < nThreads; i++) {
    try {
      threads[i].join();
      threads[i]=null;
    }
 catch (    InterruptedException e) {
      e.printStackTrace();
    }
  }
  for (int i=0; i < nThreads; i++) {
    f_direct+=result[i][0];
    n_direct+=result[i][1];
    f_inverse+=result[i][2];
    n_inverse+=result[i][3];
  }
  f_direct/=n_direct;
  f_inverse/=n_inverse;
  for (int i=0; i < nThreads; i++) {
    for (int j=0; j < grad.length; j++)     grad[j]+=(grad_direct[i][j] / n_direct) + (grad_inverse[i][j] / n_inverse);
  }
  this.partialDirectConsitencyError=this.consistencyWeight * f_direct;
  this.partialInverseConsitencyError=this.consistencyWeight * f_inverse;
  double consistencyDirectError=(n_direct == 0) ? 1.0 / FLT_EPSILON : (this.consistencyWeight * f_direct);
  double consistencyInverseError=(n_inverse == 0) ? 1.0 / FLT_EPSILON : (this.consistencyWeight * f_inverse);
  if (showMarquardtOptim) {
    IJ.log("String_Node_Str" + consistencyDirectError);
    IJ.log("String_Node_Str" + consistencyInverseError);
  }
  if (n_direct == 0 || n_inverse == 0)   return 1 / FLT_EPSILON;
  return (this.consistencyWeight * (f_direct + f_inverse));
}