0% found this document useful (0 votes)
422 views

Comparision Between Java and Fortran

The document compares the C and Fortran programming languages. Some key differences discussed include: - Fortran uses 1-based indexing for arrays by default, while C uses 0-based indexing. Fortran stores multi-dimensional arrays in column-major order, while C uses row-major order. - Fortran ignores case in variable names, while C is case-sensitive. - Fortran uses IF-THEN-ELSE syntax for conditional statements, while C uses if-else. - Fortran loops use DO loops with optional increments, while C uses for loops. - Program 7b tasks for students include writing output to NetCDF files, generating VisIt images of 2D fields over the

Uploaded by

comandantesam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
422 views

Comparision Between Java and Fortran

The document compares the C and Fortran programming languages. Some key differences discussed include: - Fortran uses 1-based indexing for arrays by default, while C uses 0-based indexing. Fortran stores multi-dimensional arrays in column-major order, while C uses row-major order. - Fortran ignores case in variable names, while C is case-sensitive. - Fortran uses IF-THEN-ELSE syntax for conditional statements, while C uses if-else. - Fortran loops use DO loops with optional increments, while C uses for loops. - Program 7b tasks for students include writing output to NetCDF files, generating VisIt images of 2D fields over the

Uploaded by

comandantesam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

!

"#$"%%&

Today
!!

Assigned
!! !!

Program 7b due date extended to 5pm Monday May 9 Final exam Wed. May 11, 7pm C programming
"! Performance "! Language

!!

Topics
!!

Computational Problems in Atmospheric Science


ATMS 391 Fri., Apr. 29, 2011

considerations (handout) differences (handout)

!!

Program 7b

ATMS 391 Spring 2011

4/29/11

C vs. Fortran
!!

From User Notes on Fortran Programming (UNFP)


!! !!

www.ibiblio.org/pub/languages/fortran and comments from the Fortran FAQ We can say that the difference between Fortran and C is ...
"! [Fortran "! [C

!!

Quotes:
!!

Comparison of C, Fortran, Java for Numerical Computation


Performance and other considerations.
!!
4

is a] language designed for numerical computations, and is a] language designed for other purposes (system programming)

!!

Fortran advantages...
"! scientifically

oriented, better optimized code, a lot of existing code, easier to learn, more efficient mathematics, easier to use and more robust, better diagnostics

Lets take a more in-depth and objective look.


ATMS 391 Spring 2011 4/29/11

%&

!"#$"%%&

Language comparison (1)


!! !!

Language comparison (2)


!!

A Comparison of C, Fortran and Java for Numerical Computation


!!

C
!!

J.V. Ashby, UKs STFC Rutherford Appleton Laboratory Fortran was one of the first high-level languages developed, and the programming model it implicitly contains is shaped by its early target market of mathematicians and scientists who wished to write programs that looked like the mathematical formulae they were used to manipulating on paper. Its very name comes from FORmula TRANslation, and the basic objects of the language, reals, integers, arrays, etc., are direct counterparts of the variables and constants which appear in any mathematical model.
ATMS 391 Spring 2011 4/29/11 6

Fortran
!! !!

C was developed in the early 1970s, largely as a systems' programming language for Unix [2] ... it is much "closer to the machine" in that its datatypes and model of programming map more directly onto the logical representation of data in memory. This gives it more power, but also can prevent several of the important optimisations available to Fortran compilers. On the other hand, a good C programmer can produce hand optimised code which is comparable with machine optimised Fortran. The price paid for this is often in code readability and maintainability. For those that deal with system functionality (such as I/O, interprocess communication, user interfaces, graphics, etc.) ... C's close-to-machine nature is a clear advantage.
ATMS 391 Spring 2011 4/29/11

!!

!!

Language comparison (3)


!!

Language comparison (4)


!!

Java
!!

Performance evaluation
!!

Java looks very similar to C and C++, and incorporates much of C++'s object-oriented programming model [3]. ..distinguishes itself from its predecessors in two major respects:
"! the

!!

We took programs written to perform a sparse matrix-matrix multiply using the same underlying algorithm in each language and ran them on a variety of matrices and on a variety of computers. The time taken was normalised by the cycle time of the processor. The differences between languages are similar in magnitude to the differences between different compilers for the same language. Fortran 90 is faster than C (though not by much and this is very dependent upon the compiler) and Java is considerably slower.

