0

I want to use Fortran with MPI to do the resample with replacement on an array DATA(10^7). I want to do 1000 times resample with replacement, and plan to split these 1000 tasks across 10 processors.

These are some of my codes:

real :: DATA(10^7), DATA_resample(10^7)
integer :: org_index_int(10^7)
integer :: new_index_int(10^7)
real :: new_index_real(10^7)

call MPI_INIT(ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, mid, ierror)

call random_init(.true., .true.)

! resample with replacement
! for myid=0 : irep_beg, irep_end = 1, 100
! for myid=9 : irep_beg, irep_end = 901, 1000
do irep=irep_beg, irep_end
   call random_number(new_index_real)
   new_index_int=floor(new_index_real*10^7)+1
   DATA_resample=DATA(new_index_int)
end do

call MPI_FINALIZE(ierror)

When I use MPI, "random_num" gives the exact same array "new_index_real" across all processors. But I want each of the 1000 resamples to be independent. What could I do make this work?

6
  • Can you explain what you intend with call random_init(.true., .true.)? Commented Jun 3, 2024 at 10:10
  • My understanding is that, the 1st argument determines whether the results (aka the random numbers) are repeatable or not, when running the whole program multiple times. So the 1st ".true." represents the results are repeatable. I am not sure about the 2nd argument, which is related to a term called "image", which I do not understand. I thought it is a standard practice to first run "random_init", and then "random_number" Commented Jun 3, 2024 at 10:27
  • If you don't understand "image" you can safely ignore the second argument (it relates to the form of parallelization that is part of Fortran itself). But on the first, if you're asking for every copy of the program to have a repeatable stream, then doesn't that also mean every copy when run at the same time (the MPI approach) will have the same stream? Commented Jun 3, 2024 at 10:38
  • 1
    Simply using .false. for the first argument isn't a solution to your problem in general (although worth a try). Random numbers in a parallel program is a big topic. Commented Jun 3, 2024 at 10:40
  • I should mentioned in my post, that I have tried "call random_init(.false., .true.)". When "call random_init(.true., .true.)" is used, each time I run "mpirun -n 2 ./a.out", the results are the same for different runs. When "call random_init(.false., .true.)" is used, each time I run "mpirun -n 2 ./a.out", the results are different for different runs. But in both cases, the results for the 1st processor are exactly the same as those for the 2nd processor, which is not what I want. I want each processor has independent samples. Commented Jun 3, 2024 at 10:50

0

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.