(Dr.R.K.) F90 Model : Collected Slides


Go to Bottom blank blank Go up one level R.K.'s Home Page Keyword Index Site Map Help Linux


Slide 1
1 Introduction F90 Model : Introduction
Fortran has a long and distinguished history. It was first developed to relieve the programmer from the tedious and non-portable complexities of low-level programming in assembly or machine code.

Several versions and standards have been developed along the way. The ``most popular'' (IMHO) was Fortran 77, which contained the older features and introduced more structured programming idioms. Much of the freely available numerical software is written to the Fortran 77 standard and is fairly portable to most architectures that support a Fortran 77 compiler.

Fortran 77 promoted more structured programming. The purpose of this presentation is to discuss the programming model that the new Fortran 90 standard appears to promote. This is not a tutorial. You should already be familiar with Fortran 90. This Fortran 90 model is highly speculative and purely opinionated and should not be taken as authoritative, but may be useful as a guide as you develop your own programming style with Fortran 90.

Qualifications (R.K.Owen):

These pages were crafted using a m4 macro package developed to isolate the task of web-page authoring from the HTML details.



Slide 2
1 F90 Model : Introduction
The Fortran 90 standard was drafted and finalized after much dissension and debate. The previous standards were more evolutionary and, for the most part, standardized existing practice. However, the Fortran 90 standard committees (ANSI X3J3 & ISO WG5) followed a more innovative route to keep Fortran viable into the future. You may be violently opposed to this approach; but, be that as it may, Fortran 90 exists and does offer many features that make the task of crafting code easier and more enjoyable.

One of the decisions the committees made was to make Fortran 77 a subset of Fortran 90, thus allowing the existing Fortran 77 code base to port directly. This has made Fortran 90 a hermaphroditic monster. You can either look at it as Fortran 77 being a wart on Fortran 90, or Fortran 90 as a wart on Fortran 77.

For this presentation, we'll confine ourselves to that part of Fortran 90 that is unique.

Note: This subset of Fortran 90 has been made into a proprietary language named Ftm largely created by Walter Brainerd(?) and available at http://www.imagine1.com/imagine1/


Slide 3
1 F90 Model : Introduction
Fortran 90 is not object-oriented, it does not have: Fortran 90 does have object-like characteristics:

