Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Use the OpenMP Libraries

This section describes the steps needed to set up and use the OpenMP Libraries from the command line. On Windows systems, you can also build applications compiled with the OpenMP libraries in the Microsoft Visual Studio development environment.

For a list of the options and libraries used by the OpenMP libraries, see OpenMP Support Libraries.

Set Up Environment

Set up your environment for access to the compiler to ensure that the appropriate OpenMP library is available during linking.

Linux

On Linux systems you can source the appropriate script file (setvars file).

Windows

On Windows systems you can either execute the appropriate batch (.bat) file or use the command-line window supplied in the compiler program folder that already has the environment set up.

To use the gfortran compiler with the Intel® OpenMP library along with the OpenMP API functions:

  1. Use the use omp_lib statement to compile the omp_lib.f90 source file, which is in the include directory.

  2. Add the -I option to the compile command line with appropriate path to the directory containing the resulting module file.

During compilation, ensure that the version of omp_lib.h or omp_lib.mod used when compiling is the version provided by that compiler.

CAUTION:

If you are mixing C and Fortran in the same program, be aware that when using the GCC or Microsoft Compiler, you may inadvertently use inappropriate header or module files. To avoid this, copy the header or module file(s) to a separate directory and put it in the appropriate include path using the -I option.

If a program uses data structures or classes that contain members with data types defined in the omp_lib.h file, then source files that use those data structures should all be compiled with the same omp_lib.h file.

Linux Examples

This section shows several examples of using OpenMP with the Intel Fortran Compiler Classic and Intel Fortran Compiler from the command line on Linux.

The command for the Intel® Fortran Compiler Classic is ifort. The command for the Intel® Fortran Compiler is ifx. In all Windows and Linux examples that follow, the command ifx can be substituted for the command ifort.

Compile and Link OpenMP Libraries

You can compile an application and link the Intel OpenMP libraries with a single command using the -qopenmp option. For example:

ifx -qopenmp hello.f90

By default, the Intel Fortran Compiler Classic and Intel Fortran Compiler performs a dynamic link of the OpenMP libraries. To perform a static link (not recommended), add the option -qopenmp-link=static. The option -qopenmp-link controls whether the linker uses static or dynamic OpenMP libraries on Linux systems (default is -qopenmp-link=dynamic). See OpenMP Support Libraries for more information about dynamic and static OpenMP libraries.

Link OpenMP Object Files Compiled with GCC or Intel Fortran Compiler Classic and Intel Fortran Compiler

You can use the ifort/ifx compilers with the gcc/g++ compilers to compile parts of an application and create object files that can then be linked (object-level interoperability).

When using gcc or the g++ compiler to link the application with the Intel Fortran Compiler Classic and Intel Fortran Compiler OpenMP compatibility library, you need to specify the following:

  • The Intel OpenMP library name using the -l option
  • The Linux pthread library using the -l option
  • The path to the Intel libraries where the Intel Fortran Compiler Classic and Intel Fortran Compiler is installed using the -L option

For example:

  1. Compile foo.c and bar.c with gcc, using the -fopenmp option to enable OpenMP support:
    gcc -fopenmp -c foo.c bar.c
    The -c prevents linking at this step.
  2. Use the gcc compiler to link the application object code with the Intel OpenMP library:
    gcc foo.o bar.o -liomp5 -lpthread -L<install_dir>/lib
    where <install_dir> is the location of the installed Intel OpenMP library.

Alternately, you can use the Intel Fortran Compiler Classic and Intel Fortran Compiler to link the application so that you don't need to specify the gcc -liomp5 option, -L option, and the -lpthread options.

For example:

  1. Compile foo.c with gcc, using the gcc -fopenmp option to enable OpenMP:
    gcc -fopenmp -c foo.c
  2. Compile bar.c with icx, using the -qopenmp option to enable OpenMP:
    icx -qopenmp -c bar.c
  3. Use the ifx compiler to link the resulting application object code with the Intel OpenMP library:
    ifx -qopenmp -nofor-main foo.o bar.o

Link Mixed C/C++ and Fortran Object Files

You can mix C/C++ and Fortran object files and link the Intel OpenMP libraries using GNU, GCC, or Intel Fortran Compiler Classic and Intel Fortran Compiler compilers.

