//在图像处理中,数组计算很耗时,考虑用openMP多并行化,
//本文对比不同办法的耗费时间。
//实际测试对比,在台式机及比较本,简单循环并行化都表现不俗。
#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 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);
func4(width, height);
system("pause");
return 0;
}