How I Set Up OpenMP for Mac

Basic Information

  • Mac Pro: OS X Yosemite Version 10.10.5

  • Clang: Apple LLVM version 7.0.2 (clang-700.1.81)

  • HomeBrew

  • Already installed X-Code and NetBeans

Update Your Compiler

The original compiler gcc/g++ probably does not support OpenMP. Most likely, you will receive an error like the following.

omp_hello.c:11:10: fatal error: 'omp.h' file not found
#include 
         ^
1 error generated.

So first, you need to update your compiler. Here, I am using HomeBrew. The following command lines will get it done. Use the second one if you have already installed gcc.
brew install gcc --without-multilib
or
brew reinstall gcc --without-multilib

By default, your HomeBrew will install all its packages under the directory /usr/local/Cellar/. You can inspect this by using brew list and brew info [package name]. For me and for now, HomeBrew has installed gcc with version 6.3.0_1, so I will be using compiler g++-6/gcc-6. The system links should have been automatically created. To inspect this, you can run ls -l /usr/local/bin | grep gcc and you will find a system link for gcc-6. The same for the another.

==The point here is to use the newest version of gcc/g++ compiler, rather than the original one in Apple's clang version==. For some reason, you will have to build or update your system links manually. You can do this by using the command ln and the parameter -s.

Reference: C OpenMP parallel for loop is not running on more than one thread

Download and Make the Actual Library

Go to the official website for OpenMP download. Follow the instruction online, and build/make the library. Here, I build my library in the out-of-tree mode, and for the DCMAKE_C_COMPILER and DCMAKE_CXX_COMPILER parameters, I both chose gcc.

After you have built the library, follow the instruction here to test the validity of your configuration. If it worked, you are so lucky!

But for most of the cases, you will find that your code is running with only one thread. Here are some tips for possible solutions.

Only One Thread ?

First of all, check if you have added the critical parameter -fopenmp to both of your compiler and library linker. This is like an open switch that enables them to support OpenMP. And annoyingly, if you simply ignore this step, you will not get any error from the parallelized code. And don't forget to specify the paths to the header file and the library using -I and -L. This is detailed in the aforementioned instruction.

Then the problem is possibly caused by your IDE. If you are using Visual Studio, you will need to switch on the IDE language support for OpenMP in the software preference. Please google it. If you are using NetBeans, you probably need to define your own Tool Collection with reference from here. The most important thing here is that you need to specify your are using the newest compiler, for example, for me the C++ Compiler will be g++-6. You can also try gcc-6, but it doesn't work for me. After you have defined your own collection, don't forget to tell your project to use this collection, and to use the newest compiler. For me, they are File -> Project Property -> Build -> C++ Compiler -> Tool -> Tool -> g++-6 and File -> Project Property -> Build -> Linker -> Tool -> Tool -> g++-6.

That's it. Have fun. Please leave me a message if you have comments or other suggestions. Thank you!

1/8/2017 Weiming

你可能感兴趣的:(openmp)