COMPILER TECHNIQUES FOR MATLAB PROGRAMS ... - CiteSeerX
COMPILER TECHNIQUES FOR MATLAB PROGRAMS ... - CiteSeerX
COMPILER TECHNIQUES FOR MATLAB PROGRAMS ... - CiteSeerX
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
S1: Ap = A p<br />
S2: alpha = (r' p) / (p' Ap)<br />
S3: x = x + alpha p<br />
S4: r = r + alpha Ap<br />
Figure 3.5: <strong>MATLAB</strong> code segment to compute conjugate gradient.<br />
A simpler approach would be to consider all variables that cannot have their type inferred<br />
statically by the compiler as having intrinsic type complex One problem with this approach<br />
is that for each of these undened variables, the type will have to be propagated as a<br />
complex variable, and this propagation will tend to perturb the inference process.<br />
For<br />
example, consider the <strong>MATLAB</strong> code segment from a function that computes conjugate<br />
gradient, as presented in Figure 3.5.<br />
Suppose that the type of A is unknown and that the compiler assumes it to be of type<br />
complex. Assume also that all other variables are known to be of type real. We notice<br />
that the variable A appears only once in this code segment however, the compiler will have<br />
to consider Ap, alpha, x, and r as complex variables because of its assumption about the<br />
type of A.<br />
The generation of dynamic code is avoided whenever there is an assignment that redenes<br />
avariable, changing its intrinsic type. To this end, the variable is renamed and a second<br />
declaration for the variable is generated. Thus, for example, for the pseudo-code presented<br />
in Figure 3.6, the compiler will declare statically three instances of the variable, one for each<br />
intrinsic type assumed by thevariable, and generate the code as shown in gure 3.7.<br />
Dynamic Shape and Rank Inference<br />
The second step of the dynamic phase is the dynamic shape and rank inference. During<br />
this step, dynamic code is generated to compute during run-time the necessary space for the<br />
dynamic allocation. To thisend,two shadowvariables are used to keep track ofvariable<br />
dimensions during execution-time. So, for example, if the shape of B in an assignment of<br />
27