source: doc/theses/colby_parsons_MMAth/benchmarks/channels/cfa/barrier.cfa @ fa5e1aa5

Last change on this file since fa5e1aa5 was 76a8400, checked in by caparson <caparson@…>, 20 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: 3.0 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
9size_t total_operations = 0;
10int Processors = 1, Tasks = 1, BarrierSize = 2;
11
12typedef channel( int ) Channel;
13
14Channel * barWait;
15Channel * entryWait;
16owner_lock o;
17
18bool done = false;
19size_t tasks_done = 0;
20
21static inline void flushBarrier() {
22    for ( j; BarrierSize ) {
23        insert( *entryWait, -1 );
24        insert( *barWait, -1 );
25    }
26}
27
28static inline void initBarrier() {
29    for ( j; BarrierSize )
30        insert( *entryWait, j );
31}
32
33static inline void barrier() {
34    int ticket = remove( *entryWait );
35    if ( ticket == -1 ) {
36                insert( *entryWait, -1 );
37                return;
38        }
39    if ( ticket == BarrierSize - 1 ) {
40                for ( j; BarrierSize - 1 )
41            insert( *barWait, j );
42        return;
43        }
44    ticket = remove( *barWait );
45    if ( ticket == -1 ) {
46                insert( *barWait, -1 );
47                return;
48        }
49
50        // last one out
51        if ( BarrierSize == 1 || ticket == BarrierSize - 2 ) {
52                for ( j; BarrierSize )
53            insert( *entryWait, j );
54        }
55}
56
57thread Task {};
58static inline void ?{}( Task & p, cluster & clu ) {
59    ((thread &)p){ clu };
60}
61void main(Task & this) {
62    size_t runs = 0;
63    for ( ;; ) {
64        if ( done ) break;
65        barrier();
66        runs++;
67    }
68    lock(o);
69    total_operations += runs;
70    // sout | "P: " | runs;
71    unlock(o);
72}
73
74
75int main( int argc, char * argv[] ) {
76    switch ( argc ) {
77          case 3:
78                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
79                        BarrierSize = atoi( argv[2] );
80            if ( Processors < 1 ) goto Usage;
81                } // if
82          case 2:
83                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
84                        Processors = atoi( argv[1] );
85                        if ( Processors < 1 ) goto Usage;
86                } // if
87          case 1:                                                                                       // use defaults
88                break;
89          default:
90          Usage:
91                sout | "Usage: " | argv[0]
92             | " [ processors (> 0) | 'd' (default " | Processors
93                         | ") ] [ BarrierSize (> 0) | 'd' (default " | BarrierSize
94                         | ") ]" ;
95                exit( EXIT_FAILURE );
96        } // switch
97    Tasks = Processors;
98    if ( Tasks < BarrierSize )
99        Tasks = BarrierSize;
100
101    size_t Clusters = 1;
102    // create a cluster
103    cluster clus[Clusters];
104    processor * proc[Processors];
105    for ( i; Processors ) {
106        (*(proc[i] = malloc())){clus[i % Clusters]};
107    }
108
109    Channel entry{ 2 * BarrierSize };
110    Channel wait{ 2 * BarrierSize };
111
112    entryWait = &entry;
113    barWait = &wait;
114
115    initBarrier();
116    // sout | "Processors: " | Processors | " ProdsPerChan: " | Producers | " ConsPerChan: " | Consumers | "Channels: " | Channels | " Channel Size: " | ChannelSize;
117
118    // sout | "start";
119    Task * t[Tasks];
120
121    for ( i; Tasks ) {
122        (*(t[i] = malloc())){ clus[i % Clusters] };
123    }
124
125    sleep(10`s);
126    done = true;
127
128    // sout | "sleep";
129
130    flushBarrier();
131
132    for ( i; Tasks ) {
133        delete(t[i]);
134    }
135   
136    sout | total_operations;
137    // print_stats_now( *active_cluster(), CFA_STATS_READY_Q);
138
139    for ( i; Processors ) {
140        delete(proc[i]);
141    }
142    // sout | "done";
143    return 0;
144}
Note: See TracBrowser for help on using the repository browser.