The predominant programming language for numeric and scientific applications is. Fortran-77 and supercomputers are primarily used to run large-scale numeric ...
C Versus Fortran-77 for Scientific Programming TOM lUACDONALD Gray Research, Inc., Eagan, MN 55121
ABSTRACT The predominant programming language for numeric and scientific applications is Fortran-77 and supercomputers are primarily used to run large-scale numeric and scientific applications. Standard C* is not widely used for numerical and scientific programming, yet Standard C provides many desirable linguistic features not present in Fortran-77. Furthermore, the existence of a standard library and preprocessor eliminates the worst portability problems. A comparison of Standard C and Fortran-77 shows several key deficiencies in C that reduce its ability to adequately solve some numerical problems. Some of these problems have already been addressed by the C standard but others remain. Standard C with a few extensions and modifications could be suitable for all numerical applications and could become more popular in supercomputing environments. © 1993 John Wiley & Sons, Inc.
1 INTRODUCTION Standard C and Fortran-?? are the two most prevalent languages used on supercomputt'r,;. A comparison of Standard C and Fortran- T! shows that C contains a wider variety of data types, eloquent sequence controL a standard preprocessor. wider variety of memory allocation options, communication with the program's environment. and additional operators not present in Fortran- 77. ~lany desirable linguistic features provided by Received February 1992 Revised :\o,·ernber 1992
* The term Standard C refers to the standards A:\Sl X3.159-1989. Arneriean :\ational Stamlard for Information Systems-Programming Language-C. approwd December 1-t. 1989 [t:, and the technically identical ISO/IEC 9399:1990. Programming Languages-C. approved approximately 1 year later. © 1993 by John Wiley & Sons. Inc. Scientific Programming. Yo!. L pp. 99-1 H (199:2) CCC 1058-92-t-t/93/0:20099-16
Standard C are not present in Fortran--::. Fortran's major strength i~ its optimization potential. High performance capability is as important as any language featurt' in the numerical and scientific arena, and this is especially true for supncomputing environment~. Additional antation,..; that i,..; not pt>rmitted with Fortran-r-r
The physical layout of memory can be spt>cified to a fine ~ranularity with Standard C"s structure:;. lnte~ral memlwr:'i can be declared to occupy an arbitran·. number of bits within a memon·. word (callt>d a bitfiekn allowing table layouts to be mapped preci,.;ely and portably. This control makes it much eaiiit>r to conserYe memmT in a standard portable manner.
2.2 Type Synonyms Standard C supports the declaration of a synonym for all declarable type,.; with thetypedefkeyword. This allow,.; a complicated type to be reprt>sented with a sn1onnn.
Example 3 typedef struct double real, i, j, k; } QUATERNIONS[lOO]; I QUATERNIONS a,
*b;
Example 3 dt>fines an identifier QUATERNIONS that is a synonym for the type. array of 100 structures containin~ four members. each with type double. The variable a has the type QLA TER.\"I0.\"S, while variable b has the type pointer to QUA TER.\JO.YS. This ability to encapsulate a complicated concept with a single meaningful name is very powerful, and an aid to both portability and maintainability. A single typedef dec-
laration can be changed (from say. an array of 100 elemt'nb to an array of 1,000 elements) and t>very va1iable declared with that typedef name is automatically changed. This expressive typing mechani,..;m is a definite advantage that Standard C has over Fortran-::.
2.3 Flow of Control Standard C contains eloquent sequence control ,..;tatements alon~ with nested scopes that facilitate the u,.;e of structured programming techniques. Standard C contains if, if-else, for, while, do-while, and switch statements. This cadre of sequence control statements is complemt'nted with the following statements that nicely alter tht> flow of control through a program:
1. continue-branch to the top of the inner loop 2. break-exit a loop or switch statement :3. return-exit from a function. Finally. Standard C supports recursi,·e function calls. Fortran- "?Ts primary flow of control statements are limited to IF, IF-ELSE, GOTO, DO, and RETURN. The proliferation of statement labels in Fortran programs leads to highly unstructured algorithms. Control flow constructs such as the Arithmetic IF Statement and Assigned GOTO are inherently unstructured, and often produce difficult to understand algorithms. One advantage that Fortran-77 does have is a DO statement that can be statically analyzed at compile time. This allows the computation of the loop's trip count (the number of times the loop
t 02
:\1:\CDO:\ALD
2. Loop limit expressions are not nece,;sarih· static 3. Trip count can be data dependent
iterates) to be clone exactly once in a straightforward wav. The for loop in Standard C does not have the same guarantees because:
The following show examples of loops who,.;e trip count cannot be computed prior to executinl! the loop.
1. ~Iodification of the loop control variable i,; allowed inside the loop body
Example 4 for
(i=O; innih a standard set of declaration,; to be included in all compilation unih 'i.e .. sourct· files allm\·ing an interface to a set of library function,;; tn he implementation dept>ndt>nt yet hiddPn from thP prof!rammer.
2.4 C Preprocessor Standard C has a prPprocessor that permits source code inclusion. conditional source ,,xclu-
#include "common.h"
I*
common library interface
*I
The following exarnplt> ;;hows hmY to f!llcll"antee that an intef!nd tyfw ha,; more than 16 l>it,; of prl"'cision.
Conditional soun·p exclusion allow;; source code to be tailored to specific enYirnnments. The #if. #el if. #else" and #end if directiye;; provide this conditional sourct' exclthion capability.
Example 5 #include see section 2.5 for limits.h discussion #if INT_MAX = = 32767 I* 16 bit int typedef long data_type;
*I
C \ERSCS FORTRA:\1-77
#else I* > 16 bits typedef int data_type; #end i f
*I
.\lacro definitions allow a complicated operation or set of statements to be defined with a function-
#define ROOT(a, b, c)
like description that enhances portability and maintainability. The following macro definition:
(b + sqrt (b*b - 4*a*c)) I 2*a )
is an example of how a cryptic expression can be f!iven a meaningful name through a macro definition. SimilarlY. access to critical data o;tructures can Le encapsulated with macro definitions that improve the flexibility and comprehensibility of the program. The following example:
#define denom(X, I)
103
(X->table[I).denom)
allows a complicated data reference to be given a meaningful name that avoids using many long cryptic expressions. In all fairness to Fortran-::. it does permit the declaration of statement functions that proYide a subset of the C macro definition capability. The presence of c· s built-in preprocessor is a tremendous asset to writing both portable and maintainable programs. The C preprocessor is often Yiewed as an adYantage by Fortran programmers ro the extent that C preprocessing directiYes and macros are sometimes inserted into Fortran programs. However. the C preprocessor is not a part of the Fortran- :: standard. The C preprocessor can easily cau"t't thi;; alias at compile time and generate a scalar loop. Hnwen~r. the following example demonstrates that C pointers can introduce hidden aliases that are not detectable at compile time.
Example 10
int a[5] = {0, 2, 4, 6, 8}; void f ( ) { int i; for
(i
a [i] I* f */
1; i < 5; i++J = a[i-1] + 1;
The semantics of this loop dictate the followin~ execution order that produces scalar results.
Example 11 01 #include 02 03 int a [6] 04 int b [6]
05 int c [6];
= =
{0, 1, 2, 3, 4, 5}; {9,
8,
0, 1, 2, 3, 4 0, 1, 3, 5, 7 indeterminate
7,
6,
5, 4};
C YERSCS FOHTRA:\-77
113
06
07 void blackbox(int *pl, int *p2, int *p3, int n); 08 09 main ( l { 10 int i; 11 12 13 14
blackbox(c, b, a, 6); for ( i = 0; i < 6; i + +) printf (" c [%dJ = %d
/*no aliases*/
15
putchar ( '\n');
16 17 18 19
blackbox(&a[lJ, &a[lJ, a, 5); /*aliases*/ for (i=O; ier 12. TIIP result in!! army c contain~ !lw sum of a and b. Thi,.; make,.; the loop illc;idt' blackbox appear to lw a candidate for parullelizution. However, w1Jen blackbox i,.; called with alimws at litw nttmber 17 c;o!lwthill!-[ different happens. Each elenwnt of the resttltint: anH:· a t'OliiHins partial sums of the \ alw:'s in the pn•n·dinf!: elt•nwnls. This tinw tlw loop nut~! lw t>xecuted as u s('alar loop to obtain tlw corn·ct result.-;. Becaww blackbox is not declared static it ean be called from a separatt>ly compiled UHHlule. Therefon•. the compiler must make the \\·orst case assumption that thi;, loop might contain aliac;t';,. C doP~ not pro\·idt• any way to l't'stricl dtt' aliasitlf! of formal paramett•rs that art> pointers. Fortran-77. lwwt•n•r, does not permit aliasing through formal pammete1·s if tht· aetual object is modified. This Hwans that a formal paranwter cannot wfen'nee the snme object refen•nct~d by
c [3] a (3]
=
9
6
c [4] a [ 4]
g
c [5]
10 a{5]
9
=
15
another formal parameter, nor a global object that is part of a COMMON block. Fortran-77 rules require subroutine argumentEo to behave as if they are eopieJ in when the subroutine is ealled and copied ou1 when 1he subrowine returns cont rolto its caller. BPcause the order in which ari!umenh are copied in aud out if' unspecified . aliac;e::; produl'f• unpn·dictablt~ results. Fortran-77 is a proven perfornwr that provid.-s reasouable senwutics for exploiting automatic parallelism. C aliasillf! is not ouly the mo,;t critical remnining deficiency, it is one of the most diflicult to resolve. \lany ideas han:• been proposed that sohe part of the prohlem hut none hm·e provided a p-eneral solution dmt encompa,;ses all poiutt•rs. One promisillf! proposal tltm is currently beintr explon~d by C01Jll11ittee X:3J 11.1 j,.; a llt~W kind of pointer called a n~,;triett'd point.-r. A restrieted pointer f.(iH~ii tbe compilt'r the liberty to assume that the pointer behaves like an array for aliasin~ purposes. That is, because the colllpiler can assume that two differetll anavi" are not ·alim;l:'s with each other. it can also assume that two different restricted pointers are uot aliases.
t 14
~L\CDO.\ALD
void func(double *restrict p, double *restrict ql This example shrnvs how formal pm·amf'lt'J'!'i p and q can be dednred as restricted pointt•rs by usinl! H
nami(: mPmory alhwation functions. calloc. malloc. and real lee. The C s:;mdanl! l J l!wrr-
new keyword restrict. The compiler can a,.;sume d~nt p and q point to diffen,nt objects. Another useful application of rt~:·nrictetl pointers is to point at spnce allocated by the dy-
antet>s dwt .. euch such allocation shall \'ield a pointt·r to an object disjoint from am· otlwr objPct.
double
*
restrict p
malloc( N * sizeof(double) );
The simplieity of tbi;; proposal makes it easy w comprehend. This is important lwcause incorrect usal!e re,.;ults in undefined hehaviol'. The simplicity of this proposal al~o makes it Pasy for an optirnizer to exploit, because the same lo~ic eurrently being applied to arrays can now he applied to restricted pointers. Finally, it is trivial to port au
application that uses the new restrict keyword to other environments ht>d as mw ol' the most difficult issut>s on which to rt>ach const'nsus.
5 CONCLUSIONS C is an important languag•· that is not wwd
Yf'rY
often as a numerical or ;-;cientilic proFrmnminl'
languagP. Ewn if C i~ the language of dwin· for somP people they might h~:• dissuadPd from tJ,..;ing it for a particular application because of the deficiencies documented above . .\lany of C\; advantageous features could he put to good use in numerical codes if the current limitations are overcome. Certainly Fortran- 77 will be a popular numrrical language Cor a long time. C, on the other hand, must continue to evolve Bml impnwe or it will remain a secondary language for numerical and scientific programming. Committee x:.U11.1 is a vehicle for exploring the evolution of
C i11to a lwtltT mmwrintl lnnp:uap:.-.. \\'h.-.tlwr tlw resulting lanl!liH/!'' l!in•s prop:nunnwrs a !lsable lanp:uage I'Pllli!ill,..; to lw ,..;e{'lt,
REFERENCES 1] :\.\!:'! .\:3j 11
Cmmnitt. The C Programming Langllage (2nd ed .) . Engl•·wood Cliff\;. .\J: Premict' llall. 1988. [ 11] S. llarhi;;on and (;. Stet•le. C, A Rffi'rence .\!antwl :3rd ed. ;. Englt~wwHl (:lilT~ • .\.I: Prentiet" I lull, 1991.
Journal of
Advances in
Industrial Engineering
Multimedia
Hindawi Publishing Corporation http://www.hindawi.com
The Scientific World Journal Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Applied Computational Intelligence and Soft Computing
International Journal of
Distributed Sensor Networks Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Advances in
Fuzzy Systems Modelling & Simulation in Engineering Hindawi Publishing Corporation http://www.hindawi.com
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Volume 2014
Submit your manuscripts at http://www.hindawi.com
Journal of
Computer Networks and Communications
Advances in
Artificial Intelligence Hindawi Publishing Corporation http://www.hindawi.com
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
International Journal of
Biomedical Imaging
Volume 2014
Advances in
Artificial Neural Systems
International Journal of
Computer Engineering
Computer Games Technology
Hindawi Publishing Corporation http://www.hindawi.com
Hindawi Publishing Corporation http://www.hindawi.com
Advances in
Volume 2014
Advances in
Software Engineering Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
International Journal of
Reconfigurable Computing
Robotics Hindawi Publishing Corporation http://www.hindawi.com
Computational Intelligence and Neuroscience
Advances in
Human-Computer Interaction
Journal of
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Journal of
Electrical and Computer Engineering Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014
Hindawi Publishing Corporation http://www.hindawi.com
Volume 2014