OpenMP: VS2010 is configured to use OpenMP

A simple OpenMP example

       First start VisualStudio 2010 and create a new C++ console application, as shown below:

       Then select the project name on the project solution explorer, right-click, and select "Properties", as shown in the following figure:

       Then select "Configuration Properties" - "C/C++" - "Language" on the left side of the property page, and then select "Yes (/openmp)" after "OpenMP Support" on the right side, as shown in the following figure:

       Add the following code to the cpp file:

[cpp]  view plain copy  
  1. #include "stdafx.h"  
  2.   
  3. #include<omp.h>  
  4.   
  5. #include<iostream>  
  6.   
  7. usingnamespace std;  
  8.   
  9.   
  10. //loop test function  
  11. void test()  
  12. {  
  13. for(int i=0;i<10000;i++)  
  14. {  
  15.   
  16. }  
  17. }  
  18.   
  19.   
  20. int _tmain(int argc,_TCHAR* argv[])  
  21. {  
  22. cout<< "This is a serial test program!\n" ;  
  23. double  start = omp_get_wtime( ); //Get the start time  
  24.   
  25. for(int i = 0; i < 10000; i++)  
  26. {   
  27. test();  
  28. }  
  29.   
  30. double end = omp_get_wtime( );  
  31.   
  32. cout<<"计算耗时为:"<<end -start<<"\n";  
  33.   
  34. cin>>end;  
  35.   
  36. return 0;  
  37. }  

       以上代码中红色字体为添加的代码,以上程序是一个典型的串行程序,经过随机运行10次,其平均耗时约0.283273s(具体所耗时间跟测试计算机有密切的关系,测试电脑CPU采用Core I7 2630QM,4核)。

       下面将其转换成并行程序,只需要在for循环加上#pragma omp parallel for即可,如下代码(注意红色部分):

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2.   
  3. #include<omp.h>  
  4.   
  5. #include <iostream>  
  6.   
  7. using namespace std;  
  8.   
  9.   
  10. //循环测试函数  
  11. void test()  
  12. {  
  13. for(inti=0;i<10000;i++)  
  14. {  
  15.   
  16. }  
  17. }  
  18.   
  19. int _tmain(int argc, _TCHAR* argv[])  
  20. {  
  21. cout<<"这是一个并行测试程序!\n";  
  22.   
  23. doublestart = omp_get_wtime( ); //Get the start time  
  24.   
  25.   
  26. #pragma ompparallel for  
  27. for (inti = 0; i <10000; i ++)   
  28. {  
  29. test();  
  30. }  
  31.   
  32.   
  33. doubleend = omp_get_wtime( );  
  34.   
  35. cout<< "Calculation time is:" <<end -start<< "\n" ;  
  36.   
  37. cin>>end;  
  38.   
  39. return0;  
  40. }  
       Similarly, after 10 random runs, the average time is about 0.06358044s. The comparison results of the two different running modes are shown in the following table:

frequency

serial

parallel

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

average value

0.283273

0.06358044

       The results of the two modes of operation are shown in the following figures:

       From the above analysis results, it can be seen that the time consumed by using OpenMP in parallel is only 22.44% of that in serial, which saves nearly 4.5 times the time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325968105&siteId=291194637