fortran essentials cs1372 auxiliary material. background fortran: formula translation fortran i...

Download FORTRAN Essentials CS1372 Auxiliary Material. Background FORTRAN: FORmula TRANslation Fortran I written 1954-1957 –First successful HLL –Team at IBM lead

Post on 26-Dec-2015




1 download

Embed Size (px)


  • Slide 1
  • FORTRAN Essentials CS1372 Auxiliary Material
  • Slide 2
  • Background FORTRAN: FORmula TRANslation Fortran I written 1954-1957 First successful HLL Team at IBM lead by John Backus Changed everything Fortran II released 1958 Fortran III never released Fortran IV released 1962 Version G in common use in the mid 60's Version H highly optimizing. Written in Fortran IV Ver G! Fortran 66 Standard released in 1966 (ANSI) First HLL standard in the world
  • Slide 3
  • Background FORTRAN 77 (1978) Fixed a number of shortcomings in Fortran 66 FORTRAN 90 (1991) Took too long and allowed C and C++ major inroads FORTRAN 95 Minor revision plus High Performance Fortran FORTRAN 2003 Latest version...major revision 2007: John Backus - Leader of Fortran Project dies at 82
  • Slide 4
  • References provides more information than you could possibly make use of. th_sEc4.6 is a web version of one of the original text books. th_sEc4.6 Good text book: Introduction to Programming with FORTRAN: Chivers and Sleightholme; Springer Press
  • Slide 5
  • The Ultimate Reference
  • Slide 6
  • Background FORTRAN was designed when punched cards were the source code input medium for computers Each card was one line of code. Large programs were contained in file cabinets full of cards
  • Slide 7
  • Restricted Character Set Only numbers, upper case letters,.,+-*/ So what about: THIS IS TEXT -> 12HTHIS IS TEXT Yes, you had to count the letters in the string >,.GT..LT..AND. etc Later versions added the quote character but retained the logical operator syntax
  • Slide 8
  • Design Goals FORTRAN is designed for speed more so even than C and many of its irritating language features derive from this goal. It is case insensitive, unlike almost all modern languages. Earlier versions did not permit recursion
  • Slide 9
  • Program Modules The main program is a PROGRAM and appears first in the source file SUBROUTINES are CALLED by the main program or other SUBROUTINES, and return nothing. They can, however, send values back via parameters.
  • Slide 10
  • Program Modules (cont) FUNCTIONS can both return values and return data via parameters. The data return feature is really only useful if you want to use the result directly in an expression or as a function parameter. Parameters are always passed by reference In later versions, you express the INTENT of a parameter to be IN, OUT, or INOUT.
  • Slide 11
  • Slide 12
  • Implicit Variable Typing In the original language, variables did not need to be predefined for type. If the variable name started with I.. N, it was assumed to be an integer. Starting with other letters, it was assumed to be real. You could override the default typing but real programmers never did. You frequently see IMPLICIT NONE at the head of a program indicating that all variables should be explicitly typed.
  • Slide 13
  • FORTRAN BADNESS Early versions had fascinating pass by reference bug! SUBROUTINE WEIRD(INT I) I = I * 2 RETURN END
  • Slide 14
  • FORTRAN BADNESS INT K, J K = 7 WEIRD(K) WEIRD(1) J = 1 + 1 + 1
  • Slide 15
  • FORTRAN BADNESS Did not require variables to be declared Had no reserved words Spaces mean nothing (i.e. not required) DO 10 I = 1,1000 TOTAL = TOTAL + SENSOR 10 CONTINUE AVG = TOTAL/1000.
  • Slide 16
  • I/O In General I/O functions need two parameters: a unit number and a format line number. In the punched card days when there was no console, unit 5 was the card reader, unit 6 the line printer and unit 7 the card punch When the console became available, unit * was the equivalent of stdin or stdout respectively.
  • Slide 17
  • Output Commands PRINT * works like MATLABs disp() function shows you values in arbitrary form. PRINT, works like MATLABs fprintf() function. For example, INTEGER:: T DO T=1,12 PRINT 100, T, T*12 100 FORMAT( ,I3, * 12 = , I3) END DO
  • Slide 18
  • Input Commands The READ command in its fullest form is: READ(UNIT=, FMT= ) It is frequently abbreviated to: READ(, ) or: READ, (unit assumed *) For example: DO I = 1,N READ 100, X(I), TEMP, Y(I) 100 FORMAT (2X, I2, A, I3) ! X means skip the spaces END DO
  • Slide 19
  • Development Environment The development environment consists of whatever text editor you like and a command window that is used to invoke the compiler. You compile a program using f95 (once it is installed and licensed) The o switch on the compiler lets you specify the name of the executable. If you dont specify an output file, it produces an executable file a.exe. You then run the program by typing the name of the executable file.
  • Slide 20
  • Example Code PROGRAM ch2201 IMPLICIT NONE ! Simple example of the use of a main program and two ! subroutines. One interacts with the user and the ! second solves a quadratic equation, ! based on the user input. REAL :: P, Q, R, Root1, Root2 INTEGER :: IFail=0 LOGICAL :: OK=.TRUE. CALL Interact(P,Q,R,OK) IF (OK) THEN CALL Solve(P,Q,R,Root1,Root2,IFail) IF (IFail == 1) THEN PRINT *,' Complex roots' PRINT *,' calculation abandoned' ELSE PRINT *,' Roots are ',Root1,' ',Root2 ENDIF ELSE PRINT*,' Error in data input program ends' ENDIF END PROGRAM ch2201
  • Slide 21
  • Example Code (Subroutines) SUBROUTINE Interact(A,B,C,OK) IMPLICIT NONE REAL, INTENT(OUT) :: A REAL, INTENT(OUT) :: B REAL, INTENT(OUT) :: C LOGICAL, INTENT(OUT) :: OK INTEGER :: IO_Status=0 PRINT*,' Type in the coefficients A, B AND C' READ(UNIT=*,FMT=*,IOSTAT=IO_Status) A, B, C IF (IO_Status == 0) THEN OK=.TRUE. ELSE OK=.FALSE. ENDIF END SUBROUTINE Interact SUBROUTINE Solve(E,F,G,Root1,Root2,IFail) IMPLICIT NONE REAL, INTENT(IN) :: E REAL, INTENT(IN) :: F REAL, INTENT(IN) :: G REAL, INTENT(OUT) :: Root1 REAL, INTENT(OUT) :: Root2 INTEGER, INTENT(INOUT) :: IFail ! Local variables REAL :: Term REAL :: A2 Term = F*F - 4.*E*G A2 = E*2.0 ! if term < 0, roots are complex IF(Term < 0.0)THEN IFail=1 ELSE Term = SQRT(Term) Root1 = (-F+Term)/A2 Root2 = (-F-Term)/A2 ENDIF END SUBROUTINE Solve
  • Slide 22
  • Results List the Directory Compile the Program Run the Program
  • Slide 23
  • ANCIENT HISTORY IF(X)10, 20, 30 10 CONTINUE PRINT *, 'Negative' GO TO 40 20 CONTINUE PRINT *, 'Zero' 30 CONTINUE PRINT *, 'Positive' 40 CONTINUE
  • Slide 24
  • Slide 25
  • Language Specifics Loops C C NEGATE AN ARRAY C IF(KFLAG.LE. -1) THEN DO 10 I=1,NN DX(I) = -DX(I) 10 CONTINUE ENDIF FORTRAN 77 Style ! ! NEGATE AN ARRAY ! IF(KFLAG.LE. -1) THEN DO I = 1,NN DX(I) = -DX(I) ENDDO ENDIF Later Style
  • Slide 26
  • Language Specifics Loops DO WHILE (Logical Expression) ENDDO DO IF(Logical Expression) EXIT ENDDO or
  • Slide 27
  • Language Specifics Shared Data Similar to global storage in MATLAB. There can be one anonymous common block (not recommended) and many named common blocks. COMMON /NAME1/ A(100), B(100) + C(100) COMMON /NAME2/ D(250). COMMON /NAME1/ ABC(300) C Different usages can use different layouts FORTRAN 77 Style MODULE name1 INTEGER a(100), B(100) & c(100) END MODULE name1 MODULE name2 INTEGER d(100) END MODULE name2. USE name1 ! Cant restructure the module (a good thing!) Later Style
  • Slide 28
  • Language Specifics GOTO Frowned upon in polite society. Early FORTRAN had 2 versions: the normal GO TO that survives in C today, and the Computed go to with many of the characteristics of switch: LOGICAL DONE =.FALSE. DO CALL USERSTUFF( DONE ) IF(DONE) GOTO 1000 ! Do something useful ENDDO 1000 CONTINUE Normal goto INTEGER IT DO IT = 1,5 GO TO (100, 200, 300, 400, 500) IT 100 PRINT *, it was 1 GO TO 600 200. 500 PRINT *, it was 5 600 CONTINUE END DO Later Style
  • Slide 29
  • Interfacing to C/C++ A really nasty exercise that is done frequently. Issues: Data type names, sizes and layout Function names, especially C++ Parameter Passing Common Blocks / Modules Byte alignment in structures (a concern with migrating C code also)
  • Slide 30
  • Language Specifics Data Types FORTRANC/C++ integer*2short int integerlong int or int integer iabc(2,3)int iabc[3][2]; logicallong int or int logical*1bool C++, One byte) realfloat real*8double complexstruct{float r, i;} double complexstruct{double dr, di;} character*6 abcchar abc[6]; parameter#define PARAMETER value Note that the array dimension ordering is reversed from C, and indexing is from 1, not 0
  • Slide 31
  • Linking FORTRAN and C Subroutines The entry point names for some FORTRAN compilers have an underscore appended to the name. This is also true for common block/structure names. FORTRAN: call subrA(... ) C: subra_(... ) The compiler flags "-fno-underscore" and "-fno- second-underscore" will alter the default naming in the object code and thus affect linking. One may view the object file with the command nm (i.e.: nm file.o).


View more >