Multithread vs. single thread: different results

General OpenMP discussion

Multithread vs. single thread: different results

Postby Allamarein » Wed Feb 05, 2014 7:32 am

Good morning folk. I am a newbie in OpenMB.
I would like to parallelize this code:

Code: Select all
      DO j=0,n
        CALL rk4(6, fun, y0(:,j), t0, dt, tf, tout,
     &           printout,
     &           1,checker,
     &           ender(j), tatmo(j), yatmo(:,j))
      END DO


rk4 is a Runge_Kutta 4th order to integrate the ODEs "fun" ("fun" is declared as external. "Printout" and "checker" are external subroutines, too). The y0 is a matrix, where each column is a set of initial conditions. The equations have to be integrate with stepsize "dt" until "tf". The results are stored in some matrices ("ender", "tatmo", "yatmo"). A modules contains global variable with some constants (I added threadprivate in the module).
I supposed to re-write the code in this way

Code: Select all
c$OMP PARALLEL DO DEFAULT(SHARED), PRIVATE(j)
      DO j=0,n
        CALL rk4(6, fun, y0(:,j), t0, dt, tf, tout,
     &           printout,
     &           1,checker,
     &           ender(j), tatmo(j), yatmo(:,j))
      END DO
c$ END PARALLEL DO

However, the two code lead to different results. Why?

Thank you.
Allamarein
 
Posts: 4
Joined: Tue Feb 04, 2014 3:09 am

Re: Multithread vs. single thread: different results

Postby MarkB » Wed Feb 05, 2014 11:12 am

If the subroutine rk4 internally uses global variables, or variables with the SAVE attribute, then these will be shared between threads which may result in race conditions and hence the wrong answer. If you have access to the source code for rk4 and all its dependencies, then you may be able to fix this by declaring such variables as threadprivate.
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am

Re: Multithread vs. single thread: different results

Postby Allamarein » Wed Feb 05, 2014 2:51 pm

Hi Mark.
The subroutine "rk4" calls some subroutines. Some of them are passed as arguments (e.g. "fun"). The subroutine "fun" also calls other subroutines.
I don't use SAVE attribute, because in my opinion it is not a safe mode to code.
Nevertheless, I have to resort to some global variable stored in a module, but they are constants. They are indeed called by the main program to be initialized.
Let me show you an example:

Code: Select all
MODULE Constants
REAL*8 :: Req, f
c$OMP THREADPRIVATE(Req,f)
END MODULE Constants


I have spotted a strange bug.
If I compare the results of my parametric study of 1000 cases, the first 250 results are similar in both cases (multithread or single thread), but I have a lot of differences in the following ones.
I have been puzzling over that for days, but I have some difficulties to debug that.
Allamarein
 
Posts: 4
Joined: Tue Feb 04, 2014 3:09 am

Re: Multithread vs. single thread: different results

Postby ftinetti » Thu Feb 06, 2014 4:30 am

Hi
If I compare the results of my parametric study of 1000 cases, the first 250 results are similar in both cases (multithread or single thread), but I have a lot of differences in the following ones.


How large is the difference? Take into account that the result could be different when the operations are made in a different order.

HTH,

Fernando.
ftinetti
 
Posts: 558
Joined: Wed Feb 10, 2010 2:44 pm

Re: Multithread vs. single thread: different results

Postby MarkB » Thu Feb 06, 2014 4:45 am

If Req and f are not modified inside the parallel region, then there is no need to make them threadprivate. Indeed, this could be the source of your bug, if the threadprivate copies on threads other than the master are not being initialized correctly. Either every thread has to initialize its own copy, or else you can use the copyin clause to propagate the values in the master thread to the other threads.
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am

Re: Multithread vs. single thread: different results

Postby Allamarein » Thu Feb 06, 2014 8:59 am

How large is the difference? Take into account that the result could be different when the operations are made in a different order.


A lot. If you were right, I would find a different order of the solutions, but each case has only one correct solution.

If Req and f are not modified inside the parallel region, then there is no need to make them threadprivate. Indeed, this could be the source of your bug, if the threadprivate copies on threads other than the master are not being initialized correctly. Either every thread has to initialize its own copy, or else you can use the copyin clause to propagate the values in the master thread to the other threads.


You are right Mark, R and f are constants. They are initialized before the parallel region. I did't know the COPYIN clause.
How should I modify the following code?
Code: Select all
PROGRAM main
USE CONSTANTS
IMPLICIT NONE
[i]The list of variables[/i]
Req = 1
f     = 5
DO j=0,n
c$OMP PARALLEL DO DEFAULT(SHARED), PRIVATE(j)
      DO j=0,n
        CALL rk4(6, fun, y0(:,j), t0, dt, tf, tout,
     &           printout,
     &           1,checker,
     &           ender(j), tatmo(j), yatmo(:,j))
      END DO
c$ END PARALLEL DO
[i]I write my results in a file[/i]
STOP
END PROGRAM

The threadprivate could be needless, but not "harmful".
It may be that "fun" passed as external irritates OpenMP?
Allamarein
 
Posts: 4
Joined: Tue Feb 04, 2014 3:09 am

Re: Multithread vs. single thread: different results

Postby MarkB » Thu Feb 06, 2014 9:54 am

Allamarein wrote:How should I modify the following code?


Code: Select all
    PROGRAM main
    USE CONSTANTS
    IMPLICIT NONE
    [i]The list of variables[/i]
    Req = 1  ! This assigns to the master thread's copy only
    f     = 5  ! ...as does this
    DO j=0,n
    c$OMP PARALLEL DO DEFAULT(SHARED), PRIVATE(j), COPYIN(Req,f)
          DO j=0,n
            CALL rk4(6, fun, y0(:,j), t0, dt, tf, tout,
         &           printout,
         &           1,checker,
         &           ender(j), tatmo(j), yatmo(:,j))
          END DO
    c$ END PARALLEL DO
    [i]I write my results in a file[/i]
    STOP
    END PROGRAM
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am

Re: Multithread vs. single thread: different results

Postby Allamarein » Fri Feb 07, 2014 2:28 am

Tank you, Mark.
I am going to try this new version.
Maybe it is a typo, but I cannot understand why you build two do cycles.

Code: Select all
    DO j=0,n
    c$OMP PARALLEL DO DEFAULT(SHARED), PRIVATE(j), COPYIN(Req,f)
          DO j=0,n
            CALL rk4(6, fun, y0(:,j), t0, dt, tf, tout,
         &           printout,
         &           1,checker,
         &           ender(j), tatmo(j), yatmo(:,j))
          END DO
    c$ END PARALLEL DO


Shall I delete the first line, do j = 0,n?

By the way, shall I delete THREADPRIVATE from the module where R and f are declared?

Thankl you for your support!
Allamarein
 
Posts: 4
Joined: Tue Feb 04, 2014 3:09 am

Re: Multithread vs. single thread: different results

Postby MarkB » Fri Feb 07, 2014 3:14 am

Allamarein wrote:Maybe it is a typo, but I cannot understand why you build two do cycles.

Shall I delete the first line, do j = 0,n?


The typo was there in the code you posted: so, yes, just delete it!

Allamarein wrote:By the way, shall I delete THREADPRIVATE from the module where R and f are declared?


Yes, you can delete the THREADPRIVATE declaration in the module and the COPYIN clause. Since the variables are read-only inside the parallel region, having them shared by default is not a problem.

Allamarein wrote:Thankl you for your support!


You're very welcome!
Mark.
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 4 guests