Changeset 1037b4c
- Timestamp:
- Apr 27, 2026, 2:03:17 AM (2 days ago)
- Branches:
- master
- Children:
- eaee33e
- Parents:
- 037dc57
- Location:
- doc/theses/mike_brooks_MMath
- Files:
-
- 3 edited
-
array.tex (modified) (25 diffs)
-
programs/hello-accordion.cfa (modified) (1 diff)
-
programs/hello-md.cfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/array.tex
r037dc57 r1037b4c 19 19 \end{cfa} 20 20 Here, the arguments to the @array@ type are @float@ (element type) and @99@ (dimension). 21 When this type is used as a function parameter, the type -system requires the argument isa perfect match.21 When this type is used as a function parameter, the type system requires the argument to be a perfect match. 22 22 \begin{cfa} 23 23 void f( @array( float, 42 )@ & p ) {} $\C{// p accepts 42 floats}$ … … 65 65 } 66 66 \end{cfa} 67 Both of the stack declared array variables, @x1@ and @x2@, have 42 elements, each element being a @float@.67 Both of the variables, @x1@ and @x2@, name 42 contiguous, stack-allocated @float@ values. 68 68 The two variables have identical size and layout, with no additional ``bookkeeping'' allocations or headers. 69 69 The C array, @x1@, has no subscript checking, while \CFA array, @x2@, does. 70 Providing this explicit generic approach requires a significant extension to the \CFA type system to support a full-feature, safe, efficient (space and time) array-type, which forms the foundation for more complex array forms in \CFA.70 This explicit, generic approach requires a significant extension to the \CFA type system to support a full-feature, safe, efficient (space and time) array type, which forms the foundation for more complex array forms in \CFA. 71 71 In all following discussion, ``C array'' means types like @x1@ and ``\CFA array'' means types like @x2@. 72 72 73 73 A future goal is to provide the new @array@ features with syntax approaching C's (declaration style of @x1@). 74 74 Then, the library @array@ type could be removed, giving \CFA a largely uniform array type. 75 At present, the C-syntax @array@ is only partially supported, so the generic @array@ is used exclusively in the thesis.75 At present, new-array features are only partially supported through C-style syntax, so the generic @array@ is used exclusively in the thesis. 76 76 77 77 My contributions in this chapter are: 78 78 \begin{enumerate}[leftmargin=*] 79 79 \item A type system enhancement that lets polymorphic functions and generic types be parameterized by a numeric value: @forall( [N] )@. 80 \item Provide a dimension/subscript-checked array -type in the \CFA standard library, where the array's length is statically managed and dynamically valued.80 \item Provide a dimension/subscript-checked array type in the \CFA standard library, where the array's length is statically managed and dynamically valued. 81 81 \item Provide argument/parameter passing safety for arrays and subscript safety. 82 82 \item Identify the interesting abilities available by the new @array@ type. 83 \item Where there is a gap concerning this feature's readiness for prime-time, identification of specific workable improvements that are likely to close the gap.84 83 \end{enumerate} 85 84 … … 121 120 122 121 \begin{figure} 122 \begin{cfa} 123 forall( [N] ) array( bool, N ) & f( array( float, N ) &, array( float, N ) & ); 124 \end{cfa} 123 125 \lstinput{30-43}{hello-array.cfa} 124 126 \lstinput{45-48}{hello-array.cfa} 125 \caption{ \lstinline{f} Example}127 \caption{Example of calling a dimension-generic function.} 126 128 \label{f:fExample} 127 129 \end{figure} … … 144 146 \item 145 147 The value of an @N@-expression is the acquired length, derived from the usage site, \ie generic declaration or function call. 148 \begin{comment} % was not discussed yet 146 149 \item 147 150 @array( T, N0, N1, ... )@ is a multi-dimensional type wrapping $\prod_i N_i$ contiguous @T@-typed objects. 151 \end{comment} 148 152 \end{itemize} 149 153 150 \VRef[Figure]{f:TemplateVsGenericType} shows @N@ is not the same as a @size_t@ declaration in a \CC \lstinline[language=C++]{template}.\footnote{ 151 The \CFA program requires a snapshot declaration for \lstinline{n} to compile, as described at the end of \Vref{s:ArrayTypingC}.} 154 \VRef[Figure]{f:TemplateVsGenericType} shows @N@ is not the same as a @size_t@ declaration in a \CC \lstinline[language=C++]{template}. It illustrates these differences: 152 155 \begin{enumerate}[leftmargin=*] 153 156 \item … … 158 161 \item 159 162 \label{p:DimensionPassing} 160 The \CC template @N@ must be passed explicitly at the call, unless @N@ has a default value, even when \CC can deduc tthe type of @T@.163 The \CC template @N@ must be passed explicitly at the call, unless @N@ has a default value, even when \CC can deduce the type of @T@. 161 164 The \CFA @N@ is part of the array type and passed implicitly at the call. 162 165 % fixed by comparing to std::array 163 166 % mycode/arrr/thesis-examples/check-peter/cs-cpp.cpp, v2 167 \begin{comment} % does not appear in the example; I think we've found CFA suffering a similar "belief" 164 168 \item 165 169 \CC cannot have an array of references, but can have an array of @const@ pointers. … … 167 171 In the \CC example, the arrays fall back on C arrays, which have a duality with references with respect to automatic dereferencing. 168 172 The \CFA array is a contiguous object with an address, which can be stored as a reference or pointer. 173 \end{comment} 169 174 % not really about forall-N vs template-N 170 175 % any better CFA support is how Rob left references, not what Mike did to arrays … … 189 194 for ( int i = 0; i < N; i += 1 ) ret[i] = x[i]; 190 195 } 191 int main() {192 const size_t n = 10; $\C[1.9in]{// must be constant}$193 int ret[n], x[n]; $\C {// static size}$196 void demo() { 197 const size_t n = 10; 198 int ret[n], x[n]; $\C[1.9in]{// static size}$ 194 199 for ( int i = 0; i < n; i += 1 ) x[i] = i; 195 200 @copy<int, n >( ret, x );@ 196 201 for ( int i = 0; i < n; i += 1 ) cout << ret[i] << ' '; 197 202 cout << endl; 203 } 204 int main() { 205 demo(); 198 206 } 199 207 \end{c++} … … 205 213 for ( i; N ) ret[i] = x[i]; 206 214 } 207 size_t n; 208 sin | n; 209 array( int, n ) ret, x; $\C{// dynamic-fixed size}$ 210 for ( i; n ) x[i] = i; $\C{// initialize}$ 211 @copy( ret, x );@ $\C{// alternative ret = x}$ 212 for ( i; n ) sout | ret[i] | nonl; $\C{// print}\CRT$ 213 sout | nl; 215 void demo( const size_t n ) { 216 217 array( int, n ) ret, x; $\C{// dynamic-fixed size}$ 218 for ( i; n ) x[i] = i; $\C{// initialize}$ 219 @copy( ret, x );@ $\C{// alternative ret = x}$ 220 for ( i; n ) sout | ret[i] | nonl; 221 sout | nl; 222 } 223 size_t n; sin | n; $\C{// obtain size}\CRT$ 224 demo( n ); 214 225 } 215 226 \end{cfa} … … 247 258 \CFA allows length-parameterized array members to be nested at arbitrary locations, with intervening member declarations. 248 259 \lstinput{10-15}{hello-accordion.cfa} 249 The structure has course- and student-level meta tdata (their respective field names) and a position-based preferences' matrix.250 Its layout has the starting offset of @studentIds@ varying according to the generic parameter @C@, and the offset of @preferences@ varying according to both generic parameters.260 The structure has course- and student-level metadata (their respective field names) and a position-based preferences' matrix. 261 The structure's layout is dynamic; the starting offset of @student_ids@ varies according to the generic parameter @C@; the offset of @preferences@ varies by both. 251 262 252 263 \VRef[Figure]{f:checkExample} shows a program main using @School@ and results with different array sizes. … … 299 310 300 311 \section{Single Dimension Array Implementation} 301 302 The core of the preexisting \CFA compiler already has the ``heavy equipment'' needed to provide the feature set just reviewed (up to bugs in cases not yet exercised). 312 \label{s:1d-impl} 313 314 The core of the preexisting \CFA compiler already has the ``heavy equipment'' needed to provide the feature set just reviewed. 303 315 To apply this equipment in tracking array lengths, I encoded a dimension (array's length) as a type. 304 316 The type in question does not describe any data that the program actually uses at runtime. … … 309 321 % Furthermore, the @array@ type itself is really ``icing on the cake.'' 310 322 % Presenting its full version is deferred until \VRef[Section]{s:ArrayMdImpl} (where added complexity needed for multiple dimensions is considered). 311 The new array implementation is broken into single and multiple dimensions \see{\VRef[Section]{s:ArrayMdImpl}}. 312 Details of the @array@ macros are sprinkles among the implementation discussion. 313 The single dimension implementation begins with a simple example without @array@ macros. 323 Discussion of the new array implementation is broken into single- and multi-dimensional features. 324 The multidimensional feature set of \VRef[Section]{s:ArrayMdImpl} is a toolkit for managing repeated application of the single-dimensional features. 325 Details of the @array@ type are introduced as needed. 326 The single-dimension discussions begins with a simple example stripped of the @array@ type. 314 327 \begin{cfa} 315 328 forall( [N] ) 316 329 struct array_1d_float { 317 float items[N]; $\C[2in]{// monomorphic type}$330 float items[N]; $\C[2in]{// float elements }$ 318 331 }; 319 332 forall( T, [N] ) 320 struct array_1d _T{321 T items[N]; $\C{// polymorphic type}\CRT$333 struct array_1d { 334 T items[N]; $\C{// any-typed elements}\CRT$ 322 335 }; 323 336 \end{cfa} 324 337 These two structure patterns, plus a subscript operator, is all that @array@ provides. 325 My main work is letting a programmer define such a structure (one whose type is parameterized by @[N]@) and functions that operate on it (these being similarly parameterized).338 My main work is letting a programmer define such a structure (one whose type is parameterized by @[N]@) and define functions that operate on it (these being parameterized similarly). 326 339 327 340 The repurposed heavy equipment is 328 341 \begin{itemize}[leftmargin=*] 329 342 \item 330 Resolver provided values for a declaration's type-system variables, gathered from type information in scope at the usage site.331 \item 332 The box pass, encoding information about type parameters into ``extra'' regular parameters and arguments on declarations andcalls.343 The ``resolver'' compiler pass, which provides argument values for a declaration's type-system parameters, gathered from type information in scope at the usage site. 344 \item 345 The ``box'' compiler pass, which encodes information about type parameters into ``extra'' regular parameters on declarations and and arguments on calls. 333 346 Notably, it conveys the size of a type @foo@ as a @__sizeof_foo@ parameter, and rewrites the @sizeof(foo)@ expression as @__sizeof_foo@, \ie a use of the parameter. 334 347 \end{itemize} … … 337 350 This work is detailed in \VRef[Section]{s:ArrayTypingC}. 338 351 However, the resolution--boxing scheme, in its preexisting state, is equipped to work on (desugared) dimension parameters. 339 The following discussion explains the desugaring and how correctly lowered code results.352 The following discussion explains the desugaring and why its lowered code is correct. 340 353 341 354 A simpler structure, and a toy function on it, demonstrate what is needed for the encoding. … … 429 442 The compiler's action produces the more complex form, which if handwritten, is error-prone. 430 443 431 At the compiler front end, the parsing changesAST schema extensions and validation rules for enabling the sugared user input.444 At the \CFA compiler's front end, changes to the parser are AST schema extensions and validation rules for enabling the sugared user input. 432 445 \begin{itemize}[leftmargin=*] 433 446 \item 434 Recognize the form @[N]@ as a type-variable declaration within a @forall@.435 \item 436 Have the new brand of type-variable, \emph{Dimension}, in the AST form of a type-variable, to represent one parsed from @[-]@.447 Recognize the syntax @forall( ... [N] ... )@ as a type-variable declaration. 448 \item 449 Where the AST's representation of a type variable encodes its \newterm{brand}, previously including full-object types @T@ and opaque datatypes @T&@, define new brand, \newterm{Dimension}. Apply this brand to the new syntax. 437 450 \item 438 451 Allow a type variable to occur in an expression. Validate (after parsing) that only dimension-branded type-variables are used here. … … 614 627 \end{itemize} 615 628 \VRef[Figure]{f:DimexprRuleCompare} gives a case-by-case comparison of the consequences of these rule sets. 616 It demonstrates that the \CFA false alarms occur in the same cases as C treats unsafe .617 It also shows that C-incompatibilities only occur in cases that C treats unsafe .629 It demonstrates that the \CFA false alarms occur in the same cases as C treats unsafely. 630 It also shows that C-incompatibilities only occur in cases that C treats unsafely. 618 631 619 632 \begin{figure} … … 753 766 \end{comment} 754 767 768 These rules being conservative is the reason that \VRef{s:1d-impl} calls the \CFA array type only \emph{limited} dependent. 769 In programming languages featuring general dependent typing, considerably more effort is invested in being able to conclude that two type-parameterizing expressions always compute the same value. 770 While conservatism is inescapable, these languages use good approximations of eventual runtime behaviour to enable conclusions about more dynamic behaviours, such as a program never popping from an empty stack. 771 755 772 The conservatism of the new rule set can leave a programmer requiring a recourse, when needing to use a dimension expression whose stability argument is more subtle than current-state analysis. 756 773 This recourse is to declare an explicit constant for the dimension value. … … 767 784 The @nr@ reference is never written, no implicit code (load) between declarations, and control does not leave the function between the uses. 768 785 As well, the build-in @?+?@ function is predictable. 769 To make these cases work, the programmer must add the follow constant declarations (cast does not work): 786 787 To make these cases work, the programmer adds these constant declarations: 770 788 \begin{cfa} 771 789 void f( int & nr, const int nv ) { … … 780 798 The result is the originally intended semantics, 781 799 achieved by adding a superfluous ``snapshot it as of now'' directive. 800 Note that casting does not help with this issue, because it does not express information about values at different points in time. 782 801 783 802 The snapshot trick is also used by the \CFA translation, though to achieve a different outcome. … … 812 831 \end{cfa} 813 832 Dimension hoisting already existed in the \CFA compiler. 814 However, it was buggy, particularly with determining, ``Can hoisting the expression be skipped here?'' ,for skipping this hoisting is clearly desirable in some cases.815 For example, when a programmer has already hoisted to perform an optimization to prelude duplicate code (expression) and/or expression evaluation.833 However, it was buggy, particularly with determining, ``Can hoisting the expression be skipped here?'' for skipping this hoisting is clearly desirable in some cases. 834 For example, when a programmer has already done manual hoisting. 816 835 In the new implementation, these cases are correct, harmonized with the accept/reject criteria. 817 836 … … 828 847 Fixed-stride memory: 829 848 this model binds the first subscript and the first memory layout dimension, offering the ability to slice by (provide an index for) only the coarsest dimension, @m[row][*]@ or @c[plane][*][*]@, \eg slice only by row (2D) or plane (3D). 830 After which, subscripting and memory layout are independent.831 849 \item 832 850 Explicit-displacement memory: 833 this model has no awareness of dimensions just the ability to access memory at a distance from a reference point (base-displacement addressing), \eg @x + 23@ or @x[23 }@ $\Rightarrow$ 23rd element fromthe start of @x@.851 this model has no awareness of dimensions just the ability to access memory at a distance from a reference point (base-displacement addressing), \eg @x + 23@ or @x[23]@ $\Rightarrow$ 23rd element after the start of @x@. 834 852 A programmer must manually build any notion of dimensions using other tools; 835 853 hence, this style is not offering multidimensional arrays \see{\VRef[Figure]{f:FixedVariable} right example}. 836 854 \end{enumerate} 837 855 838 There is some debate as to whether the abstraction point ordering above goes $\{1, 2\} < 3$, rather than my numeric ally-ordering.856 There is some debate as to whether the abstraction point ordering above goes $\{1, 2\} < 3$, rather than my numeric ordering. 839 857 That is, styles 1 and 2 are at the same abstraction level, with 3 offering a limited set of functionality. 840 858 I chose to build the \CFA style-1 array upon a style-2 abstraction. 841 (Justification of the decision follows, after the description of the design.) 859 Justification of the decision follows, after the description of the design. 842 860 843 861 Style 3 is the inevitable target of any array implementation. … … 846 864 Casting a multidimensional array as a single-dimensional array/pointer, then using @x[i]@ syntax to access its elements, is still a form of pointer arithmetic. 847 865 848 To step into the implementation of \CFA's new type-1 multidimensional arrays in terms of C's existing type-2 multidimensional arrays, it helps to clarify that the interface is low-level, \ie a C/\CFA array interface includes the resulting memory layout.849 Specifically, the defining requirement of a type- 2system is the ability to slice a column from a column-finest matrix.866 To step into the implementation of \CFA's new type-1 multidimensional arrays in terms of C's existing type-2 multidimensional arrays, it helps to clarify that the interface is low-level, \ie a C/\CFA array interface includes memory-layout detail. 867 Specifically, the defining requirement of a type-1 system is the ability to slice a column from a column-finest matrix. 850 868 Hence, the required memory shape of such a slice is fixed, before any discussion of implementation. 851 869 The implementation presented here is how the \CFA array-library wrangles the C type system to make it do memory steps that are consistent with this layout while not affecting legacy C programs. … … 856 874 Beyond what C's array type offers, the new array brings direct support for working with a \emph{noncontiguous} array slice, allowing a program to work with dimension subscripts given in a non-physical order. 857 875 858 The following examples use the matrix declaration @array( float, 5, 7 ) m@, loaded with values incremented by $0.1$, when stepping across the length-7 finely-strided column dimension, and stepping across the length-5 coarsely-strided row dimension.876 The following examples use the matrix declaration @array( float, 5, 7 ) m@, loaded with values incremented by $0.1$, when stepping across the length-7 finely-strided column dimension, and incremented by $1$, when stepping across the length-5 coarsely-strided row dimension. 859 877 \par 860 878 \mbox{\lstinput{121-126}{hello-md.cfa}} … … 880 898 \lstinput{150-151}{hello-md.cfa} 881 899 882 The example shows @x[2]@ and @x[ [2, all]]@ both refer to the same, ``2.*'' slice.883 Indeed, the various @print1d@ calls under discussion access the entry with value @2.3@ as @x[2][3]@, @x[ [2,all]][3]@, and @x[[all,3]][2]@.884 This design preserves (and extends) C array semantics by defining @x[ [i,j]]@ to be @x[i][j]@ for numeric subscripts, but also for ``subscripting by all''.900 The example shows @x[2]@ and @x[2, all]@ both refer to the same, ``2.*'' slice. 901 Indeed, the various @print1d@ calls under discussion access the entry with value @2.3@ as @x[2][3]@, @x[2,all][3]@, and @x[all,3][2]@. 902 This design preserves (and extends) C array semantics by defining @x[i,j]@ to be @x[i][j]@ for numeric subscripts, but also for ``subscripting by all''. 885 903 That is: 886 904 \begin{cquote} 887 905 \begin{tabular}{@{}cccccl@{}} 888 @x[ [2,all]][3]@ & $\equiv$ & @x[2][all][3]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is redundant) \\889 @x[ [all,3]][2]@ & $\equiv$ & @x[all][3][2]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is effective)906 @x[2,all][3]@ & $\equiv$ & @x[2][all][3]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is redundant) \\ 907 @x[all,3][2]@ & $\equiv$ & @x[all][3][2]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is effective) 890 908 \end{tabular} 891 909 \end{cquote} … … 916 934 The semantics of @-[i]@ is to dequeue from the front of the ``want subscripts'' list and lock its value to be @i@. 917 935 918 Contiguous arrays, and slices of them, are all represented by the same underlying parameterized type, which includes stride information in its meta tdata.936 Contiguous arrays, and slices of them, are all represented by the same underlying parameterized type, which includes stride information in its metadata. 919 937 The \lstinline{-[all]} operation takes subscripts, \eg @x[2][all]@, @x[all][3]@, \etc, and converts (transposes) from the base reference @x[all]@ to a specific reference of the appropriate form. 920 938 The running example's @all@-effective step, stated more concretely, is: … … 935 953 \end{figure} 936 954 937 \VRef[Figure]{fig:subscr-all} shows one element (in the whiteband) accessed two different ways: as @x[2][3]@ and as @x[all][3][2]@.955 \VRef[Figure]{fig:subscr-all} shows one element (in the shaded band) accessed two different ways: as @x[2][3]@ and as @x[all][3][2]@. 938 956 In both cases, subscript 2 selects from the coarser dimension (rows of @x@), 939 957 while subscript 3 selects from the finer dimension (columns of @x@). … … 1536 1554 1537 1555 Java arrays are references, so multi-dimension arrays are arrays-of-arrays \see{\VRef{toc:mdimpl}}. 1538 For each array, Java implicitly stor ages the array dimension in a descriptor, supporting array length, subscript checking, and allowing dynamically-sized array-parameter declarations.1556 For each array, Java implicitly stores the array dimension in a descriptor, supporting array length, subscript checking, and allowing dynamically-sized array-parameter declarations. 1539 1557 \begin{cquote} 1540 1558 \begin{tabular}{rl} -
doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa
r037dc57 r1037b4c 12 12 @array@( string, C ) course_codes; $\C{// nested VLAs}$ 13 13 @array@( string, S ) student_ids; 14 @array@( int, C, S ) preferences; $\C{// m ultidimensional}$14 @array@( int, C, S ) preferences; $\C{// matrix, C x S}$ 15 15 }; 16 16 -
doc/theses/mike_brooks_MMath/programs/hello-md.cfa
r037dc57 r1037b4c 119 119 fill( m ); 120 120 /* 121 r /c 0 1 2 3 4 56122 0 0.0 0.1 0.2 @0.3@ 0.4 0.50.6123 1 1.0 1.1 1.2 @1.3@ 1.4 1.51.6124 2 @2.0 2.1 2.2 2.3 2.4 2.52.6@125 3 3.0 3.1 3.2 @3.3@ 3.4 3.53.6126 4 4.0 4.1 4.2 @4.3@ 4.4 4.54.6121 r\c 0 1 2 3 4 5 6 122 0 0.0 0.1 0.2 @0.3@ 0.4 0.5 0.6 123 1 1.0 1.1 1.2 @1.3@ 1.4 1.5 1.6 124 2 @2.0 2.1 2.2 2.3 2.4 2.5 2.6@ 125 3 3.0 3.1 3.2 @3.3@ 3.4 3.5 3.6 126 4 4.0 4.1 4.2 @4.3@ 4.4 4.5 4.6 127 127 */ 128 128
Note:
See TracChangeset
for help on using the changeset viewer.