Every Fortran MPI program must include the MPI header file (which contains the MPI function type declarations)
include 'mpif.h' |
CALL MPI_Xxxxx( param1, .... , ierr_code); |
Every MPI subroutine returns an integer ERROR CODE as the last parameter of the subroutine
PROGRAM Main implicit none include "mpif.h" integer :: err_code CALL MPI_Init( err_code ); ..... ..... MPI_Finalize( err_code ); |
Function Name | Usage |
---|---|
MPI_Init(int err_code) | Initialize the MPI system - must be the first statement in an MPI program |
MPI_Finalize(int err_code) | Terminates the MPI system |
MPI_Comm_size(MPI_Comm comm,
int *size, int err_code) |
Determines the size of the group associated with the communictor
comm
The size is returned in the integer variable size |
MPI_Comm_rank(MPI_Comm comm,
int *rank, int err_code) |
Determines the
rank of the calling process in the communicator
comm
The rank is returned in the integer variable rank |
MPI_Send(void *buff,
int count, MPI_Datatype type, int dest, int tag, int comm, int err_code) |
Send a point-to-point message
to process
dest
in the communication group
comm
The message is stored at memory location buff and consists of count items of datatype type The message will be tagged with the tag value tag The MPI_Send() function will only return if the message sent has been received by the destination. (It's safe to reuse the buffer buff right away). |
MPI_Recv(void *buff,
int count, MPI_Datatype type, int source, int tag, int comm, MPI_Status *status int err_code) |
Receive a point-to-point message
The message MUST BE from the process source in the communication group comm AND the message MUST BE tagged with the tag value tag The message received will be stored at memory location buff which will have space to store count items of datatype type When the function exits, the exit status is stored in status. Information about the received message is returned in a status variable, e.g.,:
In most cases, you know the structure of data received and then you can ignore the status value... If you pass NULL as status parameter, MPI will not return the status value. The MPI_Recv() function will only return if the desired message (from source with tag tag) has been received - or exits with an error code... |
|
We will therefore make processes send "hello" message to each other...
|
program Hello implicit none include 'mpif.h' integer :: mpierror, numprocs integer :: i, myid integer status(MPI_STATUS_SIZE) character*128 buff ! String... call MPI_Init(mpierror) call MPI_Comm_size(MPI_COMM_WORLD, numprocs, mpierror) call MPI_Comm_rank(MPI_COMM_WORLD, myid, mpierror) if ( myid == 0 ) then !! Master processor print *, "We have ", numprocs, " processor" do i = 1, numprocs-1 write ( buff , *) "Hello ", myid call MPI_Send(buff, 128, MPI_CHARACTER, i, 0, MPI_COMM_WORLD, & mpierror) end do do i = 1, numprocs-1 call MPI_Recv(buff, 128, MPI_CHARACTER, i, 0, MPI_COMM_WORLD, & status, mpierror) write (6, *) buff end do else !! Slave processors call MPI_Recv(buff, 128, MPI_CHARACTER, 0, 0, MPI_COMM_WORLD, & status, mpierror); write(buff, *) "Hello 0, Processor ", myid, & " is present and accounted for !" call MPI_Send(buff, 128, MPI_CHARACTER, 0, 0, MPI_COMM_WORLD, & mpierror); end if call MPI_Finalize(mpierror) end program |
To compile the program:
|
To run the program:
|