如何在 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 .

 

 

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