[c7625e0] | 1 | #include <containers/array.hfa> |
---|
| 2 | |
---|
| 3 | // |
---|
| 4 | // Type-theory demo (success is "it compiles") |
---|
| 5 | // |
---|
| 6 | |
---|
[b9dae14c] | 7 | forall( [Nx], [Ny], [Nz] ) |
---|
[c7625e0] | 8 | void 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 | |
---|
| 46 | float 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 | |
---|
[b9dae14c] | 61 | forall( [Nw], [Nx], [Ny], [Nz] ) |
---|
[c7625e0] | 62 | void fillHelloData( array( float, Nw, Nx, Ny, Nz ) & wxyz ) { |
---|
[6e50a6b] | 63 | for (w; Nw) |
---|
| 64 | for (x; Nx) |
---|
| 65 | for (y; Ny) |
---|
| 66 | for (z; Nz) |
---|
[c7625e0] | 67 | wxyz[w][x][y][z] = getMagicNumber(w, x, y, z); |
---|
| 68 | } |
---|
| 69 | |
---|
[6e50a6b] | 70 | forall( [N] |
---|
[c7625e0] | 71 | , S & | sized(S) |
---|
| 72 | ) |
---|
[6e50a6b] | 73 | float total1d_low( arpk(N, S, float, float ) & a ) { |
---|
[c7625e0] | 74 | float total = 0.0f; |
---|
[6e50a6b] | 75 | for (i; N) |
---|
[c7625e0] | 76 | total += a[i]; |
---|
| 77 | return total; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | forall( A & | ar(A, float) ) |
---|
| 81 | float total1d_hi( A & a ) { |
---|
| 82 | float total = 0.0f; |
---|
| 83 | for (i; a`len) |
---|
| 84 | total += a[i]; |
---|
| 85 | return total; |
---|
| 86 | } |
---|
| 87 | |
---|
[b9dae14c] | 88 | forall( [Nw], [Nx], [Ny], [Nz] ) |
---|
[c7625e0] | 89 | void 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; |
---|
[6e50a6b] | 100 | for (i; Nw) |
---|
[c7625e0] | 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 | |
---|
[1d71208] | 107 | result = total1d_low( wxyz[all, slice_ix, slice_ix, slice_ix] ); |
---|
[63a4b92] | 108 | printf("result Ws [,,,] lo = %f\n", result); |
---|
[c7625e0] | 109 | |
---|
| 110 | result = total1d_hi( wxyz[all][slice_ix][slice_ix][slice_ix] ); |
---|
| 111 | printf("result Ws [][][][] hi = %f\n", result); |
---|
| 112 | |
---|
[1d71208] | 113 | result = total1d_hi( wxyz[all, slice_ix, slice_ix, slice_ix] ); |
---|
[63a4b92] | 114 | printf("result Ws [,,,] hi = %f\n", result); |
---|
[c7625e0] | 115 | |
---|
| 116 | // summing across X, with w=y=z=1 |
---|
| 117 | |
---|
| 118 | expect = 0; |
---|
[6e50a6b] | 119 | for (i; Nx) |
---|
[c7625e0] | 120 | expect += getMagicNumber( slice_ix, i, slice_ix, slice_ix ); |
---|
| 121 | printf("expect Xs = %f\n", expect); |
---|
| 122 | |
---|
| 123 | result = total1d_low( wxyz[slice_ix][all][slice_ix][slice_ix] ); |
---|
| 124 | printf("result Xs [][][][] lo = %f\n", result); |
---|
| 125 | |
---|
[1d71208] | 126 | result = total1d_low( wxyz[slice_ix, all, slice_ix, slice_ix] ); |
---|
[63a4b92] | 127 | printf("result Xs [,,,] lo = %f\n", result); |
---|
[c7625e0] | 128 | |
---|
| 129 | result = total1d_hi( wxyz[slice_ix][all][slice_ix][slice_ix] ); |
---|
| 130 | printf("result Xs [][][][] hi = %f\n", result); |
---|
| 131 | |
---|
[1d71208] | 132 | result = total1d_hi( wxyz[slice_ix, all, slice_ix, slice_ix] ); |
---|
[63a4b92] | 133 | printf("result Xs [,,,] hi = %f\n", result); |
---|
[c7625e0] | 134 | |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | const size_t KW = 3, KX = 4, KY = 5, KZ = 6; |
---|
| 138 | |
---|
| 139 | int main() { |
---|
| 140 | |
---|
| 141 | typesTest ( ztag(KX), ztag(KY), ztag(KZ) ); |
---|
| 142 | runtimeTest( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) ); |
---|
| 143 | } |
---|