1

I am using Visual Fortran and I'm trying to implement a very simple code using MPI.

program hello_world
include 'C:\Program Files (x86)\Microsoft SDKs\MPI\Include\mpif.h'
integer ierr
call MPI_INIT ( ierr )
write(*,*) 'Hello world'
call MPI_FINALIZE ( ierr )
pause
end program hello_world

After I have defined the library and have done lots of steps that I found in many blogs I have this error:

"Error 1 error LNK2019: unresolved external symbol MPI_INIT referenced in function MAIN__ hello_world.obj"

What is the reason for that and how to come over it?

GEP75
  • 11
  • 1
  • 1
    Two comments: (i) `include 'mpif.h'` should be sufficient (not the full path) (ii) `pause` is deprecated since Fortran 95. Now, please explain how you did build the code? Please be explicit as else there is not much anyone can do. – Pierre de Buyl Nov 05 '17 at 22:15
  • The full path might be necessary if the path to the Microsoft MPI includes wasn't added to the compiler include path. – Steve Lionel Nov 06 '17 at 00:02
  • 1
    That suggests the compiler is invoked directly, and hence the MPI library is not linked to the binary. The recommended way to build MPI applications is to use the MPI wrappers (`mpicc`, `mpifort` and friends). That will both include the path to `mpif.h` and link with `-lmpi` or whatever is needed. Last but not least, you might want to consider `use mpi_f08` or at least `use mpi` instead of the legacy `include 'mpif.h'`. – Gilles Gouaillardet Nov 06 '17 at 00:37
  • You ought to tell us what exactly you are doing. What are those *"lots of steps"? How are you compiling it? Which command are you executing or what are you clicking at? Which exact software (version) are you using? @GillesGouaillardet The problem is that OpenMPI manuals were explicitly teaching people to do `include 'mpif.h'` even in version 2. Basically in all versions that are used in the wild. Sowhen one googles for `man mpi_init`, one gets an official OpenMPI example that shows mpif.h. – Vladimir F Героям слава Nov 06 '17 at 09:56
  • Yep, that is "fixed" since Open MPI v3. I'll see if i can fix the v2 series. – Gilles Gouaillardet Nov 06 '17 at 11:02

1 Answers1

1

The fundamental problem is that the entry point in the library is MPI_Init but because Fortran is not case-sensitive, and it would seem that PGI Fortran on Windows upcases all the names, MPI_INIT doesn't match MPI_Init. (I assume you are building for x64 and are linking to the x64 libraries.)

I note that MS MPI includes an MPI.f90 file that declares a module MPI. But this doesn't deal with the case issue either.

If you were using Intel (not PGI) Visual Fortran, you could add the following lines after the INCLUDE:

!DEC$ ATTRIBUTES DECORATE, ALIAS:"MPI_Init" :: MPI_INIT
!DEC$ ATTRIBUTES DECORATE, ALIAS:"MPI_Finalize" :: MPI_FINALIZE

and see if that works. PGI may have something similar. Obviously you will need to add additional directives to rename other MPI routines you call.

Steve Lionel
  • 6,972
  • 18
  • 31
  • The MPI library generally comes with both C and (several flavors of) Fortran bindings. So assuming the MPI library was built with the same compiler, that should just work as is. – Gilles Gouaillardet Nov 06 '17 at 00:34
  • I dumped the imports in the MS MPI library, and it didn't include uppercase symbols. There are three x64 libraries: msmpi.lib is a DLL import library, msmpifec.lib and msmpifmc.lib are static libraries. The latter two have only lowercase symbols (with trailing underscore), which is what gfortran would use but Intel Fortran does not. x86 has additional libraries with STDCALL interfaces. There is no MS library that works directly with Intel Fortran. – Steve Lionel Nov 06 '17 at 01:18
  • 1
    isn't there an option to generate symbols all lower case with a trailing underscore (a la gfortran) ? if not, which external symbol is invoked when your pragma is used ? it should be `mpi_init_` and **not** `MPI_Init`. btw, the OP has set the `pgi-visual-fortran` tag, so i am not sure whether he is using Intel compilers ... – Gilles Gouaillardet Nov 06 '17 at 01:29
  • I consider such options bad practice. But I just now realized that the OP says he is using PGI Visual Fortran - I had seen the Visual Fortran and assumed Intel. (I'm still irritated at PGI for copying the Intel product name.) However, my initial response still applies, assuming that PGI also copied the form of DEC/Intel directives. – Steve Lionel Nov 06 '17 at 14:37