1 | #include <locks.hfa> |
---|
2 | #include <mutex_stmt.hfa> |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | |
---|
6 | #include "../bench.h" |
---|
7 | |
---|
8 | test_spinlock LOCKS; |
---|
9 | |
---|
10 | test_spinlock ** lock_arr; |
---|
11 | |
---|
12 | inline void locks( size_t * arr ) { |
---|
13 | if (num_locks == 2) { |
---|
14 | mutex( *lock_arr[arr[0]], *lock_arr[arr[1]] ) {} |
---|
15 | } else if (num_locks == 4) { |
---|
16 | mutex( *lock_arr[arr[0]], *lock_arr[arr[1]], *lock_arr[arr[2]], *lock_arr[arr[3]] ) {} |
---|
17 | } else if (num_locks == 8) { |
---|
18 | mutex( *lock_arr[arr[0]], *lock_arr[arr[1]], *lock_arr[arr[2]], *lock_arr[arr[3]], *lock_arr[arr[4]], *lock_arr[arr[5]], *lock_arr[arr[6]], *lock_arr[arr[7]] ) {} |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | bool done = false; |
---|
23 | uint64_t total = 0; |
---|
24 | size_t num_gen = 100; // number of rand orderings per thd |
---|
25 | size_t ** rand_arrs; |
---|
26 | |
---|
27 | // generate repeatable orderings for each experiment |
---|
28 | void gen_orders() { |
---|
29 | rand_arrs = aalloc( threads ); |
---|
30 | for ( i; threads ) |
---|
31 | rand_arrs[i] = aalloc( num_locks * num_gen ); |
---|
32 | |
---|
33 | size_t work_arr[num_locks]; |
---|
34 | |
---|
35 | for ( i; num_locks ) |
---|
36 | work_arr[i] = i; |
---|
37 | |
---|
38 | size_t curr_idx; |
---|
39 | for ( i; threads ) { |
---|
40 | state = i; |
---|
41 | curr_idx = 0; |
---|
42 | for ( j; num_gen ) { |
---|
43 | for ( size_t k = num_locks; k > 0; k-- ) { |
---|
44 | size_t rand_idx = next_int() % k; // choose one of remaining elems in work_arr |
---|
45 | rand_arrs[i][curr_idx] = work_arr[rand_idx]; |
---|
46 | curr_idx++; |
---|
47 | |
---|
48 | // swap chosen elem to end so it isn't picked again |
---|
49 | size_t temp = work_arr[rand_idx]; |
---|
50 | work_arr[rand_idx] = work_arr[k - 1]; |
---|
51 | work_arr[k - 1] = temp; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | thread worker { size_t * my_arr; }; |
---|
59 | static inline void ?{}( worker & this, cluster & clu, size_t id ) { |
---|
60 | ((thread &)this){ clu }; |
---|
61 | this.my_arr = rand_arrs[id]; |
---|
62 | } |
---|
63 | |
---|
64 | void main( worker & w ) with(w) { |
---|
65 | uint64_t count = 0; |
---|
66 | while (true) { |
---|
67 | locks( my_arr + (count % num_gen) * num_locks ); // go to start of next sequence of locks |
---|
68 | count++; |
---|
69 | if (done) break; |
---|
70 | } |
---|
71 | __atomic_add_fetch(&total, count, __ATOMIC_SEQ_CST); |
---|
72 | } |
---|
73 | |
---|
74 | int main( int argc, char * argv[] ) { |
---|
75 | BENCH_START() |
---|
76 | if ( num_locks == -1 ) { printf("must pass # of locks to program!\n"); exit( EXIT_FAILURE ); } |
---|
77 | cluster clus; |
---|
78 | processor * proc[threads]; |
---|
79 | for ( i; threads ) // create procs |
---|
80 | (*(proc[i] = alloc())){clus}; |
---|
81 | |
---|
82 | lock_arr = aalloc( num_locks ); |
---|
83 | |
---|
84 | if (num_locks >= 2) { |
---|
85 | lock_arr[0] = &l1; lock_arr[1] = &l2; |
---|
86 | } |
---|
87 | if (num_locks >= 4) { |
---|
88 | lock_arr[2] = &l3; lock_arr[3] = &l4; |
---|
89 | } |
---|
90 | if (num_locks == 8) { |
---|
91 | lock_arr[4] = &l5; lock_arr[5] = &l6; lock_arr[6] = &l7; lock_arr[7] = &l8; |
---|
92 | } |
---|
93 | |
---|
94 | gen_orders(); |
---|
95 | |
---|
96 | worker * w[threads]; |
---|
97 | for ( i; threads ) // create threads |
---|
98 | (*(w[i] = alloc())){ clus, i }; |
---|
99 | |
---|
100 | sleep( 10`s ); |
---|
101 | done = true; |
---|
102 | |
---|
103 | for ( i; threads ) // delete threads |
---|
104 | delete(w[i]); |
---|
105 | |
---|
106 | for ( i; threads ) // delete procs |
---|
107 | delete(proc[i]); |
---|
108 | |
---|
109 | for ( i; threads ) |
---|
110 | adelete(rand_arrs[i]); |
---|
111 | adelete(rand_arrs); |
---|
112 | |
---|
113 | adelete(lock_arr); |
---|
114 | |
---|
115 | printf( "%lu\n", total ); |
---|
116 | } |
---|
117 | |
---|
118 | // Local Variables: // |
---|
119 | // tab-width: 4 // |
---|
120 | // End: // |
---|