使用 AVX2 对嵌套循环进行矢量化

发布于 2025-01-12 08:16:29 字数 2095 浏览 1 评论 0原文

我正在尝试将内部循环矢量化为以下嵌套循环。首先,这是一种好的做法,还是应该避免尝试矢量化嵌套循环?

下面的工作,它已经有一些基本的循环展开。

int sparsemv(struct mesh *A, const double * const x, double * const y) {

  const int nrow = (const int) A->local_nrow;
  int j = 0;
  double sum = 0.0;
  #pragma omp parallel for private(j, sum)
  for (int i=0; i< nrow; i++) {
    sum = 0.0;
    const double * const cur_vals = (const double * const) A->ptr_to_vals_in_row[i];
    const int * const cur_inds = (const int * const) A->ptr_to_inds_in_row[i];
    const int cur_nnz = (const int) A->nnz_in_row[i];
    int unroll = (cur_nnz/4)*4;
    for (j=0; j< unroll; j+=4) {
      sum += cur_vals[j] * x[cur_inds[j]];
      sum += cur_vals[j+1] * x[cur_inds[j+1]];
      sum += cur_vals[j+2] * x[cur_inds[j+2]];
      sum += cur_vals[j+3] * x[cur_inds[j+3]];
    }
    for (; j < cur_nnz; j++) {
      sum += cur_vals[j] * x[cur_inds[j]];
    }
    y[i] = sum;
  }
  return 0;
}

然而,当我尝试在 AVX2 中使用 256 位矢量寄存器进行矢量化时,我得到的要么是不正确的答案,要么是段错误。 x 和 y 对齐,但 A 未对齐,但目前,所有加载和存储都是使用未对齐操作完成的,因为这是我唯一一次没有遇到 seg 错误:

int sparsemv(struct mesh *A, const double * const x, double * const y) {

  const int nrow = (const int) A->local_nrow;
  int j = 0;
  double sum = 0.0;
  #pragma omp parallel for private(j, sum)
  for (int i=0; i< nrow; i++) {
    sum = 0.0;
    const double * const cur_vals = (const double * const) A->ptr_to_vals_in_row[i];
    const int * const cur_inds = (const int * const) A->ptr_to_inds_in_row[i];
    const int cur_nnz = (const int) A->nnz_in_row[i];
    int unroll = (cur_nnz/4)*4;
    __m256d sumVec = _mm256_set1_pd(sum);
    for (j=0; j< unroll; j+=4) {
      __m256d cur_valsVec = _mm256_loadu_pd(cur_vals + j);
      __m256d xVec = _mm256_loadu_pd(x + cur_inds[j]);
      sumVec = _mm256_add_pd(sumVec, _mm256_mul_pd(cur_valsVec, xVec));
    }
    _mm256_storeu_pd(y + i, sumVec);  // Is this storing in y + i + 1, 2 and 3 aswell?
    for (; j < cur_nnz; j++) {
      sum += cur_vals[j] * x[cur_inds[j]];
    }
    y[i] += sum;
  }
  return 0;
}

I am trying to vectorise the inner loop the following nested loop. Firstly, is this good practice, or should one avoid attempting to vectorise nested loops?

The following works, it has already some basic loop unrolling.

int sparsemv(struct mesh *A, const double * const x, double * const y) {

  const int nrow = (const int) A->local_nrow;
  int j = 0;
  double sum = 0.0;
  #pragma omp parallel for private(j, sum)
  for (int i=0; i< nrow; i++) {
    sum = 0.0;
    const double * const cur_vals = (const double * const) A->ptr_to_vals_in_row[i];
    const int * const cur_inds = (const int * const) A->ptr_to_inds_in_row[i];
    const int cur_nnz = (const int) A->nnz_in_row[i];
    int unroll = (cur_nnz/4)*4;
    for (j=0; j< unroll; j+=4) {
      sum += cur_vals[j] * x[cur_inds[j]];
      sum += cur_vals[j+1] * x[cur_inds[j+1]];
      sum += cur_vals[j+2] * x[cur_inds[j+2]];
      sum += cur_vals[j+3] * x[cur_inds[j+3]];
    }
    for (; j < cur_nnz; j++) {
      sum += cur_vals[j] * x[cur_inds[j]];
    }
    y[i] = sum;
  }
  return 0;
}

However, when I try to vectorise using 256-bit Vector registers in AVX2, I get either the incorrect answers or seg faults. x and y are aligned but A is not, but for the moment, all loading and storing is done using unaligned operations since that is the only time I don't get seg faults:

int sparsemv(struct mesh *A, const double * const x, double * const y) {

  const int nrow = (const int) A->local_nrow;
  int j = 0;
  double sum = 0.0;
  #pragma omp parallel for private(j, sum)
  for (int i=0; i< nrow; i++) {
    sum = 0.0;
    const double * const cur_vals = (const double * const) A->ptr_to_vals_in_row[i];
    const int * const cur_inds = (const int * const) A->ptr_to_inds_in_row[i];
    const int cur_nnz = (const int) A->nnz_in_row[i];
    int unroll = (cur_nnz/4)*4;
    __m256d sumVec = _mm256_set1_pd(sum);
    for (j=0; j< unroll; j+=4) {
      __m256d cur_valsVec = _mm256_loadu_pd(cur_vals + j);
      __m256d xVec = _mm256_loadu_pd(x + cur_inds[j]);
      sumVec = _mm256_add_pd(sumVec, _mm256_mul_pd(cur_valsVec, xVec));
    }
    _mm256_storeu_pd(y + i, sumVec);  // Is this storing in y + i + 1, 2 and 3 aswell?
    for (; j < cur_nnz; j++) {
      sum += cur_vals[j] * x[cur_inds[j]];
    }
    y[i] += sum;
  }
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文