source: doc/theses/colby_parsons_MMAth/benchmarks/channels/cfa/contend.cfa @ 1803d4d

ast-experimental
Last change on this file since 1803d4d was 76a8400, checked in by caparson <caparson@…>, 15 months ago

added all cfa benchmarks, contend is the only one used in the thesis, but other may be used later so Im keeping them around

  • Property mode set to 100644
File size: 5.2 KB
Line 
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
10// #define BIG 1
11
12owner_lock o;
13
14size_t total_operations = 0;
15
16struct 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
27void ?{}( bigObject & this, size_t i ) with(this) { a = i; b = i; c = i; d = i; e = i; f = i; g = i; h = i; }
28void ?{}( bigObject & this ) { this{0}; }
29
30#ifdef BIG
31typedef channel( bigObject ) Channel;
32#else
33typedef channel( size_t ) Channel;
34#endif
35
36Channel * channels;
37
38bool cons_done = false, prod_done = false;
39volatile int cons_done_count = 0;
40size_t cons_check = 0, prod_check = 0;
41
42thread Consumer {
43    size_t i;
44};
45static inline void ?{}( Consumer & c, size_t i, cluster & clu ) {
46    ((thread &)c){ clu };
47    c.i = i;
48}
49void main(Consumer & this) {
50    size_t runs = 0;
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
72thread Producer {
73    size_t i;
74};
75static inline void ?{}( Producer & p, size_t i, cluster & clu ) {
76    ((thread &)p){ clu };
77    p.i = i;
78}
79void main(Producer & this) {
80    size_t runs = 0;
81    size_t my_check = 0;
82    size_t my_id = this.i;
83    for ( ;; ) {
84        if ( prod_done ) break;
85        #ifdef BIG
86        bigObject j{(size_t)runs};
87        insert( channels[ my_id ], j );
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
90        insert( channels[ my_id ], (size_t)runs );
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
102static inline int test( size_t Processors, size_t Channels, size_t Producers, size_t Consumers, size_t ChannelSize ) {
103    size_t Clusters = 1;
104    // create a cluster
105    cluster clus[Clusters];
106    processor * proc[Processors];
107    for ( i; Processors ) {
108        (*(proc[i] = malloc())){clus[i % Clusters]};
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
123    for ( j; Channels ) {
124        for ( i; Producers ) {
125            (*(p[i] = malloc())){ j, clus[j % Clusters] };
126        }
127
128        for ( i; Consumers ) {
129            (*(c[i] = malloc())){ j, clus[j % Clusters] };
130        }
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
188int main( int argc, char * argv[] ) {
189    size_t Processors = 1, Channels = 1, Producers = 1, Consumers = 1, ChannelSize = 128;
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
210    Producers = Processors / 2;
211    Consumers = Processors / 2;
212    test(Processors, Channels, Producers, Consumers, ChannelSize);
213}
Note: See TracBrowser for help on using the repository browser.