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 | size_t total_operations = 0; |
---|
10 | int Processors = 1, Tasks = 1, BarrierSize = 2; |
---|
11 | |
---|
12 | typedef channel( int ) Channel; |
---|
13 | |
---|
14 | Channel * barWait; |
---|
15 | Channel * entryWait; |
---|
16 | owner_lock o; |
---|
17 | |
---|
18 | bool done = false; |
---|
19 | size_t tasks_done = 0; |
---|
20 | |
---|
21 | static inline void flushBarrier() { |
---|
22 | for ( j; BarrierSize ) { |
---|
23 | insert( *entryWait, -1 ); |
---|
24 | insert( *barWait, -1 ); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | static inline void initBarrier() { |
---|
29 | for ( j; BarrierSize ) |
---|
30 | insert( *entryWait, j ); |
---|
31 | } |
---|
32 | |
---|
33 | static 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 | |
---|
57 | thread Task {}; |
---|
58 | static inline void ?{}( Task & p, cluster & clu ) { |
---|
59 | ((thread &)p){ clu }; |
---|
60 | } |
---|
61 | void 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 | |
---|
75 | int 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 | } |
---|