vs2010中調用openMP,並添加頭文件#include<omp.h>

代碼來源:
作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib")
void EdgeOpenMP(IplImage *src,IplImage *dst,int thresh)
{
int height = src->height;
int width = src->width;
int step = src->widthStep;
uchar *data1 = (uchar *)src->imageData;
uchar *data2 = (uchar *)dst->imageData;
int i=step;
#pragma omp parallel for
for(i=step+1;i<height*width;i++){
if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)
data2[i]=255;/* 對於單通道,前后兩幀差分大於門限
或者對於多通道前后兩幀的一個指標差分大於門限,則視為邊緣*/
else
data2[i]=0;
}
}
void Edge(IplImage *src,IplImage *dst,int thresh)
{
int height = src->height;
int width = src->width;
int step = src->widthStep;
uchar *data1 = (uchar *)src->imageData;
uchar *data2 = (uchar *)dst->imageData;
int i=step;
for(i=step+1;i<height*width;i++){
if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)
data2[i]=255;
else
data2[i]=0;
}
}
int main()
{
char filename[512];
IplImage *src,*edge1,*edge2;
puts("File name:");
gets(filename);
src = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE );
edge1=cvCloneImage(src);
edge2=cvCloneImage(src);
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvMoveWindow("src", 100, 100);
cvShowImage( "src", src);
cvNamedWindow("Edge", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Edge", 200, 100);
cvNamedWindow("EdgeOpenMP", CV_WINDOW_AUTOSIZE);
cvMoveWindow("EdgeOpenMP", 300, 100);
/* 以上都是准備一些窗口和圖形基本數據 */
int tekrar=100;//運行次數
int thresh=30;
double start, end,t1, t2;
/* 計算沒有使用OpenMP優化的時間 */
start= (double)cvGetTickCount();//記下開始的時鍾計數,以便計算函數或用戶代碼執行時間
for(int i=0;i<tekrar;i++)
Edge(src,edge1,thresh);
end= (double)cvGetTickCount();//記下結束的時鍾計數
t1= (end-start)/((double)cvGetTickFrequency()*1000.);//計算運行時間,以毫秒為單位
printf( "Run time without OpenMP = %g ms\n", t1 );
/* 計算使用了OpenMP優化的時間 */
start= (double)cvGetTickCount();
for(int i=0;i<tekrar;i++)
EdgeOpenMP(src,edge2,thresh);
end= (double)cvGetTickCount();
t2= (end-start)/((double)cvGetTickFrequency()*1000.);
printf( "Run time with OpenMP = %g ms\n", t2 );
printf( "Performance ratio (%%) = %% %.1f \n", 100*(t1/t2-1) );
cvShowImage( "Edge", edge1);
cvShowImage( "EdgeOpenMP", edge2);
cvWaitKey();
cvDestroyWindow("Edge");
cvDestroyWindow("EdgeOpenMP");
cvReleaseImage(&src);
cvReleaseImage(&edge1);
cvReleaseImage(&edge2);
}
這是我的結果:這里的測試結果: http://blog.csdn.net/augusdi/article/details/8808226 在cpp文件中添加如下代碼:
- #include "stdafx.h"
- #include<omp.h>
- #include<iostream>
- usingnamespace std;
- //循環測試函數
- void test()
- {
- for(int i=0;i<10000;i++)
- {
- }
- }
- int _tmain(int argc,_TCHAR* argv[])
- {
- cout<<"這是一個串行測試程序!\n";
- double start = omp_get_wtime( );//獲取起始時間
- for(int i = 0; i < 10000; i++)
- {
- test();
- }
- double end = omp_get_wtime( );
- cout<<"計算耗時為:"<<end -start<<"\n";
- cin>>end;
- return 0;
- }
#include "stdafx.h"
#include<omp.h>
#include<iostream>
usingnamespace std;
//循環測試函數
void test()
{
for(int i=0;i<10000;i++)
{
}
}
int _tmain(int argc,_TCHAR* argv[])
{
cout<<"這是一個串行測試程序!\n";
double start = omp_get_wtime( );//獲取起始時間
for(int i = 0; i < 10000; i++)
{
test();
}
double end = omp_get_wtime( );
cout<<"計算耗時為:"<<end -start<<"\n";
cin>>end;
return 0;
}
以上代碼中紅色字體為添加的代碼,以上程序是一個典型的串行程序,經過隨機運行10次,其平均耗時約0.283273s(具體所耗時間跟測試計算機有密切的關系,測試電腦CPU采用Core I7 2630QM,4核)。
下面將其轉換成並行程序,只需要在for循環加上#pragma omp parallel for即可,如下代碼(注意紅色部分):
- #include "stdafx.h"
- #include<omp.h>
- #include <iostream>
- using namespace std;
- //循環測試函數
- void test()
- {
- for(inti=0;i<10000;i++)
- {
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout<<"這是一個並行測試程序!\n";
- doublestart = omp_get_wtime( );//獲取起始時間
- #pragma ompparallel for
- for(inti = 0; i < 10000; i++)
- {
- test();
- }
- doubleend = omp_get_wtime( );
- cout<<"計算耗時為:"<<end -start<<"\n";
- cin>>end;
- return0;
- }
#include "stdafx.h"
#include<omp.h>
#include <iostream>
using namespace std;
//循環測試函數
void test()
{
for(inti=0;i<10000;i++)
{
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"這是一個並行測試程序!\n";
doublestart = omp_get_wtime( );//獲取起始時間
#pragma ompparallel for
for(inti = 0; i < 10000; i++)
{
test();
}
doubleend = omp_get_wtime( );
cout<<"計算耗時為:"<<end -start<<"\n";
cin>>end;
return0;
}
同樣,也經過10次隨機的運行,其平均耗時約為0.06358044s,兩種不同運行方式的比較結果如下表所示:
| 次數 |
串行 |
並行 |
| 1 |
0.283382 |
0.0746704 |
| 2 |
0.283654 |
0.0686404 |
| 3 |
0.283212 |
0.0536631 |
| 4 |
0.280234 |
0.0517737 |
| 5 |
0.283041 |
0.0717588 |
| 6 |
0.283126 |
0.0524264 |
| 7 |
0.281881 |
0.0580316 |
| 8 |
0.283301 |
0.0730386 |
| 9 |
0.284545 |
0.0745088 |
| 10 |
0.286353 |
0.0572926 |
| 平均值 |
0.283273 |
0.06358044 |
兩種運行方式的結果如下圖所示:
從上面的分析結果可見,采用OpenMP並行所耗時間僅為串行的22.44%,節約近4.5倍的時間。
相關程序源碼下載地址:
http://download.csdn.net/detail/xwebsite/3843187

