1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Alexander, for a quick response. I am using the commands that you mentioned in the subroutine, where the MPI is initialized (and finalized). But from what you are writing, it seems that I have to initialize the MPI already in the main program and determine the rank of each process there in order to avoid double printing.
That is correct. If you are using MPI you should initialize it as soon as possible.
Sorry, Alexander. Didn't know I have to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.