I am writing a Fortran 95 code (with gfortran
as a compiler). In one of the subroutines, I initialize the Message Passing Interface by calling MPI_Init
. I close the interface by calling MPI_Finalize
in the same subroutine. Neither in the main program nor in any other subroutines do I use MPI commands.
The code is running well; however, every WRITE(*,*) "text"
statement in the main program is executed twice when I run the code (I am testing the code on my laptop with two physical cores). So it seems that both cores are processing all the commands of the main program.
Is this what one should expect? What is the right way of initializing and finalizing the MPI?
I would like one core to process all the sequential tasks and use multicore processing only in the subroutine.
Well, this is more an extended comment than an answer...
With MPI the program is executed once for each process (in your case twice), and you have to make sure which process executes which code or operates on which chunk of data. This is usually done by determining the rank of the process by calling MPI_Comm_rank()
. Based on the rank and the number of processes obtained with MPI_Comm_size()
you can distribute the workload.
Since you do not do that, each process is doing exactly the same work, and consequently prints the same text onto the terminal.
This is an example to illustrate this:
program test
use mpi
implicit none
integer :: myrank, size, stat
! Init MPI
call MPI_Init(stat)
! Get process ID
call MPI_Comm_rank ( MPI_COMM_WORLD, myrank, stat )
! Get number of processes
call MPI_Comm_size ( MPI_COMM_WORLD, size, stat )
! This is only written by the first process
if ( myrank == 0 ) write(*,*) 'Starting output...'
! This is written by every process
write(*,*) "Hello world"
write(*,*) "I am ",myrank,"of",size
call MPI_Finalize(stat)
end program
With the output:
$ mpirun -np 2 ./a.out
Starting output...
Hello world
Hello world
I am 1 of 2
I am 0 of 2
You can see that without the check for the rank of the process, "Hello World" is printed twice.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments