source: doc/theses/colby_parsons_MMAth/benchmarks/channels/cfa/contend.cfa @ 2f6a9391

ADTast-experimental
Last change on this file since 2f6a9391 was 2f6a9391, checked in by caparsons <caparson@…>, 13 months ago

added first channel bench and copied over scripts that will need to be edited for channel usage. intermediate commit so that benchmarks can be pulled and run on another server

  • 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
14unsigned long long 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
38volatile bool 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    unsigned long long 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    unsigned long long runs = 0;
81    size_t my_check = 0;
82    for ( ;; ) {
83        if ( prod_done ) break;
84        #ifdef BIG
85        bigObject j{(size_t)runs};
86        insert( channels[ this.i ], j );
87        my_check = my_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h);
88        #else
89        insert( channels[ this.i ], (size_t)runs );
90        my_check = my_check ^ ((size_t)runs);
91        #endif
92        runs++;
93    }
94    lock(o);
95    total_operations += runs;
96    prod_check = prod_check ^ my_check;
97    // sout | "P: " | runs;
98    unlock(o);
99}
100
101int test( size_t Processors, size_t Channels, size_t Producers, size_t Consumers, size_t ChannelSize ) {
102    size_t Clusters = Processors;
103    // create a cluster
104    cluster clus[Clusters];
105    processor * proc[Processors];
106    for ( i; Processors ) {
107        (*(proc[i] = alloc())){clus[i % Clusters]};
108    }
109
110    channels = aalloc( Channels );
111
112    // sout | "Processors: " | Processors | " ProdsPerChan: " | Producers | " ConsPerChan: " | Consumers | "Channels: " | Channels | " Channel Size: " | ChannelSize;
113   
114    for ( i; Channels ) {
115        channels[i]{ ChannelSize };
116    }
117
118    // sout | "start";
119    Consumer * c[Consumers * Channels];
120    Producer * p[Producers * Channels];
121
122    for ( i; Consumers * Channels ) {
123        (*(c[i] = alloc())){ i % Channels, clus[i % Clusters] };
124    }
125
126    for ( i; Producers * Channels ) {
127        (*(p[i] = alloc())){ i % Channels, clus[i % Clusters] };
128    }
129
130    sleep(10`s);
131    prod_done = true;
132
133    for ( i; Producers * Channels ) {
134        delete(p[i]);
135    }
136
137    // sout | "prods";
138    cons_done = true;
139    while( cons_done_count != Consumers * Channels ) {
140        for ( i; Channels ) {
141            if ( has_waiters( channels[i] ) ){
142                #ifdef BIG
143                bigObject b{0};
144                insert( channels[i], b );
145                #else
146                insert( channels[i], 0 );
147                #endif
148            }
149        }
150       
151    }
152
153    // sout | "cons";
154    for ( i; Consumers * Channels ) {
155        delete(c[i]);
156    }
157
158    // sout | "flush";
159    for ( i; Channels ) {
160        for ( ;; ) {
161            if ( get_count( channels[i] ) > 0 ) {
162                #ifdef BIG
163                bigObject j = remove( channels[ i ] );
164                cons_check = cons_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h);
165                #else
166                size_t j = remove( channels[ i ] );
167                cons_check = cons_check ^ j;
168                #endif
169            } else break;
170        }
171    }
172
173    adelete( channels );
174    sout | total_operations;
175    if ( cons_check != prod_check )
176        sout | "CHECKSUM MISMATCH !!!";
177    // print_stats_now( *active_cluster(), CFA_STATS_READY_Q);
178
179    for ( i; Processors ) {
180        delete(proc[i]);
181    }
182    // sout | "done";
183    return 0;
184}
185
186int main( int argc, char * argv[] ) {
187    size_t Processors = 10, Channels = 1, Producers = 10, Consumers = 10, ChannelSize = 128;
188    switch ( argc ) {
189          case 3:
190                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
191                        ChannelSize = atoi( argv[2] );
192                } // if
193          case 2:
194                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
195                        Processors = atoi( argv[1] );
196                        if ( Processors < 1 ) goto Usage;
197                } // if
198          case 1:                                                                                       // use defaults
199                break;
200          default:
201          Usage:
202                sout | "Usage: " | argv[0]
203             | " [ processors (> 0) | 'd' (default " | Processors
204                         | ") ] [ channel size (>= 0) | 'd' (default " | ChannelSize
205                         | ") ]" ;
206                exit( EXIT_FAILURE );
207        } // switch
208    Producers = Processors;
209    Consumers = Processors;
210    test(Processors, Channels, Producers, Consumers, ChannelSize);
211}
Note: See TracBrowser for help on using the repository browser.