1

I had to replace mpich2 with OpenMPI due to dependency of OpenFOAM on OpenMPI.

Earlier (when using mpich2) in my code I was using gethostname() function to get the name of the machine for debugging purpose. However this function does not seem to be a standard MPI function, and is not working anymore with OpenMPI libraries. Is there any other function for getting the host name in OpenMPI, or MPI standard? I am using mpicc for compiling and mpirun for running the code.

Thanks,

Sourabh

Sourabh Bhat
  • 1,793
  • 16
  • 21
  • 2
    `gethostname` is not a standard MPI function. Are you confusing it with `MPI_Get_processor_name` which is ? – High Performance Mark Mar 04 '14 at 14:40
  • Actually gethostname() used to work earlier, after installing OpenMPI it stopped working. I just checked MPI_Get_processor_name() is working, Thanks. – Sourabh Bhat Mar 04 '14 at 14:45
  • The prototype of `gethostname()` is in `unistd.h`. How exactly is it not working with Open MPI? – Hristo Iliev Mar 04 '14 at 17:51
  • I am not sure how! I had not included `unistd.h` but `gethostname()` was working when I was including only `mpi.h` and `stdio.h` The only explanation I have is that `gethostname()` might be defined inside some library in `mpich2`'s `mpi.h`. – Sourabh Bhat Mar 04 '14 at 18:55

1 Answers1

5

gethostname() is defined in unistd.h that was included by mpi.h, in the previous version. That's not a feature you should rely on, since you should always explicitly include the files which define the symbols you use. Clearly you were relying on it without realizing.

However if your MPI code is supposed to run on POSIX systems only, its safe to add

#include <unistd.h>

gethostname() is POSIX2001.1 standard.

However the MPI portable solution is MPI_Get_processor_name() as shown in the comment by High Performance Mark

Sigi
  • 4,826
  • 1
  • 19
  • 23