Changeset dab9fb93 for doc/theses/mike_brooks_MMath/programs
- Timestamp:
- Dec 11, 2023, 1:46:50 PM (8 months ago)
- Branches:
- master
- Children:
- 40ab446
- Parents:
- 2554f24
- Location:
- doc/theses/mike_brooks_MMath/programs
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa
r2554f24 rdab9fb93 1 #include "stdlib.hfa" 2 #include "array.hfa" 1 #include <fstream.hfa> 2 #include <stdlib.hfa> 3 #include <array.hfa> 4 5 6 7 8 9 10 11 forall( T, [Nclients], [Ncosts] ) 12 struct request { 13 unsigned int requestor_id; 14 array( T, Nclients ) impacted_client_ids; // nested VLA 15 array( float, Ncosts ) cost_contribs; // nested VLA 16 float total_cost; 17 }; 18 19 20 // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?) 21 22 forall( T, [Nclients], [Ncosts] ) 23 void ?{}( T &, request( T, Nclients, Ncosts ) & this ) {} 24 25 forall( T &, [Nclients], [Ncosts] ) 26 void ^?{}( request( T, Nclients, Ncosts ) & this ) {} 3 27 4 28 … … 15 39 16 40 17 18 19 20 forall( ztype(Nclients), ztype(Ncosts) ) 21 struct request { 22 unsigned int requestor_id; 23 array( unsigned int, Nclients ) impacted_client_ids; 24 array( float, Ncosts ) cost_contribs; 25 float total_cost; 26 }; 27 28 29 // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?) 30 31 forall( ztype(Nclients), ztype(Ncosts) ) 32 void ?{}( request(Nclients, Ncosts) & this ) {} 33 34 forall( ztype(Nclients), ztype(Ncosts) ) 35 void ^?{}( request(Nclients, Ncosts) & this ) {} 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 forall( ztype(Nclients), ztype(Ncosts) ) 51 void summarize( request(Nclients, Ncosts) & r ) { 41 forall( T, [Nclients], [Ncosts] ) 42 void summarize( request( T, Nclients, Ncosts ) & r ) { 52 43 r.total_cost = 0; 53 for( i; z(Ncosts))44 for( i; Ncosts ) 54 45 r.total_cost += r.cost_contribs[i]; 55 46 // say the cost is per-client, to make output vary 56 r.total_cost *= z(Nclients);47 r.total_cost *= Nclients; 57 48 } 58 49 … … 68 59 69 60 61 int main( int argc, char * argv[] ) { 62 const int ncl = ato( argv[1] ); 63 const int nco = 2; 70 64 65 request( int, ncl, nco ) r; 66 r.cost_contribs[0] = 100; 67 r.cost_contribs[1] = 0.1; 71 68 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 int main( int argc, char ** argv ) { 97 98 99 100 const int ncl = atoi(argv[1]); 101 const int nco = 2; 102 103 request( Z(ncl), Z(nco) ) r; 104 r.cost_contribs[0] = 100; 105 r.cost_contribs[1] = 0.1; 106 107 summarize(r); 108 printf("Total cost: %.1f\n", r.total_cost); 109 69 summarize(r); 70 sout | "Total cost:" | r.total_cost; 71 } 110 72 /* 111 ./a.out 573 $\$$ ./a.out 5 112 74 Total cost: 500.5 113 ./a.out 675 $\$$ ./a.out 6 114 76 Total cost: 600.6 115 77 */ 116 117 118 119 120 } -
doc/theses/mike_brooks_MMath/programs/hello-array.cfa
r2554f24 rdab9fb93 1 2 #include <common.hfa> 3 #include <bits/align.hfa> 4 5 extern "C" { 6 int atoi(const char *str); 7 } 8 9 10 #include "stdlib.hfa" 11 #include "array.hfa" // learned has to come afer stdlib, which uses the word tag 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 1 #include <fstream.hfa> 2 #include <stdlib.hfa> 3 #include <array.hfa> // learned has to come afer stdlib, which uses the word tag 27 4 28 5 // Usage: … … 31 8 32 9 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 forall( ztype( N ) ) 10 forall( [N] ) // array bound 51 11 array(bool, N) & f( array(float, N) & a, array(float, N) & b ) { 52 array(bool, N) & ret = *alloc(); 53 for( i; z(N) ) { 54 float fracdiff = 2 * abs( a[i] - b[i] ) 55 / ( abs( a[i] ) + abs( b[i] ) ); 56 ret[i] = fracdiff < 0.005; 12 array(bool, N) & ret = *alloc(); // sizeof used by alloc 13 for( i; N ) { 14 ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i])); 57 15 } 58 16 return ret; … … 68 26 69 27 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 28 // TODO: standardize argv 99 29 100 int main( int argc, char * * argv) {101 int n = ato i(argv[1]);102 array(float, Z(n)) a, b;103 for ( i; n) {104 a[i] = 3.14 / (i +1);30 int main( int argc, char * argv[] ) { 31 int n = ato( argv[1] ); 32 array(float, n) a, b; // VLA 33 for ( i; n ) { 34 a[i] = 3.14 / (i + 1); 105 35 b[i] = a[i] + 0.005 ; 106 36 } 107 array(bool, Z(n)) & answer = f( a, b );108 printf("answer:");109 for ( i; n)110 printf(" %d", answer[i]);111 printf("\n");112 free( & answer );37 array(bool, n) & result = f( a, b ); // call 38 sout | "result: " | nonl; 39 for ( i; n ) 40 sout | result[i] | nonl; 41 sout | nl; 42 free( &result ); // free returned storage 113 43 } 114 44 /* 115 $ ./a.out 5116 answer: 1 1 1 0 0 117 $ ./a.out 7118 answer: 1 1 1 0 0 0 0 45 $\$$ ./a.out 5 46 result: true true true false false 47 $\$$ ./a.out 7 48 result: true true true false false false false 119 49 */ 120 50 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 forall( ztype(M), ztype(N) ) 137 void not_so_bad(array(float, M) &a, array(float, N) &b ) { 51 void fred() { 52 array(float, 10) a; 53 array(float, 20) b; 138 54 f( a, a ); 139 55 f( b, b ); 56 f( a, b ); 140 57 } 141 58 142 143 144 145 146 147 148 59 #ifdef SHOWERR1 149 150 forall( ztype(M), ztype(N) ) 60 forall( [M], [N] ) 151 61 void bad( array(float, M) &a, array(float, N) &b ) { 152 62 f( a, a ); // ok … … 154 64 f( a, b ); // error 155 65 } 156 157 66 #endif 158 67 159 68 160 69 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 forall( ztype(M), ztype(N) ) 197 void bad_fixed( array(float, M) &a, array(float, N) &b ) { 198 199 200 if ( z(M) == z(N) ) { 201 f( a, ( array(float, M) & ) b ); // fixed 70 forall( [M], [N] ) 71 void bad_fixed( array(float, M) & a, array(float, N) & b ) { 72 if ( M == N ) { 73 f( a, (array(float, M) &)b ); // cast b to matching type 202 74 } 203 204 75 } -
doc/theses/mike_brooks_MMath/programs/hello-md.cfa
r2554f24 rdab9fb93 1 #include "array.hfa"2 1 #include <fstream.hfa> 2 #include <array.hfa> 3 3 4 4 … … 60 60 forall( [N] ) 61 61 void print1d_cstyle( array(float, N) & c ) { 62 for ( i; N ) {63 printf("%.1f ", c[i]);62 for ( i; N ) { 63 sout | c[i] | nonl; 64 64 } 65 printf("\n");65 sout | nl; 66 66 } 67 68 67 69 68 … … 81 80 void print1d( C & c ) { 82 81 for( i; N ) { 83 printf("%.1f ", c[i]);82 sout | c[i] | nonl; 84 83 } 85 printf("\n");84 sout | nl; 86 85 } 87 86 … … 103 102 for ( j; 7 ) { 104 103 a[i,j] = 1.0 * i + 0.1 * j; 105 printf("%.1f ", a[i,j]);104 sout | a[[i,j]] | nonl; 106 105 } 107 printf("\n");106 sout | nl; 108 107 } 109 printf("\n");108 sout | nl; 110 109 } 111 110 112 111 int main() { 113 114 112 115 113 … … 128 126 */ 129 127 128 129 130 130 131 131 … … 168 168 169 169 } 170 171 172
Note: See TracChangeset
for help on using the changeset viewer.