Page 1 of 3

Threads

PostPosted: Wed May 22, 2013 11:04 am
by Regal
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!

Re: Threads

PostPosted: Thu May 23, 2013 3:24 am
by MarkB
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.

Re: Threads

PostPosted: Thu May 23, 2013 4:54 am
by Regal
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.

Re: Threads

PostPosted: Thu May 23, 2013 6:06 am
by MarkB
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!

Re: Threads

PostPosted: Thu May 23, 2013 6:22 am
by Regal
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.

Re: Threads

PostPosted: Thu May 23, 2013 6:34 am
by MarkB
That is odd! Are you calling any libraries that might be multithreaded?

Re: Threads

PostPosted: Thu May 23, 2013 7:11 am
by Regal
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.

Re: Threads

PostPosted: Thu May 23, 2013 7:45 am
by MarkB
I'm running out of ideas here. What does your script look like?

Re: Threads

PostPosted: Thu May 23, 2013 7:58 am
by ftinetti
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.

Re: Threads

PostPosted: Thu May 23, 2013 8:11 am
by Regal
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.