Threads

General OpenMP discussion

Threads

Postby Regal » Wed May 22, 2013 11:04 am

Hi, I am quite new to OpenMP and I am trying to understand the concepts.

I have already used OpenMP instructions to parallelize a part of a Fortran program but I am not sure if I understand the function of the threads. By playing with the number of the threads (hardcoded inside the program) I see that there is no significant difference in speed. In fact, I would expect that using one thread would get me back to my serial setting (speed-wise). However, even with one thread, the code runs as fast as with 2,4, ..., 10 or more threads, and much faster than the serial version. After some point in thread number I see a slowdown.

Can anyone explain what all this means? The machine on which the code runs has 64 quad-core CPU's.

Thanks!
Regal
 
Posts: 15
Joined: Wed May 22, 2013 3:04 am

Re: Threads

Postby MarkB » Thu May 23, 2013 3:24 am

Hi there,

A few questions for you, to help us try to figure out what's going on:

What compiler and compiler optimisation options are you using to build the serial and parallel versions?
What are you using to measure the execution time?
Please could you post the execution times you observed for the serial and parallel versions?

Many thanks,
Mark.
MarkB
 
Posts: 481
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Threads

Postby Regal » Thu May 23, 2013 4:54 am

Here are my settings.

Compiler: gfortran 4.4.3.
Optimisation options: none; compilation of parallel code done with the option -fopenmp.
Execution time: measured with the shell command "time" on the script that launches the binaries. Serial: ~139 min, parallel (even with just one thread): 2.2 min.

It is obvious that the execution time in parallel mode is, more or less, the time in serial mode divided by the number of CPU's that are present in the system. I am very happy with the speed boost, but I don't understand what's going on.
Regal
 
Posts: 15
Joined: Wed May 22, 2013 3:04 am

Re: Threads

Postby MarkB » Thu May 23, 2013 6:06 am

Are you printing out the number of threads used from inside the program? How are you setting the number of threads? If, for example, you misspell OMP_NUM_THREADS, the number of threads may default to the number of cores.

And it's always worth using optimisation flags if you care about performance!
MarkB
 
Posts: 481
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Threads

Postby Regal » Thu May 23, 2013 6:22 am

MarkB wrote:Are you printing out the number of threads used from inside the program? How are you setting the number of threads? If, for example, you misspell OMP_NUM_THREADS, the number of threads may default to the number of cores.

The number of threads is defined inside the program by calling the appropriate subroutine. My code goes like:

Code: Select all
      CALL OMP_SET_NUM_THREADS(4)
!$OMP PARALLEL PRIVATE(K,TID)
      TID = OMP_GET_THREAD_NUM()
      IF (TID .EQ. 0) THEN
        NTHREADS = OMP_GET_NUM_THREADS()
        PRINT *, 'Number of threads =', NTHREADS
      END IF
      PRINT *, 'Thread',TID,' starting...'
!$OMP DO
      DO K = 1, N
       CALL subroutine(K)
      ENDDO
!$OMP END DO
      PRINT *, 'Thread',TID,' done.'
!$OMP END PARALLEL


It displays what I expect about the number of threads. So, I guess that I am doing this part correctly.
Regal
 
Posts: 15
Joined: Wed May 22, 2013 3:04 am

Re: Threads

Postby MarkB » Thu May 23, 2013 6:34 am

That is odd! Are you calling any libraries that might be multithreaded?
MarkB
 
Posts: 481
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Threads

Postby Regal » Thu May 23, 2013 7:11 am

MarkB wrote:Are you calling any libraries that might be multithreaded?

No. The only particular feature is that the compilation is performed using a makefile. What happens inside the makefile is: compilation and generation of object files from the programs/subroutines source code, compilation and linking with the main program. Nothing unusual, no other multi-threaded subroutines/libraries, only what I already showed and which is located inside one subroutine. I suppose that going through such a simple makefile should not have any effect on the binary produced.
Regal
 
Posts: 15
Joined: Wed May 22, 2013 3:04 am

Re: Threads

Postby MarkB » Thu May 23, 2013 7:45 am

I'm running out of ideas here. What does your script look like?
MarkB
 
Posts: 481
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Threads

Postby ftinetti » Thu May 23, 2013 7:58 am

Hi,

It's complex without actually seeing subroutine(K) code, is it a problem to post that subroutine (or, at least, the declarative part)? Are there saved data in the subroutine, for example? Are you checking the results do not change? Just guessing...

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

Re: Threads

Postby Regal » Thu May 23, 2013 8:11 am

MarkB wrote:I'm running out of ideas here. What does your script look like?

It is really elementary. Like:

Code: Select all
./main_program: ./main_program.o\
                            ./subroutine_1.o\
                            ./subroutine_2.o\
                            .
                            .
                            .
                            ./subroutine_n.o
gfortran               ./main_program.o\
                            ./subroutine_1.o\
                            ./subroutine_2.o\
                            .
                            .
                            .
                            ./subroutine_n.o\

-fopenmp -o ./main_program -L/usr/lib

#-----------------------------------------------
./main_program.o: main_program.f
   gfortran main_program.f -c -o ./main_program.o

./subroutine_1.o: subroutine_1.f
   gfortran subroutine_1.f -c -o ./subroutine_1.o
                            .
                            .
                            .
./subroutine_n.o: subroutine_n.f
   gfortran subroutine_n.f -c -o ./subroutine_n.o

That's all.
Regal
 
Posts: 15
Joined: Wed May 22, 2013 3:04 am

Next

Return to Using OpenMP

Who is online

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