source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/future.cfa@ 3a7aa94

Last change on this file since 3a7aa94 was 6c121eed, checked in by caparsons <caparson@…>, 2 years ago

intermediate commit to move some benchmarking to another machine

  • Property mode set to 100644
File size: 2.9 KB
Line 
1#include <select.hfa>
2#include <thread.hfa>
3#include <future.hfa>
4#include <fstream.hfa>
5#include <stdio.h>
6#include <time.hfa>
7#include <string.h>
8
9size_t Clients = 1, Time = 10;
10
11size_t globalTotal = 0;
12future(size_t) A, B, C;
13volatile bool server_done_loop = false;
14volatile bool client_done_loop = false;
15volatile bool client_done = false;
16volatile bool server_done = false;
17
18volatile size_t client_count = 0;
19volatile size_t client_loop_count = 0;
20thread Client {};
21void main( Client & this ) {
22 size_t i = 0;
23 for(;; i++ ) {
24 waituntil( A ) { get(A); }
25 and waituntil( B ) { get(B); }
26 or waituntil( C ) { get(C); }
27
28 // needs to check after waituntil for termination synchronization
29 if ( client_done ) break;
30
31 // Barrier-like synch needed to reset futures safely
32 if ( __atomic_add_fetch( &client_count, 1, __ATOMIC_SEQ_CST ) == Clients ) { // synchronize reset
33 client_count = 0;
34 reset( A );
35 reset( B );
36 reset( C );
37 client_done_loop = true; // unblock clients
38 }
39 while( !client_done_loop ) {} // client barrier
40 if ( __atomic_add_fetch( &client_loop_count, 1, __ATOMIC_SEQ_CST ) == Clients ) {
41 client_done_loop = false; // reset barrier before clients can proceed past waituntil
42 server_done_loop = true; // unblock server to restart iteration
43 client_loop_count = 0;
44 }
45 }
46 __atomic_fetch_add( &globalTotal, i, __ATOMIC_SEQ_CST );
47}
48
49thread Server {};
50void main( Server & this ) {
51 for( size_t i = 0; !server_done; i++ ) {
52 if ( i % 4 == 0 ) {
53 fulfil(A, i);
54 fulfil(B, i);
55 } else if ( i % 4 == 1 ) {
56 fulfil(A, i);
57 fulfil(C, i);
58 } else if ( i % 4 == 2 ) {
59 fulfil(B, i);
60 fulfil(C, i);
61 } else {
62 fulfil(C, i);
63 }
64 while( !server_done_loop && !server_done ) {} // server barrier
65 server_done_loop = false; // reset server barrier
66 }
67}
68
69int main( int argc, char * argv[] ) {
70 switch ( argc ) {
71 case 3:
72 if ( strcmp( argv[2], "d" ) != 0 ) { // default ?
73 Time = atoi( argv[2] );
74 } // if
75 case 2:
76 if ( strcmp( argv[1], "d" ) != 0 ) { // default ?
77 Clients = atoi( argv[1] );
78 if ( Clients < 1 ) goto Usage;
79 } // if
80 case 1: // use defaults
81 break;
82 default:
83 Usage:
84 sout | "Usage: " | argv[0]
85 | " [ clients (> 0) | 'd' (default " | Clients
86 | ") ] [ time (>= 0) | 'd' (default " | Time
87 | ") ]" ;
88 exit( EXIT_FAILURE );
89 } // switch
90 processor p[Clients];
91
92 {
93 Client c[Clients];
94 {
95 Server s;
96
97 sleep(Time`s);
98 server_done = true;
99 }
100 while( available(A) || available(B) || available(C) ) {}
101 client_done = true;
102 fulfil( C, 0 );
103 }
104 printf("%zu\n", globalTotal);
105}
Note: See TracBrowser for help on using the repository browser.