如何在 VS2008下使用Openmp编程

如何在 VS2008下使用Openmp编程

如何建立Openmp编程环境
    OpenMP在Windows环境下比较容易实现,只要打开VS2008中的编译选项/openmp,设置一下环境变量
OMP_NUM_THREADS就可以了.一般是新建一个c++项目,以次选择项目->(*)属性->配置属性 ->c"c++->语言,打开OpenMP支持;设置环境变量,我的电脑->属性->高级->环境变量,新建一个 OMP_NUM_THREADS变量,值设为2,即为程序执行的线程数.
    至于其它环境变量,在使用的时候我们再设置就可以了,所以暂时不考虑.
    这样,就可以进行OpenMP程序设计了.
 2     #include  < omp.h >  3  4      int  main()  5      {  6         omp_set_num_threads( 2 );  7      #pragma  omp parallel  8         printf( " Hello from Thread NO.%d " n " ,omp_get_thread_num());  9          return   0 ; 10     }

比如我们可以用下面的程序来测试一下:

 1 #include  < stdio.h >
 2 #include  < omp.h >
 3
 4 int  main()
 5 {
 6         omp_set_num_threads( 2 );
 7      #pragma  omp parallel
 8         printf( " Hello from Thread NO.%d/n " ,omp_get_thread_num());
 9          return   0 ;
10     }

 

程序运行结果为:
    Hello from Thread NO.0
    Hello from Thread NO.1

 

 

==============================================================================

OpenMP on Visual Studio

You can create C++ application in Visual Studio that use OpenMP. When you run an application created with OpenMP and VS.NET, however, you may get this annoying error message: “This application has failed to start because vcompd.dll was not found. Re-installing the application may fix this problem.”:

omp-error

When we tried this, we were puzzled by this error message, especially since it works with the Intel Compiler flawlessly. Well, it turns out that you need to include omp.h in your files ALWAYS when you use OpenMP from Visual Studio. This is not required on other compilers if you’re only using the OpenMP pragmas, but it is an issue with Visual Studio.

Thanks to Kang Su for pointing this out in his blog – I was going crazy trying to figure out what was wrong.

Also remember to enable OpenMP support in the C++ Project properties. This setting is in Configuration Properties->C/C++->Language->OpenMP Support .

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章