tutorial_lud

This shows you the differences between two versions of the page.

Next revision | Previous revision Last revision Both sides next revision | ||

tutorial_lud [2014/05/30 11:49] 127.0.0.1 external edit |
tutorial_lud [2018/06/19 15:45] sanjay |
||
---|---|---|---|

Line 1: | Line 1: | ||

- | In this tutorial, we write an Alphabets program, starting from a mathematical equation for LU decomposition. Then we will generate code to execute the alphabets program, and test the generated code for correctness. | + | In this tutorial, we write an Alphabets (or Alpha, for now the two are synonymous) program, starting from a mathematical equation for LU decomposition. Then we will generate code to execute the alpha program, and test the generated code for correctness. |

The equation for LU Decomposition, derived from first principles using simple algebra in {{:foundations.pdf|Foundations}} (pg.3), is as follows: | The equation for LU Decomposition, derived from first principles using simple algebra in {{:foundations.pdf|Foundations}} (pg.3), is as follows: | ||

- | <latex> | + | |

- | $U_{i,j}=\begin{cases} | + | /*<latex>*/ |

+ | $$ | ||

+ | U_{i,j}=\begin{cases} | ||

1=i\le j & A_{i,j}\\ | 1=i\le j & A_{i,j}\\ | ||

1<i\le j & A_{i,j}-\sum_{k=1}^{i-1}L_{i,k}U_{k,j} | 1<i\le j & A_{i,j}-\sum_{k=1}^{i-1}L_{i,k}U_{k,j} | ||

- | \end{cases} | + | \end{cases}\\ |

L_{i,j}=\begin{cases} | L_{i,j}=\begin{cases} | ||

- | 1=j\le i & \frac{A_{i,j}}{U_{j,j}}\\ | + | 1 = i\le j & \frac{A_{i,j}}{U_{j,j}}\\ |

- | 1<i\le j & \frac{1}{U_{j,j}}(A_{i,j}-\sum_{k=1}^{j-1}L_{i,k}U_{k,j}) | + | 1< i\le j & \frac{1}{U_{j,j}}(A_{i,j}-\sum_{k=1}^{j-1}L_{i,k}U_{k,j}) |

- | \end{cases}$ | + | \end{cases} |

- | </latex> | + | $$ |

+ | /*<\latex>*/ | ||

+ | | ||

+ | | ||

+ | [Temp note due to : in the last case of L, the condition is "1 < j <= i"] | ||

- | =====Writing Alphabets===== | + | =====Writing Alpha===== |

====Step 1 : Affine System and Parameters ==== | ====Step 1 : Affine System and Parameters ==== | ||

- | Let's start from an empty alphabets file, with LUD as the name of the system, and a positive integer N as its parameter. | + | Let's start from an empty alpha file, with LUD as the name of the system, and a positive integer N as its parameter. |

- | A system (Affine System) takes its name from system of affine recurrence equations, and represents a block of computation. An Alphabets program may contain multiple systems. | + | A system (Affine System) takes its name from system of affine recurrence equations, and represents a block of computation. An Alpha program may contain multiple systems. |

**Caveat:** Remember the phrase, "It's not a bug, it's a feature"? Well, in a tutorial, a feature is called a "learning opportunity." | **Caveat:** Remember the phrase, "It's not a bug, it's a feature"? Well, in a tutorial, a feature is called a "learning opportunity." | ||

Parameters are runtime constants represented with some symbol in the code. In this example, parameter N will be used to define the size of the matrices, which is not known until runtime. | Parameters are runtime constants represented with some symbol in the code. In this example, parameter N will be used to define the size of the matrices, which is not known until runtime. | ||

- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |

affine LUD {N|N>0} | affine LUD {N|N>0} | ||

. | . | ||

Line 28: | Line 34: | ||

====Step 2 : Variable Declarations==== | ====Step 2 : Variable Declarations==== | ||

- | In most cases, a computation uses some inputs and produces outputs. Such variables must be declared with a name, a data type, and a shape/size. In Alphabets, the shape/size is represented with polyhedral domains. | + | In most cases, a computation uses some inputs and produces outputs. Such variables must be declared with a name, a data type, and a shape/size. In Alpha, the shape/size is represented with polyhedral domains. |

For this example, the ''A'' matrix is given, and we are computing two triangular matrices ''L'' and ''U''. ''A'' is an NxN square matrix. The declaration for ''A'' looks as follows: | For this example, the ''A'' matrix is given, and we are computing two triangular matrices ''L'' and ''U''. ''A'' is an NxN square matrix. The declaration for ''A'' looks as follows: | ||

- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |

float A {i,j|1<=(i,j)<=N}; //starting from 1 to be consistent with the equation in the notes | float A {i,j|1<=(i,j)<=N}; //starting from 1 to be consistent with the equation in the notes | ||

</sxh> | </sxh> | ||

Similarly, ''L'' is a lower triangular matrix of size N (with unit diagonals, implicit) and ''U'' is an upper triangular matrix of size N. The declarations should look like the following: | Similarly, ''L'' is a lower triangular matrix of size N (with unit diagonals, implicit) and ''U'' is an upper triangular matrix of size N. The declarations should look like the following: | ||

- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |

// The convention is that i is the vertical axis going down, and j is the horizontal axis | // The convention is that i is the vertical axis going down, and j is the horizontal axis | ||

