OpenMP DO loop SCHEDULE and PRIVATE

General OpenMP discussion

OpenMP DO loop SCHEDULE and PRIVATE

Postby trav9 » Thu Jun 14, 2012 8:29 am

I am trying to create a fortran code that will use openMP amongst different cores in order to solve a simple do loop calculation. I want to be able to dynamically allocate the number of threads and monitor which threads are doing what. I am having trouble in using the PARALLEL DO along with both SCHEDULE and PRIVATE. The code runs as is, but won't once I uncomment the p,d, and id lines in order to monitor the code. Also, I am still relatively new to this so I am not sure whether the number of threads is actually being dynamically set to the maximum number possible. Any help would be appreciated.


Code: Select all
PROGRAM TEST
use omp_lib          ! Fortran 95; omp_get_thread_num, omp_get_num_threads
implicit none

    ! Serial Region  (master thread)
    ! Parameters of the Application
   
    ! OpenMP Parameters
    REAL*8 id, sum, t, s, r, z, w
    integer i, j, nthreads, chunk
    logical p, d
    w = 3
   
    ! Master thread obtains information about itself and its environment.
    !nthreads = omp_get_num_threads()       ! get number of threads
    !id = omp_get_thread_num()              ! get thread
    !WRITE (*,*) 'Serial NTHREADS: ',nthreads

    !$OMP PARALLEL DO SCHEDULE(DYNAMIC) PRIVATE(id, nthreads,p,d)
       !nthreads = omp_get_num_threads()    ! get number of threads
       !p = OMP_IN_PARALLEL() 
       !d = OMP_GET_DYNAMIC()
       !id = omp_get_thread_num()          ! get thread 
          
       do j = 0,100,2
         do i = 0,100,2
            t = t + i
            s = s+i/2
            r = 4/3*3.141592*i**1.5
            z = w/(i+1)
            write(*,*) 'i,j =', i,j
            write(*,*) 't =', t
            write(*,*) 's =', s
            write(*,*) 'r =', r
            write(*,*) 'z =', z
            !write (*,*) 'thread =', id
         enddo
       enddo 
           
       !WRITE (*,*) 'Parallel NTHREADS: ',nthreads
       !WRITE (*,*) 'This loop is in parallel: ',p   
       !WRITE (*,*) 'This loop is dynamically threaded: ',d
    !$OMP END PARALLEL DO
   
END PROGRAM TEST
trav9
 
Posts: 7
Joined: Wed Jun 13, 2012 3:01 pm

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby ftinetti » Thu Jun 14, 2012 8:53 am

Hi trav9,

The problem is that you are "applying" an OMP PARALLEL DO to something that is not a DO if you uncomment the line

!nthreads = omp_get_num_threads() ! get number of threads

Instead, I'd use
Code: Select all
    !$OMP PARALLEL PRIVATE(id, nthreads, p, d, j, i)
       nthreads = omp_get_num_threads()         ! get number of threads
       p        = OMP_IN_PARALLEL()
       d        = OMP_GET_DYNAMIC()
       id       = omp_get_thread_num()          ! get thread

       !$OMP DO SCHEDULE(DYNAMIC)
       do j = 0,100,2
       ...

Note, however, that I've added j, i to the list of private variables, but that may be not necessary.

I'm curious: does the code compile otherwise? I've uncommented the first assignment and the compiler identifies the problem:

$ gfortran -fopenmp test.f90 -o test
test.f90:20.66:

nthreads = omp_get_num_threads() ! get number of threads
1
Error: Unexpected assignment statement at (1)


HTH,

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

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby trav9 » Thu Jun 14, 2012 9:57 am

Thanks that seems to have done the trick. The code compiles both when i,j are added to private variables and when they aren't. I have adjusted my code to look like so:

