Search code examples
c++xcodeparallel-processingmpimpich

How do I use the mpi.h in Xcode?


I have installed MPICH3.2 from the following the direction in given website

http://mpitutorial.com/tutorials/installing-mpich2/

I downloaded the tar file and in Downloads/ directory and installed using terminal.I wanted to test if my program works on my local machine before I submit to the remote machine.

The problem is when I write the header #include "mpi.h" Xcode doesn't recognize it. How do I add the MPICH library to the Xcode ? or how do I make it so that Xcode compiles using mpi?


Solution

  • I, personally, would go like this (please note I never compile MPI code in XCode - I do it via Makefile, CMake).

    I would have got MPI installed in certain location (from sources) - take a look here how to do it: Running Open MPI on macOS

    Then, I'd have created supper simple MPI code (e.g. like this)

    #include <stdio.h>
    #include "mpi.h"
    
    int main(int argc, char * argv[]){
        int my_rank, p, n;
    
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &p);
        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    
        printf("size: %d rank: %d\n", p, my_rank);
    
        MPI_Finalize();
    }
    

    Then, I'd have it compiled using mpicc (as mentioned by @Gilles).

    > mpicc -v -o mpi ./mpi.c
    > mpirun -np 4 ./mpi
    size: 4 rank: 1
    size: 4 rank: 2
    size: 4 rank: 0
    size: 4 rank: 3
    

    Option -v will give you all the locations you need. Then, I'd have taken all these options into XCode.

    You can also use mpicc -show to get flags required to build MPI based code.

    In fact, it should work out of the box. Take a look here:

    You need to set includes (as at the picture)

    enter image description here

    and MPI lib (as in the picture)

    enter image description here

    and, you should be able to run it as well

    enter image description here

    To get it running using mpirun, make sure to set: Product->Scheme->Edit Scheme. And, make sure to change Arguments to

    enter image description here

    and Executable to mpirun - it should be inside bin directory where you have your MPI installed.

    enter image description here

    Update

    It looks like XCode 11.0 requires passing library location as well. In the past it was inferred from the file. Now, you have to pass it explicitly inside build settings

    enter image description here

    Once you update this location. It will work as expected.

    enter image description here