[2f6a9391] | 1 | #include <locks.hfa> |
---|
| 2 | #include <fstream.hfa> |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <channel.hfa> |
---|
| 5 | #include <thread.hfa> |
---|
| 6 | #include <time.hfa> |
---|
| 7 | #include <string.h> |
---|
| 8 | |
---|
| 9 | // user defines this |
---|
[76a8400] | 10 | // #define BIG 1 |
---|
[2f6a9391] | 11 | |
---|
| 12 | owner_lock o; |
---|
| 13 | |
---|
[76a8400] | 14 | size_t total_operations = 0; |
---|
[2f6a9391] | 15 | |
---|
| 16 | struct bigObject { |
---|
| 17 | size_t a; |
---|
| 18 | size_t b; |
---|
| 19 | size_t c; |
---|
| 20 | size_t d; |
---|
| 21 | size_t e; |
---|
| 22 | size_t f; |
---|
| 23 | size_t g; |
---|
| 24 | size_t h; |
---|
| 25 | }; |
---|
| 26 | |
---|
| 27 | void ?{}( bigObject & this, size_t i ) with(this) { a = i; b = i; c = i; d = i; e = i; f = i; g = i; h = i; } |
---|
| 28 | void ?{}( bigObject & this ) { this{0}; } |
---|
| 29 | |
---|
| 30 | #ifdef BIG |
---|
| 31 | typedef channel( bigObject ) Channel; |
---|
| 32 | #else |
---|
| 33 | typedef channel( size_t ) Channel; |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | Channel * channels; |
---|
| 37 | |
---|
[76a8400] | 38 | bool cons_done = false, prod_done = false; |
---|
[2f6a9391] | 39 | volatile int cons_done_count = 0; |
---|
| 40 | size_t cons_check = 0, prod_check = 0; |
---|
| 41 | |
---|
| 42 | thread Consumer { |
---|
| 43 | size_t i; |
---|
| 44 | }; |
---|
| 45 | static inline void ?{}( Consumer & c, size_t i, cluster & clu ) { |
---|
| 46 | ((thread &)c){ clu }; |
---|
| 47 | c.i = i; |
---|
| 48 | } |
---|
| 49 | void main(Consumer & this) { |
---|
[76a8400] | 50 | size_t runs = 0; |
---|
[2f6a9391] | 51 | size_t my_check = 0; |
---|
| 52 | for ( ;; ) { |
---|
| 53 | if ( cons_done ) break; |
---|
| 54 | #ifdef BIG |
---|
| 55 | bigObject j = remove( channels[ this.i ] ); |
---|
| 56 | my_check = my_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h); |
---|
| 57 | #else |
---|
| 58 | size_t j = remove( channels[ this.i ] ); |
---|
| 59 | my_check = my_check ^ j; |
---|
| 60 | #endif |
---|
| 61 | |
---|
| 62 | if ( !prod_done ) runs++; |
---|
| 63 | } |
---|
| 64 | lock(o); |
---|
| 65 | total_operations += runs; |
---|
| 66 | cons_done_count++; |
---|
| 67 | cons_check = cons_check ^ my_check; |
---|
| 68 | // sout | "C: " | runs; |
---|
| 69 | unlock(o); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | thread Producer { |
---|
| 73 | size_t i; |
---|
| 74 | }; |
---|
| 75 | static inline void ?{}( Producer & p, size_t i, cluster & clu ) { |
---|
| 76 | ((thread &)p){ clu }; |
---|
| 77 | p.i = i; |
---|
| 78 | } |
---|
| 79 | void main(Producer & this) { |
---|
[76a8400] | 80 | size_t runs = 0; |
---|
[2f6a9391] | 81 | size_t my_check = 0; |
---|
[76a8400] | 82 | size_t my_id = this.i; |
---|
[2f6a9391] | 83 | for ( ;; ) { |
---|
| 84 | if ( prod_done ) break; |
---|
| 85 | #ifdef BIG |
---|
| 86 | bigObject j{(size_t)runs}; |
---|
[76a8400] | 87 | insert( channels[ my_id ], j ); |
---|
[2f6a9391] | 88 | my_check = my_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h); |
---|
| 89 | #else |
---|
[76a8400] | 90 | insert( channels[ my_id ], (size_t)runs ); |
---|
[2f6a9391] | 91 | my_check = my_check ^ ((size_t)runs); |
---|
| 92 | #endif |
---|
| 93 | runs++; |
---|
| 94 | } |
---|
| 95 | lock(o); |
---|
| 96 | total_operations += runs; |
---|
| 97 | prod_check = prod_check ^ my_check; |
---|
| 98 | // sout | "P: " | runs; |
---|
| 99 | unlock(o); |
---|
| 100 | } |
---|
| 101 | |
---|
[76a8400] | 102 | static inline int test( size_t Processors, size_t Channels, size_t Producers, size_t Consumers, size_t ChannelSize ) { |
---|
| 103 | size_t Clusters = 1; |
---|
[2f6a9391] | 104 | // create a cluster |
---|
| 105 | cluster clus[Clusters]; |
---|
| 106 | processor * proc[Processors]; |
---|
| 107 | for ( i; Processors ) { |
---|
[76a8400] | 108 | (*(proc[i] = malloc())){clus[i % Clusters]}; |
---|
[2f6a9391] | 109 | } |
---|
| 110 | |
---|
| 111 | channels = aalloc( Channels ); |
---|
| 112 | |
---|
| 113 | // sout | "Processors: " | Processors | " ProdsPerChan: " | Producers | " ConsPerChan: " | Consumers | "Channels: " | Channels | " Channel Size: " | ChannelSize; |
---|
| 114 | |
---|
| 115 | for ( i; Channels ) { |
---|
| 116 | channels[i]{ ChannelSize }; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | // sout | "start"; |
---|
| 120 | Consumer * c[Consumers * Channels]; |
---|
| 121 | Producer * p[Producers * Channels]; |
---|
| 122 | |
---|
[76a8400] | 123 | for ( j; Channels ) { |
---|
| 124 | for ( i; Producers ) { |
---|
| 125 | (*(p[i] = malloc())){ j, clus[j % Clusters] }; |
---|
| 126 | } |
---|
[2f6a9391] | 127 | |
---|
[76a8400] | 128 | for ( i; Consumers ) { |
---|
| 129 | (*(c[i] = malloc())){ j, clus[j % Clusters] }; |
---|
| 130 | } |
---|
[2f6a9391] | 131 | } |
---|
| 132 | |
---|
| 133 | sleep(10`s); |
---|
| 134 | prod_done = true; |
---|
| 135 | |
---|
| 136 | for ( i; Producers * Channels ) { |
---|
| 137 | delete(p[i]); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | // sout | "prods"; |
---|
| 141 | cons_done = true; |
---|
| 142 | while( cons_done_count != Consumers * Channels ) { |
---|
| 143 | for ( i; Channels ) { |
---|
| 144 | if ( has_waiters( channels[i] ) ){ |
---|
| 145 | #ifdef BIG |
---|
| 146 | bigObject b{0}; |
---|
| 147 | insert( channels[i], b ); |
---|
| 148 | #else |
---|
| 149 | insert( channels[i], 0 ); |
---|
| 150 | #endif |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // sout | "cons"; |
---|
| 156 | for ( i; Consumers * Channels ) { |
---|
| 157 | delete(c[i]); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | // sout | "flush"; |
---|
| 161 | for ( i; Channels ) { |
---|
| 162 | for ( ;; ) { |
---|
| 163 | if ( get_count( channels[i] ) > 0 ) { |
---|
| 164 | #ifdef BIG |
---|
| 165 | bigObject j = remove( channels[ i ] ); |
---|
| 166 | cons_check = cons_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h); |
---|
| 167 | #else |
---|
| 168 | size_t j = remove( channels[ i ] ); |
---|
| 169 | cons_check = cons_check ^ j; |
---|
| 170 | #endif |
---|
| 171 | } else break; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | adelete( channels ); |
---|
| 176 | sout | total_operations; |
---|
| 177 | if ( cons_check != prod_check ) |
---|
| 178 | sout | "CHECKSUM MISMATCH !!!"; |
---|
| 179 | // print_stats_now( *active_cluster(), CFA_STATS_READY_Q); |
---|
| 180 | |
---|
| 181 | for ( i; Processors ) { |
---|
| 182 | delete(proc[i]); |
---|
| 183 | } |
---|
| 184 | // sout | "done"; |
---|
| 185 | return 0; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | int main( int argc, char * argv[] ) { |
---|
[76a8400] | 189 | size_t Processors = 1, Channels = 1, Producers = 1, Consumers = 1, ChannelSize = 128; |
---|
[2f6a9391] | 190 | switch ( argc ) { |
---|
| 191 | case 3: |
---|
| 192 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
| 193 | ChannelSize = atoi( argv[2] ); |
---|
| 194 | } // if |
---|
| 195 | case 2: |
---|
| 196 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
| 197 | Processors = atoi( argv[1] ); |
---|
| 198 | if ( Processors < 1 ) goto Usage; |
---|
| 199 | } // if |
---|
| 200 | case 1: // use defaults |
---|
| 201 | break; |
---|
| 202 | default: |
---|
| 203 | Usage: |
---|
| 204 | sout | "Usage: " | argv[0] |
---|
| 205 | | " [ processors (> 0) | 'd' (default " | Processors |
---|
| 206 | | ") ] [ channel size (>= 0) | 'd' (default " | ChannelSize |
---|
| 207 | | ") ]" ; |
---|
| 208 | exit( EXIT_FAILURE ); |
---|
| 209 | } // switch |
---|
[76a8400] | 210 | Producers = Processors / 2; |
---|
| 211 | Consumers = Processors / 2; |
---|
[2f6a9391] | 212 | test(Processors, Channels, Producers, Consumers, ChannelSize); |
---|
| 213 | } |
---|