source: tests/array-container/array-basic.cfa @ 6528d75

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6528d75 was c7625e0, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Adding manged-length arrays

  • Property mode set to 100644
File size: 3.8 KB
Line 
1#include <containers/array.hfa>
2
3//
4// Type-theory demo (success is "it compiles")
5//
6
7forall( ztype(Nx), ztype(Ny), ztype(Nz) )
8void typesTest( tag(Nx), tag(Ny), tag(Nz) ) {
9
10    array( float, Nx, Ny, Nz )  xyz;
11
12    // numeric subscripts
13    ptrdiff_t ix = 1, iy = 2, iz = 3;
14    array( float, Ny, Nz ) & yz = xyz[ix];
15    array( float, Nz ) & z = xyz[ix][iy];
16    float & val = xyz[ix][iy][iz];
17
18    // deferral subscript
19    typeof( xyz[all] ) yzx = xyz[all];
20
21    // longform check that -[all] gives intended type (user doesn't write this)
22    arpk( Ny
23        , array( float, Nz)
24        , arpk( Nz
25              , float
26              , arpk( Nx
27                    , array( float, Ny, Nz )
28                    , float
29                    , float
30                    )
31              , float
32              )
33        , float
34        )
35        & yzx_long = xyz[all];
36    & yzx_long = & yzx;
37    & yzx  = & yzx_long;
38}
39
40//
41// Runtime demo
42//
43
44#include <assert.h>
45
46float getMagicNumber( ptrdiff_t w, ptrdiff_t x, ptrdiff_t y, ptrdiff_t z ) {
47
48    assert( 0 <= w && w < 3 );
49    assert( 0 <= x && x < 4 );
50    assert( 0 <= y && y < 5 );
51    assert( 0 <= z && z < 6 );
52
53    float ww = (2.0f \ w) / 1.0f;
54    float xx = (2.0f \ x) / 100.0f;
55    float yy = (2.0f \ y) / 10000.0f;
56    float Nz = (2.0f \ z) / 1000000.0f;
57
58    return ww+xx+yy+Nz;
59}
60
61forall( ztype(Nw), ztype(Nx), ztype(Ny), ztype(Nz) )
62void fillHelloData( array( float, Nw, Nx, Ny, Nz ) & wxyz ) {
63    for (w; z(Nw))
64    for (x; z(Nx))
65    for (y; z(Ny))
66    for (z; z(Nz))
67        wxyz[w][x][y][z] = getMagicNumber(w, x, y, z);
68}
69
70forall( ztype(Zn)
71      , S & | sized(S)
72      )
73float total1d_low( arpk(Zn, S, float, float ) & a ) {
74    float total = 0.0f;
75    for (i; z(Zn))
76        total += a[i];
77    return total;
78}
79
80forall( A & | ar(A, float) )
81float total1d_hi( A & a ) {
82    float total = 0.0f;
83    for (i; a`len)
84        total += a[i];
85    return total;
86}
87
88forall( ztype(Nw), ztype(Nx), ztype(Ny), ztype(Nz) )
89void runtimeTest( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
90
91    array( float, Nw, Nx, Ny, Nz ) wxyz;
92    fillHelloData(wxyz);
93
94    float expect, result;
95    ptrdiff_t slice_ix = 1;
96
97    // summing across W, with x=y=z=1
98
99    expect = 0;
100    for (i; z(Nw))
101        expect += getMagicNumber( i, slice_ix, slice_ix, slice_ix );
102    printf("expect Ws             = %f\n", expect);
103
104    result = total1d_low( wxyz[all][slice_ix][slice_ix][slice_ix] );
105    printf("result Ws [][][][] lo = %f\n", result);
106
107    // fixme: -[[-,-,-,-]] not working
108    // result = total1d_low( wxyz[[all, slice_ix, slice_ix, slice_ix]] );
109    // printf("result Ws [,,,]    lo = %f\n", result);
110
111    result = total1d_hi( wxyz[all][slice_ix][slice_ix][slice_ix] );
112    printf("result Ws [][][][] hi = %f\n", result);
113
114    // fixme: -[[-,-,-,-]] not working
115    // result = total1d_hi( wxyz[[all, slice_ix, slice_ix, slice_ix]] );
116    // printf("result Ws [,,,]    hi = %f\n", result);
117
118    // summing across X, with w=y=z=1
119
120    expect = 0;
121    for (i; z(Nx))
122        expect += getMagicNumber( slice_ix, i, slice_ix, slice_ix );
123    printf("expect Xs             = %f\n", expect);
124
125    result = total1d_low( wxyz[slice_ix][all][slice_ix][slice_ix] );   
126    printf("result Xs [][][][] lo = %f\n", result);
127
128    // fixme: -[[-,-,-,-]] not working
129    // result = total1d_low( wxyz[[slice_ix, all, slice_ix, slice_ix]] );
130    // printf("result Xs [,,,]    lo = %f\n", result);
131
132    result = total1d_hi( wxyz[slice_ix][all][slice_ix][slice_ix] );   
133    printf("result Xs [][][][] hi = %f\n", result);
134
135    // fixme: -[[-,-,-,-]] not working
136    // result = total1d_hi( wxyz[[slice_ix, all, slice_ix, slice_ix]] );
137    // printf("result Xs [,,,]    hi = %f\n", result);
138
139}
140
141const size_t  KW = 3,  KX = 4,  KY = 5,  KZ = 6;
142
143int main() {
144
145    typesTest  (           ztag(KX), ztag(KY), ztag(KZ) );
146    runtimeTest( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
147}
Note: See TracBrowser for help on using the repository browser.