This places Fortran 90 somewhere in between `C' and C++. Unfortunately, because of the lengthy standards process and the inavailability of conforming compilers, Fortran 90 use has been minimal, but appears to be increasing.

Opinion: Fortran 90 should have been a new language, without any Fortran 77 encumbrances. This would have allowed for more rational optimizing compilers sooner.



Slide 4
2 Style F90 Model : Programming Style

Programming style is a matter of taste and can lead to long lasting religious wars. There's no wrong or right way. Do that which seems natural to you.



Slide 5
3 F90 features F90 Model : Features
Fortran 90 has several nice features which we'll explore. Many of which extend current practices - others are new additions to the language.

Slide 6
3.1 Kinds of Type F90 Model : Kinds of Type
Fortran 90 introduces the concept of KIND, which, if used properly, can free the programmer of targeting code for a certain platform. For example, workstations generally have 32-bit REALs and 64-bit DOUBLE PRECISION. Contrast this to SGI/Crays which have default 64-bit REALs and implements a 128-bit DOUBLE PRECISION in software.

For this example I also use the `C' preprocessor for conditional compilation. The source file is tkind.F90, which #includes tkind.h. This method is common on UNIX systems, and on other platforms as well. The `C' preprocessor is more flexible and provides much more capability than the Fortran 90 standard `INCLUDE' (which is deprecated).

In this case, the conditional compilation is necessary for the GENERIC INTERFACE code. Generic interfaces allow the overloading of user functions, similar to overloading ABS to equate to ABS, IABS, DABS, CABS, or etc. It selects the correct function depending on the type of argument given. Generic interfaces require the functions to be distinguishable by Type, Kind, or Rank (TKR). One implementation couldn't handle the KIND=16 and demoted it to KIND=8 instead of failing. This created an ambiguity between show_real8 and show_real16, which yielded a compiler error.

The selected kinds, precision and ranges, are typical for IEEE implementation of floating point representations. Also, notice that parameter values are kept in a MODULE for easy inclusion in all the subroutines.

Code examples



Slide 7
3.1.1 Define Specifications F90 Model : Define Minimum Numeric Specification
Fortran 90 frees the programmer from integer and floating point representation specificity. Use the KINDs that meet the needed precision for the problem. For most numeric work, 64-bit REALs and 32-bit INTEGERs are sufficient. The specification can be encapsulated in a MODULE for easy inclusion.

Fortran 90 has defined several intrinsic functions for retrieving the defining characteristics for each intrinsic type. The following example defines three KIND parameters to handle the three intrinsic types which will be used.

Note how literal constants and variables are declared using these parameter types.

Code examples



Slide 8
3.2 Introduction to Modules F90 Model : Modules
Fortran 90 added the new feature of modules, which provides a way to consolidate, isolate and encapsulate code into an ``easily'' maintainable package. As with object-oriented languages, it provides a way to isolate many of the operations and data into a single source.

Modules is a popular concept in software now (e.g. Fortran 90 modules, perl modules, kernel modules, environment modules, apache web-server modules, pluggable authentication modules, etc.). The concept is to provide a documented API, with rules and limitations.

One attribute of modules is the ability to limit ``name space'' pollution. That is, avoid accidental name collision between two variables. Many software development problems stem from undeclared variables, or from accidentally reusing variables unintentionally. Fortran 90 modules can solve this common problem.

Modules can contain and encapsulate the data providing ``methods'' for handling the data. Unlike object-oriented languages there is no way to shield the user from the object details. However, object details can be minimized and the wise user will take advantage of them.



Slide 9
3.2.1 Modules Not COMMON BLOCKs F90 Model : Modules Over COMMON BLOCKs
Modules can provide an extremely robust alternative to the Fortran 77 COMMON BLOCK.

Code examples



Slide 10
3.2.2 Encapsulate Dynamic Memory F90 Model : Encapsulate Dynamic Memory
Fortran 77 did not have any native way to dynamically allocate memory. As a result several hacks were developed to accomplish much of the same capability, such as:

Fortran 90 includes dynamic memory allocation with a vengeance. It introduces the concept of pointers, automatic arrays, and allocatable arrays; however, be careful! Dynamic memory allocation & deallocation is expensive and it's generally better to avoid repetitious use of it in often called routines.

The following example demonstrates where a data array could be tailored to the problem size, thus minimizing program memory requirements which could be determined at run-time. The allocation is expected to be infrequent, and the data arrays are expected to be shared through out the program.

The module CONTAINS two routines that ``construct'' and ``destruct'' the arrays, as if in an object-oriented language. However, for Fortran 90 these routines will need to be called explicitly since such constructors and destructors are not called automatically. As with C++ an optional argument can be passed to the constructor to pre-set all the array values to a specified value. If no arguments are given, pre-set to zero.

Code examples



Slide 11
3.2.3 Create User-derived Types F90 Model : Create User-derived Types
Fortran 90 adds the concept of user-derived types. That is, the user may define some entity in terms of intrinsic types or other user-derived types, along with the operations that can be performed on them. The new derived type can be used as if it were an intrinsic type and part of the language.

The lay-out in memory for a derived type is contiguous, but the amount of memory used may not be as expected since the compiler is free to pad memory as needed to honor word boundaries.

The following example shows a typical Cartesian vector type, and defines what the `+' operation will do between two such types. The advantages are that such code can be isolated and rigorously tested, leading to more robust and less buggy code overall. The derived-type modules then can be re-used in other coding projects.

IMHO: derived-types add 'fun' to the language as it gives you the ability to create useful and meaningful components.

Code examples



Slide 12
3.3 Standard NAMELIST I/O F90 Model : NAMELIST I/O
Fortran 90 adds NAMELIST as a part of the standard; however, with I/O syntax that varies from that used by the existing Fortran 77 compilers. This isn't a major catastrophe since many of the Fortran 77 compilers had subtle differences between them since NAMELIST was not part of the Fortran 77 standard.

