source: tests/array-collections/array-basic.cfa @ 1a2ba84

Last change on this file since 1a2ba84 was 0b8c951d, checked in by Peter A. Buhr <pabuhr@…>, 16 months ago

more rename directories containers to collections

  • Property mode set to 100644
File size: 3.6 KB
Line 
1#include <collections/array.hfa>
2
3//
4// Type-theory demo (success is "it compiles")
5//
6
7forall( [Nx], [Ny], [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( [Nw], [Nx], [Ny], [Nz] )
62void fillHelloData( array( float, Nw, Nx, Ny, Nz ) & wxyz ) {
63    for (w; Nw)
64    for (x; Nx)
65    for (y; Ny)
66    for (z; Nz)
67        wxyz[w][x][y][z] = getMagicNumber(w, x, y, z);
68}
69
70forall( [N]
71      , S & | sized(S)
72      )
73float total1d_low( arpk(N, S, float, float ) & a ) {
74    float total = 0.0f;
75    for (i; N)
76        total += a[i];
77    return total;
78}
79
80forall( [N], A & | ar(A, float, N) )
81float total1d_hi( A & a ) {
82    float total = 0.0f;
83    for (i; N)
84        total += a[i];
85    return total;
86}
87
88forall( [Nw], [Nx], [Ny], [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; 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    result = total1d_low( wxyz[all, slice_ix, slice_ix, slice_ix] );
108    printf("result Ws [,,,]    lo = %f\n", result);
109
110    result = total1d_hi( wxyz[all][slice_ix][slice_ix][slice_ix] );
111    printf("result Ws [][][][] hi = %f\n", result);
112
113    result = total1d_hi( wxyz[all, slice_ix, slice_ix, slice_ix] );
114    printf("result Ws [,,,]    hi = %f\n", result);
115
116    // summing across X, with w=y=z=1
117
118    expect = 0;
119    for (i; Nx)
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
126    result = total1d_low( wxyz[slice_ix, all, slice_ix, slice_ix] );
127    printf("result Xs [,,,]    lo = %f\n", result);
128
129    result = total1d_hi( wxyz[slice_ix][all][slice_ix][slice_ix] );   
130    printf("result Xs [][][][] hi = %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}
136
137const size_t  KW = 3,  KX = 4,  KY = 5,  KZ = 6;
138
139int main() {
140
141    typesTest  (           ztag(KX), ztag(KY), ztag(KZ) );
142    runtimeTest( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
143}
Note: See TracBrowser for help on using the repository browser.