Debugging & running MPI programs in Xcode 4

This tutorial discusses how to debug, compile, and run MPI programs in Xcode 4.

Install OpenMPI & Xcode

First of all, make sure you have already installed Xcode & MPI.(I’m using Xcode 4.5 and OpenMPI 1.6.1)

Xcode now available on the Mac App Store:

*this version(4.5) of Xcode has a pretty annoying bug – it only shows the first letter of input, but the variable would take in the full string

*Xcode 4.5.1 fixes the issue where the debug console failed to display some text.

Open MPI is available here :

Open MPI Installation tutorial:

Create a new Xcode Project

Let’s create a command line C project called mpi_hello. I will use the the example given at <> to demonstrate. The author of this program is Prof.Pacheco.

Change Project Build Settings

As Prof.Pacheco said in class,

“The mpicc command is just a “wrapper” for gcc. It runs gcc and makes sure that gcc knows where the MPI header files are and how to link in the MPI libraries.”

Open a new terminal window and type $ mpicc -showme

gcc -I/usr/local/include -L/usr/local/lib -lmpi -lm

Then we got the include path, library path and some other flags.

So click on the project in Xcode and then click on Build Settings and change the following:

add “/usr/local/include” to “Search Paths – Header Search Paths”

add”/usr/local/lib” to “Search Paths – Library Search Paths”

add “-lmpi -lm” to “Linking – Other Linker Flags”

the “mpi.h file not found error” should be gone now.

In order to run the program with multi processors, we need to edit the “schemes” of the project.

when we run the MPI program via command line, we use “mpiexec -n <number of processes>”

so, we need to change the “Executable” to mpiexec

mpiexec is located at “/usr/local/bin”, it’a an alias of “orterun”

“/usr/local/bin” is by default hidden in the Finder. You can press “command + shift + G” then type in “/usr/local/bin” and press “Go”. Then choose “orterun”

For running with multi processors, we also the arguments “-n <number of processors> <executable file>”.

So, click on “Arguments”, add “-n 4” and “$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH” to “Arguments Passed On Launch”

($BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH is the combination of environment variables that specify the executable file.)

Now you can run your MPI programs~!

Debugging & running MPI programs in Xcode 4》上有9条评论

    1. Yaonan Li 文章作者

      1. since my programs are really easy…(i was taking “intro to parallel computing” that time) so i used conditional compile, #ifdef DEBUG, print some debug messages. Sometimes i would also use “Breakpoints” in Xcode.

      2. you can open a new terminal window, and type which mpiexec

  1. SophieHMC

    😉 It is a great is walkthrough for running mpi code in xcode~

    Could you please also write something about debugging mpi code?
    When I set breakpoints on mph codes they seem ignored by xcode when run is hit.

    1. Yaonan Li 文章作者

      Thanks! :smile:

      I wish I could. However I only took a “intro to mpi” class. The code I wrote is really simple. I don’t really know the good ways to debug mpi code. Really sorry for that. :sad: 😥

  2. Michael

    First at all, I wanna thank you Sir, you made a great tutorial. 😀 💡
    But I have a problem and maybe you or any other found a solution. The problem is how can I use the stdin and the console in XCode. I’m using scanf(“%d”, &numblanks); ❓
    But the Console is not responding if I use XCode.
    If I starting the program from comand line with mpiexec -n 2 ./myexecutable the stdin is working perfectly.

  3. Rucha

    Hi..Thank u so much for this tutorial.It helped me get rid of mpi.h file not found.Hoever when i build my code it is giving me an error:

    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1

    Can you please help me with this?



电子邮件地址不会被公开。 必填项已用*标注