This example shows mixed C and Fortran sources, linked using the Intel Fortran Compiler Classic and Intel Fortran Compiler . Consider the mixed source files ibar.c, gbar.c, and foo.f:

  1. Compile ibar.c using the icx compiler:
    icx -qopenmp -c ibar.c
  2. Compile gbar.c using the gcc compiler:
    gcc -fopenmp -c gbar.c
  3. Compile foo.f using the ifx compiler:
    ifx -qopenmp -c foo.f
  4. Use the ifx compiler to link the resulting object files:
    ifx -qopenmp foo.o ibar.o gbar.o

When using ifort or ifx, if the main program does not exist in a Fortran object file that is compiled by either ifort or ifx, specify the -nofor-main option on the command line during linking.

NOTE:

Do not mix objects created by the Intel Fortran Compiler Classic and Intel Fortran Compiler with the GNU Fortran Compiler (gfortran); instead, recompile all Fortran sources with either ifort or ifx, or recompile all Fortran sources with the GNU Fortran Compiler. The GNU Fortran Compiler is only available on Linux operating systems.

When using the GNU gfortran Compiler to link the application with the Intel Fortran Compiler Classic and Intel Fortran Compiler OpenMP compatibility library, you need to specify the following:

  • The Intel® OpenMP compatibility library name and the Intel® irc libraries using the -l option
  • The Linux pthread library using the -l option
  • The path to the Intel® libraries where the is installed using the -L option
You do not need to specify the -fopenmp option on the link line.

For example, consider the mixed source files ibar.c, gbar.c, and foo.f:

  1. Compile ibar.c using the icx compiler:
    icx -qopenmp -c ibar.c
  2. Compile gbar.c using the GCC compiler:
    gcc -fopenmp -c gbar.c
  3. Compile foo.f using the gfortran compiler:
    gfortran -fopenmp -c foo.f
  4. Use the gfortran compiler to link the application object code with the Intel OpenMP library. You do not need to specify the -fopenmp option in the link command:

    Component directory layout example:

    gfortran foo.o ibar.o gbar.o -lirc -liomp5 -lpthread -lc -L<install_dir>/lib

    Unified directory layout example:

    gfortran foo.o ibar.o gbar.o -lirc -liomp5 -lpthread -lc -L<install_dir>/<toolkit_version>/lib

    where <install_dir> is the location of the installed Intel OpenMP library.

Alternately, you can use the Intel Fortran Compiler Classic and Intel Fortran Compiler . to link the application object code but need to pass multiple gfortran libraries using the -l options at the link step.

This example shows mixed C and GNU Fortran sources linked using the icx compiler. Consider the mixed source files ibar.c and foo.f:

  1. Compile the C source with the icx compiler:
    icx -qopenmp -c ibar.c
  2. Compile the GNU Fortran source with gfortran:
    gfortran -fopenmp -c foo.f
  3. Use ifx to link the resulting object files with the -l option to pass the needed gfortran libraries:
    ifx -qopenmp -nofor-main foo.o ibar.o -lgfortran

Windows Examples

This section shows several examples of using OpenMP with the Intel Fortran Compiler Classic and Intel Fortran Compiler from the command line on Windows.

Compile and Link OpenMP Libraries

You can compile an application and link the Compatibility libraries with a single command using the /Qopenmp option. By default, the Intel Fortran Compiler Classic and Intel Fortran Compiler performs a dynamic link of the OpenMP libraries.

For example, to compile source file hello.cpp and link Compatibility libraries using the Intel Fortran Compiler Classic and Intel Fortran Compiler :

ifx /MD /Qopenmp hello.f90

Mix OpenMP Object Files Compiled with Visual C++ Compiler or Intel Fortran Compiler Classic and Intel Fortran Compiler

You can use the Intel Fortran Compiler Classic and Intel Fortran Compiler with the Visual C++ Compiler to compile parts of an application and create object files that can then be linked (object-level interoperability).

For example:

  1. Compile f1.c and f2.c with the Visual C++ Compiler, using the /openmp option to enable OpenMP support:
    cl /MD /openmp /c f1.c f2.c
    The /c prevents linking at this step.
  2. Compile f3.f90 and f4.f90 with the ifx compiler, using the /Qopenmp option to enable OpenMP support:
    ifx /MD /Qopenmp /c f3.f90 f4.f90
  3. Use the ifxIntel Fortran Compiler Classic and Intel Fortran Compiler compiler to link the resulting application object code with the Intel C++ Compiler OpenMP library:
    ifx /MD /Qopenmp f1.obj f2.obj f3.obj f4.obj /Feapp /link /nodefaultlib:vcomp
    The /Fe specifies the generated executable file name.