c++ - 为什么为openmp并行处理不适用于矢量化色彩空间转换?

标签 c++ x86 openmp avx icc

我已经向量化了色彩空间转换算法(RGB到YCbCr)。当我不使用线程(#pragma omp parallel for)时,一切似乎都很好。但是,当我尝试使用线程时,它不能提高代码的矢量化版本的性能(,它也反对)。

线程加快了标量代码,自动矢量化代码和OpenMP SIMDized代码(#pragma omp parallel for simd)的速度

我不知道发生了什么,需要您的帮助。

提前致谢

我使用fedora 31,Intel corei7 6700HQ,12GB RAM,ICC 19.0.3(-Ofast [-no-vec] -qopenmp -xHOST

代码如下:

标量:

//Scalar for basline
#include <stdio.h>
#define MAX1 512
#define MAX2 MAX1


float  __attribute__(( aligned(32))) image_r[MAX1][MAX2], image_g[MAX1][MAX2], image_b[MAX1][MAX2], image_y[MAX1][MAX2], image_cb[MAX1][MAX2], image_cr[MAX1][MAX2];
float coeff_RTY[3][3] = {{0.299, 0.587, 0.114},{-0.169, -0.331, 0.500},{0.500, -0.419, -0.081}};

inline void fill_float(float a[MAX1][MAX1])
{
    int i,j;
    for(i=0; i<MAX1; i++){

        for(j=0; j<MAX2; j++){
            a[i][j] = (i+j+100)%256;

        }
    }
}
int main()
{
    fill_float(image_r);
    fill_float(image_g);
    fill_float(image_b);

    int i, j;
    long t1,t2,min=100000000000000;
    do{
        t1=_rdtsc();
        //#pragma omp parallel for
        for( i=0; i<MAX1; i++){
        for( j=0; j<MAX2; j++){

        image_y[i][j] = coeff_RTY[0][0]*image_r[i][j] + coeff_RTY[0][1]*image_g[i][j] + coeff_RTY[0][2]*image_b[i][j];
        image_cb[i][j] = coeff_RTY[1][0]*image_r[i][j] + coeff_RTY[1][1]*image_g[i][j] + coeff_RTY[1][2]*image_b[i][j] + 128;
        image_cr[i][j] = coeff_RTY[2][0]*image_r[i][j] + coeff_RTY[2][1]*image_g[i][j] + coeff_RTY[2][2]*image_b[i][j] + 128;

        }
        }

        t2=_rdtsc();

        if((t2-t1)<min){
            min=t2-t1;
            printf("\n%li", t2-t1);
        }
    }while(1);
    printf("%f", image_y[MAX1/2][MAX2/2]);
    printf("%f", image_cb[MAX1/2][MAX2/2]);
    printf("%f", image_cr[MAX1/2][MAX2/2]);
    return 0;
}

以及使用AVX(浮点数)的向量化版本:
//AVX
#include <stdio.h>
#include <x86intrin.h>
#define MAX1 512
#define MAX2 MAX1

float  __attribute__(( aligned(32))) image_r[MAX1][MAX2], image_g[MAX1][MAX2], image_b[MAX1][MAX2], image_y[MAX1][MAX2], image_cb[MAX1][MAX2], image_cr[MAX1][MAX2];
float coeff_RTY[3][3] = {{0.299, 0.587, 0.114},{-0.169, -0.331, 0.500},{0.500, -0.419, -0.081}};

inline void fill_float(float a[MAX1][MAX1])
{
    int i,j;
    for(i=0; i<MAX1; i++){

        for(j=0; j<MAX2; j++){
            a[i][j] = (i+j+100)%256;

        }
    }
}
int main()
{


    //program variables:
    //calculate filter coeff or use an existing one
    __m256 vec_c[3][3], vec_128;
    __m256 vec_r, vec_g, vec_b, vec_y, vec_cb, vec_cr;
    __m256 vec_t[3][3], vec_sum;

    vec_c[0][0] = _mm256_set1_ps(coeff_RTY[0][0]);
    vec_c[0][1] = _mm256_set1_ps(coeff_RTY[0][1]);
    vec_c[0][2] = _mm256_set1_ps(coeff_RTY[0][2]);

    vec_c[1][0] = _mm256_set1_ps(coeff_RTY[1][0]);
    vec_c[1][1] = _mm256_set1_ps(coeff_RTY[1][1]);
    vec_c[1][2] = _mm256_set1_ps(coeff_RTY[1][2]);

    vec_c[2][0] = _mm256_set1_ps(coeff_RTY[2][0]);
    vec_c[2][1] = _mm256_set1_ps(coeff_RTY[2][1]);
    vec_c[2][2] = _mm256_set1_ps(coeff_RTY[2][2]);

    vec_128 = _mm256_set1_ps(128);
    //iorder to avoid optimization for zero values
    fill_float(image_r);
    fill_float(image_g);
    fill_float(image_b);
    int i, j=0;
    long t1,t2,min=100000000000000;
    do{
        t1=_rdtsc();

        //#pragma omp parallel for
        for( i=0; i<MAX1; i++){
            for( j=0; j<MAX2; j+=8){
            //_mm_prefetch(&image_r[i][j+8],_MM_HINT_T0);
            //_mm_prefetch(&image_g[i][j+8],_MM_HINT_T0);
            //_mm_prefetch(&image_b[i][j+8],_MM_HINT_T0);
            vec_r = _mm256_load_ps(&image_r[i][j]);
            vec_g = _mm256_load_ps(&image_g[i][j]);
            vec_b = _mm256_load_ps(&image_b[i][j]);


            vec_t[0][0] = _mm256_mul_ps(vec_r, vec_c[0][0]);
            vec_t[0][1] = _mm256_mul_ps(vec_g, vec_c[0][1]);
            vec_t[0][2] = _mm256_mul_ps(vec_b, vec_c[0][2]);

            vec_t[1][0] = _mm256_mul_ps(vec_r, vec_c[1][0]);
            vec_t[1][1] = _mm256_mul_ps(vec_g, vec_c[1][1]);
            vec_t[1][2] = _mm256_mul_ps(vec_b, vec_c[1][2]);

            vec_t[2][0] = _mm256_mul_ps(vec_r, vec_c[2][0]);
            vec_t[2][1] = _mm256_mul_ps(vec_g, vec_c[2][1]);
            vec_t[2][2] = _mm256_mul_ps(vec_b, vec_c[2][2]);

            //vec_y = vec_t[0][0] + vec_t[0][1] + vec_t[0][2]
            vec_sum = _mm256_add_ps(vec_t[0][0], vec_t[0][1]);
            vec_y = _mm256_add_ps(vec_t[0][2], vec_sum);

            //vec_cb = vec_t[1][0] + vec_t[1][1] + vec_t[1][2] +128
            vec_sum = _mm256_add_ps(vec_t[1][0], vec_t[1][1]);
            vec_sum = _mm256_add_ps(vec_t[1][2], vec_sum);
            vec_cb = _mm256_add_ps(vec_128, vec_sum);

            //vec_cr = vec_t[2][0] + vec_t[2][1] + vec_t[2][2] +128
            vec_sum = _mm256_add_ps(vec_t[2][0], vec_t[2][1]);
            vec_sum = _mm256_add_ps(vec_t[2][2], vec_sum);
            vec_cr = _mm256_add_ps(vec_128, vec_sum);

            _mm256_stream_ps(&image_y[i][j], vec_y);
            _mm256_stream_ps(&image_cb[i][j], vec_cb);
            _mm256_stream_ps(&image_cr[i][j], vec_cr);

            }
        }
        t2=_rdtsc();

        if((t2-t1)<min){
            min=t2-t1;
            printf("\n%li", t2-t1);
        }
    }while(1);

    //inorder to avoid optimization for non used values
    printf("%f", image_y[MAX1/2][MAX2/2]);
    printf("%f", image_cb[MAX1/2][MAX2/2]);
    printf("%f", image_cr[MAX1/2][MAX2/2]);

    return 0;
}

更新:

128x128图像尺寸的最佳记录周期如下:

单核:
Scalar code: 88k
Auto-vectorized: 59k
Vectorized using intrinsics: **21k** 
vectorized by #pragma omp simd: 59k

多核:
Scalar code: 25k
Auto-vectorized: 13k
Vectorized using intrinsics: **226k** 
vectorized by #pragma omp .. simd: 22k

对于1024x1024,图像尺寸如下:

单核:
Scalar code: 7M
Auto-vectorized: 3M
Vectorized using intrinsics: **3M** 
vectorized by #pragma omp simd: 3M

多核:
Scalar code: 6M
Auto-vectorized: 6M
Vectorized using intrinsics: **15M** 
vectorized by #pragma omp parallel for simd: 8M

最佳答案

在尝试了不同的想法之后,通过在#pragma omp parallel for之前添加以下OpenMP语句行解决了该问题。
omp_set_dynamic(3);
因此结果是:

Vectorized using intrinsics and Multi-core:


MAX1=128 --> 28k

MAX1=1024 --> 3M

这些结果不再奇怪。

任何新结果将在 future 更新中添加到此答案中。

关于c++ - 为什么为openmp并行处理不适用于矢量化色彩空间转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60234806/

相关文章:

x86 - 强制 L1 缓存未命中

android - Google Play 不过滤 x86 设备

linux - 为什么递增对较大数字的作用与对单个数字的作用不同?

multithreading - Openmp:与omp_get_thread_num()并行使用

c - 循环中缺少错误(OpenMP with C)

c++ - irrlicht 模型加载/动画

c++ - 使用BLAS和OpenMP优化本征重组(矩阵-对角矩阵-矩阵)产品C++

c++ - Bison 在错误的行后输出字符串

C# 和 C++,从 C# 调用 C++ dll 时出现运行时错误

c++ - 在调用第二个函数之前等待整个 omp block 完成