//在图像处理中,计算很耗时,考虑用openMP并行化,
//本文对比不同办法的循环处理数组的耗费时间,在visual studio 2017调试通过。
//实际测试对比,简单循环并行化、分块技术、编译制导的效果都比较显著。
//但是使用分块技术,两重循环对应行数和列数,比较直观,应用价值大。
#include <iostream>
#include <windows.h>
#include <thread>
#include <omp.h>
#include "time.h"
const int width = 10000;
const int height = 10000;
long array1[width * height];
long array2[width * height];
long array3[width * height];
int func1(int w, int h)//不使用并行化
{
clock_t t1 = clock();
long t = w * h;
for (long i = 0; i < t; i++)//遍历每个元素
{
array1[i] = 0; array2[i] = 0;
array3[i] = array1[i] + array2[i];
}
clock_t t2 = clock();
std::cout << t2 - t1 << std::endl;
std::cout << "---" << std::endl;
return 0;
}
int func2(int w, int h)//简单循环并行化
{
clock_t t1 = clock();
long t = w * h;
#pragma omp parallel for
for (long i = 0; i < t; i++)
{
array1[i] = 0; array2[i] = 0;
array3[i] = array1[i] + array2[i];
}
clock_t t2 = clock();
std::cout << t2 - t1 << std::endl;
std::cout << "---" << std::endl;
return 0;
}
int func3(int w, int h)//使用分块技术将循环转换为等效的多线程
{
clock_t t1 = clock();
int i; int j; int m; int n;
#pragma omp parallel for private(i,j)
for (i = 0; i < h; i++)//遍历行
{
for (j = i * w; j < i*w + w; j++)//遍历列
{
array1[j] = 0; array2[j] = 0;
array3[j] = array1[j] + array2[j];
}
}
clock_t t2 = clock();
std::cout << t2 - t1 << std::endl;
std::cout << "---" << std::endl;
return 0;
}
int func3p(long srcArry1[], long srcArry2[], int w, int h, long destsrcArry[])
///数组做参数,使用分块技术将循环转换为等效的多线程
{
clock_t t1 = clock();
int i; int j; int m; int n;
#pragma omp parallel for private(i,j)
for (i = 0; i < h; i++)//遍历行
{
for (j = i * w; j < i * w + w; j++)//遍历列
{
srcArry1[j] = 0; srcArry2[j] = 0;
destsrcArry[j] = srcArry1[j] + srcArry1[j];
}
}
clock_t t2 = clock();
std::cout << t2 - t1 << std::endl;
std::cout << "---" << std::endl;
return 0;
}
int func4(int w, int h)//使用编译制导将循环转换成等效的多线程度
{
clock_t t1 = clock();
long t = w * h;
long i;
#pragma omp parallel sections private (i)
{
#pragma omp section
{
for (i = 0; i < t / 2; i++)
{
array1[i] = 0; array2[i] = 0;
array3[i] = array1[i] + array2[i];
}
}
#pragma omp section
{ for (i = t / 2 + 1; i < t; i++)
{
array1[i] = 0; array2[i] = 0;
array3[i] = array1[i] + array2[i];
}
}
}
clock_t t2 = clock();
std::cout << t2 - t1 << std::endl;
std::cout << "---" << std::endl;
return 0;
}
int main()
{
func1(width, height);
func2(width, height);
func3(width, height);
func3p(array1, array2, width, height, array3);
func4(width, height);
system("pause");
return 0;
}