MERCURY autocode: Principles of the program library

MERCURY autocode: Principles of the program library

MERCURY Autocode: Principles of the Program Library R. A. BROOKER, M.A., A.R.C.S. University of Manchesk r INTRODUCTION THIS paper describes the...

963KB Sizes 8 Downloads 62 Views

MERCURY Autocode: Principles of the Program Library R.

A.

BROOKER,

M.A.,

A.R.C.S.

University of Manchesk r

INTRODUCTION THIS paper describes the arrangements for using programs written in Autocode language as subprograms within a larger program. A subprogram implies a fairly substantial amount of calculation since the time to call it in (and return from it) is 320 msec. Thus it is a significantly larger unit of computation than is normally understood by a subroutine, which has no formal place in the Autocode system-although, of course, subroutines are employed behind the scenes when executing such instructions asy = 4 sin (277X). Before explaining the significance of this development it is necessary to summarize the principal strategic features of the Autocode system of programming for MERCURY. A complete account of Autocode can be found in Ref. 1. A short description has also been published as two articles in the Computer Journal (April and October 1958) and it is assumed that the reader is familiar with these. THE

AUTOCODE

The Autocode

LANGUAGE

language

employs the following symbols:

abcdefghuvwxyzrr ijklmnopqrst .0123456789 +-#=>>*(,)/+‘?-M 93

R. A. Brooker

94

which are represented on the punched paper tape input medium as fivehole teleprint characters following a Letter Shift or Figure Shift. A Carriage Return symbol is used to denote the end of an instruction. Autocode employs the following kinds of instructions 1. Arithmetical 2. Discrimination

e.g.

u = x/a + y/b + z/c

e.g.

jump 3, x, = 0

which means ‘jump to the instruction labelled 3 if X, is zero’. address system of labelling instructions is employed.)

(A floating-

3. For initiating cycles of operation. e.g. i = O(l)10

.

repeat which executes the intervening

instructions

10 times.

4. Input and output e.g. which means ‘read the next number from the input tape and plant it in the location referred to as x’.

read (x)

print (xx + yy) 1,5 which means ‘print the quantity xa + y2 to five decimal places allowing one place in the integral part’. 5. For matrix arithmetic e.g. x’ = &

(Y’, n, 1)

6. Miscellaneous

which means ‘replace the matrix Y by the matrix Y-IX, where Y is an n x n matrix and X is an n x 1 matrix (i.e. a vector) associated with the quantities y’ and x’ respectively’.

instructions e.g. ‘hoot’ and ‘end’

which are concerned the machine.

with the actual operation

of the program

on

The Autocode scheme alleviates but does not attempt to disguise the two-level nature of the storage system of MERCURY. This affects both the program and the data as explained in the following sections. Program storage

Large programs have to be partitioned into chapters, each chapter being restricted in length to the equivalent of 832 machine orders. The chapters are normally stored on the magnetic drum, and as each chapter

MERCURY

95

Autocode

is called in it is transferred to the fast store, but only one chapter occupies timethe fast store at any instant. Chapter changing is a comparatively consuming operation (160 msec) and the general idea is that each chapter will keep the machine sufficiently occupied with computation so that the time spent in changing from one chapter to another is not significantly large. Before describing any further the partitioning of a program into chapters, it is necessary to explain the arrangements for data storage. Data storage

Just as a chapter has to be transferred to the fast store before it can be used, the immediate data requirements of a calculation are kept in another part of the fast store (the tvorkiplg store). These quantities are referred to as the working variables. Numerical information in excess of this has to be kept on the drum, and these are referred to as the auxiliary variables. There are 508 working variables and up to 10,752 auxiliary variables. All variables are represented in the machine by floating binary numbers, of magnitude between 2*256, with a relative precision of thirty binary digits. In addition to the variables however there are also twelve index quantities taking integral values in the range -512
These are split into two sets.

1. The main set consists of 480 variables which can be divided maximum of 15 groups associated with the variable letters

into

abcdefghuvwxyzn

For example,

they can be arranged Do01 u2

as a single group of 480 variables * * * V47B

in which case the directive v + 479

is written at the head of the appropriate chapter. be arranged in three equal groups, thus a0 al a2 . . . kg 60 blb2.

* *bm

co Cl CP - * * Cl59

the necessary directives

being a-+ b-+

159 159 c--t 159

Alternatively

they could

R. A. Brooker

96

It is intended that these groups shall reflect any natural grouping of the quantities occurring in the problem, and provided that the total number of variables does not exceed 480 the number and size of the groups is at the disposal of the programmer. 2. In addition to the main variables, there are 28 special variables represented by the letters abcdefghuvwxyz a’b’c’d’elf’g’h’u’vIW)XLYIZ)

