source: tests/concurrent/channels/parallel_harness.cfa @ cca034e

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

added another channel test and refactored the existing one to reuse code

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#include <locks.hfa>
2#include <fstream.hfa>
3#include <stdio.h>
4#include <string.h>
5#include <channel.hfa>
6#include <thread.hfa>
7#include <time.hfa>
8#include <stats.hfa>
9
10// user defines this
11// #define BIG 1
12
13owner_lock o;
14
15unsigned long long total_operations = 0;
16
17struct bigObject {
18    size_t a;
19    size_t b;
20    size_t c;
21    size_t d;
22    size_t e;
23    size_t f;
24    size_t g;
25    size_t h;
26};
27
28void ?{}( bigObject & this, size_t i ) with(this) { a = i; b = i; c = i; d = i; e = i; f = i; g = i; h = i; }
29void ?{}( bigObject & this ) { this{0}; }
30
31#ifdef BIG
32typedef channel( bigObject ) Channel;
33#else
34typedef channel( size_t ) Channel;
35#endif
36
37Channel * channels;
38
39volatile bool cons_done = false, prod_done = false;
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_check = cons_check ^ my_check;
67    // sout | "C: " | runs;
68    unlock(o);
69}
70
71thread Producer {
72    size_t i;
73};
74static inline void ?{}( Producer & p, size_t i, cluster & clu ) {
75    ((thread &)p){ clu };
76    p.i = i;
77}
78void main(Producer & this) {
79    unsigned long long runs = 0;
80    size_t my_check = 0;
81    for ( ;; ) {
82        if ( prod_done ) break;
83        #ifdef BIG
84        bigObject j{(size_t)runs};
85        insert( channels[ this.i ], j );
86        my_check = my_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h);
87        #else
88        insert( channels[ this.i ], (size_t)runs );
89        my_check = my_check ^ ((size_t)runs);
90        #endif
91        runs++;
92    }
93    lock(o);
94    total_operations += runs;
95    prod_check = prod_check ^ my_check;
96    // sout | "P: " | runs;
97    unlock(o);
98}
99
100
101int test( size_t Processors, size_t Channels, size_t Producers, size_t Consumers, size_t ChannelSize ) {
102    size_t Clusters = 1;
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 = anew( 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    for ( i; Channels ) {
140        // sout | get_count( channels[i] );
141        if ( get_count( channels[i] ) < Consumers ){
142            #ifdef BIG
143            bigObject b{0};
144            #endif
145            for ( j; Consumers ) {
146                #ifdef BIG
147                insert( channels[i], b );
148                #else
149                insert( channels[i], 0 );
150                #endif
151            }
152        }
153    }
154    sout | "cons";
155    for ( i; Consumers * Channels ) {
156        delete(c[i]);
157    }
158
159    sout | "flush";
160    for ( i; Channels ) {
161        for ( ;; ) {
162            if ( get_count( channels[i] ) > 0 ) {
163                #ifdef BIG
164                bigObject j = remove( channels[ i ] );
165                cons_check = cons_check ^ (j.a + j.b + j.c + j.d + j.d + j.e + j.f + j.g + j.h);
166                #else
167                size_t j = remove( channels[ i ] );
168                cons_check = cons_check ^ j;
169                #endif
170            } else break;
171        }
172    }
173
174    adelete( channels );
175    // sout | "total channel ops: " | total_operations;
176    if ( cons_check != prod_check )
177        sout | "CHECKSUM MISMATCH !!!";
178    // print_stats_now( *active_cluster(), CFA_STATS_READY_Q);
179
180    for ( i; Processors ) {
181        delete(proc[i]);
182    }
183    sout | "done";
184    return 0;
185}
Note: See TracBrowser for help on using the repository browser.