• FORTRAN OpenMP not working properly

    From Arumugam ce19d044@21:1/5 to All on Tue Nov 8 15:40:31 2022
    Hi all

    I am trying to run a FORTRAN program with OpenMP in a cluster, but it is not using all the threads. Below is the code and the output. I am new to FORTRAN and OpenMP, please guide me on overcoming the issue

    use OMP_LIB
    implicit none
    integer:: thread_num
    integer:: num_threads, num_procs

    num_threads = omp_get_max_threads()
    call omp_set_num_threads(num_threads)

    !$omp parallel
    !$omp critical
    !$thread_num = omp_get_thread_num()
    print *, "Hello world thread number", thread_num
    !$omp end critical
    !$omp end parallel

    end
    Output:
    Hello world thread number, 10

    Thanks
    Aru

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lynn McGuire@21:1/5 to All on Tue Nov 8 19:28:43 2022
    On 11/8/2022 5:40 PM, Arumugam ce19d044 wrote:
    Hi all

    I am trying to run a FORTRAN program with OpenMP in a cluster, but it is not using all the threads. Below is the code and the output. I am new to FORTRAN and OpenMP, please guide me on overcoming the issue

    use OMP_LIB
    implicit none
    integer:: thread_num
    integer:: num_threads, num_procs

    num_threads = omp_get_max_threads()
    call omp_set_num_threads(num_threads)

    !$omp parallel
    !$omp critical
    !$thread_num = omp_get_thread_num()
    print *, "Hello world thread number", thread_num
    !$omp end critical
    !$omp end parallel

    end
    Output:
    Hello world thread number, 10

    Thanks
    Aru

    What platform ? Which compiler ?

    Lynn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arjen Markus@21:1/5 to All on Wed Nov 9 02:38:59 2022
    On 11/8/2022 5:40 PM, Arumugam ce19d044 wrote:

    Output:
    Hello world thread number, 10


    Odd, because from the message you did get it looks like at least 10 threads are running. The end parallel statement means a barrier is present there, so all threads should wait there and all messages would be printed.

    You might try as an alternative to write to individual files, just to see if something is messing with the output to the screen. Add:

    write(10+thread_num*) 'Thread number:', thread_num

    to the loop. No need to open the files explicity. You should get files with names like fort.10, fort.11 etc. The 10 is there to avoid possibly reserved LU-numbers.

    Regards,

    Arjen

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Bryan May@21:1/5 to All on Wed Nov 9 04:16:39 2022
    If you were expecting each thread to print output then changing the line: !$thread_num = omp_get_thread_num() -> thread_num = omp_get_thread_num() gives me the following output (both ifort & gfortran):

    Hello world thread number 2
    Hello world thread number 0
    Hello world thread number 6
    Hello world thread number 1
    Hello world thread number 4
    Hello world thread number 5
    Hello world thread number 8
    Hello world thread number 10
    Hello world thread number 3
    Hello world thread number 9
    Hello world thread number 7
    Hello world thread number 11

    Jon

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From FortranFan@21:1/5 to All on Wed Nov 9 06:11:53 2022
    On Tuesday, November 8, 2022 at 6:40:33 PM UTC-5, Arumugam ce19d044 wrote:

    Hi all

    I am trying to run a FORTRAN program with OpenMP in a cluster, but it is not using all the threads. Below is the code and the output. I am new to FORTRAN and OpenMP, please guide me on overcoming the issue ..

    Hello @Arumugam ce19d044,

    You may also want to inquire at the relatively recent Fortran Discourse site where you can attach Fortran source code with your posts, provide links, include code snippets with syntax highlighting, etc.
    https://fortran-lang.discourse.group/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From pehache@21:1/5 to All on Wed Nov 9 17:17:57 2022
    Le 09/11/2022 à 00:40, Arumugam ce19d044 a écrit :

    use OMP_LIB
    implicit none
    integer:: thread_num
    integer:: num_threads, num_procs

    num_threads = omp_get_max_threads()

    If called outside of a parallel region, omp_get_max_threads() always
    returns 1. The right way is

    !$omp parallel
    !$omp single
    num_threads = omp_get_max_threads()
    !$omp end single
    !$omp end parallel

    call omp_set_num_threads(num_threads)

    So here you say that you want to use a single thread, since num_threads=1

    !$omp parallel
    !$omp critical
    !$thread_num = omp_get_thread_num()
    print *, "Hello world thread number", thread_num

    The previous is commented, so "thread_num" has not be set, so it can print virtually anything.

    !$omp end critical
    !$omp end parallel

    end
    Output:
    Hello world thread number, 10


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ev. Drikos@21:1/5 to All on Fri Nov 11 11:47:19 2022
    On 09/11/2022 01:40, Arumugam ce19d044 wrote:
    Hi all

    I am trying to run a FORTRAN program with OpenMP in a cluster, but it is not using all the threads. Below is the code and the output. I am new to FORTRAN and OpenMP, please guide me on overcoming the issue

    use OMP_LIB
    implicit none
    integer:: thread_num
    integer:: num_threads, num_procs

    num_threads = omp_get_max_threads()
    call omp_set_num_threads(num_threads)

    !$omp parallel
    !$omp critical
    !$thread_num = omp_get_thread_num()
    print *, "Hello world thread number", thread_num
    !$omp end critical
    !$omp end parallel

    end
    Output:
    Hello world thread number, 10

    Thanks
    Aru


    Hello,

    IMHO, the problem is missing spaces in the 6th line before the end: !$thread_num = omp_get_thread_num()

    If we insert a space there, then it works:
    !$ thread_num = omp_get_thread_num()

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From pehache@21:1/5 to All on Fri Nov 11 12:37:00 2022
    Le 11/11/2022 à 10:47, Ev. Drikos a écrit :
    On 09/11/2022 01:40, Arumugam ce19d044 wrote:
    Hi all

    I am trying to run a FORTRAN program with OpenMP in a cluster, but it
    is not using all the threads. Below is the code and the output. I am
    new to FORTRAN and OpenMP, please guide me on overcoming the issue

        use OMP_LIB
        implicit none
        integer:: thread_num
             integer:: num_threads, num_procs
           num_threads = omp_get_max_threads()
           call omp_set_num_threads(num_threads)

        !$omp parallel
               !$omp critical
                   !$thread_num = omp_get_thread_num()
                print *, "Hello world thread number", thread_num
               !$omp end critical
        !$omp end parallel

               end
    Output:
    Hello world thread number, 10

    Thanks
    Aru


    Hello,

    IMHO, the problem is missing spaces in the 6th line before the end: !$thread_num = omp_get_thread_num()

    If we insert a space there, then it works:
    !$ thread_num = omp_get_thread_num()

    What??? Are you kidding? Have you tested, or is it just IYHO ?

    --
    "...sois ouvert aux idées des autres pour peu qu'elles aillent dans le
    même sens que les tiennes.", ST sur fr.bio.medecine
    ST passe le mur du çon : <j3nn2hFmqj7U1@mid.individual.net>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to pehache on Fri Nov 11 11:39:57 2022
    On Friday, November 11, 2022 at 3:37:06 AM UTC-8, pehache wrote:

    (snip)

    What??? Are you kidding? Have you tested, or is it just IYHO ?

    Different rules for fixed-form.

    Since the news host removes leading blanks, is is hard to tell, but you might be interested:

    https://www.openmp.org/spec-html/5.0/openmpsu23.html

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to pehache on Fri Nov 11 11:37:08 2022
    On Friday, November 11, 2022 at 3:37:06 AM UTC-8, pehache wrote:
    Le 11/11/2022 à 10:47, Ev. Drikos a écrit :

    (snip)

    IMHO, the problem is missing spaces in the 6th line before the end: !$thread_num = omp_get_thread_num()

    If we insert a space there, then it works:
    !$ thread_num = omp_get_thread_num()

    What??? Are you kidding? Have you tested, or is it just IYHO ?

    Directives like !$OMP must NOT have a space.

    Conditional compilation, like above, MUST have a space.

    https://www.openmp.org/spec-html/5.0/openmpsu24.html#x40-470002.2.2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From pehache@21:1/5 to All on Fri Nov 11 21:41:31 2022
    Le 11/11/2022 à 20:37, gah4 a écrit :
    On Friday, November 11, 2022 at 3:37:06 AM UTC-8, pehache wrote:
    Le 11/11/2022 à 10:47, Ev. Drikos a écrit :

    (snip)

    IMHO, the problem is missing spaces in the 6th line before the end:
    !$thread_num = omp_get_thread_num()

    If we insert a space there, then it works:
    !$ thread_num = omp_get_thread_num()

    What??? Are you kidding? Have you tested, or is it just IYHO ?

    Directives like !$OMP must NOT have a space.

    Conditional compilation, like above, MUST have a space.

    https://www.openmp.org/spec-html/5.0/openmpsu24.html#x40-470002.2.2


    My bad, I answered too quickly without reading the post correctly :(

    --
    "...sois ouvert aux idées des autres pour peu qu'elles aillent dans le
    même sens que les tiennes.", ST sur fr.bio.medecine
    ST passe le mur du çon : <j3nn2hFmqj7U1@mid.individual.net>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ev. Drikos@21:1/5 to pehache on Sat Nov 12 05:51:10 2022
    On 11/11/2022 22:41, pehache wrote:
    Le 11/11/2022 à 20:37, gah4 a écrit :
    On Friday, November 11, 2022 at 3:37:06 AM UTC-8, pehache wrote:
    Le 11/11/2022 à 10:47, Ev. Drikos a écrit :
    ..
    If we insert a space there, then it works:
    !$ thread_num = omp_get_thread_num()
    ...

    Conditional compilation, like above, MUST have a space.

    https://www.openmp.org/spec-html/5.0/openmpsu24.html#x40-470002.2.2


    My bad, I answered too quickly without reading the post correctly :(


    Never mind. Just for the record, I used my syntax checker that treats
    spacing rules with high accuracy and the assignment isn't listed as we
    can see at the end of the message.

    The tool supports syntax for Fortran 2018 and OpenMP 5.0 but is untested
    in newer operating systems: https://github.com/drikosev/Fortran



    ----------------------------------------------------------------------
    $ fcheck -fopenmp openmp.f90 -debug
    Line=00001,65659 USE buffer=[USE]
    Line=00001,66137 name-r buffer=[OMP_LIB]
    Line=00001,65800 ; buffer=[\r]
    Line=00002,65670 IMPLICIT buffer=[IMPLICIT]
    Line=00002,65669 NONE buffer=[NONE]
    Line=00002,65800 ; buffer=[\r]
    Line=00003,65590 INTEGER buffer=[INTEGER]
    Line=00003,65582 :: buffer=[::]
    Line=00003,66137 name-r buffer=[THREAD_NUM]
    Line=00003,65800 ; buffer=[\r]
    Line=00004,65590 INTEGER buffer=[INTEGER]
    Line=00004,65582 :: buffer=[::]
    Line=00004,66106 name-l buffer=[NUM_THREADS]
    Line=00004,00044 , buffer=[,]
    Line=00004,66137 name-r buffer=[NUM_PROCS]
    Line=00004,65800 ; buffer=[\r]
    Line=00006,66106 name-l buffer=[NUM_THREADS]
    Line=00006,00061 = buffer=[=]
    Line=00006,66106 name-l buffer=[OMP_GET_MAX_THREADS]
    Line=00006,00040 ( buffer=[(]
    Line=00006,00041 ) buffer=[)]
    Line=00006,65800 ; buffer=[\r]
    Line=00007,65638 CALL buffer=[CALL]
    Line=00007,66106 name-l buffer=[OMP_SET_NUM_THREADS]
    Line=00007,00040 ( buffer=[(]
    Line=00007,66106 name-l buffer=[NUM_THREADS]
    Line=00007,00041 ) buffer=[)]
    Line=00007,65800 ; buffer=[\r]
    Line=00009,66916 OMP buffer=[!$omp]
    Line=00009,66915 PARALLEL buffer=[PARALLEL]
    Line=00009,65800 ; buffer=[\r]
    Line=00010,66916 OMP buffer=[!$omp]
    Line=00010,65789 CRITICAL buffer=[CRITICAL]
    Line=00010,65800 ; buffer=[\r]
    Line=00012,65849 PRINT buffer=[PRINT]
    Line=00012,00042 * buffer=[*]
    Line=00012,00044 , buffer=[,]
    Line=00012,65882 char-literal-constant buffer=["Hello world
    thread number"]
    Line=00012,00044 , buffer=[,]
    Line=00012,66137 name-r buffer=[THREAD_NUM]
    Line=00012,65800 ; buffer=[\r]
    Line=00013,66916 OMP buffer=[!$omp]
    Line=00013,65570 END buffer=[END]
    Line=00013,65789 CRITICAL buffer=[CRITICAL]
    Line=00013,65800 ; buffer=[\r]
    Line=00014,66916 OMP buffer=[!$omp]
    Line=00014,65570 END buffer=[END]
    Line=00014,66915 PARALLEL buffer=[PARALLEL]
    Line=00014,65800 ; buffer=[\r]
    Line=00016,65570 END buffer=[END]
    Line=00016,65800 ; buffer=[\r]
    Line=00017,-0001 EOF buffer=[]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)