employed without a suffix. These always refer to the same locations in the working store regardless of the directive settings. In addition there is the special location T which unless otherwise altered is assumed to contain 3.14159. . . Indices.

These quantities

are represented

by the 12 letters

ijklmnopprst

Although permitted integral values in the range - 512 < i < 511, emphasis is placed on positive values because they are primarily intended to be combined with variables in the form, e.g.

xi to represent a free suffix; of the variables

or

x(,_~)

or

%+50)

that is, these expressions may represent

any one

x0 xi xs . . . depending on the particular value of the index in question. Thus, if n = 4, then x(,-i) will refer to xs. The last two expressions illustrate the most general form which a suffix may take, namely, (index & integer). In calculations of a repetitive nature an index will assume a range of values, and to arrange this it is necessary to be able to compute with indices as separate items in much the same way as variables. In preparing the input tape, all expressions are recorded in a onedimensional form, thus xs, xi, and x(,+so) appear as x3, xi and x(s + 50) respectively. Consequently it is not possible to distinguish, in a product, between (say) xi meaning xi and xi meaning ‘x times i’. In order to resolve this difficulty, a convention is introduced that the product is written ix. The auxiliary variables. The auxiliary storage locations may be regarded as numbered 0, 1, . . ., 10,751 but the last 512n of these are occupied by Thus in a program extending up to and chapters 1 - n inclusive. including chapter 4 the last available location is 8703.

MERCURY

Autocode

97

To use the auxiliary variables they must first be transferred to the working store, and at some time or other information must be transferred to the auxiliary store from the working store. Both of these are relatively time-consuming operations, and programs employing them require fairly careful planning. The transfer instructions take the form & (x) W, n ‘read’ from auxiliary store #, (x) w, n ‘write’ to auxiliary store Here the (integral) value of x (any variable expression) specifies a starting location in the auxiliary store, and the variable w is regarded as a starting location in the working store: n (any index or whole number) is the The expression x is computed number of consecutive variables transferred. without rounding. The variable ze,will generally be one of the main set, unless n = 1 in which case it could be a special variable. Example : 46 woh t#~~(d tp, (f)

10

transfers contents of auxiliary locations 1000-1009 (inclusive) to x~, x(~+~), . . . x~~+~)

n + I) al, n transfers

the variables a,, u2, . . . a, to auxiliary locationsd--+ 1,. . .d

P? 1

replaces the special variable g by a number the auxiliary store.

from

It is very often appropriate to associate letters with groups of numbers in the auxiliary store, e.g. when dealing with matrices. For this purpose the fourteen ‘primed’ special variables a’ b’ c’ . . . z’ are used to designate the starting locations of groups of auxiliary variables. Thus, for example, by setting a’ = 1000 we define a group of unlimited extent located in 1000, 1001, etc. If the group is an (n x n) matrix and recorded so that the (i,j) element stands in a’ + (i - 1)~ + (j - I), then the instruction &(a’ + in - n)b,, n transfers the ith row of the matrix to the working store. The execution time for a group transfer cannot be given very precisely but is less than (17[n/32] + 34 + 0.36n) msec. where [] denotes transferred.

‘integral

part of’, and II is the number

of variables

R. A. Booker

98 Chapters.

one chapter

Each chapter has its own labelling system, and to jump from to another, an across instruction is employed, e.g. across 2f3

means ‘jump to the instruction labelled 2 in chapter 3’. The diagram illustrates the layout of a multi-chapter program. Each chapter is headed with the chapter number and the variable setting directives, and terminates with the directive word close. If the directives in chapter 3 are the same as those in chapter 2 then we may simply write: chapter

1

1

instructions

1

close chapter 1 chapter

2

directives

/

3 I1

variables 1

i~tructions close

close

2

chapter

1

3

r-.zzCl 1