Code: Select all
PROGRAM TEST
use omp_lib          ! Fortran 95; omp_get_thread_num, omp_get_num_threads
implicit none

    ! Serial Region  (master thread)
    ! Parameters of the Application   

    ! OpenMP Parameters
    REAL*8 id, sum, t, s, r, z, w
    integer i, j, nthreads, chunk
    logical p, d
    w = 3
   
    ! Master thread obtains information about itself and its environment.
    !nthreads = omp_get_num_threads()       ! get number of threads
    !id = omp_get_thread_num()              ! get thread
    !WRITE (*,*) 'Serial NTHREADS: ',nthreads

    !$OMP PARALLEL PRIVATE(id, nthreads,p,d)
       nthreads = omp_get_num_threads()    ! get number of threads
       p = OMP_IN_PARALLEL() 
       d = OMP_GET_DYNAMIC()
       id = omp_get_thread_num()          ! get thread 
       
    !$OMP DO SCHEDULE(DYNAMIC)
          
       do j = 0,1000,2
         do i = 0,100,2
            t = t + i
            s = s+i/2
            r = 4/3*3.141592*i**1.5
            z = w/(i+1)
            write(*,*) 'i,j =', i,j
            write(*,*) 't =', t
            write(*,*) 's =', s
            write(*,*) 'r =', r
            write(*,*) 'z =', z
            write (*,*) 'thread =', id
         enddo
       enddo 
       
    !$OMP END DO 
       
       WRITE (*,*) 'Parallel NTHREADS: ',nthreads
       WRITE (*,*) 'This loop is in parallel: ',p   
       WRITE (*,*) 'This loop is dynamically threaded: ',d
   
    !$OMP END PARALLEL
     
END PROGRAM TEST


The only problem now is that I don't think that the DO section is being dynamically adjusted for thread number. The logic statement in the given code is returning a FALSE. I moved it inside of the DO section inside of the do loops and it still returned a FALSE statement. The code is definitely showing that 4 threads are being used, but still saying it isn't DYNAMIC.
trav9
 
Posts: 7
Joined: Wed Jun 13, 2012 3:01 pm

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby trav9 » Thu Jun 14, 2012 2:05 pm

Also, a recurring thing I am noticing is that threading the code doesn't seem to be speeding it up any. I have a separate equivalent code that runs serially. When I run a timer on both of them they always come out to be about the same amount in real time. This is unaffected by say changing the size of the loops. Any advice on what may be happening?
trav9
 
Posts: 7
Joined: Wed Jun 13, 2012 3:01 pm

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby ftinetti » Thu Jun 14, 2012 2:11 pm

Hi,

I apologize, I lost a very important issue in your code: there are race conditions on t, s, r, and z, unless they are private... the rest are minor details. Also, I do not understand
The only problem now is that I don't think that the DO section is being dynamically adjusted for thread number. The logic statement in the given code is returning a FALSE. I moved it inside of the DO section inside of the do loops and it still returned a FALSE statement. The code is definitely showing that 4 threads are being used, but still saying it isn't DYNAMIC.

More specifically:
  • OMP_IN_PARALLEL() should return TRUE unless you are not compiling with the proper OpenMP flag for your compiler.
  • OMP_GET_DYNAMIC() is not directly related to SCHEDULE(DYNAMIC).
    • OMP_GET_DYNAMIC() is related to the number of threads that will be in a parallel region (see section 2.4.1 of the Spec. V 3.1)
    • SCHEDULE(DYNAMIC) is related to the loop iterations assignment to threads (see section 2.5.1.1 of the Spec. V 3.1).

HTH,

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

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby MarkB » Fri Jun 15, 2012 1:27 am

trav9 wrote:Also, a recurring thing I am noticing is that threading the code doesn't seem to be speeding it up any. I have a separate equivalent code that runs serially. When I run a timer on both of them they always come out to be about the same amount in real time. This is unaffected by say changing the size of the loops. Any advice on what may be happening?



I expect that pretty much all the time in your code is spent doing the I/O: this will likely be serialised at some point inside the Fortran I/O library.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: OpenMP DO loop SCHEDULE and PRIVATE

Postby trav9 » Fri Jun 15, 2012 6:52 am

Thanks that pretty much answers all of my questions. The Spec. is very useful as well. Definitely going to help me as I learn more.
trav9
 
Posts: 7
Joined: Wed Jun 13, 2012 3:01 pm


Return to Using OpenMP

Who is online

Users browsing this forum: ftinetti, Google [Bot], Yahoo [Bot] and 7 guests