jeudi 6 mai 2021

Get different random number on different processes fortran x mpi

I am trying to set an array of 100 seeds so I don't have to set the seed every time. However, it seems only the first one works. I get a different random number on each processor, but only the first time. Am I misunderstanding how the array of seeds works?

program main                                                                                                                            
                                                                                                                                        
  use mpi                                                                                                                               
  integer :: ierr, idnum, numprocs                                                                                                      
                                                                                                                                        
  integer stat(MPI_STATUS_SIZE)                                                                                                         
                                                                                                                                        
  real :: data                                                                                                                          
  integer :: n = 100 !number of random seeds per processor                                                                              
                                                                                                                                        
  integer, dimension(:), allocatable :: seed                                                                                            
                                                                                                                                        
                                                                                                                                        
  call MPI_INIT(ierr)                                                                                                                   
  call MPI_COMM_RANK(MPI_COMM_WORLD, idnum, ierr)                                                                                       
  call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)                                                                                    
                                                                                                                                        
  allocate(seed(n))                                                                                                                     
  do i = 1,n                                                                                                                            
     seed(i) = i + idnum + time()                                                                                                          
  end do                                                                                                                                
                                                                                                                                        
                                                                                                                                        
  call random_seed(put=seed)                                                                                                            
  call random_number(data)                                                                                                                                      
  write(*,*) "Processor: ", idnum, " Random Number [0,1] ", data
  !Right here the seed fails even though I allocated 100 seeds??
  call random_number(data)                                                                  
  write(*,*) "Processor: ", idnum, " Random Number [0,1] ", data    
  call MPI_FINALIZE(ierr)

end program main                                                                

Output:

 Processor:            1  Random Number [0,1]   0.237901032    
 Processor:            1  Random Number [0,1]    7.62939453E-06
 Processor:            2  Random Number [0,1]   0.692074537    
 Processor:            2  Random Number [0,1]    7.62939453E-06
 Processor:            3  Random Number [0,1]   0.642463863    
 Processor:            3  Random Number [0,1]    7.62939453E-06



Aucun commentaire:

Enregistrer un commentaire