OpenCV统一Simd框架使用(二):数组、矩阵与点云优化

一维数组Simd优化

c风格数组

用c语言风格神奇如下的一维数组:

1
2
3
4
5
6
const size_t count = 10000;

//c风格数组
int* arr_a = new int[count];
int* arr_b = new int[count];
int* arr_c = new int[count];

假设数组需要做如下的计算处理,即c = a + b,

1
2
3
4
5
6
7
//源程序
{
    for (size_t i = 0; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

运算非常简单,只需要一个循环遍历,单只能每次循环计算一遍。下面使用simd优化,当固定simd向量位宽的时候:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
//simd固定向量长优化
{        
#ifdef CV_SIMD128
    size_t step = cv::v_int32x4::nlanes;
    size_t i = 0;
    for (; i < count - step; i += step)
    {
        cv::v_int32x4 va = cv::v_load(arr_a + i);
        cv::v_int32x4 vb = cv::v_load(arr_b + i);
        cv::v_int32x4 vc = va + vb;
        cv::v_store(arr_c + i, vc);
    }
    //处理数组尾部未够一个step的数据
    for (; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
#endif 
}

选择自适应simd位宽时:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//自适应长度simd优化
{
    size_t step = cv::v_int32::nlanes;
    size_t i = 0;
    for (; i < count - step; i += step)
    {
        cv::v_int32 va = cv::vx_load(arr_a + i);
        cv::v_int32 vb = cv::vx_load(arr_b + i);
        cv::v_int32 vc = va + vb;
        cv::vx_store(arr_c + i, vc);
    }
    //处理数组尾部未够一个step的数据
    for (; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

同时,还可以结合openmp的并行多线程优化:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
    //c风格数组优化结合openmp并行
    int step = cv::v_int32::nlanes;
#pragma omp parallel for 
    for (int i = 0; i < count - step; i += step)
    {
        cv::v_int32 va = cv::vx_load(arr_a + i);
        cv::v_int32 vb = cv::vx_load(arr_b + i);
        cv::v_int32 vc = va + vb;
        cv::vx_store(arr_c + i, vc);
    }
    //处理数组尾部未够一个step的数据
    int current_i = count - count % step;
    for (int i = current_i; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

ste::vector数组

同样地,初始化如下数组

1
2
3
std::vector<float> arr_a(count, 1.0f);
std::vector<float> arr_b(count, 1.0f);
std::vector<float> arr_c(count, 1.0f);

做一样两数组元素相加计算处理:

1
2
3
4
5
6
7
//源程序
{
    for (size_t i = 0; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

下面使用Simd优化vector数组,参考c风格数组,处理非常相似。当固定Simd位宽时:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//simd固定向量长优化
{
#ifdef CV_SIMD128
    size_t i = 0;
    for (; i < count - 4; i += 4)
    {
        cv::v_float32x4 va = cv::v_load(arr_a.begin()._Ptr + i);    //或者cv::v_float32x4 va(arr_a[i], arr_a[i + 1], arr_a[i + 2], arr_a[i + 3]);
        cv::v_float32x4 vb = cv::v_load(arr_b.begin()._Ptr + i);    //或者cv::v_float32x4 vb(arr_b[i], arr_b[i + 1], arr_b[i + 2], arr_b[i + 3]);
        cv::v_float32x4 vc = va + vb;
        cv::v_store(arr_c.begin()._Ptr + i, vc);
    }
    //处理数组尾部未够一个step的数据
    for (; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
#endif 
}

自适应位宽simd优化如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//自适应长度simd优化
{
    size_t i = 0;
    size_t step = cv::v_float32::nlanes;
    float* temp_arr = new float[step];
    for (; i < count - step; i += step)
    {
        cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
        cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
        cv::v_float32 vc = va + vb;
        cv::vx_store(arr_c.begin()._Ptr + i, vc);
    }
    //处理数组尾部未够一个step的数据
    for (; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

依然可以兼容openmp多线程并行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
//std::vector动态数组优化结合openmp并行
{
    int step = cv::v_float32::nlanes;
    float* temp_arr = new float[step];
#pragma omp parallel for
    for (int i  = 0; i < count - step; i += step)
    {
        cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
        cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
        cv::v_float32 vc = va + vb;
        cv::vx_store(arr_c.begin()._Ptr + i, vc);
    }
    //处理数组尾部未够一个step的数据
    int current_i = count - count % step;
    for (int i = current_i; i < count; i++)
    {
        arr_c[i] = arr_a[i] + arr_b[i];
    }
}

示例代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * @brief 一维数组simd优化示例
 * 
 */
void ArraySimd()
{
    const size_t count = 10000;
    //c风格数组优化
    {
        //c风格数组
        int* arr_a = new int[count];
        int* arr_b = new int[count];
        int* arr_c = new int[count];

        //源程序
        {
            for (size_t i = 0; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //simd固定向量长优化
        {        
#ifdef CV_SIMD128
            size_t step = cv::v_int32x4::nlanes;
            size_t i = 0;
            for (; i < count - step; i += step)
            {
                cv::v_int32x4 va = cv::v_load(arr_a + i);
                cv::v_int32x4 vb = cv::v_load(arr_b + i);
                cv::v_int32x4 vc = va + vb;
                cv::v_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
#endif 
        }
        //自适应长度simd优化
        {
            size_t step = cv::v_int32::nlanes;
            size_t i = 0;
            for (; i < count - step; i += step)
            {
                cv::v_int32 va = cv::vx_load(arr_a + i);
                cv::v_int32 vb = cv::vx_load(arr_b + i);
                cv::v_int32 vc = va + vb;
                cv::vx_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        {
            //c风格数组优化结合openmp并行
            int step = cv::v_int32::nlanes;
#pragma omp parallel for 
            for (int i = 0; i < count - step; i += step)
            {
                cv::v_int32 va = cv::vx_load(arr_a + i);
                cv::v_int32 vb = cv::vx_load(arr_b + i);
                cv::v_int32 vc = va + vb;
                cv::vx_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            int current_i = count - count % step;
            for (int i = current_i; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
    }

    //std::vector动态数组优化
    {
        std::vector<float> arr_a(count, 1.0f);
        std::vector<float> arr_b(count, 1.0f);
        std::vector<float> arr_c(count, 1.0f);

        //源程序
        {
            for (size_t i = 0; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //simd固定向量长优化
        {
#ifdef CV_SIMD128
            size_t i = 0;
            for (; i < count - 4; i += 4)
            {
                cv::v_float32x4 va = cv::v_load(arr_a.begin()._Ptr + i);    //或者cv::v_float32x4 va(arr_a[i], arr_a[i + 1], arr_a[i + 2], arr_a[i + 3]);
                cv::v_float32x4 vb = cv::v_load(arr_b.begin()._Ptr + i);    //或者cv::v_float32x4 vb(arr_b[i], arr_b[i + 1], arr_b[i + 2], arr_b[i + 3]);
                cv::v_float32x4 vc = va + vb;
                cv::v_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
#endif 
        }
        //自适应长度simd优化
        {
            size_t i = 0;
            size_t step = cv::v_float32::nlanes;
            float* temp_arr = new float[step];
            for (; i < count - step; i += step)
            {
                cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
                cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
                cv::v_float32 vc = va + vb;
                cv::vx_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //std::vector动态数组优化结合openmp并行
        {
            int step = cv::v_float32::nlanes;
            float* temp_arr = new float[step];
#pragma omp parallel for
            for (int i  = 0; i < count - step; i += step)
            {
                cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
                cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
                cv::v_float32 vc = va + vb;
                cv::vx_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            int current_i = count - count % step;
            for (int i = current_i; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
    }
}

二维矩阵Simd优化

下面介绍二维矩阵数据的Simd优化,以OpenCV的主要数据结构Mat为例。首先构造一个Mat对象,并初始化数据元素为0-255之间的随机数,得到一副随机的灰度图。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const size_t mat_size = 1000;
cv::Mat gray_image = cv::Mat(mat_size, mat_size, CV_8UC1);
cv::Mat binary_image = cv::Mat(mat_size, mat_size, CV_8UC1);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<>  dist(0, 255);
const uchar threshold = 200;
for (size_t i = 0; i < mat_size; i++)
{
    uchar* pg = gray_image.ptr<uchar>(i);
    for (size_t j = 0; j < mat_size; j++)
    {
        pg[j] = dist(gen);
    }
}

以最常见的阈值二值化为例,将构造的随机灰度图,二值化。未经优化的二值化处理程序如下,代码非常简单,遍历每个像素与阈值对比后置为0或者255即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//源程序:二值化图像
{
    for (size_t i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        uchar* pb = binary_image.ptr<uchar>(i);
        for (size_t j = 0; j < mat_size; j++)
        {
            pb[j] = pg[j] > threshold ? 255 : 0;
        }
    }
}

固定向量长优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//simd固定向量长优化
{
#ifdef CV_SIMD128
    size_t step = cv::v_uint8x16::nlanes;//4
    cv::v_uint8x16 v_threshold = cv::v_setall(threshold);
    cv::v_uint8x16 v_255 = cv::v_setall(uchar(255));
    cv::v_uint8x16 v_0 = cv::v_setall(uchar(0));
    for (size_t i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        uchar* pb = binary_image.ptr<uchar>(i);
        
        for (size_t j = 0; j < mat_size - step; j += step)
        {
            cv::v_uint8x16 vg = cv::v_load(pg + j);
            auto condition = vg > v_threshold;
            cv::v_uint8x16 vb = cv::v_select(condition,v_255 , v_0);
            cv::v_store(pb + j, vb);
        }
        //处理一行尾部未够一个step的像素
        size_t current_j = mat_size - mat_size % step;
        for (size_t j = current_j; j < mat_size; j ++)
        {
            pb[j] = pg[j] > threshold ? 255 : 0;
        }
    }
#endif
}

自适应长度Simd:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//自适应长度simd优化
{
    size_t step = cv::v_uint8::nlanes;
    cv::v_uint8 v_threshold = cv::vx_setall(threshold);
    cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
    cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
    for (size_t i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        uchar* pb = binary_image.ptr<uchar>(i);

        for (size_t j = 0; j < mat_size - step; j += step)
        {
            cv::v_uint8 vg = cv::vx_load(pg + j);
            auto condition = vg > v_threshold;
            cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
            cv::vx_store(pb + j, vb);
        }
        //处理一行尾部未够一个step的像素
        size_t current_j = mat_size - mat_size % step;
        for (size_t j = current_j; j < mat_size; j++)
        {
            pb[j] = pg[j] > threshold ? 255 : 0;
        }
    }
}

与OpenMP结合多线程优化:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//优化结合openmp并行
{
    size_t step = cv::v_uint8::nlanes;
    cv::v_uint8 v_threshold = cv::vx_setall(threshold);
    cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
    cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
    //一般双循环openmp并行用于优化外循环
#pragma omp parallel for
    for (int i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        uchar* pb = binary_image.ptr<uchar>(i);

        for (size_t j = 0; j < mat_size - step; j += step)
        {
            cv::v_uint8 vg = cv::vx_load(pg + j);
            auto condition = vg > v_threshold;
            cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
            cv::vx_store(pb + j, vb);
        }
        //处理一行尾部未够一个step的像素
        size_t current_j = mat_size - mat_size % step;
        for (size_t j = current_j; j < mat_size; j++)
        {
            pb[j] = pg[j] > threshold ? 255 : 0;
        }
    }
}

本质上,Mat底层数据也是一维数组数据,如果能把双层循环展开降低为一层循环,那可以做进一步优化。OpenCV的Mat可以展开为单循环遍历像素点,但是需要先检查底层数据是否为连续的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//展开为一维数组优化
{
    size_t step = cv::v_uint8::nlanes;
    cv::v_uint8 v_threshold = cv::vx_setall(threshold);
    cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
    cv::v_uint8 v_0 = cv::vx_setall(uchar(0));

    //cv::Mat需要判断内存是否为连续分配的才可以展开
    if(gray_image.isContinuous() && binary_image.isContinuous())
    {
        uchar* pg = gray_image.ptr<uchar>(0);
        uchar* pb = binary_image.ptr<uchar>(0);
        //展开为单循环
        int len = gray_image.rows * gray_image.cols;
#pragma omp parallel for
        for (int j = 0; j < len - step; j += step)
        {
            cv::v_uint8 vg = cv::vx_load(pg + j);
            auto condition = vg > v_threshold;
            cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
            cv::vx_store(pb + j, vb);
        }
        //处理一行尾部未够一个step的像素
        size_t current_j = len - len % step;
        for (size_t j = current_j; j < len; j++)
        {
            pb[j] = pg[j] > threshold ? 255 : 0;
        }
    }
}

二维矩阵优化示例代码:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
void MatSimd()
{
    const size_t mat_size = 1000;
    cv::Mat gray_image = cv::Mat(mat_size, mat_size, CV_8UC1);
    cv::Mat binary_image = cv::Mat(mat_size, mat_size, CV_8UC1);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<>  dist(0, 255);
    const uchar threshold = 200;
    for (size_t i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        for (size_t j = 0; j < mat_size; j++)
        {
            pg[j] = dist(gen);
        }
    }
    
    //源程序:二值化图像
    {
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);
            for (size_t j = 0; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //simd固定向量长优化
    {
#ifdef CV_SIMD128
        size_t step = cv::v_uint8x16::nlanes;//4
        cv::v_uint8x16 v_threshold = cv::v_setall(threshold);
        cv::v_uint8x16 v_255 = cv::v_setall(uchar(255));
        cv::v_uint8x16 v_0 = cv::v_setall(uchar(0));
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);
            
            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8x16 vg = cv::v_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8x16 vb = cv::v_select(condition,v_255 , v_0);
                cv::v_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j ++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
#endif
    }
    //自适应长度simd优化
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);

            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //优化结合openmp并行
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
        //一般双循环openmp并行用于优化外循环
#pragma omp parallel for
        for (int i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);

            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //展开为一维数组优化
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));

        //cv::Mat需要判断内存是否为连续分配的才可以展开
        if(gray_image.isContinuous() && binary_image.isContinuous())
        {
            uchar* pg = gray_image.ptr<uchar>(0);
            uchar* pb = binary_image.ptr<uchar>(0);
            //展开为单循环
            int len = gray_image.rows * gray_image.cols;
#pragma omp parallel for
            for (int j = 0; j < len - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = len - len % step;
            for (size_t j = current_j; j < len; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
}

三维点云Simd优化

点云的Simd优化就稍微麻烦一点,因为点云有xyz三维数据坐标点集成,底层数据并非连续的,通常都是一个结构体表示坐标点,结构体数组构成点云。首先定义空间坐标点结构体, 以及点云

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//点结构体
struct Point
{
    float x;
    float y;
    float z;
    Point(): x(1.0f),y(2.0f),z(3.f){}
    Point(const float& x_, const float& y_, const float& z_): x(x_),y(y_),z(z_){}
};

const size_t count = 400;
std::vector<Point> cloud(count);

以计算点云的质心为例,未作优化的源程序如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//源程序
{
    //计算质心
    Point centroid(0,0,0);
    for (size_t i = 0; i < count; i++)
    {
        centroid.x += cloud[i].x;
        centroid.y += cloud[i].y;
        centroid.z += cloud[i].z;
    }
    centroid.x /= count;
    centroid.y /= count;
    centroid.z /= count;

    std::cout << "unoptimization centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
}

固定位宽的Simd优化:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//simd固定向量长优化
{
#ifdef  CV_SIMD128
    cv::v_float32x4 centroidx(0, 0, 0, 0);
    cv::v_float32x4 centroidy(0, 0, 0, 0);
    cv::v_float32x4 centroidz(0, 0, 0, 0);
    for (size_t i = 0; i < count; i += 4)
    {
        //x
        cv::v_float32x4 vax(cloud[i].x, cloud[i + 1].x, cloud[i + 2].x, cloud[i + 3].x);
        //y
        cv::v_float32x4 vay(cloud[i].y, cloud[i + 1].y, cloud[i + 2].y, cloud[i + 3].y);
        //z
        cv::v_float32x4 vaz(cloud[i].z, cloud[i + 1].z, cloud[i + 2].z, cloud[i + 3].z);
        centroidx += vax;
        centroidy += vay;
        centroidz += vaz;
    }
    Point centroid(0, 0, 0);
    centroid.x = cv::v_reduce_sum(centroidx);
    centroid.y = cv::v_reduce_sum(centroidy);
    centroid.z = cv::v_reduce_sum(centroidz);
    size_t current_i = count - count % 4;
    for (size_t i = current_i; i < count; i++)
    {
        centroid.x += cloud[i].x;
        centroid.y += cloud[i].y;
        centroid.y += cloud[i].y;
    }
    centroid.x /= count;
    centroid.y /= count;
    centroid.z /= count;

    std::cout << "128bit simd centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";

#endif //  CV_SIMD128

}

自适应位宽的Simd优化:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//自适应长度simd优化
{
    cv::v_float32 centroidx = cv::vx_setzero_f32();
    cv::v_float32 centroidy = cv::vx_setzero_f32();
    cv::v_float32 centroidz = cv::vx_setzero_f32();
    int step = cv::v_float32::nlanes;
    for (int i = 0; i < count; i += step)
    {
        cv::v_float32 vax = cv::vx_setzero_f32();
        cv::v_float32 vay = cv::vx_setzero_f32();
        cv::v_float32 vaz = cv::vx_setzero_f32();
        //Point内存布局为{x, y, z}
        //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
        //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
        cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
        centroidx += vax;
        centroidy += vay;
        centroidz += vaz;
    }
    Point centroid(0, 0, 0);
    centroid.x = cv::v_reduce_sum(centroidx);
    centroid.y = cv::v_reduce_sum(centroidy);
    centroid.z = cv::v_reduce_sum(centroidz);
    size_t current_i = count - count % 4;
    for (size_t i = current_i; i < count; i++)
    {
        centroid.x += cloud[i].x;
        centroid.y += cloud[i].y;
        centroid.y += cloud[i].y;
    }
    centroid.x /= count;
    centroid.y /= count;
    centroid.z /= count;

    std::cout << "auto simd width centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
}

结合OpenMP多线程并行:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//自适应长度simd优化结合OpenMP
{
    cv::v_float32 centroidx = cv::vx_setzero_f32();
    cv::v_float32 centroidy = cv::vx_setzero_f32();
    cv::v_float32 centroidz = cv::vx_setzero_f32();
    int step = cv::v_float32::nlanes;
#pragma omp parallel for
    for (int i = 0; i < count; i += step)
    {
        cv::v_float32 vax = cv::vx_setzero_f32();
        cv::v_float32 vay = cv::vx_setzero_f32();
        cv::v_float32 vaz = cv::vx_setzero_f32();
        //Point内存布局为{x, y, z}
        //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
        //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
        cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
        centroidx += vax;
        centroidy += vay;
        centroidz += vaz;
    }
    Point centroid(0, 0, 0);
    centroid.x = cv::v_reduce_sum(centroidx);
    centroid.y = cv::v_reduce_sum(centroidy);
    centroid.z = cv::v_reduce_sum(centroidz);
    size_t current_i = count - count % 4;
    for (size_t i = current_i; i < count; i++)
    {
        centroid.x += cloud[i].x;
        centroid.y += cloud[i].y;
        centroid.y += cloud[i].y;
    }
    centroid.x /= count;
    centroid.y /= count;
    centroid.z /= count;

    std::cout << "simd&openmp centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
}

完整的三维点云优化代码示例

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
void PointCloudSimd()
{
    //点结构体
    struct Point
    {
        float x;
        float y;
        float z;
        Point(): x(1.0f),y(2.0f),z(3.f){}
        Point(const float& x_, const float& y_, const float& z_): x(x_),y(y_),z(z_){}
    };
    const size_t count = 400;
    std::vector<Point> cloud(count);

    //源程序
    {
        //计算质心
        Point centroid(0,0,0);
        for (size_t i = 0; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.z += cloud[i].z;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "unoptimization centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }
    //simd固定向量长优化
    {
#ifdef  CV_SIMD128
        cv::v_float32x4 centroidx(0, 0, 0, 0);
        cv::v_float32x4 centroidy(0, 0, 0, 0);
        cv::v_float32x4 centroidz(0, 0, 0, 0);
        for (size_t i = 0; i < count; i += 4)
        {
            //x
            cv::v_float32x4 vax(cloud[i].x, cloud[i + 1].x, cloud[i + 2].x, cloud[i + 3].x);
            //y
            cv::v_float32x4 vay(cloud[i].y, cloud[i + 1].y, cloud[i + 2].y, cloud[i + 3].y);
            //z
            cv::v_float32x4 vaz(cloud[i].z, cloud[i + 1].z, cloud[i + 2].z, cloud[i + 3].z);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "128bit simd centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";

#endif //  CV_SIMD128

    }
    //自适应长度simd优化
    {
        cv::v_float32 centroidx = cv::vx_setzero_f32();
        cv::v_float32 centroidy = cv::vx_setzero_f32();
        cv::v_float32 centroidz = cv::vx_setzero_f32();
        int step = cv::v_float32::nlanes;
        for (int i = 0; i < count; i += step)
        {
            cv::v_float32 vax = cv::vx_setzero_f32();
            cv::v_float32 vay = cv::vx_setzero_f32();
            cv::v_float32 vaz = cv::vx_setzero_f32();
            //Point内存布局为{x, y, z}
            //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
            //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
            cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "auto simd width centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }

    //自适应长度simd优化结合OpenMP
    {
        cv::v_float32 centroidx = cv::vx_setzero_f32();
        cv::v_float32 centroidy = cv::vx_setzero_f32();
        cv::v_float32 centroidz = cv::vx_setzero_f32();
        int step = cv::v_float32::nlanes;
#pragma omp parallel for
        for (int i = 0; i < count; i += step)
        {
            cv::v_float32 vax = cv::vx_setzero_f32();
            cv::v_float32 vay = cv::vx_setzero_f32();
            cv::v_float32 vaz = cv::vx_setzero_f32();
            //Point内存布局为{x, y, z}
            //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
            //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
            cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "simd&openmp centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }
}

运行结果:

1
2
3
4
unoptimization centroid: (1,2,3)
128bit simd centroid: (1,2,3)
auto simd width centroid: (1,2,3)
simd&openmp centroid: (1,2,3)

可以看到,优化并未对结果精度造成影响。

完整程序代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
 * @file example1.cpp
 * @author your name ([email protected])
 * @brief 
 * @version 0.1
 * @date 2021-10-21
 * 
 * @copyright Copyright (c) 2021
 * 
 */

#include <iostream>
#include <chrono>
#include <random>

#include "opencv2/opencv.hpp"
#include "opencv2/core/simd_intrinsics.hpp"

#include "omp.h"

/**
 * @brief 一维数组simd优化示例
 * 
 */
void ArraySimd()
{
    const size_t count = 10000;
    //c风格数组优化
    {
        //c风格数组
        int* arr_a = new int[count];
        int* arr_b = new int[count];
        int* arr_c = new int[count];

        //源程序
        {
            for (size_t i = 0; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //simd固定向量长优化
        {        
#ifdef CV_SIMD128
            size_t step = cv::v_int32x4::nlanes;
            size_t i = 0;
            for (; i < count - step; i += step)
            {
                cv::v_int32x4 va = cv::v_load(arr_a + i);
                cv::v_int32x4 vb = cv::v_load(arr_b + i);
                cv::v_int32x4 vc = va + vb;
                cv::v_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
#endif 
        }
        //自适应长度simd优化
        {
            size_t step = cv::v_int32::nlanes;
            size_t i = 0;
            for (; i < count - step; i += step)
            {
                cv::v_int32 va = cv::vx_load(arr_a + i);
                cv::v_int32 vb = cv::vx_load(arr_b + i);
                cv::v_int32 vc = va + vb;
                cv::vx_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        {
            //c风格数组优化结合openmp并行
            int step = cv::v_int32::nlanes;
#pragma omp parallel for 
            for (int i = 0; i < count - step; i += step)
            {
                cv::v_int32 va = cv::vx_load(arr_a + i);
                cv::v_int32 vb = cv::vx_load(arr_b + i);
                cv::v_int32 vc = va + vb;
                cv::vx_store(arr_c + i, vc);
            }
            //处理数组尾部未够一个step的数据
            int current_i = count - count % step;
            for (int i = current_i; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
    }

    //std::vector动态数组优化
    {
        std::vector<float> arr_a(count, 1.0f);
        std::vector<float> arr_b(count, 1.0f);
        std::vector<float> arr_c(count, 1.0f);

        //源程序
        {
            for (size_t i = 0; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //simd固定向量长优化
        {
#ifdef CV_SIMD128
            size_t i = 0;
            for (; i < count - 4; i += 4)
            {
                cv::v_float32x4 va = cv::v_load(arr_a.begin()._Ptr + i);    //或者cv::v_float32x4 va(arr_a[i], arr_a[i + 1], arr_a[i + 2], arr_a[i + 3]);
                cv::v_float32x4 vb = cv::v_load(arr_b.begin()._Ptr + i);    //或者cv::v_float32x4 vb(arr_b[i], arr_b[i + 1], arr_b[i + 2], arr_b[i + 3]);
                cv::v_float32x4 vc = va + vb;
                cv::v_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
#endif 
        }
        //自适应长度simd优化
        {
            size_t i = 0;
            size_t step = cv::v_float32::nlanes;
            float* temp_arr = new float[step];
            for (; i < count - step; i += step)
            {
                cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
                cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
                cv::v_float32 vc = va + vb;
                cv::vx_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            for (; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
        //std::vector动态数组优化结合openmp并行
        {
            int step = cv::v_float32::nlanes;
            float* temp_arr = new float[step];
#pragma omp parallel for
            for (int i  = 0; i < count - step; i += step)
            {
                cv::v_float32 va = cv::vx_load(arr_a.begin()._Ptr + i);
                cv::v_float32 vb = cv::vx_load(arr_b.begin()._Ptr + i);
                cv::v_float32 vc = va + vb;
                cv::vx_store(arr_c.begin()._Ptr + i, vc);
            }
            //处理数组尾部未够一个step的数据
            int current_i = count - count % step;
            for (int i = current_i; i < count; i++)
            {
                arr_c[i] = arr_a[i] + arr_b[i];
            }
        }
    }
}

void MatSimd()
{
    const size_t mat_size = 1000;
    cv::Mat gray_image = cv::Mat(mat_size, mat_size, CV_8UC1);
    cv::Mat binary_image = cv::Mat(mat_size, mat_size, CV_8UC1);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<>  dist(0, 255);
    const uchar threshold = 200;
    for (size_t i = 0; i < mat_size; i++)
    {
        uchar* pg = gray_image.ptr<uchar>(i);
        for (size_t j = 0; j < mat_size; j++)
        {
            pg[j] = dist(gen);
        }
    }
    
    //源程序:二值化图像
    {
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);
            for (size_t j = 0; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //simd固定向量长优化
    {
#ifdef CV_SIMD128
        size_t step = cv::v_uint8x16::nlanes;//4
        cv::v_uint8x16 v_threshold = cv::v_setall(threshold);
        cv::v_uint8x16 v_255 = cv::v_setall(uchar(255));
        cv::v_uint8x16 v_0 = cv::v_setall(uchar(0));
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);
            
            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8x16 vg = cv::v_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8x16 vb = cv::v_select(condition,v_255 , v_0);
                cv::v_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j ++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
#endif
    }
    //自适应长度simd优化
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
        for (size_t i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);

            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //优化结合openmp并行
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));
        //一般双循环openmp并行用于优化外循环
#pragma omp parallel for
        for (int i = 0; i < mat_size; i++)
        {
            uchar* pg = gray_image.ptr<uchar>(i);
            uchar* pb = binary_image.ptr<uchar>(i);

            for (size_t j = 0; j < mat_size - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = mat_size - mat_size % step;
            for (size_t j = current_j; j < mat_size; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
    //展开为一维数组优化
    {
        size_t step = cv::v_uint8::nlanes;
        cv::v_uint8 v_threshold = cv::vx_setall(threshold);
        cv::v_uint8 v_255 = cv::vx_setall(uchar(255));
        cv::v_uint8 v_0 = cv::vx_setall(uchar(0));

        //cv::Mat需要判断内存是否为连续分配的才可以展开
        if(gray_image.isContinuous() && binary_image.isContinuous())
        {
            uchar* pg = gray_image.ptr<uchar>(0);
            uchar* pb = binary_image.ptr<uchar>(0);
            //展开为单循环
            int len = gray_image.rows * gray_image.cols;
#pragma omp parallel for
            for (int j = 0; j < len - step; j += step)
            {
                cv::v_uint8 vg = cv::vx_load(pg + j);
                auto condition = vg > v_threshold;
                cv::v_uint8 vb = cv::v_select(condition, v_255, v_0);
                cv::vx_store(pb + j, vb);
            }
            //处理一行尾部未够一个step的像素
            size_t current_j = len - len % step;
            for (size_t j = current_j; j < len; j++)
            {
                pb[j] = pg[j] > threshold ? 255 : 0;
            }
        }
    }
}

void PointCloudSimd()
{
    //点结构体
    struct Point
    {
        float x;
        float y;
        float z;
        Point(): x(1.0f),y(2.0f),z(3.f){}
        Point(const float& x_, const float& y_, const float& z_): x(x_),y(y_),z(z_){}
    };
    const size_t count = 400;
    std::vector<Point> cloud(count);

    //源程序
    {
        //计算质心
        Point centroid(0,0,0);
        for (size_t i = 0; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.z += cloud[i].z;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "unoptimization centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }
    //simd固定向量长优化
    {
#ifdef  CV_SIMD128
        cv::v_float32x4 centroidx(0, 0, 0, 0);
        cv::v_float32x4 centroidy(0, 0, 0, 0);
        cv::v_float32x4 centroidz(0, 0, 0, 0);
        for (size_t i = 0; i < count; i += 4)
        {
            //x
            cv::v_float32x4 vax(cloud[i].x, cloud[i + 1].x, cloud[i + 2].x, cloud[i + 3].x);
            //y
            cv::v_float32x4 vay(cloud[i].y, cloud[i + 1].y, cloud[i + 2].y, cloud[i + 3].y);
            //z
            cv::v_float32x4 vaz(cloud[i].z, cloud[i + 1].z, cloud[i + 2].z, cloud[i + 3].z);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "128bit simd centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";

#endif //  CV_SIMD128

    }
    //自适应长度simd优化
    {
        cv::v_float32 centroidx = cv::vx_setzero_f32();
        cv::v_float32 centroidy = cv::vx_setzero_f32();
        cv::v_float32 centroidz = cv::vx_setzero_f32();
        int step = cv::v_float32::nlanes;
        for (int i = 0; i < count; i += step)
        {
            cv::v_float32 vax = cv::vx_setzero_f32();
            cv::v_float32 vay = cv::vx_setzero_f32();
            cv::v_float32 vaz = cv::vx_setzero_f32();
            //Point内存布局为{x, y, z}
            //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
            //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
            cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "auto simd width centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }

    //自适应长度simd优化结合OpenMP
    {
        cv::v_float32 centroidx = cv::vx_setzero_f32();
        cv::v_float32 centroidy = cv::vx_setzero_f32();
        cv::v_float32 centroidz = cv::vx_setzero_f32();
        int step = cv::v_float32::nlanes;
#pragma omp parallel for
        for (int i = 0; i < count; i += step)
        {
            cv::v_float32 vax = cv::vx_setzero_f32();
            cv::v_float32 vay = cv::vx_setzero_f32();
            cv::v_float32 vaz = cv::vx_setzero_f32();
            //Point内存布局为{x, y, z}
            //Cloud内存布局则为{x1,y1,z1,x2,y2,z2,...,xn,yn,zn)
            //{x1,y1,z1,x2,y2,z2,...,xn,yn,zn) => v1{x1,x2,...,xn}, v2{y1,y2...yn}, v3{z1,z2,...,zn}
            cv::v_load_deinterleave(&(cloud.begin()._Ptr + i)->x, vax, vay, vaz);
            centroidx += vax;
            centroidy += vay;
            centroidz += vaz;
        }
        Point centroid(0, 0, 0);
        centroid.x = cv::v_reduce_sum(centroidx);
        centroid.y = cv::v_reduce_sum(centroidy);
        centroid.z = cv::v_reduce_sum(centroidz);
        size_t current_i = count - count % 4;
        for (size_t i = current_i; i < count; i++)
        {
            centroid.x += cloud[i].x;
            centroid.y += cloud[i].y;
            centroid.y += cloud[i].y;
        }
        centroid.x /= count;
        centroid.y /= count;
        centroid.z /= count;

        std::cout << "simd&openmp centroid: (" << centroid.x << "," << centroid.y << "," << centroid.z << ")\n";
    }
}

int main(int argc, char** argv)
{
    //一维数组Simd优化
    ArraySimd();
    //二维矩阵Simd优化
    MatSimd();
    //三维点云Simd优化
    PointCloudSimd();
    return 0;
}

本文由芒果浩明发布,转载请注明出处。 本文链接:https://blog.mangoeffect.net/opencv/opencv-universal-intrinsics-simd-2.html


微信公众号