fortran subroutines produced from computer ... - Semantic Scholar

1 downloads 0 Views 144KB Size Report
I will use. Fortran, meaning FORTRAN77, as my example numeric language below. ... NAG library such as the Naglinks/Numlinks from Waikato (under Macsyma.
FORTRAN SUBROUTINES PRODUCED FROM COMPUTER ALGEBRA SYSTEMS: USING GENTRANS FROM REDUCE AND FROM MACSYMA G. KEADY Mathematics Department, University of Western Australia. CTAC 91 Conference Proceedings, Adelaide, 1991

1 INTRODUCTION GENTRANs are Fortran (or C) code production tools best known through the forms distributed with the Computer Algebra (CA) systems REDUCE and Symbolics Macsyma. There are many uses for such tools. The original author of GENTRAN uses them to produce nite element codes (Wang, 1986). In Keady (1991) I describe case studies involving nonlinear systems and multivariable optimization problems where the CA system does both the calculus and also the Fortran (or C) code production for the functions and for the derivatives. For many years the major classical CA systems - REDUCE, Macsyma, Maple and Mathematica - have provided output options of \Fortran style". This tends to be useful for producing code fragments. GENTRANs, however, are oriented towards producing whole subprograms. (MACROFORT from Maple, Gomez (1990), which is available free from the Maple share library, and MathCode from Mathematica, Kant et al. (1990), I believe, perform a similar role in these two CA systems. The `modern' CA system AXIOM, formerly called SCRATCHPAD and now distributed by NAG, also has Fortran code generation capabilities. I haven't had the opportunity of testing these three.) There is no implication in this paper that REDUCE and Macsyma with their GENTRANs are `the best' CA systems. See Harper et al. (1991) for comparisons of the CA systems, and another published 1

REDUCE-GENTRAN example. The emphasis on GENTRANs in this article is solely because, associated with work described in Section 1.3, I've worked on the underlying LISP code, and hence know its capabilities and limitations more than I know those of other systems. There are two parts to this paper. The rst is a general introduction: the second gives examples of GENTRAN code under Macsyma. An extended Research Report account of material related to Section 2.1 is given in Keady (1991). My future plans are given in Section 2.2.

1.1 Fortran code production from Computer Algebra systems

My own uses of the code production tools have involved FORTRAN77 and C as the target language. Others have sometimes had di erent target languages. For example, some forms of GENTRAN produce Ratfor. Later variants of Wang's GENTRAN are used to produce codes in vectorized Fortran (e.g. GENCRAY, (see Weerawarana and Wang, 1990) and in Pascal-like languages for parallel computers (e.g. see Tan and Wang, 1990, Sharma and Wang, 1990). In other contexts, not yet from GENTRAN, extensions to languages allowing for interval arithmetic have been the target language. In the near future, FORTRAN90 may become a target language. I will use Fortran, meaning FORTRAN77, as my example numeric language below. CA systems have long been used in association with numeric computing. Their value is greatest when there is some \algebra" or \manipulative calculus" to be done. My own examples concentrate on Jacobian code production, for nonlinear systems of equations by Newton's method, for related techniques in smooth unconstrained optimization, for use in solving sti systems of ordinary di erential equations and for use in solving optimal control problems. Unfortunately, it is very easy to ask computers to do long and boring tasks, such as \write the Fortran for this Jacobian", and the computer can be instructed in \unintelligent" ways. Thus one can produce massively long, and unoptimized Fortran code, very easily. This is still possible from GENTRAN, but GENTRAN does have facilities enabling the user to exploit the structure of his or her problem to produce better Fortran code and is consequently easier to use \intelligently". For an example involving matrices with band structure, see the end of Section 2.1. I consider optimal code to be a goal towards which one strives rather than something one expects to achieve. Frequently getting some code optimization from the automatic system is good enough, and the last parts may 2

be more bother, from the automatic system, than they are worth. Provided the Fortran code produced is readable, it should be possible to make decisions on how to improve it by looking at the code. If it needs improvement, rst try to get the automatic code generator to do better, and, if its results still are not good enough, do some further optimizing by hand.

1.2 Uses of GENTRANs: general comments

GENTRAN stands for GENerate and TRANslate. GENTRAN has many capabilities, but the most notable ones are the ability to translate whole program segments into the target numeric language, and to \ esh-out" skeletal programs or templates, provided by the user. GENTRANs are suciently

exible that they can be used to produce whole subprograms. A person wishing to solve many cases of the same basic problem can write, in Fortran or C, the main program and any general subroutines once, and can write - again just once -, in GENTRAN, templates to produce the other, problem-speci c subroutines. The GENTRAN interacts with the CA system (REDUCE or Macsyma) and is used to ll-in the particular problemspeci c parts: the user only needs to specify the problem-speci c parts to the CA system, then issue the same one-liner to invoke GENTRAN, asking it to use the pre-prepared templates. This process generates as output \readyto-run" Fortran program(s). The user is still in charge of compiling the Fortran so produced, linking it with other pre-prepared parts, and running the program. (See, for example, Wang, 1986, van den Heuvel et al., 1989, Savage, 1990 and Harper et al., 1991.) I wouldn't write one-o programs with GENTRAN. However, I do recommend, to REDUCE and to Macsyma users, the use of GENTRAN for problems where one is repeatedly solving broadly similar problems, but with sucient di erence that they need problem-speci c subprograms. Those subprograms which have special features for particular problems are worthwhile candidates for automatic code production. In examples of uses of GENTRANs in Section 2.1 I provide a template le. Template les contain passive and active parts, the active parts delimited by >. The passive parts are transcribed unchanged to the output le. The active parts contain, amongst much else, short algebraic computations whose results are required to be translated into the target numeric language.

3

1.3 Uses of GENTRANs in links to numeric libraries

There are demands for the production of whole subroutines in the links to NAG library such as the Naglinks/Numlinks from Waikato (under Macsyma or under SENAC respectively) and IRENA, the Interface between REduce and NAg, from Bath, England, and ESC, Environments for Scienti c Computation, from Finland. (See Broughan et al., 1991.) In any of these, the routines of the NAG library are made available from a front end designed for interactive use. IRENA uses GENTRAN, and, at present, produces more readable and better optimized FORTRAN than the others. Broughan expects his `(Paramax-) Naglink', a new link to NAG under Paradigm's Common LISP Macsyma to be fully working soon. We had known that, as currently distributed, Paradigm's Macsyma does not have a GENTRAN. The main LISP-coding part of my work for Broughan at Waikato was porting a useful subset of Wang's GENTRAN which runs with VAXIMA and old Franz Macsymas, from Franz to Common LISP. This Common LISP GENTRAN-N is now running under Paramax at Waikato. The likely use of this in association with Naglink is discussed in Broughan and Keady (1991). The goal of quality FORTRAN code production from these systems is worth pursuing. If suciently human-readable, quality FORTRAN is produced one might expect users to trial small-scale problems in the easy-to-use interactive environment before moving to large-scale production numerics. In the latter, main programs in FORTRAN are added to the subroutines produced, though possibly generalised by hand, from the automatic system - Naglink, IRENA, ESC or whatever.

1.4 How GENTRAN does the translation

A brief overview of any FORTRAN or C code generation from any of the systems used in the LISP-based CA packages is as follows. This overview is from such a large distance that the `details' are lost, and then the story is elegant. Brie y, a signi cant part of the translation is just a translation from pre x form to in x form. This is a standard advanced undergraduate LISP exercise. (See Winston and Horn, 1989, Exercises 32-1, 32-2 and Project 32-1.) A simple example of such a translation is to translate (setq LHS (plus A (times B (expt x 2))))

into

LHS = A + B*x**2

and this isn't far from FORTRAN (except for some indentation), or far 4

from C (except for a terminating semicolon and the treatment of powers). Obviously the details require further work, and the translators actually used vary considerably both in the range of translatable items, and also in the methods they treat the translation of the same item. We now turn to GENTRANs. The Symbolics Macsyma GENTRAN is in Common LISP: REDUCE's GENTRAN is in R-LISP (Gates, 1985a,b, 1986). Wang's GENTRAN was originally in Franz LISP (Wang and Gates, 1984), and he has later forms (Weerawarana and Wang, 1988) with parts in lex and in yacc. I have only worked with some of the LISP versions. The translation by GENTRANs of the algebraic computation results into the target numeric language is essentially a two-stage process. (1) The rst stage is highly speci c to the algebraic package and involves a translation of the internal forms into a pseudo-LISP. An item such as (A + Bx2 )=(C + sin(x)) would be mapped to something like (quotient (plus A (times B (expt x 2))) (plus C (sin x)) ) . (1+) Segmentation and, in the REDUCE form, the common subexpression optimization, is done at this pseudo-LISP stage. In the Symbolics Macsyma form the optimization is done earlier. (2) In the nal stage, the pseudo-LISP pre x forms are translated into the target numeric language. This is where the item in the rst paragraph of this subsection enters the process in GENTRAN. This stage is, I expect, rather independent of the algebraic system which provided the initial input to the rst stage. (See Weerawarana and Wang, 1988.) The following diagram indicates the stages. translatable expressions at top-level of CA language

translatable expressions in internal forms pseudo-LISP intermediate translatable translatable language statements in statements in statements top-level of internal CA language forms

C statements

FORTRAN77 statements

The `pseudo-LISP' in all the forms of GENTRAN to which I have had access is a subset of Franz LISP. References for the `computer language theory' are given in Keady (1991). 5

2 JACOBIAN CODE PRODUCTION Before turning to GENTRAN for Jacobians, we mention that CA is not the only approach. `Automatic di erentiation' codes which avoid the use of CA packages are another possibility. Some references for these are given in Keady (1991), many more are given in Griewank (1991). Again, even using CA, GENTRAN is not the only approach. For REDUCE or Macsyma users the gains of using GENTRAN are made clear, using this Jacobian example, in Keady (1991).

2.1 From Macsyma GENTRAN

The main test application in Keady (1991) concerns nonlinear systems of equations arising in connection with equilibrium ows in water-supply pipe networks. In one formulation this is a convex optimization problem: it has a positive-de nite Hessian with lots of sparsity, some in the form of band structure. (Actually it proved to be an exceptionally interesting test application. Thinking about both the numerical results and the problem led to the mathematical results reported in Calvert and Keady, 1992.) I have yet to exploit the sparsity outside the band structure. In van den Heuvel et al. (1989) GENTRAN is used and block structure is exploited, and this could be used as a model for further work. Write the nonlinear system as F (X ) = 0. For the water-supply pipe network problem engineers have found that Newton iteration codes, successively solving (DF (Xm ))(Xm+1 ? Xm) = ?F (Xm ); starting from a suitable X0, are e ective. For anything other than small networks with few variations made to the parameters de ning the system, I found that the best approach for me was to have my own, hand-written main program (which gave better control over input and output than other arrangements) and to combine with this the network independent subroutines (initially at least from libraries) and automatically (in my case GENTRAN) generated code for each di erent network topology. It is even possible that in certain real instances involving moderately large networks, but with many re-runs with di erent data, this approach would be more ecient than a stand-alone Fortran program which, at each iteration, had to check network-topology related items. A complete account is given in Keady (1991). The essentials can be presented with a combination of a simpler example - the NAG C05PCF test example - and, after this, some fragments of GENTRAN to illustrate 6

some of its capabilities for writing more ecient, more problem-speci c code appropriate to the pipe network problem. The NAG test example is as follows. Let c be some number between 0 and 2, and let n be a positive integer. Consider vectors X in IRn , X = (x1 ; x2; : : :; xn ). De ne x0 = 0 = xn+1 , and for X in IRn extended with zeros like this, de ne F mapping IRn to IRn componentwise, F = (f1; f2 ; : : :; fn ), by fi(X ) = ?xi?1 + (3 ? 2xi)xi ? 2xi+1 + c; for i = 1 : : :n: Let e be the vector in IRn all or whose entries are 1. Then F (e) < 0 and F (0) > 0. The problem is to nd a solution of F (X ) = 0 in the order interval [?e; 1]. The NAG documentation, e.g. the decision ow-charts in their MiniManual, tell one that C05PCF is the routine to use for a version of Newton iteration. (NAG's example for their C05PCF routine is used as the test example in all of the three links to NAG described in Broughan et al. (1991), and the code in the symbolic systems are given there. Far fewer lines of user-written code are required in the symbolic links than in the GENTRAN approach here: I'm advocating that the GENTRAN approach is more appropriate than some of the symbolic links only for larger-scale problems, like the pipe-network example, where repeated computations of similar problems are needed.) There are many di erent ways of representing F and X . In the links to libraries it is usual, for good reasons, to use functions rather than expressions. The di erent ways, and the use of GENTRAN for them are given in Keady (1991). /* NAG's C05PCF Test Example */ nn: 9; /* c=1 in NAG's example */ r[1]: (3-2*x[1])*x[1]-2*x[2]+c; FOR j:2 THRU (nn-1) DO ( r[j]: -x[j-1]+(3-2*x[j])*x[j]-2*x[j+1]+c ); r[nn]: -x[nn-1]+(3-2*x[nn])*x[nn]+c; /* ------------ Macsyma now knows r --------------- */ gentranlang(fortran); ldfjac: nn; gentranin("c05fcn.tem");

Though the Macsyma above is problem-speci c, and produces the required problem-speci c Fortran subroutine, the template below could be 7

called in by other, similar, pieces of Macsyma with di erent functions, r in the Macsyma, f in the preceding mathematical statement. more c05fcn.tem SUBROUTINE FCN(N,X,FVEC,FJAC,LDFJAC,IFLAG) COMMON C C Template for an unstructured nonlinear system C This version to be called with N=nn and LDFJAC=nn C Arranged to be in the form required by NAG's C05PCF C See NAG documentation or on-line help where it specifies C if IFLAG=1, FVEC is updated, C if IFLAG=2, FJAC is updated. > IF (IFLAG.NE.2) THEN > ELSE > END IF RETURN END

More generally, if a matrix Mmacs is set up in Macsyma: gentran(M: eval(Mmacs))$

will produce code 8

M(1,1)= ... M(1,2)= ... ...

If one triangle of a matrix is available, and code is required to set up the other triangle so the matrix is symmetric: gentran( FOR i:1 THRU eval(nn-1) DO FOR j: (i+1) THRU nn DO M(j,i): M(i,j) )

Moving the call to gentran inside the FOR is very di erent. If the bandwidth of a matrix is known, it can be used in various ways. For example, with a symmetric banded Jacobian, one can reduce the quantity of code using the following: FOR i:2 THRU (n-1) DO FOR j: (i+1) THRU max(i+mbw,n) DO gentran(FJAC[eval(i),eval(j)]: eval(diff(R[i],x[j])) )$

GENTRAN code associated with general bandwidth is given in Keady (1991, Part II). This can be used in the application to equilibrium ows in pipe networks.

2.2 Jacobian code production for control theory problems

My next uses of CA for Jacobian code-writing tasks will be for the control theory group in Applied Mathematics at the University of Western Australia. Highly ecient Jacobian codes are required by their control theory package MISER3. (See also Gomez, 1985.) I'm expecting that, for real controltheory problems, attempts at great generality will be less satisfactory than one would want. Rather than attempting to automate general versions, I propose to write-up case studies on particular classes, with the CA code which produces acceptable Jacobian codes given in the write-up. Added June 92. A rst version of the Jacobian code generation for MISER3, using REDUCE GENTRAN, is now available. This does a satisfactory job with the test examples in the MISER3 manual. The user has only to supply, to REDUCE, the mathematical description of the optimal control problem and the system then generates all the `user-supplied' code required by MISER3. Common subexpression optimization has yet to be incorporated. 9

ACKNOWLEDGEMENT

I am grateful to the New Zealand U.G.C. for the Research Fellowship which supported this work for Kevin Broughan's Mathematical Software Project at the University of Waikato.

References [1] Broughan, K.A. (1990) Interactive access to subroutine libraries: an interlink for SENAC, in Proceedings of the Computational Techniques and Applications Conference (CTAC-89), Brisbane 1989, ed J. Horvath, Hemisphere Press. [2] Broughan, K.A., Keady, G., Robb, T.D., Richardson, M.G. and Dewar, M.C. (1991) Some symbolic computing links to the NAG numeric library, SIGSAM Bulletin, 25(3), 28-37. [3] Broughan, K.A. and Keady, G. (1991) Numlink and Naglink: links to the NAG library from SENAC and Macsyma, in Proceedings of the Workshop on Symbolic and Numeric Computation, ed. H. Apiola, University of Helsinki Tech. Report Series, 19-34. [4] Calvert, B. and Keady, G. (1992) Braess's paradox and power-law nonlinearities in networks, J. Australian Math. Soc. B, to appear. [5] Gates, B.L. (1985) GENTRAN design and implementation, REDUCE version, Memorandum of Dept of Informatics, Twente University of Technology, (August 1985). [6] Gates, B.L. (1985) GENTRAN: An automatic code generation facility for REDUCE, SIGSAM Bulletin, 19(3), 24-42. [7] Gates, B.L. (1986) A numerical code generation facility for REDUCE, in Symsac 86: Proc. 1986 Symposium on symbolic and algebraic computation, Waterloo, Ontario, A.C.M., New York. [8] Gates, B.L. and Wang, P.S. (1984) A LISP-based RATFOR code generator, in Proceedings of the 1984 Macsyma users conference, General Electric, New York, 319-329. [9] Gomez, C., Quadrat, J.P. and Sulem, A. (1985) Computer algebra as a tool for solving optimal control problems in Applications of computer algebra, ed R. Pavelle, Kluwer-Academic, U.S.A.. 10

[10] Gomez, C. (1990) MACROFORT: A FORTRAN code generator in Maple, INRIA-Rocquencourt Rapport Technique, 119 (May). [11] Griewank, A. (ed.) (1991) Proceedings of the SIAM Workshop on automatic di erentiation, Colorado, U.S.A., 1991, SIAM Publications, U.S.A.. [12] Harper, D., Woo , C. and Hodgkinson, D. (1991) A guide to Computer Algebra systems, John Wiley, U.K.. [13] Kant, E., Daube, F., MacGregor, W. and Wald, J. (1990) MathCode: a code generation package for Mathematica, draft Report, Schlumberger Laboratory for Computer Science, Austin, Texas, U.S.A.. [14] Keady, G. (1991) SENAC and other symbolic front ends for subsequent numerical computation, Parts I-IV, Waikato University Maths Research Reports. [15] Savage, S.B. (1990) Symbolic computation of the ow of granular avalanches, J. Symbolic Computation, 9 , 515-530. [16] Sharma, N. and Wang, P.S. (1989) Symbolic derivation and automatic generation of parallel routines for nite element analysis in Proceedings ISSAC-88, Rome, Italy, July 1988, Springer Lecture Notes in Computer Science, 358, 33-56. [17] Tan, T.B. and Wang, P.S. (1989) Automatic generation of parallel code for the Warp computer, in Proceedings, rst International Workshop on Computer Algebra and Parallelism, Grenoble, France, June 1988, Academic Press, 91-117. [18] van den Heuvel, P., van Hulzen, J.A. and Goldman, V.V. (1989) Automatic generation of Fortran coded Jacobians and Hessians, in EuroCAl 1987, ed J.H. Davenport, Springer Lecture Notes in Computer Science, 378, 120-131. [19] van Hulzen, J.A., Hulshof, B.J.A., Gates, B.L. and van Heerwaarden, M.C. (1989) A code optimization package for REDUCE, in Proceedings ISSAC-89, Portland, U.S.A., July 1989, ACM, Academic, 163170. [20] Wang, P. (1986) FINGER: a symbolic system for automatic generation of numerical programs in nite element analysis, J. Symbolic Computation, 2, 305-316. 11

[21] Weerawarana, S. and Wang, P.S. (1990) GENCRAY: a portable code generator for Cray Fortran, in Proceedings ISSAC-89, Portland, U.S.A., July 1989, ACM, Academic, 186-191. [22] Winston, P.H. and Horn, B.K.P. (1989) LISP, Addison-Wesley, U.S.A..

12