3

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

3

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

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • .oOo. Cool it worked for you ;) If you find this answer helpful, please consider accepting it and marking as a solution for your question ;) .oOo. – Oo.oO Aug 16 '17 at 19:19
  • Is the original poster still here? I am getting a '''ld: library not found for -lmpi.20''' error – Rio1210 Nov 27 '19 at 23:31
  • @Rio1210 - it looks like in `XCode 11.0` you have to pass library location into search path explicitly. To be honest, I haven't tried that since 2017, but I can confirm that for new `XCode` it is required to set little bit more. – Oo.oO Nov 28 '19 at 20:02
1

the "natural" way of building a MPI application is to used the MPI wrappers (e.g. mpicc and friends) instead of the native compiler (e.g. gcc / clang and friends). by doing this, mpi.h will be in the include path, and you will not need to manually link with the various MPI libraries (which is MPI implementation specific by the way)

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30