Difference between revisions of "Programming/Fortran"

From HPC
Jump to: navigation , search
m
m
Line 118: Line 118:
 
==HDF5 Library==
 
==HDF5 Library==
  
HDF5 is a versatile data model that can represent very complex data objects and a wide variety of metadata.  
+
HDF5 is a versatile data model that can represent very complex data objects and a wide variety of metadata, which is ideal for data which has a large disparity to be stored in a standard format description. [https://en.wikipedia.org/wiki/NetCDF NetCDF] is a similar system as well.
 +
 
 
(''Example from the HDF group'')
 
(''Example from the HDF group'')
  

Revision as of 08:37, 16 April 2018

Programming Details

Fortran (formerly FORTRAN, derived from Formula Translation) is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing. The GFortran compiler is fully compliant with the Fortran 95 Standard and includes legacy F77 support. In addition, a significant number of Fortran 2003 and Fortran 2008 features are implemented. Intel's Fortran compiler (ifort) is also compliant with Fortran back to F77 and earlier to most implementations.

Icon tick.png There is direct support for openMP and openMPI.

Programming example

Non Parallel Example

program variableTesting
implicit none

   ! declaring variables
   integer :: total,average
   complex :: cx
   logical :: done
   character(len=80) :: message ! a string of 80 characters

   !assigning values
   total = 20000
   average = 1666
   done = .true.
   message = "A big Hello from HPC"

   cx = (3.0, 5.0) ! cx = 3.0 + 5.0i

        if (total .ge. average) then
                print *, total, " greater or equal than average"
        else
                print *, total, " less than average"
        endif

   Print *, average
   Print *, cx
   Print *, done
   Print *, message

end program variableTesting

Parallel Example (openMP)

program helloworld
  use omp_lib
  implicit none

!$OMP PARALLEL
!$OMP CRITICAL
  print *,'Hello from thread number',OMP_GET_THREAD_NUM()
!$OMP END CRITICAL
!$OMP END PARALLEL

end program helloworld

Modules Available

The following modules are available:

  • module add gcc/4.9.3 (GNU compiler)
  • module add intel/compiler/64/2016.2.181 (Intel compiler)

Note : It is generally accepted that the intel compiler generates faster code on Intel hardware than gcc, although gcc is generally more compatible with a wide range of hardware.

Compilation

The program would be compiled in the following way, optional Intel compiler available too:

[username@login01 ~]$  module add gcc/4.9.3
[username@login01 ~]$  gfortran -o testFortran  testFortran.f03

Note : mpifort would be used as the compiler wrapper if the above example used openMPI.

Usage Examples

Batch example


#!/bin/bash

#SBATCH -J openmpi-single-node
#SBATCH -N 1
#SBATCH --ntasks-per-node 28
#SBATCH -o %N.%j.%a.out
#SBATCH -e %N.%j.%a.err
#SBATCH -p compute
#SBATCH --exclusive

echo $SLURM_JOB_NODELIST

module purge
module add gcc/4.9.3

export I_MPI_DEBUG=5
export I_MPI_FABRICS=shm:tmi
export I_MPI_FALLBACK=no

/home/user/CODE_SAMPLES/OPENMP/demo
[username@login01 ~]$ sbatch demo.job
Submitted batch job 239552

HDF5 Library

HDF5 is a versatile data model that can represent very complex data objects and a wide variety of metadata, which is ideal for data which has a large disparity to be stored in a standard format description. NetCDF is a similar system as well.

(Example from the HDF group)


PROGRAM H5_CRTDAT

  USE HDF5 ! This module contains all necessary modules

  IMPLICIT NONE

  CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name
  CHARACTER(LEN=4), PARAMETER :: dsetname = "dset"     ! Dataset name

  INTEGER(HID_T) :: file_id       ! File identifier
  INTEGER(HID_T) :: dset_id       ! Dataset identifier
  INTEGER(HID_T) :: dspace_id     ! Dataspace identifier


  INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/4,6/) ! Dataset dimensions
  INTEGER     ::   rank = 2                        ! Dataset rank

  INTEGER     ::   error ! Error flag

  !
  ! Initialize FORTRAN interface.
  !
  CALL h5open_f(error)

  !
  ! Create a new file using default properties.
  !
  CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)

  !
  ! Create the dataspace.
  !
  CALL h5screate_simple_f(rank, dims, dspace_id, error)

  !
  ! Create the dataset with default properties.
  !
  CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dspace_id, &
       dset_id, error)

  !
  ! End access to the dataset and release resources used by it.
  !
  CALL h5dclose_f(dset_id, error)

  !
  ! Terminate access to the data space.
  !
  CALL h5sclose_f(dspace_id, error)

  !
  ! Close the file.
  !
  CALL h5fclose_f(file_id, error)

  !
  ! Close FORTRAN interface.
  !
  CALL h5close_f(error)

END PROGRAM H5_CRTDAT

Further Information

Icon home.png