Programming

From NA-Wiki

Revision as of 02:37, 12 July 2007 by Holst (Talk | contribs)
Jump to: navigation, search

Contents

Howtos in programming

General programming topics

  • Why use Eclipse or a similar development environment?

Fortran

Fortran 90 goes back to the seventies

This is how you can use Fortran 90 without having to use the highly non-portable MODULE system.

You will shoot down any Fortran MODULE on sight after you have seen this solution.(*)

Include file:

TYPE STUFFS                   ! We can use Fortran 90 constructs
    REAL VALUE
END TYPE
INTERFACE FOO                 ! Even the Fortran 90 interfaces!
    SUBROUTINE BAR(X)
    INTEGER X
    END SUBROUTINE
END INTERFACE
INTEGER GLOBAL                ! Now we make use of an old friend:
                              ! The COMMON BLOCK!
COMMON /GLOBAL/ GLOBAL        ! GLOBAL variable is named global_ in .o file

Main program:

PROGRAM TEST
    INCLUDE 'test.inc'
    PRINT*,'GLOBAL:',GLOBAL   ! GLOBAL=0 (or compiler dependent)
    CALL FOO(1)               ! CALL FOO(1.0) will not work. 
                              ! We are in strongly typed land now!
    PRINT*,'GLOBAL:',GLOBAL   ! GLOBAL=1
END PROGRAM TEST

("external") subroutine which access and modify shared (global or common) data:

SUBROUTINE BAR(X)
    INTEGER X
    INCLUDE 'test.inc'
    PRINT*,'INSIDE THE BAR'
    GLOBAL=GLOBAL+1
END SUBROUTINE

Compile the test with

f90 -c test.f90
f90 -c bar.f90
f90 -o test test.o bar.o

I also recommend that you analyze the object files with the nm command.

(*) There might be some reasons to use MODULE files but I cannot think of one right now. :-)

Links

  • Software carpentry - an on-line course on how to work more like a developer when you write your research code.
  • Freely available software for linear algebra on the web link.
Personal tools