// Adapted from the submitted patch for HPCG. See third_party/hpcg/LICENSE.
#include "ComputeSPMV.hpp"

#include <cassert>

#ifndef HPCG_NO_MPI
#include "ExchangeHalo.hpp"
#endif

int ComputeSPMV(const SparseMatrix& matrix, Vector& x, Vector& y) {
  assert(x.localLength >= matrix.localNumberOfColumns);
  assert(y.localLength >= matrix.localNumberOfRows);
#ifndef HPCG_NO_MPI
  ExchangeHalo(matrix, x);
#endif
  matrix.isSpmvOptimized = true;
  const double* const input = x.values;
#ifndef HPCG_NO_OPENMP
#pragma omp parallel for
#endif
  for (local_int_t row = 0; row < matrix.localNumberOfRows; ++row) {
    const double* const values = matrix.matrixValues[row];
    const local_int_t* const columns = matrix.mtxIndL[row];
    const int count = matrix.nonzerosInRow[row];
    const int limit = count - count % 4;

    // Independent partial sums shorten the dependency chain in the inner loop.
    double sum0 = 0.0;
    double sum1 = 0.0;
    double sum2 = 0.0;
    double sum3 = 0.0;
    int j = 0;
    for (; j < limit; j += 4) {
      sum0 += values[j] * input[columns[j]];
      sum1 += values[j + 1] * input[columns[j + 1]];
      sum2 += values[j + 2] * input[columns[j + 2]];
      sum3 += values[j + 3] * input[columns[j + 3]];
    }

    // Boundary rows and 27-entry interior rows can have a scalar tail.
    double sum = sum0 + sum1 + sum2 + sum3;
    for (; j < count; ++j) {
      sum += values[j] * input[columns[j]];
    }
    y.values[row] = sum;
  }
  return 0;
}
