=========================================================
Compiling and executing a parallel OpenMP Fortran program
=========================================================

Let's compile and execute the program :doc:`progPAR_OpenMP_f90`.

**Definition of the compiling environment**:

Supposing that we want to use Cascade partition, we have to connect to one of the login nodes for this partition, for example *s92node01* (see :doc:`../../clusters_usage/login_nodes`).

To use the software modules for this partition, we have to execute:

.. code-block:: bash

    module use /applis/PSMN/debian11/Cascade/modules/all

Now we can see available modules:

.. code-block:: bash

    module avail

Load a module for GNU compilers (GCC):

.. code-block:: bash

    module load GCC/10.3.0
    export OMP_NUM_THREADS=16

We can check the compiler version:

.. code-block:: bash

    gfortran --version
    GNU Fortran (GCC) 10.3.0
    Copyright (C) 2020 Free Software Foundation, Inc. 

**Compilation**

.. code-block:: bash

    gfortran -fopenmp -o ProgPAR_OpenMP.f90.exe ProgPAR_OpenMP.f90 

The binary file (executable) *SommeVecVecSEQ.f90.exe* has been generated.

.. NOTE:: If you prefer to use the Intel compiler, then you have to load the *intel/2021a* module (instead of *GCC/10.3.0*) and launch the compilation with the *ifort* command (instead of *gfortran*)

**Interactive execution (on login node):**

.. code-block:: bash

    ./ProgPAR_OpenMP.f90.exe


The result is displayed on the screen :

.. code-block:: bash

     Hello World from thread =            0
     Number of threads =           16
     Hello World from thread =            5
     Hello World from thread =            4
     Hello World from thread =           15
     Hello World from thread =           10
     Hello World from thread =           13
     Hello World from thread =            2
     Hello World from thread =           11
     Hello World from thread =           12
     Hello World from thread =            3
     Hello World from thread =            1
     Hello World from thread =           14
     Hello World from thread =            6
     Hello World from thread =            9
     Hello World from thread =            7
     Hello World from thread =            8

**Batch execution (by submitting to Cascade partition):**

We use a submission :doc:`script_OpenMP` to submit a job which will run the program on compute nodes.

This submission script configures the environment and then run the binary (with its options, if any) on the compute node.

.. code-block:: bash

    sbatch script.sh
    Submitted batch job 649

The job is waiting that asked ressources become availables and then it will be in *Running* state: 

.. code-block:: bash

    squeue
        JOBID PARTITION     NAME     USER     ST       TIME     NODES  NODELIST(REASON)
        649   Cascade      Par_Open  mylogin  R        0:00      1     s92node02


As prescribed in submission script, the **standard output** is redirected to the file *Par_OpenMP.649.s92node02.out* and the **standard error** is redirected to the file *Par_OpenMP.649.s92node02.err*.

.. code-block:: bash

        -rw-r--r-- 1 mylogin cbp     0 25 janv. 09:21 Par_OpenMP.649.s92node02.err
        -rw-r--r-- 1 mylogin cbp   419 25 janv. 09:21 Par_OpenMP.649.s92node02.out

When the job is finished, we can see the output file as following:

.. code-block:: bash

    cat Par_OpenMP.649.s92node02.out

    Hello World from thread =            0
    Hello World from thread =            9
    Hello World from thread =           12
    Hello World from thread =           11
    Hello World from thread =           13
    Hello World from thread =           10
    Hello World from thread =           14
    Hello World from thread =            4
    Hello World from thread =            3
    Hello World from thread =            5
    Hello World from thread =            6
    Hello World from thread =            7
    Hello World from thread =            8
    Hello World from thread =            1
    Hello World from thread =            2
    Number of threads =             16
    Hello World from thread =           15