3

I am new to parallel programming, and am having trouble getting a simple parallel Fortran program to use multiple threads in OpenMP. The following program:

   Program Hello
   Use omp_lib
   Implicit None

   INTEGER        nthreads
   nthreads = 4

   CALL OMP_SET_NUM_THREADS(nthreads)

   write(*,*) omp_get_num_procs()
   write(*,*) omp_get_max_threads()
   write(*,*) omp_get_num_threads()

   !$OMP PARALLEL
     Write(*,*) 'Hello'
     Write(*,*) omp_get_num_threads()
   !%OMP END PARALLEL

   End Program Hello

Produces the result:

      32
       4
       1
   Hello
       1

What is the reason that the number of threads inside the parallel region is not the same as nthreads that I set above? I am compiling the program using gfortran -f openmp Hello.f on a Windows machine running cygwin.

3 Answers 3

1

I try it compiling in Linux with gfortran. And I get error because the OMP directives. I changed it to:

!$OMP PARALLEL
Write(*,*) 'Hello'
Write(*,*) omp_get_num_threads()
!$OMP END PARALLEL

(Notice !$OMP). And now it works. The output:

$ ./a.out 
      16
       4
       1
Hello
       4
Hello
       4
Hello
       4
Hello
       4
Sign up to request clarification or add additional context in comments.

Comments

0

The sentinel, i.e. !$omp or *$omp or c$omp must appear at the beginning of the line by itself. It simply launches a single thread otherwise and doesn't complain.

!$OMP PARALLEL
      Write(*,*) 'Hello'
      Write(*,*) omp_get_num_threads()
!$OMP END PARALLEL

3 Comments

Not true for !$OMP which does not need to start at the column 1
@FrancoisJacq Do you mean the uppercase variant? I encountered this issue when I was trying to run it. It throws no errors, but it also runs on a single thread.
No : uppercase and lowercase are equivalent in Fortran. I just mention that ! is the symbol of the bang comment : it may be put at any column in a Fortran instruction : al the rest of the line is assumed to be a comment. In the framework of OpenMP, all OMP declarations are standard Fortran comment lines which therefore follow the rules of these comments : in fixed form, such comment line begins by a 'c' or '*' in the first column or a '!' in any column... In free form, only the last possibility exists. The mistake of the OP was just !%OMP instead of !$OMP : nothing to do with the location of !
0

I don't know if it's the issue or not, but the last directive in the OP's code has a % instead of a $. May be just a typo, but I had recently posted code that a silly typo like that caused me trouble.

1 Comment

Oh! Francois Jacq, said it in a comment... he should get the credit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.