SSE2向量上的OpenMP减少

时间:2019-01-16 21:36:42

标签: openmp sse simd

我想使用SSE2内在函数为每个通道计算图像的平均值(3个感兴趣的通道+ 1个我们在这里忽略的alpha通道)。我尝试过:

  __m128 average = _mm_setzero_ps();

  #pragma omp parallel for reduction(+:average)
  for(size_t k = 0; k < roi_out->height * roi_out->width * ch; k += ch)
  {
    float *in = ((float *)temp) + k;
    average += _mm_load_ps(in);
  }

但是我在GCC中遇到此错误:user-defined reduction not found for average

使用SSE2可以吗?怎么了?

修改

这有效:

float sum[4] = { 0.0f };

#pragma omp parallel for simd reduction(+:sum[:4])
for(size_t k = 0; k < roi_out->height * roi_out->width * ch; k += ch)
{
  float *in = ((float *)temp) + k;
  for (int i = 0; i < ch; ++i) sum[i] += in[i];
}

const __m128 average = _mm_load_ps(sum) / ((float)roi_out->height * roi_out->width);

1 个答案:

答案 0 :(得分:2)

您可以这样定义用户自定义归约:

#pragma omp declare reduction \
    (addps:__m128:omp_out+=omp_in) \
    initializer(omp_priv=_mm_setzero_ps())

然后像这样使用它:

#pragma omp parallel for reduction(addps:average)
for(size_t k = 0; k < size * ch; k += ch)
{
  average += _mm_loadu_ps(data+k);
}

我认为,最重要的是,openmp需要知道如何获得中性元素(此处为_mm_setzero_ps())。

完整的示例:https://godbolt.org/z/Fpqttc

有趣的链接:http://pages.tacc.utexas.edu/~eijkhout/pcse/html/omp-reduction.html#User-definedreductions