user8334023
user8334023

Reputation:

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?

Upvotes: 3

Views: 5796

Answers (2)

Oo.oO
Oo.oO

Reputation: 13375

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

Upvotes: 4

Gilles Gouaillardet
Gilles Gouaillardet

Reputation: 8380

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)

Upvotes: 1

Related Questions