tags:

views:

27

answers:

2

my program is as follows:
module x
use mpi !x includes mpi module
implicit none
...
contains

subroutine do_something_with_mpicommworld  

    !use mpi !uncommenting this makes a difference (****)  
    call MPI_...(MPI_COMM_WORLD,...,ierr)  

end subroutine  

...
end module x

program main
use mpi use x
MPI_INIT(...)
call do_something_with_mpicommworld end program main

This program fails with the following error: MPI_Cart_create(199): Invalid communicator, unless the line marked with (**) is uncommented.

Now, maybe my knowledge of Fortran 90 is incomplete, but i thought if you have a use clause in the module definition (see my module x), whichever global variable exists in the included module (in case of x : MPI_COMM_WORLD from include module mpi) will have the same value in any of the contained subroutines ( do_something_with_mpicommworld ) even when those subroutines do not explicitly include the module (e.g. when (**) is commented out). Or, to put it simply, if you include a module within another module, the subroutines contained in the second module will have access to the globals in the included module without a special use statement.

When I ran my programme, I saw a different behaviour. The sub contained in x was creating errors unless it had the 'use mpi' statement.

So what is the problem, do I have a wrong idea about Fortran 90, or is there something special about MPI module which induces such behaviour?

A: 

Its annoyingly hard to find exact details about what should and shouldn't happen in these cases, and my expectation was the same as yours -- the `use mpi' should work as above. So I tried the following:

module hellompi
use mpi
implicit none
contains

subroutine hello
    integer :: ierr, nprocs, rank
    call MPI_INIT(ierr)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, ierr)
    call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
    print *, 'Hello world, from ', rank, ' of ', nprocs
    print *, MPI_COMM_WORLD
    call MPI_FINALIZE(ierr)
    return
end subroutine hello

end module hellompi

and it works fine under both gfortran and ifort with OpenMPI. Adding a cart_create doesn't change anything.

What strikes me as weird with your case is that it isn't complaining that MPI_COMM_WORLD isn't defined -- so obviously some of the relevant information is being propagated to the subroutine. Can you post a simpler full example which still fails to work?

Jonathan Dursi
A: 

Thank you Johnatan for your answer. The problem was really, really simple. I added the subroutine in question after the "end module"
:-D, 'implicit none' did not apply to now external sub and compiler happily initialised a brand new variable MPI_COMM_WORLD to whatever it thought suitable following the standard implicit rules.

This is just a lesson to me to enforce 'implicit none' not only by keywords, but also via the compiler flag. Evil lurks after every end statement.

I'm sorry you went trough the trouble of making the test example, I'd buy you a beer if I could :-)

txr