legol18
legol18

Reputation: 27

OpenMP ensemble execution

I am new to the OpenMP and at the moment with no access to my workstation where I can check the details. Had a quick question to set the basics right before moving on to the hands on part.

Suppose I have a serial program written in FORTRAN90 which evolves a map with iterations and gives the final value of the variable after the evolution, the code looks like:

 call random_number(xi) !! RANDOM INITIALIZATION OF THE VARIABLE
 do i=1,50000  !! ITERATION OF THE SYSTEM
  xf=4.d0*xi*(1.d0-xi) !! EVOLUTION OF THE SYSTEM
  xi=xf
 enddo     !! END OF SYSTEM ITERATION
print*, xf

I want to run the same code as independent processes on a cluster for 100 different random initial conditions and see how the output changes with the initial conditions. A serial program for this purpose would look like:

do iter=1,100 !! THE INITIAL CONDITION LOOP
 call random_number(xi) !! RANDOM INITIALIZATION OF THE VARIABLE
 do i=1,50000  !! ITERATION OF THE SYSTEM
  xf=4.d0*xi*(1.d0-xi) !! EVOLUTION OF THE SYSTEM
  xi=xf
 enddo     !! END OF SYSTEM ITERATION
print*, xf

Will the OpenMP implementation that I could think of work? The code I could come up with is as follows:

!$ OMP PARALLEL PRIVATE(xi,xf,i)
!$ OMP DO
  do iter=1,100 !! THE INITIAL CONDITION LOOP
   call random_number(xi) !! RANDOM INITIALIZATION OF THE VARIABLE
    do i=1,50000  !! ITERATION OF THE SYSTEM
     xf=4.d0*xi*(1.d0-xi) !! EVOLUTION OF THE SYSTEM
     xi=xf
    enddo     !! END OF SYSTEM ITERATION
  print*, xf
!$ OMP ENDDO
!$ OMP END PARALLEL

Thank you in advance for any suggestions or help.

Upvotes: 0

Views: 335

Answers (2)

Jim Cownie
Jim Cownie

Reputation: 2859

I want to run the same code as independent processes on a cluster

Then you do not want OpenMP. OpenMP is about exploiting parallelism inside a single address space.

I suggest you look at MPI, if you want to operate on a cluster

Upvotes: 0

High Performance Mark
High Performance Mark

Reputation: 78316

I think that this line

   call random_number(xi) !! RANDOM INITIALIZATION OF THE VARIABLE

might cause some problems. Is the implementation of random_number on your system thread-safe ? I haven't a clue, I know nothing about your compiler or operating system. If it isn't thread-safe then your program might do a number of things when the OpenMP threads all start using the random number generator; those things include crashing or deadlocking.

If the implementation is thread-safe you will want to figure out how to ensure that the threads either do or don't all generate the same sequence of random numbers. It's entirely sensible to write programs which use the same random numbers in each thread, or that use different sequences in different threads, but you ought to figure out that what you get is what you want.

And if the random number generator is thread safe and generates different sequences for each thread, do those sequences pass the sort of tests for randomness that a single-threaded random number generator might pass ?

It's quite tricky to generate properly independent sequences of pseudo-random numbers in parallel programs; certainly not something I can cover in the space of an SO answer.

While you figure all that out one workaround which might help would be to generate, in a sequential part of your code, all the random numbers you need (into an array perhaps) and let the different threads read different elements out of the array.

Upvotes: 1

Related Questions