instructions close

Note,

however, that this substitutional directive may only refer to a previous chapter on the program tape. It is conventional to arrange chapters in simple numerical order as shown. 7% s~gn~~a~e of the vur~able ~~rect~ues. When resetting directives subsequent chapters it may be necessary to appreciate the significance these statements, Thus, e.g. chapter

1

O- 99 to a,, a,, . . . ass a 4 99 allocates 100-199 to b,, b,, . . . b,, b ---f 99 storage c 4 99 locations 200-299 to co, cl, . . . egg

in of

MERCURY

chapter

Autocode

99

2

c + 99 allocates x ---f 49 storage y + 49 locations

0- 99 to cO, cr, . . . cge 100-149 to Xg, X1, . . . Xa,J 150-199 to lo, yr, . . . ya9

Thus the ‘c’s’ of chapter 1 are not those of chapter 2. If it is intended that they should be, then the latter directives might be recast as follows x + 49 y + 49 37 ---t

99 (waste)

c-99 A further consequence of this scheme is that ‘~~0) is identical with y,,, ‘X61’with yr, and so on. Such ‘overlapping’ references are sometimes useful. Subchapters. At any point within a chapter it is possible to call in a ‘subchapter’ and subsequently to return to the original chapter at the instruction following the point of departure. This is done by means of a down instruction in the main chapter and an up instruction in the subchapter. For example down 213

calls in chapter 3 as a subchapter and enters it at the instruction labelled 2. When the subchapter has completed its task the single word instruction

will return control to the main chapter at the instruction following the original down instruction. Alternatively the up instruction may be used in any chapter reached by means of across instructions from the original subchapter. A subchapter may have its own sub-subchapter, but there the regression stops. The instructions ‘preserve’ and ‘restore’. These apply to the use of subchapters. If necessary the working variables can be preserved during the operation of the subchapter and restored on return to the main chapter. This can be done by writing the word preserve before the down instructions, and the word restore immediately after it. The variables in question are dumped in hidden locations of the auxiliary store. There are two dumps, the first being used by the master chapter when calling in a subchapter, and the second by a subchapter when calling in a sub-subchapter. A

100

R. A. Booker

program may thus extend over three levels, as illustrated in the following diagram : chapter 1 chapter 2 chapter 3 1) preserve down l/2 restore

1) preserve down l/3 restore

up

up

A consequence of this arrangement is that any results calculated by the subchapter would have to be recorded in the auxiliary store in order to preserve them. (If they are left in the working store, then the restore instruction could be postponed until they have been dealt with in the master chapter.) subprograms

When used in this way a subchapter (or sub-set of chapters) provides a convenient means of arranging a calculation for possible future use as a subprogram.

The master program has to provide the subprogram with certain information before the latter can carry out its task. Thus, it has to be told where to find the operands and where to place the results. In either case these may take the form of individual numbers and/or sets of numbers, e.g. series, matrices, etc. In addition the subprogram may refer to other programs (or small routines), as in the case of a quadrature program where the ‘argument’ is a function. This information is usually provided when calling-in the subprogram, the parameters being known as program-parameters. This is to distinguish them from preset parameters which are set during input and apply to every occasion that the subprogram is called in. (See Wilkes, Wheeler, and Gill, The Preparation of Programmes for an Electronic Digital Computer, 1st edition, p. 23.) Program-parameters

The preserve and restore instructions enable a subchapter to use the working store quite independently of the master chapter, and hence the names of the main variables (e.g. a ---t 19) in the subchapter need not bear any relation to those in the master chapter. The special variables, however, a, b, c, . , .) a’, b’, c‘, , . ., and the indices i, j, k, . . ., all refer

MERCURY

Autocode

101

to fixed locations in the working store and it is by means of these that the master chapter communicates with the subchapter. Thus after the preserve instruction in the main chapter these quantities can be reset to new values which are then available to the subchapter, which will be designed to select its information in accordance with the following conventions. (0) Individual

arguments,

field dimensions,

etc., will be selected from

abcdefghuvwxyz ijklmnopqrst (b) Fields

of numbers (e.g. vectors, matrices) will be kept in the auxiliary store, and their locations (i.e. the location of 1st element or other reference position) specified by primed variables, i.e. a’b’c’d’elflg’h’u’v’WlXlylZI

