#include #include #include #include #include "../bench.h" test_spinlock LOCKS; test_spinlock ** lock_arr; inline void locks( size_t * arr ) { if (num_locks == 2) { mutex( *lock_arr[arr[0]], *lock_arr[arr[1]] ) {} } else if (num_locks == 4) { mutex( *lock_arr[arr[0]], *lock_arr[arr[1]], *lock_arr[arr[2]], *lock_arr[arr[3]] ) {} } else if (num_locks == 8) { 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]] ) {} } } bool done = false; uint64_t total = 0; size_t num_gen = 100; // number of rand orderings per thd size_t ** rand_arrs; // generate repeatable orderings for each experiment void gen_orders() { rand_arrs = aalloc( threads ); for ( i; threads ) rand_arrs[i] = aalloc( num_locks * num_gen ); size_t work_arr[num_locks]; for ( i; num_locks ) work_arr[i] = i; size_t curr_idx; for ( i; threads ) { state = i; curr_idx = 0; for ( j; num_gen ) { for ( size_t k = num_locks; k > 0; k-- ) { size_t rand_idx = next_int() % k; // choose one of remaining elems in work_arr rand_arrs[i][curr_idx] = work_arr[rand_idx]; curr_idx++; // swap chosen elem to end so it isn't picked again size_t temp = work_arr[rand_idx]; work_arr[rand_idx] = work_arr[k - 1]; work_arr[k - 1] = temp; } } } } thread worker { size_t * my_arr; }; static inline void ?{}( worker & this, cluster & clu, size_t id ) { ((thread &)this){ clu }; this.my_arr = rand_arrs[id]; } void main( worker & w ) with(w) { uint64_t count = 0; while (true) { locks( my_arr + (count % num_gen) * num_locks ); // go to start of next sequence of locks count++; if (done) break; } __atomic_add_fetch(&total, count, __ATOMIC_SEQ_CST); } int main( int argc, char * argv[] ) { BENCH_START() if ( num_locks == -1 ) { printf("must pass # of locks to program!\n"); exit( EXIT_FAILURE ); } cluster clus; processor * proc[threads]; for ( i; threads ) // create procs (*(proc[i] = alloc())){clus}; lock_arr = aalloc( num_locks ); if (num_locks >= 2) { lock_arr[0] = &l1; lock_arr[1] = &l2; } if (num_locks >= 4) { lock_arr[2] = &l3; lock_arr[3] = &l4; } if (num_locks == 8) { lock_arr[4] = &l5; lock_arr[5] = &l6; lock_arr[6] = &l7; lock_arr[7] = &l8; } gen_orders(); worker * w[threads]; for ( i; threads ) // create threads (*(w[i] = alloc())){ clus, i }; sleep( 10`s ); done = true; for ( i; threads ) // delete threads delete(w[i]); for ( i; threads ) // delete procs delete(proc[i]); for ( i; threads ) adelete(rand_arrs[i]); adelete(rand_arrs); adelete(lock_arr); printf( "%lu\n", total ); } // Local Variables: // // tab-width: 4 // // End: //