Fortran Flags

gfortran has many different command line options (also known as flags) that control the behavior of the compiler. To use these flags, simply include them on the command line when you run gfortran, e.g.

$ gfortran -Wall -Wextra -c mysubroutine.f90 -o mysubroutine.o

The various flags required to compile the various source files in a larger project can become complicated. We will soon see how to manage these flags through Makefiles.

For more information on the available flags, see the gfortran man page, or alternatively run the man gfortran or info gfortran commands.

Warning

Different compilers use different names for similar flags! For example, the gfortran compiler flags -fdefault-real-8 -fdefault-double-8 turn into -real-size 64 -double-size 64 when using the ifort compiler.

Double precision flags

All real variables can be promoted to double precision by default with the following flags:

$ gfortran -fdefault-real-8 -fdefault-double-8 -c mysubroutine.f90 -o mysubroutine.o

Compare this to Sample codes where the kind of each real variable is explicitly specified. Note that this only changes the default kind, and that variables that are explicitly declared with (kind=4) would still be single precision.

Note

I will still be explicitly setting the kind for real variables with selected_real_kind throughout these examples and in the homeworks. I prefer to have the kind obvious within the code, rather than moving it into compile flags.

However, it is important to know that some codes will choose compile flags as the way to enforce precision. You may use whatever style you prefer, just remain consistent.

Output flags

These flags control what kind of output gfortran generates, and where that output goes.

  • -c: Compile to an object file, rather than producing a standalone program. This flag is useful if your program source code is split into multiple files. The object files produced by this command can later be linked together into a complete program.

  • -J: Controls where *.mod files are put when created. We’ll cover this again in the modules section.

  • -o FILENAME: Specifies the name of the output file. Without this flag, the default output file is a.out if compiling a complete program, or SOURCEFILE.o if compiling to an object file, where SOURCEFILE.f90 is the name of the Fortran source file being compiled.

For more information feel free to revisit the examples we looked at in the section on Compiling, linking, and running a Fortran code.

Warning flags

Warning flags tell gfortran to warn you about legal but potentially questionable sections of code. These sections of code may be correct, but warnings will often identify bugs before you even run your program. I strongly recommend compiling with warnings enabled, and resolving all warnings as they appear. You can avoid many bugs by writing code that produces no warnings. A few useful flags to enable warnings are listed here:

  • -Wall: Short for “warn about all,” this flag tells gfortran to generate warnings about many common sources of bugs, such as having a subroutine or function with the same name as a built-in one, or passing the same variable as an intent(in) and an intent(out) argument of the same subroutine. In spite of its name, this does not turn all possible -W options on.

  • -Wextra: In conjunction with -Wall, gives warnings about even more potential problems. In particular, -Wextra warns about subroutine arguments that are never used, which is almost always a bug.

  • -Wconversion-extra: Warns about implicit conversion. For example, if you want a double precision variable sqrt2 to hold an accurate value for the square root of 2, you might write by accident sqrt2 = sqrt(2.0). Since 2.0 is a single-precision value, the single-precision sqrt function will be used, and the value of sqrt2 will not be as accurate as it could be. -Wconversion-extra will generate a warning here, because the single-precision result of sqrt is implicitly converted into a double-precision value.

  • -pedantic: Generate warnings about language features that are supported by gfortran but are not part of the official Fortran 95 standard. Useful if you want be sure your code will work with any Fortran 95 compiler.

  • -Werror: Turn warnings into errors. Normally, code that produces warnings will still compile. This flag converts all warnings into errors, which prevents code that emits warnings from compiling at all.

Debugging flags

Debugging flags tell the compiler to include information inside the compiled program that is useful in debugging, or alter the behavior of the program to help find bugs.

  • -g: Generates extra debugging information usable by GDB. -g3 includes even more debugging information.

  • -fbacktrace: Specifies that if the program crashes, a backtrace should be produced if possible, showing what functions or subroutines were being called at the time of the error.

  • -fcheck=bounds: Add a check that array indices are within the bounds of the array every time an array element is accessed. This slows down a program using it, but is a very useful way to find bugs related to arrays; without this flag, an illegal array access will produce either a subtle error that might not become apparent until much later in the program, or will cause an immediate segmentation fault with very little information about cause of the error.

  • -ffpe-trap=zero,overflow,underflow tells Fortran to trap the listed floating point errors (fpe). Having zero on the list means that if you divide by zero the code will die rather than setting the result to +INFINITY and continuing. Similarly, if overflow is on the list it will halt if you try to store a number larger than can be stored for the type of real number you are using because the exponent is too large.

    Trapping underflow will halt if you compute a number that is too small because the exponent is a very large negative number. For 8-byte floating point numbers, this happens if the number is smaller than around 1E-324. If you don’t trap underflows, such numbers will just be set to 0, which is generally the correct thing to do. However, computing with such small numbers may indicate a bug of some sort in the program, so you might want to trap them.

The -g flag has no effect on the speed of your program, and can be used at all times. The -fbacktrace and -fbounds-check can slow down your program. This slow down is rather minimal, so they should be used during development of your code, but can be removed once you are confident that everything is working correctly.

Note

The examples and assignments in this class will not be particularly computationally demanding. You can leave the backtrace and bounds checking flags on at all times with no noticable impact on performance.

Optimization flags

Optimization options control how the compiler optimizes your code. Optimization seeks to make your program run faster, but this is not always true.

  • -Olevel: Use optimizations up to and including the specified level. Higher levels usually produce faster code but take longer to compile, and may work to hide bugs. Levels range from -O0 (no optimization, the default) to -O3 (enable most available optimizations).

You should only enable optimizations when you are totally confident that your program is bug free. Optimizations make debugging very difficult.

Practical gfortran flags for development and production runs

One very good set of choices for these flags can be found at http://www.fortran90.org/src/faq.html.

In short, for development you should turn on all warnings and checks, and maintain symbols to aid in debugging:

-Wall -Wextra -Wimplicit-interface -fPIC -fmax-errors=5 -g -fcheck=all -fbacktrace

This turns on lots of useful debugging checks, stops after five errors have been emitted, and turns on backtrace printing which can help diagnose runtime errors (typically accessing an array out of bounds).

Once you have finished all of your debugging and code verification, you will want to compile an optimized version of your software. This will allow you to handle substantially larger runs and reduce your time-to-solution by potentially orders of magnitude. In this case you may want to use

-Wall -Wextra -Wimplicit-interface -fPIC -O3 -march=native -ffast-math -funroll-loops

This turns off all active debugging options (like bounds checks) which significantly slow down the code performance, and at the same time, turns on optimizing options (fast math and platform dependent code generation), providing accelerated code performance.

Optimization can modify the internal representation of your program substantially, and has the potential to hide problematic bugs. To quote Prof. Donald Knuth:

Warning

Premature optimization is the root of all evil (or at least most of it) in programming.

Always start with relevant debugging flags present!!

Further reading

This list is by no means exhaustive. A more complete list of gfortran specific flags can be found here or within the man-pages.

gfortran is part of the GCC family of compilers; more general information on GCC command line options is available at http://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html, although some of this information is specific to compiling C programs rather than Fortran.