The primed variables will also specify where the subprogram is to place its results, which must all be transferred to the auxiliary store before returning to the main program. This applies to individual numbers as well as sets of numbers, for otherwise, if left in the working store, they would be destroyed by the subsequent restore instruction in the main program. Instead they are recovered from the auxiliary store by means of +s instructions after restoring the contents of the working store. The steps to be followed when calling in a subprogram are therefore as follows : 1. preserve ; 2. transfer arrays of data to auxiliary store; 3. set the program-parameters; 4,. call in the subchapter (down) ; 5. restore; 6. recover results from auxiliary store. Very often the data is already located in the auxiliary store, as would be the case, for example, when using the matrix operations (see Ref. 3). In such cases steps 2 and/or 6 may be omitted. Example. Consider a program for the best solution of a set of linear ‘equations’ in the sense of least squares. Let these be J$aijx,

= 6,

i=

l(l)m,m>n

j=l

The program

would have to be supplied with the numerical values of to the coefficient matrix and the vector of sides. Thus, for instance, it may assume that ai, stands

m and n, and means to refer

the right-hand 8

R. A. Brooker

102 in a’ + (; -

1)fz + (j -

specified on entry.

l),

and

bi

in

b’ + (; -

I),

a’,

being

b’,

In addition the program may require further variables

as working space in which to set up the normal equations, for this would have to be made in the main program.

and provision

Finally

provision

for the results xi has to be made. The specification

of the program-parameters

follows : m, n field dimensions a’, b’ location of coefficient WI X'

read as

and r.h.s. vector

first of &z(n + 1) consecutive auxiliary variables* location of result vector (xi will be placed in x’ + (; -

The following program

matrix

might therefore

sequence

illustrates,

for a particular

1)).

case, how such a

would be called in from the main program

preserve m = 70 n = 50 a’ = 0 b’ = 3500 x’ = 3570 W’ = 3620 down ?I? -+ enters subprogram restore t returns from subprogram A(3570)

The

last instruction

down instruction introduce

recovers

has been

the results from the auxiliary deliberately

left incomplete

store.

The

as it serves

to

the assembly facilities.

Assembly of the program tape In order that subprograms essential

can be prepared

to have some system whereby

as library

cross references

tapes,

between

it is

the con-

stituent chapters can be made independent of the final chapter numbers which they may assume. More precisely it is convenient, when writing a subprogram, to call the constituent chapters 1, 2, 3, etc., and leave it to the assembly program to adjust the across and down instructions to take * A desirable minimum. This assumes that only the distinct elements of the symmetric coefficient array are calculated, and that the r.h.s. can be stored in the space allocated for the result vector, x.

MERCURY

Autocode

103

account of the fact that they will eventually become r + 1, r + 2, r + 3, etc. This can now be effected by a special directive of the form programme

-N

which is placed at the head of the group of chapters concerned. It also serves to associate a number N with the subprogram which can be referred to when calling it in. For this purpose the down and across instructions exist in extended versions, e.g. down l/2 -N which enters the subprogram N at the instruction labelled 1 in chapter The across instructions can be used in a similar fashion. To illustrate these facilities the tape layout of a typical problem shown below (absolute chapter numbers are given in parentheses). programme (1)

-chapter

(2)

Ichapter

-1

(5) programme (6)

(chapter1

(7)

-chapter programme

(8)

-550

/ programme

(9)

-2

-584

Ichapter

(10)

1 chapter

2 1

(11)

1 chapter

3 1

1 chapter

0 1

2. is

R. A. Brooker

104

(The last chapter, 0, is the means by which the program is initiated: it is automatically entered at the first instruction.) Any program can be assigned a number in the range I-1023, and it is suggested that the range 501 upwards be reserved for library programs. Thus in the above example the first two programs would have been specially written for the problem, while the last two programs would be drawn from the library. In order to illustrate how the subprogram itself is prepared we give below the details of a subchapter (program - 1, say) for calculating the sum of the absolute values of the elements of a matrix (A,,). It is assumed that the matrix is recorded in the auxiliary store, the location of .4ij being given by u’ + (; - 1)n +j - 1, for ;,j = l(l)n. It is required to place the result in the auxiliary location specified by e’. (subchapter)

(main chapter)

preserve a’ = ? e’ =

?

down l/l -

1

programme -1 chapter 1 n 3 479 1)x = 0 i = l(l)?r &(a + in - n)a,, n j= l(l)n a = 0 mod (aj)