float L {i,j|1<i<=N && 1<=j<i}; // Note that the diagonal elements of L are not explicitly declared | float L {i,j|1<i<=N && 1<=j<i}; // Note that the diagonal elements of L are not explicitly declared | ||

Line 40: | Line 46: | ||

</sxh> | </sxh> | ||

Now these variable declarations need to be placed at appropriate places to specify whether they are input/output/local. | Now these variable declarations need to be placed at appropriate places to specify whether they are input/output/local. | ||

- | ''given'' is the keyword for input, ''returns'' is the keyword for output, and ''using'' is the keyword for local variables. | + | ''input''/''given'' is the keyword for input, ''output''/''returns'' is the keyword for output, and ''local''/''using'' is the keyword for local variables. |

- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |

affine LUD {N|N>0} | affine LUD {N|N>0} | ||

- | given | + | input float A {i,j|1<=(i,j)<=N}; |

- | float A {i,j|1<=(i,j)<=N}; | + | output |

- | returns | + | |

float L {i,j|1<=j<i<=N}; | float L {i,j|1<=j<i<=N}; | ||

float U {i,j|1<=i<=j<=N}; | float U {i,j|1<=i<=j<=N}; | ||

Line 142: | Line 147: | ||

L = case | L = case | ||

{i,j|1==j} : (A / (i,j->j,j)@U); | {i,j|1==j} : (A / (i,j->j,j)@U); | ||

- | {i,j|1<i} : (A - reduce(+, (i,j,k->i,j), (i,j,k->i,k)@L*(i,j,k->k,j)@U))/(i,j->i,i)@U; | + | {i,j|1<i} : (A - reduce(+, (i,j,k->i,j), (i,j,k->i,k)@L*(i,j,k->k,j)@U))/(i,j->j,j)@U; |

esac; | esac; | ||

</sxh> | </sxh> | ||

====Final Alphabets Program==== | ====Final Alphabets Program==== | ||

- | Combine all of the above, and you will get the Alphabets program for LU decomposition. Don't forget the keyword ''through'' before equations the period at the end (since our example has no local variables). Notice how we can mix and match Show and AShow syntax within the program, but each equation must obviously, be consistent. | + | Combine all of the above, and you will get the Alphabets program for LU decomposition. Don't forget the keyword ''let''/''through'' before equations the period at the end (since our example has no local variables). Notice how we can mix and match Show and AShow syntax within the program, but each equation must obviously, be consistent. |

- | <sxh alphabets; gutter:false> | + | <sxh alphabets; gutter:true> |

affine LUD {N|N>0} | affine LUD {N|N>0} | ||

- | given | + | input |

float A {i,j|1<=(i,j)<=N}; | float A {i,j|1<=(i,j)<=N}; | ||

- | returns | + | output |

float L {i,j|1<i<=N && 1<=j<i}; | float L {i,j|1<i<=N && 1<=j<i}; | ||

float U {i,j|1<=j<=N && 1<=i<=j}; | float U {i,j|1<=j<=N && 1<=i<=j}; | ||

- | through | + | let |

U[i,j] = case | U[i,j] = case | ||

{|1==i} : A[i,j]; | {|1==i} : A[i,j]; | ||

Line 161: | Line 166: | ||

L = case | L = case | ||

{i,j|1==j} : A / (i,j->j,j)@U; | {i,j|1==j} : A / (i,j->j,j)@U; | ||

- | {i,j|1<i} : (A - reduce(+, (i,j,k->i,j), (i,j,k->i,k)@L*(i,j,k->k,j)@U))/(i,j->j,j)@U; | + | {i,j|1<j} : (A - reduce(+, (i,j,k->i,j), (i,j,k->i,k)@L*(i,j,k->k,j)@U))/(i,j->j,j)@U; |

esac; | esac; | ||

. | . | ||

Line 168: | Line 173: | ||

Analyses, transformations, and code generation of Alphabets programs are performed using the AlphaZ system. The normal interface for using AlphaZ is the scripting interface called compiler scripts. | Analyses, transformations, and code generation of Alphabets programs are performed using the AlphaZ system. The normal interface for using AlphaZ is the scripting interface called compiler scripts. | ||

Given below is an example script for that does several things using the LUD program we wrote above. | Given below is an example script for that does several things using the LUD program we wrote above. | ||

- | <sxh cs; gutter:false> | + | <sxh cs; gutter:true> |

# read program and store the internal representation in variable prog | # read program and store the internal representation in variable prog | ||

prog = ReadAlphabets("./LUD.ab"); | prog = ReadAlphabets("./LUD.ab"); | ||

Line 224: | Line 229: | ||

====OOPS WHAT HAPPENED==== | ====OOPS WHAT HAPPENED==== | ||

You will see that when you execute the code, **//it will produce an error//**. You may be able to easily fix the error in your Alpha program and regenerate correctly executing C code, or you may want a bit of help. In either case, we would like to know. Please email <Sanjay.Rajopadhye@colostate.edu> with the error message that is produced. | You will see that when you execute the code, **//it will produce an error//**. You may be able to easily fix the error in your Alpha program and regenerate correctly executing C code, or you may want a bit of help. In either case, we would like to know. Please email <Sanjay.Rajopadhye@colostate.edu> with the error message that is produced. | ||

+ |

tutorial_lud.txt · Last modified: 2019/04/05 09:02 by sanjay