Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sunybcs!boulder!ruede From: ruede@boulder.Colorado.EDU (Ulrich Ruede) Newsgroups: comp.lang.misc Subject: Re: NOT Educating FORTRAN programmers to use C Summary: Why I use C Keywords: Memory Management, FORTRAN, C Message-ID: <15913@boulder.Colorado.EDU> Date: 20 Jan 90 21:02:15 GMT References: <12950@cbnewsc.ATT.COM> <14199@lambda.UUCP> Sender: news@boulder.Colorado.EDU Reply-To: ruede@boulder.Colorado.EDU (Ulrich Ruede) Organization: University of Colorado, Boulder Lines: 60 In the FORTRAN vs. C wars I have not seen some of the following arguments. I am presently working on multilevel finite element codes using unstructured meshes. The basic data structure is a graph. In C I represent a node of the mesh (graph) by a structure like (actually it's more complicated): struct node{ Real u, /* approximate solution */ f; /* right hand side */ Real x, y; /* coordinates of the node */ Edge_Ptr c; /* edges starting from this node */ unsigned char num_edges; /* number of edges 1 Byte number < 255 */ Node_Ptr coarse, /* to coarser grid node under this one */ fine; /* to finer grid node above this one */ Node_Ptr next; /* link to next node on same level */ Flags flags; } My meshes are created dynamically based on error estimation techniques. The flexibility of the method requires even the space for the pointers to be allocated dynamically. I do not have a FORTRAN-Version of the code to really compare, but I claim that C gives me the following advantages: - The memory allocation problems become much easier, I do not have to waste statically allocated storage (like assuming that each node has a maximum number of edges). Programming a similarly storage efficient scheme in FORTRAN would be much more painful. - I can save storage by using 8 Bit numbers, where appropriate. - Machines with memory hierarchies (virtual memory or Cache) will work more efficiently. When processing the meshes (say for a relaxation pass) I usually access several components of a node at the same time; this data will typically reside on the same (or a few adjacent) memory pages. A straightforward FORTRAN implementation would use separate arrays for each member of the structure, so that several pages must swapped in for accessing one single node. Simulating the C memory structure in FORTRAN would require use of EQUIVALENCE and be difficult to make portable. - I am using preprocessing for making the code mode flexible and readable. A FOR_ALL_NODES/END_ALL_NODES macro pair is easier to implement for C than for FORTRAN, (where I'd be bothered to create unique label numbers, unique temporary variable names for loop-indices and would have to be careful about FORTRAN-format restrictions). In summary, I believe that C gives me a chance to write better structured and more efficient code. FORTRAN does have some efficiency advantages in an array-dominated world, where memory structures and access to the memory is regular. By the way, the authors of NUMERICAL RECIPES claim that aliased arguments are common coding practice in FORTRAN and use them systematically. Ulrich Ruede (ruede@boulder.colorado.edu)