restore

x=x+a

&j! ?) ?, 1

repeat repeat W’)x, UP close

1

clears ‘accumulator’ transfers ith row of matrix to a,, . * *,a, accumulates values

absolute

records result

Auxiliary routines

Consider, for example,

a program S.W(x,r>

to evaluate

a triple integral

2) dx dv dz

Depending on the actual process used, the integrand has to be calculated at certain specified points x,3’, z so that if the program is to be used for a ‘general function this must be calculated by an auxiliary routine provided by the user. This should be designed so that, given the particular arguments x, y, z the routine calculates the corresponding function value and places it in a pre-assigned location. If there is a substantial amount of calculation involved, the auxiliary routine can be a separate chapter and treated as a sub-subprogram of the main program. The

MERCURY Autocode

105

program-parameters of the ‘function’ program would be the arguments x, y, z and the auxiliary location (sayf’) of the resultf(x, y, z). Alternatively the function program can be called in by an across instruction, in which case it must also terminate with an across instruction, returning control to a pm-assigned point in the library program. This arrangement has the advantage that it confines the whole integration process to one ‘level’. In both cases the library program will assign a definite program number to the auxiliary ‘function’ chapter because it is not (easily) possible to make this a program-parameter.

It is not likely that the library program will make use of all the special variables and indices, in which case those that remain undisturbed can serve as ‘by-pass’ program parameters linking the main program directly with the auxiliary function program (whether treated as a sub or across This may be useful if the function program of the library program). involves certain parameters, e.g. f(x, y, z; a, 6, c) and it is desired to specify the values a, b, c in the main program before starting the integration process. The auxiliary chapter may also consist of several different function routines, any of which can be selected from the main program by using a ‘by-pass’ index to serve as a multiway switch in the function chapter, as illustrated by the following diagram. main level

sublevel

auxiliary

chapter

(sub-subIeve1)

m=2 I)--

r(r --____

2)-

3)-

106 Incomplete chapters If the ‘function’ is of a fairly simple nature it is desirable to incorporate the auxiliary routine as an integral part of the library chapter in which it is used, and eliminate the operations of chapter changing and preserve and restore. The quadrature program -503, e.g. consists of two chapters, the first of which computes the coefficients of the quadrature formula, while the second carries out the integration proper. It is intended that the instructions for calculating the integrand shall be included in the second chapter, which is otherwise only about one-third full. The structure of the program is therefore as follows: programme

-503

on library tape

provided

bY

close In such a program it is obviously necessary to know how much of the chapter space is available for the auxiliary sequence, what labels can be used, and so on. It is convenient to assume that it would be entered at the first instruction and terminated by returning control to a pre-assigned point in the first part of the chapter, e.g. label 127. Once again communication with the auxiliary sequence is by means of the special variables and indices, but since there is now no preserve instruction, these are limited to those which are not used for other purposes by the first part of the chapter. On the other hand, however, the absence of the restore operation means that they can now be used for communication in both ~~$ectio~s,so that, e.g. a function value calculated by the auxiliary sequence can be recorded directly as a working variable or index. Those available special variables and indices which are not used

MERCURY Autocode

107

to communicate between the two parts of the chapter can serve as ‘bypass’ parameters to link the main program with the auxiliary sequence. As already explained this allows the quadrature program to be used on different integrands, although the multiway switch device described in the previous section is relatively time-consuming in the present context Instead the different function [R.) = m) takes 18 msec to execute]. sequences can be called in by a series of conditional jump instructions, thus jumpl,m=l jump2,m=2 jump3,m=3 etc. This is particularly suitable if there are only two or three different functions involved. It is necessary to say something about the directives which are written at the head of the auxiliary sequence. These are limited only in their extent, i.e. the total number of variables specified must not exceed the number left unspecified in the fnst part of the chapter. There is no limitation, however, in the letters which can be employed, and if necessary these may duplicate those used in the first part. Thus, e.g. we may write: chapter

1

u-3 119 63 119

(O-1 19) (120-239)

a+ 119 c -+ 119

