[openMP] OpenMP在visual studio和mac上的配置


今天弄了半天才弄好mac上的openmp,一方面智商下限,另一方面竟然發現網上也沒有什么詳細過程,特意把我的配置過程貼上來

多核編程可以認為是對多線程編程做了一定程度的抽象,提供一些簡單的API,使得用戶不必花費太多精力來了解多線程的底層知識,從而提高編程效率。這兩天關注的多核編程的工具包括openMP和TBB。按照目前網上的討論,TBB風頭要蓋過openMP,比如openCV過去是使用openMP的,但從2.3版本開始拋棄openMP,轉向TBB。但我試下來,TBB還是比較復雜的,相比之下,openMP則非常容易上手。因為精力和時間有限,沒辦法花費太多時間去學習TBB,就在這里分享下這兩天學到的openMP的一點知識,和大家共同討論。

 

openMP支持的編程語言包括C語言、C++和Fortran,支持OpenMP的編譯器包括Sun Studio,Intel Compiler,Microsoft Visual Studio,GCC。

Microsoft Visual Studio上OpenMP的配置

總共分2步: 

  • 新建一個工程。這個不再多講。
  • 建立工程后,點擊 菜單欄->Project->Properties,彈出菜單里,點擊 Configuration Properties->C/C++->Language->OpenMP Support,在下拉菜單里選擇Yes。 至此配置結束。

Using clang-omp with Xcode

  • Install clang-omp using homebrew:
  • brew install clang-omp
  • Create a new Xcode project.
  • Under click 'the name of the project' —> Build Settings
    • Editor --> Add Build Setting --> Add User-Defined Setting (Press ‘delete’ on the keyboard to delete the user-defined setting when you do not want it)

 

      • set the setting name as CC 
      • set its value as /usr/local/bin/clang-omp

 

    • Add -fopenmp to Other C Flags
    • Add /usr/local/include to Header Search Paths
    • Set Enable Modules (C and Objective-C) to No.
  • Under Build Phases
    • Add /usr/local/lib/libiomp5.dylib to Link Binary With Libraries
  • Done. You can now #include <libiomp/omp.h> and start using #pragma omp ... in your source code.

 

測試編譯器和環境配置是否成功的代碼如下:

#include <omp.h>
#include <stdio.h> 
int main() { 
  #pragma omp parallel 
  printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
/* OUTPUT:
Hello from thread 0, nthreads 4
Hello from thread 3, nthreads 4
Hello from thread 2, nthreads 4
Hello from thread 1, nthreads 4
Program ended with exit code: 0
*/

You should see more than one "Hello" line with different thread numbers. Note that the lines may be mixed together. If you see only one, try setting the environment variable OMP_NUM_THREADS to some number (say 4) and try again.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM