Hi all,

 I got a problem when call MPI subroutines in a loop. For example, I have Fortran codes to randomly 
select 10 points in a 2D space domain and change the values at those points near these 10 points to -10:

 real A( (100*rank+1):(100*rank+100),100 )
 real inmax(2),outmax(2)
 integer maxlocation(2),maxrank
  

call random_number(A)   
maxlocation=maxloc(A);  !!! find the coordinates of the local maximum;
inmax(1)=maxval(A);        !!! get the local maximum value
inmax(2)=myrank;            !!!! put the process rank
  do i=1, 10
   
       call MPI_allreduce(inmax,outmax,1,mpi_2real, mpi_maxloc,MPI_comm_world,error)  !!!get the global maximum and the corresponding rank 
       maxrank=outmax(2)
       call MPI_Bcast(maxlocation,2,mpi_integer,maxrank,mpi_comm_world,error);
       ...
       let points around maxlocation within distance of 10 equal to -10;
       ....
  enddo

  
The problem is there is runtime error like " segmentation fault".
But If I put the codes within the loop into a subroutine, then write the code as
 do i=1,10
     call subroutine

 enddo

 there will be no error.

  Another problem is MPI_allreduce seems not as efficient as the combined use of " MPI_reduce & MPI_Bcast" to realize the same purpose.


Ben