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. I prefer the Intel compiler in terms of reliability and speed. 

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 Fortran and C++ version are confirmed in the terminal.

:: initializing oneAPI environment ...
   bash: BASH_VERSION = 5.2.15(1)-release
   args: Using "$@" for setvars.sh arguments: 
:: advisor -- latest
:: ccl -- latest
:: clck -- latest
:: compiler -- latest
:: dal -- latest
:: debugger -- latest
:: dev-utilities -- latest
:: dnnl -- latest
:: dpcpp-ct -- latest
:: dpl -- latest
:: inspector -- latest
:: ipp -- latest
:: ippcp -- latest
:: ipp -- latest
:: itac -- latest
:: mkl -- latest
:: mpi -- latest
:: tbb -- latest
:: vtune -- latest
:: oneAPI environment initialized ::
 
pagona@cfj9 _14:53:11: ~$ ifort --version
ifort (IFORT) 2021.9.0 20230302
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

pagona@cfj9 _14:53:25: ~$ icx --version
Intel(R) oneAPI DPC++/C++ Compiler 2023.1.0 (2023.1.0.20230320)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/intel/oneapi/compiler/2023.1.0/linux/bin-llvm
Configuration file: /opt/intel/oneapi/compiler/2023.1.0/linux/bin-llvm/../bin/icx.cfg
pagona@cfj9 _14:53:34: ~$ 

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

Source Code Editor

My main editor is gedit which is the default editor in Ubuntu rather than Microsoft VS Code, while I used to utilize Eclipse as a part of the IDE in my company career.  For an Android/Linux commingle tablet, the FLOSS VSCodium or the community version code-oss (headmelted) are used rather than vim.

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

”Make” in Windows 10

The “make” operation in Windows 10 is not compatible with one in Linux.
The “make -i” could be utilized to make force in operation by ignoring errors induced by “make”. The “make clean” is aborted for further operation due to the error associated with “rm -f” command.

The ”make”(make.exe) command in Windows10 which is generally taken from the binary files is found to be very old at Yr 2006 version. In line with the version of “make” command in Linux, I compile the source files, 0f which the executable module can be utilized. Please refer to README.W32 in the download package as to how to compile sources for more in detail.

~$ .\build_w32.bat gcc

The compiled product “gnumake.exe” which is created in the GccRel directory would be renamed as “make.exe”.

~$ make -v
GNU Make 4.3
Built for Windows32
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

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

以上

September 2023