Fortran TIPS

Fortran TIPS

This is my private memorandum for the Fortran TIPS in area of the compiler option and the coding rule since it is easier for me to forget on those particulars while being apart of the practical coding. I am utilizing the Intel oneAPI fortran compiler (successor of the Parallel Studio XE) for the Linux system as a main compiler together with gfortran as the de facto standard compiler which is occasionally used on Windows 10 as well as the light weight machine like a tablet. The compiler option is slightly different among them, especially in Fortran Dialect.
In terms of reliability and speed, I used to prefer the Intel compiler. However, the Intel compiler may produce different results depending on the optimisation level (-O). The Intel compiler may be unavailable depending on the platform. Therefore, I have stopped using the Intel compiler and now use gfortran as my default Fortran compiler.

Source Code Editor

My main editor is gedit, the former default editor in Ubuntu, because I can easily set my preferred tab size of three in gedit.  For an Android/Linux commingle tablet, the FLOSS VSCodium or the community version code-oss (headmelted) were used in the past.

makefile

Here is a “makefile” example for the inhouse black oil reservoir simulator “SMART”.

As the most important aspect, the dependency structure in “SMART” is relatively complex to some extent, where modules defined in two source files being explicitly named as _mod_ (i.e.:smart_mod_par.f90 and smart_mod_dim.f90) play a core role so as to address the size and type of dimensions, and other related issues over the entire program.

Compiling the source files which contains the module statement generates the module definition file “.mod” together with the object file “.o”. However, the compile does not necessarily guarantee as to where a change of the .f90 source files ensure update of the related .mod files automatically while the .mod depends on the .f90.

To enable maintain a proper buildup of the object files so as not to neglect updating a kind of the module related issue, therefore, this dependency structure is cited and underpinned in the latter of makefile.

Please note as well that some relatively unessential parts are omitted.

Please note that I have encountered severe problem on “fast” option used for ifort release option. Finally give up to use the “fast” option for the time being.

#*************************************************************************** 
# makefile for SMART:Simulation Model Approached Reservoir Evaluation Tool 
# March 2020:	Ver 3.0 in Linux
#***************************************************************************
SRCDIR = src
#OBJDIR = obj 
#BINDIR = bin

FC = ifort 
#FC = gfortran

TARGET = smart
OBJECT = smart_mod_par.o smart_mod_dim.o \
         smart_main.o                    \
         ................................\
         smart_lib.o smart_matrix.o

#Code generation
#Optimization level
#Fortran dialect
#Warnings and errors
#Optimization level {Default, 1, 2, 3(release), fast}
#Generating debug information
#Warnings and errors
ifeq ($(FC),ifort)
	ifeq ($(BUILDS),release)
#		FFLAGS = -fast
		FFLAGS = -O
#             -O0 \
#             -CB -check uninit -init=snan,arrays -fpe0 -ftrapuv -traceback \
#             -g \
#             -O3
#             -warn all -std
else ifeq ($(FC),gfortran)
#		FFLAGS = -ffree-line-length-none	-fbackslash -Ofast
		FFLAGS = -ffree-line-length-none	-fbackslash -O
#              -O0 \
#              -fbounds-check -Wuninitialized -finit-real=snan -ffpe-trap=invalid,overflow,zero -fbacktrace \
#              -g \
#              -O3
#              -Wall -pedantic -std=f95
endif

$(TARGET): $(OBJECT)
__$(FC) -o $@ $(OBJECT)
__echo "@Link completed."

# note: $(OBJDIR) in front of %.o is not required. 
%.o: $(SRCDIR)/%.f90
__$(FC) $(FFLAGS) -c $<
__echo "@"$<": Compile done successfully."

%.mod: $(SRCDIR)/%.f90 %.o
__@:

smart_main.o                \
............................\
smart_lib.o smart_matrix.o: \
$(SRCDIR)/smart_mod_par.f90 $(SRCDIR)/smart_mod_dim.f90

Automatic variables (minimum):

  • $@
    The file name of the target of the rule.
  • $<
    The name of the first prerequisite. I remember that a left shift shown in “<-” ultimately ends to the first one.
  • $^
    The names of all the prerequisites, with spaces between them. I remember that ^ indicates an image to pick up the all one.

Execution option (minimum):

  • -n
    Do not execute it but print the recipe that would be executed.
  • -p
    – Print the data base (rules and variable values) that results from reading the makefiles. Then execute as usual.
    – Can be used for a debugging tool.
  • -s
    Silent operation. Do not print the recipes as they are executed.

Notable matters in my particular:

  • The makefile is very nervous in which it is required to fully understand the explicit and implicit rules.
  • The current directory existing makefile can be treated as the root directory under the implicit rule.
  • Do not place any comment commencing # at the end of the command line.

Execute target module

~$ smart run

Delete obj & target modules

~$ make clean

Reference: GNU Make manual.

~$ make --version
GNU Make 4.3

References:

Intel oneAPI

The Intel Fortran compiler is now free for usage. The Intel® oneAPI HPC Toolkit containing the Fortran compiler is downloaded from the Intel site.

It would be a better choice to use the APT installer rather than the online or offline installer in terms of its maintenance and update. In order to install the compiler, please follow the instruction guide for more details.

After completing the installation, edit “~/.bashrc” file to add the sentence below setout in the end of the file so as to register the compiler in Path.

Once Logout, the registration is activated.

~$ nano ~/.bashrc

source /opt/intel/oneapi/setvars.sh

The ifx fortran version can be confirmed in the terminal.

:: initializing oneAPI environment ...
   bash: BASH_VERSION = 5.2.21(1)-release
   args: Using "$@" for oneapi-vars.sh arguments: 
:: advisor -- processing etc/advisor/vars.sh
:: ccl -- processing etc/ccl/vars.sh
:: compiler -- processing etc/compiler/vars.sh
:: dal -- processing etc/dal/vars.sh
:: debugger -- processing etc/debugger/vars.sh
:: dnnl -- processing etc/dnnl/vars.sh
:: dpct -- processing etc/dpct/vars.sh
:: dpl -- processing etc/dpl/vars.sh
:: ipp -- processing etc/ipp/vars.sh
:: ippcp -- processing etc/ippcp/vars.sh
:: ishmem -- processing etc/ishmem/vars.sh
:: mkl -- processing etc/mkl/vars.sh
:: mpi -- processing etc/mpi/vars.sh
:: pti -- processing etc/pti/vars.sh
:: tbb -- processing etc/tbb/vars.sh
:: vtune -- processing etc/vtune/vars.sh
:: oneAPI environment initialized ::
 
pagona@cfj9:~$ ifx --version
ifx (IFX) 2025.2.0 20250605
Copyright (C) 1985-2025 Intel Corporation. All rights reserved.

As for gfortran, execute “~$ sudo apt install gfortran” in Terminal. The compiler for gcc is installed automatically.

Preprocessor

Fortranは複数行をComment Outすること容易でない為、プリプロセッサを用いてソースコードの一部のCompileをOn/Offさせる。前処理を有効にするには、-cppをCompile Commandに組込む。

ifort -cpp  main.f90

非Compile対象コードは次のように#if~#endifで括る。

#if 1
!start of code
...
!end of code
#endif

以上

August 2025