- Timestamp:
- Aug 16, 2024, 12:03:54 PM (3 months ago)
- Branches:
- master
- Children:
- cef5bfc
- Parents:
- 661e7b0
- Location:
- doc/theses/mike_brooks_MMath
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/array.tex
r661e7b0 r4558df2 205 205 Orthogonally, the new @array@ type works with \CFA's generic types, providing argument safety and the associated implicit communication of array length. 206 206 Specifically, \CFA allows aggregate types to be generalized with multiple type parameters, including parameterized element types and lengths. 207 Doing so gives a refinement of C's ``flexible array member'' pattern, allowing nesting structures with array members anywhere within otherstructures.207 Doing so gives a refinement of C's ``flexible array member'' pattern, allowing nesting structures with array members anywhere within the structures. 208 208 \lstinput{10-15}{hello-accordion.cfa} 209 This structure's layout has the starting offset of @ municipalities@ varying in @NprovTerty@, and the offset of @total_pt@ and @total_mun@ varying in both generic parameters.210 For a function that operates on a @ CanPop@ structure, the type system handles this variationtransparently.209 This structure's layout has the starting offset of @studentIds@ varying in generic parameter @C@, and the offset of @preferences@ varying in both generic parameters. 210 For a function that operates on a @School@ structure, the type system handles this memory layout transparently. 211 211 \lstinput{40-45}{hello-accordion.cfa} 212 \VRef[Figure]{f:checkHarness} shows the @ CanPop@ harness and results with different array sizes, if the municipalities changed after a census.212 \VRef[Figure]{f:checkHarness} shows the @School@ harness and results with different array sizes, where multidimensional arrays are discussed next. 213 213 214 214 \begin{figure} 215 \lstinput{60-68}{hello-accordion.cfa} 216 \lstinput{70-75}{hello-accordion.cfa} 217 \caption{\lstinline{check} Harness} 215 % super hack to get this to line up 216 \begin{tabular}{@{}ll@{\hspace{25pt}}l@{}} 217 \begin{tabular}{@{}p{3.25in}@{}} 218 \lstinput{60-66}{hello-accordion.cfa} 219 \vspace*{-3pt} 220 \lstinput{73-80}{hello-accordion.cfa} 221 \end{tabular} 222 & 223 \raisebox{0.32\totalheight}{% 224 \lstinput{85-93}{hello-accordion.cfa} 225 }% 226 & 227 \lstinput{95-109}{hello-accordion.cfa} 228 \end{tabular} 229 \caption{\lstinline{school} Harness and Output} 218 230 \label{f:checkHarness} 219 231 \end{figure} … … 488 500 From there, @x[all]@ itself is simply a two-dimensional array, in the strict C sense, of these building blocks. 489 501 An atom (like the bottommost value, @x[all][3][2]@), is the contained value (in the square box) 490 and a lie about its size (the wedgeabove it, growing upward).502 and a lie about its size (the left diagonal above it, growing upward). 491 503 An array of these atoms (like the intermediate @x[all][3]@) is just a contiguous arrangement of them, done according to their size; 492 504 call such an array a column. 493 505 A column is almost ready to be arranged into a matrix; 494 506 it is the \emph{contained value} of the next-level building block, but another lie about size is required. 495 At first, an atom needs to be arranged as if it were bigger, but now a column needs to be arranged as if it is smaller (the wedgeabove it, shrinking upward).507 At first, an atom needs to be arranged as if it were bigger, but now a column needs to be arranged as if it is smaller (the left diagonal above it, shrinking upward). 496 508 These lying columns, arranged contiguously according to their size (as announced) form the matrix @x[all]@. 497 509 Because @x[all]@ takes indices, first for the fine stride, then for the coarse stride, it achieves the requirement of representing the transpose of @x@. … … 502 514 compared with where analogous rows appear when the row-level option is presented for @x@. 503 515 504 \PAB{I don't understand this paragraph: These size lies create an appearance of overlap. 505 For example, in \lstinline{x[all]}, the shaded band touches atoms 2.0, 2.1, 2.2, 2.3, 1.4, 1.5 and 1.6. 516 For example, in \lstinline{x[all]}, the shaded band touches atoms 2.0, 2.1, 2.2, 2.3, 1.4, 1.5 and 1.6 (left diagonal). 506 517 But only the atom 2.3 is storing its value there. 507 The rest are lying about (conflicting) claims on this location, but never exercising these alleged claims. }518 The rest are lying about (conflicting) claims on this location, but never exercising these alleged claims. 508 519 509 520 Lying is implemented as casting. … … 511 522 This structure uses one type in its internal field declaration and offers a different type as the return of its subscript operator. 512 523 The field within is a plain-C array of the fictional type, which is 7 floats long for @x[all][3][2]@ and 1 float long for @x[all][3]@. 513 The subscript operator presents what is really inside, by casting to the type below the wedgeof the lie.524 The subscript operator presents what is really inside, by casting to the type below the left diagonal of the lie. 514 525 515 526 % Does x[all] have to lie too? The picture currently glosses over how it it advertises a size of 7 floats. I'm leaving that as an edge case benignly misrepresented in the picture. Edge cases only have to be handled right in the code. -
doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa
r661e7b0 r4558df2 8 8 9 9 10 forall( T, @[NprovTerty]@, @[Nmunicipalities]@ )11 struct CanPop{12 array( T, @NprovTerty@ ) provTerty; $\C{// nested VLA}$13 array( T, @Nmunicipalities@ ) municipalities; $\C{// nested VLA}$14 int total_pt, total_mun;10 forall( [C], [S] ) $\C{// Class size, Students in class}$ 11 struct School { 12 @array( int, C )@ classIds; $\C{// nested VLAs}$ 13 @array( int, S )@ studentIds; 14 @array( int, C, S )@ preferences; $\C{// multidimensional}$ 15 15 }; 16 16 17 17 18 18 19 // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?) 19 20 20 forall( T, [NprovTerty], [Nmunicipalities] )21 void ?{}( T &, CanPop( T, NprovTerty, Nmunicipalities) & this ) {}21 forall( [C], [S] ) 22 void ?{}( School( C, S ) & this ) {} 22 23 23 forall( T &, [NprovTerty], [Nmunicipalities] )24 void ^?{}( CanPop( T, NprovTerty, Nmunicipalities) & this ) {}24 forall( [C], [S] ) 25 void ^?{}( School( C, S ) & this ) {} 25 26 26 27 … … 37 38 38 39 39 40 forall( T, [NprovTerty], [Nmunicipalities] ) 41 void check( CanPop( T, NprovTerty, Nmunicipalities ) & pop ) with( pop ) { 42 total_pt = total_mun = 0; 43 for ( i; NprovTerty ) total_pt += provTerty[i]; 44 for ( i; Nmunicipalities ) total_mun += municipalities[i]; 40 forall( [C], [S] ) 41 void init( @School( C, S ) & classes@, int class, int student, int pref ) with( classes ) { 42 classIds[class] = class; $\C{// handle dynamic offsets of fields within structure}$ 43 studentIds[student] = student; 44 preferences[class][student] = pref; 45 45 } 46 46 … … 58 58 59 59 60 int main( int argc, char * argv[] ) { 61 const int npt = ato( argv[1] ), nmun = ato( argv[2] ); 62 @CanPop( int, npt, nmun ) pop;@ 63 // read in population numbers 64 @check( pop );@ 65 sout | setlocale( LC_NUMERIC, getenv( "LANG" ) ); 66 sout | "Total province/territory:" | pop.total_pt; 67 sout | "Total municipalities:" | pop.total_mun; 60 int main() { 61 int classes, students; 62 sin | classes | students; 63 @School( classes, students ) school;@ 64 int class, student, preference; 65 // read data into school calling init 66 // for each student's class/preferences 67 try { 68 for ( ) { 69 sin | class | student | preference; 70 init( school, class, student, preference ); 71 } 72 } catch( end_of_file * ) {} 73 for ( s; students ) { 74 sout | "student" | s | nonl; 75 for ( c; classes ) { 76 sout | school.preferences[c][s] | nonl; 77 } 78 sout | nl; 79 } 68 80 } 81 82 83 69 84 /* 70 $\$$ ./a.out 13 3573 71 Total province/territory: 36,991,981 72 Total municipalities: 36,991,981 73 $\$$ ./a.out 13 3654 74 Total province/territory: 36,991,981 75 Total municipalities: 36,991,981 85 $\$$ cat school1 86 2 2 87 0 0 1 88 1 0 7 89 0 1 12 90 1 1 13 91 $\$$ a.out < school1 92 student 0 1 7 93 student 1 12 13 94 95 $\$$ cat school2 96 3 3 97 0 0 1 98 1 0 7 99 2 0 8 100 0 1 12 101 1 1 13 102 2 1 14 103 0 2 26 104 1 2 27 105 2 2 28 106 $\$$ a.out < school2 107 student 0 1 7 8 108 student 1 12 13 14 109 student 2 26 27 28 76 110 */ 77 111
Note: See TracChangeset
for help on using the changeset viewer.