Programming

From NA-Wiki

(Difference between revisions)
Jump to: navigation, search
m (Fortran 90 goes back to the seventies)
Line 13: Line 13:
=== Fortran 90 goes back to the seventies ===
=== Fortran 90 goes back to the seventies ===
-
This is how you can use Fortran 90 without having to use the <i>highly non-portable</i> module system.  
+
This is how you can use Fortran 90 without having to use the <i>highly non-portable</i> MODULE system. a
-
There might be some reasons to use MODULE files but I cannot think of one right now. :-)
+
You will shoot down any Fortran MODULE on sight after you have seen this solution.(*)  
Include file:
Include file:
<pre>
<pre>
-
TYPE STUFFS
+
TYPE STUFFS                   ! We can use Fortran 90 constructs
     REAL VALUE
     REAL VALUE
END TYPE
END TYPE
-
INTEGER GLOBAL
+
INTERFACE FOO                 ! Even the Fortran 90 interfaces!
-
COMMON /GLOBAL/ GLOBAL        ! GLOBAL variable is named global_ in .o file
+
-
INTERFACE FOO
+
     SUBROUTINE BAR(X)
     SUBROUTINE BAR(X)
     INTEGER X
     INTEGER X
     END SUBROUTINE
     END SUBROUTINE
END INTERFACE
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
</pre>
</pre>
Line 59: Line 60:
</pre>
</pre>
I also recommend that you analyze the object files with the <tt>nm</tt> command.
I also recommend that you analyze the object files with the <tt>nm</tt> command.
 +
 +
(*) There might be some reasons to use MODULE files but I cannot think of one right now. :-)
== Links ==
== Links ==

Revision as of 02:36, 12 July 2007

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. a 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