definition in the original language of a large range of class libraries, particularly for user interface activities (window management, mouse interaction, menus, etc.) and being platform independent. This independence is achieved since Java code is either interpreted at run-time (as with BASIC) or it is compiled into machine code for a virtual machine, the Java Virtual Machine (JVM). This compiled code can then be run on any platform

!!

!!

First results
!!

"! in

!!

ATMS 391 Spring 2011

4/29/11

ATMS 391 Spring 2011

4/29/11

#&

!"#$"%%&

Language comparison (5)


!!

Overall summary
!!

The results ... suggest that (at least for the limited range of problems considered) Java has still a long way to go before it can be considered a serious contender for numerically intensive computing. On the other hand, the performance of C and Fortran 90 is now so close as to make any distinction between the languages on performance grounds specious. Programmers will, and indeed, should, use the language they are most comfortable with to develop their code.

!!

C vs. Fortran Programming


Language differences.

!!

ATMS 391 Spring 2011

4/29/11

10

ATMS 391 Spring 2011

4/29/11

C vs. Fortran language differences


!!

C vs. Fortran: variables


!!

Handout: C-Fortran Interface, from Sun/Oracle.


!! !! !!

Reference handout for variable data type compatibility


!!

Online versions of full 15-page doc available, such as this: www.nd.edu/~hpcc/solaris_opt/SUNWspro.forte6u1/WS6U1/lib/ locale/C/html/manuals/fortran/prog_guide/11_cfort.html this document is for calling C from Fortran (or v.v.) but contains useful descriptions of language differences. data types I/O character strings loop functionality
4/29/11

Types
"! Fortran "! Fortran

INTEGER is typically C int REAL is typically C float "! Fortran LOGICAL is typically C int "! Fortran CHARACTER is typically C char
!!

!!

Overview
subroutines and functions arrays and index order variable names: case if-then syntax

See handout for details of Fortran 77 vs. 90 vs. C comparisons

!!

Case
"! Fortran

ignores case; C is case-sensitive.

11

ATMS 391 Spring 2011

12

ATMS 391 Spring 2011

4/29/11

'&

!"#$"%%&

C vs. Fortran: Arrays


!!

C vs. Fortran: If-Then-Else


!!

Starting element index


!!

Fortran:
IF (a == 3) THEN ... ELSE IF (b == 7.5) THEN ... ELSE ... ENDIF

!!

C:
If (a == 3) { ... } else if (b == 7.5) { ... } else { ... }

Fortran: one by default.


"! So

REAL TMP(10) is assumed to run elements 1,2,...,10 "! You can change it when declaring the array, e.g. REAL TMP(0:9)
!!

C starts at zero (always). REAL TMP(M,N) in Fortran; FLOAT TMP[M][N] in C Fortran stored in column-major order; C is row-major If exchanging arrays between Fortran and C, be aware of this.
"! Fortran: A(3,2) "! C:

!! !!

Notation
!!

Array order (for 2- and more dimensions)


!! !!

is stored in memory as A(1,1),A(2,1),A(3,1),A(1,2),.. A[3][2] is stored in memory as A[0][0],A[0][1],A[0][2],A[1][0]...

13

ATMS 391 Spring 2011

4/29/11

14

ATMS 391 Spring 2011

4/29/11

C vs. Fortran: LOOPS


!!

Program 7b tasks:
!!

Fortran:
DO i = 2, 14, 3 ... ENDDO

!!

C:
for ( i=2; i<=14; i+=3) { ... }

Program 7b
!! !!

write out NetCDF files and make/hand in VisIt images


"!

I will provide the sample code.

!!

Defaults
!!

!! !!

Defaults
!!

compute and plot the 2D field for lifetime of simulation: of max surface vorticity, wind speed, rain mixing ratio, and min pressure
"!

increment value is one if not explicitly set (example here: is 3)

none.

this is one plot per 2D field (at end of code)

And...
you can use multiple variable initializations, tests, increments: for ( i=2,j=4; i<3 && j>1; i++, j--)
!!

!!

create plots vs. radius of vertical velocity (max w/height), rainwater mixing ratio, and wind speed
"!

well do this for just one time. well do this for just one time.

!!

create histograms of vertical velocity


"!

15

ATMS 391 Spring 2011

4/29/11

16

ATMS 391 Spring 2011

4/29/11

!&

You might also like