source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/contend.cfa @ b5e3a80

Last change on this file since b5e3a80 was 382467f, checked in by caparsons <caparson@…>, 18 months ago

added go and cfa waituntil channel benchmarks

  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include <select.hfa>
2#include <thread.hfa>
3#include <channel.hfa>
4#include <locks.hfa>
5#include <fstream.hfa>
6#include <stdio.h>
7#include <time.hfa>
8#include <string.h>
9
10// #define NUM_CHANS 1
11#define GLUE_HELPER(x, y) x##y
12#define FN_GLUE(x, y) GLUE_HELPER(x, y)
13
14size_t Processors = 2, Producers = 1, Consumers = 1, ChannelSize = 10;
15
16channel(size_t) * chans;
17
18static inline void cons_wait_1( size_t & val ) { waituntil( val << chans[0] ) {} }
19static inline void cons_wait_2( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} }
20static inline void cons_wait_3( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} }
21static inline void cons_wait_4( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} }
22static inline void cons_wait_5( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} }
23static inline void cons_wait_6( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} }
24static inline void cons_wait_7( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} or waituntil( val << chans[6] ) {} }
25static inline void cons_wait_8( size_t & val ) { waituntil( val << chans[0] ) {} or waituntil( val << chans[1] ) {} or waituntil( val << chans[2] ) {} or waituntil( val << chans[3] ) {} or waituntil( val << chans[4] ) {} or waituntil( val << chans[5] ) {} or waituntil( val << chans[6] ) {} or waituntil( val << chans[7] ) {}}
26
27static inline void prods_wait_1( size_t val ) { waituntil( val >> chans[0] ) {} }
28static inline void prods_wait_2( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} }
29static inline void prods_wait_3( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} }
30static inline void prods_wait_4( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} }
31static inline void prods_wait_5( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} or waituntil( val >> chans[4] ) {} }
32static inline void prods_wait_6( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} or waituntil( val >> chans[4] ) {} or waituntil( val >> chans[5] ) {} }
33static inline void prods_wait_7( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} or waituntil( val >> chans[4] ) {} or waituntil( val >> chans[5] ) {} or waituntil( val >> chans[6] ) {} }
34static inline void prods_wait_8( size_t val ) { waituntil( val >> chans[0] ) {} or waituntil( val >> chans[1] ) {} or waituntil( val >> chans[2] ) {} or waituntil( val >> chans[3] ) {} or waituntil( val >> chans[4] ) {} or waituntil( val >> chans[5] ) {} or waituntil( val >> chans[6] ) {} or waituntil( val >> chans[7] ) {}}
35
36size_t globalTotal = 0;
37
38thread Consumer {};
39void main( Consumer & this ) {
40    size_t val, i = 0;
41    try {
42        for(;; i++ ) {
43            FN_GLUE(cons_wait_, NUM_CHANS)(val);
44        }
45    } catch( channel_closed * e ) {}
46    __atomic_fetch_add( &globalTotal, i, __ATOMIC_SEQ_CST );
47}
48
49thread Producer {};
50void main( Producer & this ) {
51    try {
52        for( size_t i = 0;; i++ ) {
53            FN_GLUE(prods_wait_, NUM_CHANS)(i);
54        }
55    } catch( channel_closed * e ) {}
56}
57
58int main( int argc, char * argv[] ) {
59    switch ( argc ) {
60          case 3:
61                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
62                        ChannelSize = atoi( argv[2] );
63                } // if
64          case 2:
65                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
66                        Processors = atoi( argv[1] );
67                        if ( Processors < 1 ) goto Usage;
68                } // if
69          case 1:                                                                                       // use defaults
70                break;
71          default:
72          Usage:
73                sout | "Usage: " | argv[0]
74             | " [ processors (> 0) | 'd' (default " | Processors
75                         | ") ] [ channel size (>= 0) | 'd' (default " | ChannelSize
76                         | ") ]" ;
77                exit( EXIT_FAILURE );
78        } // switch
79    Producers = Processors / 2;
80    Consumers = Processors / 2;
81
82    processor p[Processors - 1];
83
84    chans = aalloc( NUM_CHANS );
85    for ( i; NUM_CHANS )
86        chans[i]{ ChannelSize };
87
88    {
89        Producer p[Producers];
90        Consumer c[Consumers];
91
92        sleep(10`s);
93
94        for ( i; NUM_CHANS )
95            close( chans[i] );
96    }
97    adelete( chans );
98    printf("%zu\n", globalTotal);
99}
Note: See TracBrowser for help on using the repository browser.