(240-359) (360-479)

m

IEpart close The duplicate ‘a’ directive simply means that wherever (say) as occurs in the first part of the chapter it refers to the absolute location 3, whereas in the second part it refers to location 243. To summarize : in order to be able to write an auxiliary sequence for a library program of the kind considered above, the specification must include the following information: I. proportion of chapter avaifable 2. permissible range of labels 3. entry* and exit * Unless otherwise sequence.

(or number

of registers)

specified this would be the first instruction,

i.e. at the head of the

R. A. Booker

108 4. permissible

working space

(i) number of main variables available (ii) list of special variables and indices available meters) 5. program-parameters to place the results)

(by-pass para-

(i.e. where to find the arguments

and where

A further example of a chapter embodying an auxiliary sequence is the following program for the forward integration of a set of differential equations. It also serves to illustrate the formal specification of a library program. Tabular solution of dyerential

equations : program -508

This program will tabulate the solution of a set of differential

d-b,

=A(_hh,

dx

. .

dn;

equations

xl, i = l(l)n

at a uniform interval d, starting from the initial conditionsy,(x,,) at x = x,,. The step-by-step Runge-Kutta process is used. To ensure a given accuracy in each interval, the results of taking first p steps of size d/p, and then (p + 1) steps of size d/(p + l), are compared. If these differ by less than a pre-assigned small quantity e (a program-parameter) we proceed to the next interval; otherwisep is advanced by 1 until agreement is obtained, or until there is no further improvement. (In this case e is For the first interval we start with replaced by a more realistic quantity.) p = 1, and in subsequent intervals we start with a value ofp less than or equal to that which proved successful in the previous interval. Thus the program always seeks to reduce the number of steps performed. The final value of e (which is adjusted in a monotonically increasing fashion from one interval to the next) is recorded. The program requires an auxiliary sequence to calculate the fi’s. This forms the second part of the single chapter program, which is made up as follows : programme -508

/

lScthP,;:rof I) on library tape

El/) -1

close

punched ’

by user

MERCURY Specijcation

1. 2. 3. 4.

Autocode

109

of auxiliary sequence

chapter space available : 37 1 registers labels 3)-99) available conventional entry: exit by jumping to label 101) permissible working space (i) 240 main variables (but directive letters y and f must not be used). (ii) by-pass parameters : all special variables and indices other than h, x, 1, o.

5. the auxiliary sequence must be designed to placeJ).(_y,, y?, . . .,y,,; x) infi, i = l(l)n. Program-parameters y’

assumes x,, is placed in y’, and yi(xo) in y’ + t (Subsequently the program places x,, + jd in y’ + j(n + 1) and

yi(xo+ jd) iny’ + j(n + 1) + n number of equations (< 30) m number of steps d tabular interval e accuracy required e’ final accuracy placed in e’

i.1

To call in the program

down l/1-508 down 2/l-508

simply generates the solution generates the solution but also prints the values of p and the comparative accuracy for each step (in a selfexplanatory form).

Time of execution. This depends on the nature of the auxiliary sequence : no example is yet available as the program is still under development. Other library programs

A very useful set of programs, drawn up on similar lines to the above, has been written by Mr. R. Kerr, of the Computing Machine Laboratory, University of Manchester. These are concerned with analytical processes such as quadrature, harmonic analysis, summation of series, etc. The majority of these are single chapter programs. Even a single chapter, however, is a comparatively large unit, and it has been possible to build in a good deal of automatic analysis, e.g. to recognize the noise level in a difference table, that is the point at which rounding errors invalidate

110

R. A. Brooker

The availability of such programs, which also take further differencing. account of all ‘special cases’, promise to simplify considerably the preparation of many scientific and engineering calculations for the computer. ACKNOWLEDGEMENTS

I am grateful to Mr. A. Davey for helping me to develop the program -508 described here. REFERENCES The Manchester MERCURY 1. 2. 3. 4.

Autocode System

BROOKER,R. A., Basic Facilities. BROOKER,R. A. and RICHARDS,B., Operational Facilities. BERG,E. and BROOKER,R. A., Matrix Operations. BROOKER,R. A. and KERR, R., The Autocode Library.

Privately distributed by the Computing Machine Laboratory, Manchester, 13. Copies available on request.

The University,