LAM/MPI logo

LAM/MPI General User's Mailing List Archives

  |   Home   |   Download   |   Documentation   |   FAQ   |   all just in this list

From: volcanofire2003 (volcanofire2003_at_[hidden])
Date: 2005-11-08 13:35:48


Dear Jeff Squyres and other guys,
The current version of MPI in our computer is mpich-1.2.4, not MPI-2.

Question1. Is there solution to my problem in mpich-1.2.4?

Question2. Some friends suggest me to transfer variable of MPI created in
FORTRAN to C, but I am not very clear about it. What's your opinion about
this idea?

Thank you so much,

FENG
-----Original Message-----
From: Jeff Squyres [mailto:jsquyres_at_[hidden]]
Sent: 2005Äê11ÔÂ7ÈÕ 20:43
To: General LAM/MPI mailing list
Subject: Re: LAM: Can I use MPI in C and Fortran files at a same time?

On Nov 7, 2005, at 7:30 PM, volcanofire2003 wrote:

> My main file programs using Fortran, of course MPI_init etc. are
> performed in this Fortran file. Then this main Fortran file needs to
> call a C file in which there are some MPI-communications.
> How to solve this problem? You know that the MPI variables initialized
> in Fortran, but C file does not know how to communicate among
> processors.

In general, yes, it is perfectly acceptable to have an MPI application
that has some parts in C and some parts in Fortran -- and both parts
can use MPI. If you are using Fortran 90, be sure to read the second
half of the language bindings chapter in MPI-2.

You may need to use the MPI_*_f2c and MPI_*_c2f routines to pass
handles between the two languages, however. For example, if you create
an MPI_Comm in Fortran and want to use it in C, you need to receive the
Fortran comm as an (int*) in C (assuming that sizeof(INTEGER) ==
sizeof(int)), and then call MPI_Comm_f2c() on it. For example:

-----
int my_c_function(int *f_comm) {
   int size, rank;
   MPI_Comm c_comm = MPI_Comm_f2c(*f_comm);
   MPI_Comm_rank(c_comm, &rank);
   MPI_Comm_size(c_comm, &size);
   printf("Hello world, I am %d of %d Fortran world\n", rank, size);
   /* do not MPI_Comm_free(&c_comm) here; it is the same communicator
      that you still have in fortran; just a different handle to it --
      so freeing it here would also free it in fortran */
}
-----

Typed off the top of my head -- pardon any typos. :-)

You may need to play some tricks to make a C function callable by
Fortran (i.e., the 4 different symbol conventions of Fortran
compilers), but a) if you're only working with one Fortran compiler,
then you can customize for that and not worry about portability, and b)
it's not really an MPI problem, so I won't go into it here. :-)

-- 
{+} Jeff Squyres
{+} The Open MPI Project
{+} http://www.open-mpi.org/