User:HerrHartmuth/sandbox

Polymorphism edit

One can derive one type from another by using

Parallel Programming in Fortran edit

Parallel programming is included in the standard from Fortran 2008 onwards. Fortran builds on the technique Single Program Multiple Data (SPMD) and the use of coarrays, and images are the central building blocks.

Images edit

The execution of the program is split up over multiple processes which are dubbed images in Fortran. Each image has control over its own data and executes the same program. Nevertheless, the execution on each image might vary, i.e. one could use select case statements that differ for individual images.

The following intrinsic functions are often used

  • this_image(): returns the current image number
  • num_images(): returns the total image count.

OOP edit

Polymorphism edit

Derived data types edit

In Fortran one can derive structures off of other structures, so called derived data types. The derived types will have the features of the parent type as well as the newly added ones and the general syntax is

type, extends(<parentTypeName>) :: <newTypeName>
  <definitions>
end type <newTypeName>

The following example shows different types of people within a company.

module company_data_mod

  implicit none
  private

  type, public :: phone_type
    integer :: area_code, number
  end type phone_type

  type, public :: address_type
    integer :: number
    character(len=30) :: street, city
    character(len=2) :: state
    integer :: zip_code
  end type address_type

  type, public :: person_type
    character(len=40) :: name
    type(address_type) :: address
    type(phone_type) :: phone
    character(len=100) :: remarks
  end type person_type

  type, public, extends(person_type) :: employee_type
    integer :: phone_extension, mail_stop, id_number
  end type employee_type

  type, public, extends(employee_type) :: salaried_worker_type
    real :: weekly_salary
  end type salaried_worker_type

  type, public, extends(employee_type) :: hourly_worker_type
    real :: hourly_wage, overtime_factor, hours_worked
  end type hourly_worker_type

end module company_data_mod

program main
  use company_data_mod
  implicit none

  type(hourly_worker_type) :: obj

end program main

Memory management with pointers edit

In Fortran one can use pointers as some kind of alias for other data, e.g. such as a row in a matrix.

Pointer states edit

Each pointer is in one of the following states

  • undefined: right after definition if it has not been initialized
  • defined
    • null/not associated: not the alias of any data
    • associated: alias of some data.

The intrinsic function associated distinguished between the second and third states.

Assignments edit

Overview edit

We will use the following example: Let a pointer ptr be the alias of some real value x.

  ...
  real, target :: x
  real, pointer :: ptr
  ptr => x
  ...