NAMELIST provides an excellent way to add annotated input. Ignoring much of the difficulties of precise spacing and precision specifications needed for standard formatted READs. NAMELIST usually shines as a superior method for input, and is hardly used for output, except for checking what is acceptable NAMELIST input. It uses a syntax similar to keyword argument parsing. The order of the keywords is unimportant within the NAMELIST block. However, the order of NAMELIST blocks is important for READs. Some implementations will skip over NAMELIST blocks until the expected one is reached. If no REWIND is performed, you can have multiple NAMELIST blocks of the same name and differing data and read them in as needed.

Code examples



Slide 13
3.4 Pointers and Advanced Memory Management F90 Model : Pointers and Advanced Memory Management
Fortran 90 provides advanced dynamic memory management on par with `C'. Therefore, many of the common `C' data structures can be translated into equivalent Fortran 90 forms. This example shows how to implement a singularly-linked list, where we anchor the first element to head and add successive elements until finished. All access to this data is in the same order as input. This method is ideal for situations where the number of elements is known at run-time, but only after the list is entered. Any element can be accessed in linear time, hence, random access is discouraged.

Each element is a user-derived type that can contain other user-derived types, intrinsic types, and, most importantly, a pointer of the element type. The last element's pointer will be nullified to alert the user that there's no more data.

If you use the detailed knowledge of the element structure, this is straight forward to implement. The disadvantage is that any changes in the structure (say you want to add a member or change one) reflects itself in code changes not isolated to the MODULE. This can be mitigated by providing object-like ``methods'' that hide the details. There is little performance penalty since such routines will in-line with good optimizing compilers. The coding becomes a little more difficult, but the advantages are more robust codes.

Code examples



Slide 14
4 Interfacing With F90 F90 Model : Interfacing with F77
The main reason Fortran 90 includes the Fortran 77 language as a subset is because of the large body of existing Fortran 77 code. For example: NCAR, LAPACK, SLATEC, netCDF, etc. to name a few available packages, and any personal legacy code. However, Fortran 77 has made certain assumptions about the memory lay-out of arrays, primarily, that arrays are column oriented and contiguous. In many of the matrix oriented packages you need to pass the maximum leading dimension. As far as the routines were concerned, all arrays were one-dimensional. If given the maximum leading dimension and current matrix dimension the routine could determine where the array data was stored. This is called an assumed-size specification.

Fortran 90 frees the program of much of these burdens by packaging the array dimensions into the passed array reference. All the routine needs to know is the array shape (i.e. 1,2,3, or more dimensions). This is the assumed-shape specification.

These are mutually exclusive and often lead to either compiling or run-time problems. The solution is to provide an INTERFACE block, which gives the Fortran 90 compiler more information regarding the Fortran 77 routine.

Code examples



Slide 15
5 F77 EQUIVALENCE? F90 Model : F77 EQUIVALENCE?
Fortran 77 provided the EQUIVALENCE statement as a means for the programmer to micro-manage memory by re-using space for different variables and arrays. This often leads to buggy code and endless hours of debugging. There is no need for such memory management, nowadays. Hence, Fortran 90 has made EQUIVALENCE deprecated and it may vanish in some subsequent standard release.

In nearly 30 years, I have found only one bona fide need for EQUIVALENCE, and it was easily replaced by passing the array twice to a subroutine with suitably declared array dimensions.

Code examples



Slide 16
6 Conclusion F90 Model : Conclusion
The new Fortran 90 standard promotes a model or method for coding that departs from the structured programming style of Fortran 77. This new style, though not strictly object-oriented, does have object-like capabilities that make codes robust and more encapsulated.

As Fortran 90 matures and more compilers come to exist, hopefully, there will be more codes and packages freely available.

Fortran 90 adds a `fun' factor that makes code crafting even more enjoyable.


Last Modified:
Go to Top blank blank Go up one level R.K.'s Home Page Keyword Index Site Map Help Linux
Brought to you by: R.K. Owen, Ph.D.
This page is http://owen.sj.ca.us/rkowen/howto/slides/f90model/slides/ALL.html