Computation as a chain of mappings

We usually think of a computational method as a formula, or as an algorithm. It is useful to take a different view: a computational method is a chain of mappings. Starting from a problem stated in one representation, we transform it into another representation in which some operation becomes easy, then into another one, and so on, until we arrive at a form that the machine can execute efficiently. Each mapping is chosen so that the next one becomes possible: the design of a computational method is largely the choice of a sequence of representations.

A familiar example: the compiler

Mapping chains are not specific to numerical simulation. The most familiar example is a compiler, which maps a program through a sequence of representations down to machine code [1][2]. The compiler receives the source text: a sequence of characters which has a meaning only for the programmer. The first mapping is the lexical analysis: the sequence of characters is transformed into a list of tokens, each of which is recognized as meaningful by the compiler. Consider the following piece of C code:

int main( int argc, char *argv[] ) { printf( "%f\n", sin( M_PI / 3. ) ); return 0; }

Its token list is:

intmain(intargc,
char*argv[])
{printf("%f\n",sin
(M_PI/3.))
;return0;}

This sequence of tokens is then passed to the parser, which builds an internal representation of the program: the code flow, that is the order in which the instructions must be executed, and the numerical expressions, stored as directed acyclic graphs, or DAGs. For instance the expression $2*(a+5)$ is represented by the following DAG:

*2+a5

This representation makes the structure of the computation explicit, and it is the input of the optimizer. Constant expressions are evaluated at compilation time: in the example above the value of $\sin(\pi/3)$ is calculated by the compiler, so that no call to the sin function will be necessary at execution time. Repeated sub-expressions are calculated only once and stored in a temporary; function calls can be substituted by the function body; short loops can be unrolled. After the optimization the DAGs are translated into assembler code, and finally into machine code. The translation, however, is incomplete: the calls to functions which are not defined in the source file, like printf and sin, are left as instructions like call func, to be resolved later. It is the job of the linker to resolve them: the linker takes the object file, searches the libraries for the functions called by the program, and substitutes the calls with machine instructions that jump to the memory locations where the library functions are stored.

Each stage of this chain exists because it makes the next stage simple: tokens make parsing possible, the code flow and the DAGs make optimization local and machine independent, the object representation makes linking systematic. If we handed the character sequence directly to the machine code generator, the task would be hopeless.

The chain of a simulation

A computer simulation follows a similar pattern. The starting representation is a law of nature expressed as an equation, typically a differential equation: for instance $\frac{dx(t)}{dt} = f(t, x(t))$ for an ordinary differential equation, or the Laplacian equation for a stationary problem. The first mapping discretizes it: the continuum is replaced by a finite representation, the independent variable by a discrete sequence of steps, the fields by arrays of numbers, the derivatives by difference formulas or by integrals over small elements. The second mapping reformulates the discretized problem into an alternative form that makes its structure explicit: a matrix form for the integration of ODEs, a set of coefficient tables for multistep methods, a master equation for stochastic processes, a weak form for the finite elements. The third mapping turns the reformulated problem into algebraic computation: matrix operations, sparse tables, eigenvalue problems, linear systems. The final mapping is the fast execution of these algebraic operations: vectorized instructions, multithreading, GPUs, direct or iterative solvers.

Exact and approximating mappings

Here we find the important difference between compiling a program and simulating a phenomenon. The mappings of a compiler are exact: each of them preserves the meaning of the program, and the machine code produced by a correct compiler is, from the point of view of the result, indistinguishable from the source. The mappings of a simulation are approximating: the continuum is replaced by a finite grid, an infinite expansion by a truncated one, real arithmetic by floating point arithmetic.

The consequence of these compounding approximations is the accumulation of errors into the final answer, namely the question whether the initial equation is the right description of the phenomenon, plus the error introduced by each mapping: the discretization error, the error of the formulation, the round-off of the arithmetic. The end-to-end error is the interplay of all of these, but it can be controlled: each mapping comes with an error estimate, an order of accuracy, a stability condition, a convergence result, and the design of a good method consists in balancing them.

This is also where the two uses of simulation discussed in Why simulate? meet. The research use stands along the whole chain: each mapping must be under control, because the mechanism we are studying must not be an artifact of one of the mappings. The engineering use stands at the end of the chain: what matters there is the validated end-to-end error of the forecast.

References

[1] Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman. Compilers: Principles, Techniques and Tools. Addison-Wesley (1986).

[2] Christopher W. Fraser and David R. Hanson. A Retargetable C Compiler: Design and Implementation. Addison-Wesley (1995).