source: doc/theses/colby_parsons_MMAth/benchmarks/mutex_stmt/bench.h

Last change on this file was 5f648fb3, checked in by caparsons <caparson@…>, 15 months ago

updated runscript and shared header for rand bench

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#pragma once
2
3#if defined(__cforall)
4extern "C" {
5#endif
6        #include <stdlib.h>
7        #include <stdint.h>                             // uint64_t
8        #include <unistd.h>                             // sysconf
9#if ! defined(__cforall)
10        #include <time.h>
11        #include <sys/time.h>
12#else
13}
14#include <time.hfa>
15#endif
16
17#define L1 l1
18#define L2 L1, l2
19#define L3 L2, l3
20#define L4 L3, l4
21#define L5 L4, l5
22#define L6 L5, l6
23#define L7 L6, l7
24#define L8 L7, l8
25
26static inline uint64_t bench_time() {
27        struct timespec ts;
28        clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
29        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
30} // bench_time
31
32
33#if defined(__cforall)
34struct test_spinlock {
35        volatile bool lock;
36};
37
38static inline void lock( test_spinlock & this ) {
39        for ( ;; ) {
40                if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
41        }
42}
43
44static inline void unlock( test_spinlock & this ) {
45        __atomic_clear( &this.lock, __ATOMIC_RELEASE );
46}
47#endif
48
49size_t threads = 1, num_locks = -1;
50
51#define BENCH_START()                           \
52        if ( argc > 3 ) exit( EXIT_FAILURE );   \
53        if ( argc == 2 ) {                      \
54                threads = atoi( argv[1] );      \
55        } else if ( argc == 3 ) {                       \
56                threads = atoi( argv[1] );      \
57        num_locks = atoi( argv[2] );    \
58        }
59
60#define BENCH(statement, output, done_flag)             \
61        uint64_t count = 0;             \
62    while (true) {          \
63        statement;                              \
64    count++;                \
65    if (done_flag) break; \
66    }                       \
67    __atomic_add_fetch(&output, count, __ATOMIC_SEQ_CST);
68        // EndTime = bench_time();                      \
69        // double output = (double)( EndTime - StartTime ) / times;
70
71
72#if defined(__cforall)
73Duration default_preemption() {
74        return 0;
75}
76#endif
77#if defined(__U_CPLUSPLUS__)
78unsigned int uDefaultPreemption() {
79        return 0;
80}
81#endif
82
83// splitmix64 rand num generator
84// https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64
85uint64_t state;                                  /* The state can be seeded with any (upto) 64 bit integer value. */
86
87uint64_t next_int() {
88    state += 0x9e3779b97f4a7c15;               /* increment the state variable */
89    uint64_t z = state;                          /* copy the state to a working variable */
90    z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;  /* xor the variable with the variable right bit shifted 30 then multiply by a constant */
91    z = (z ^ (z >> 27)) * 0x94d049bb133111eb;  /* xor the variable with the variable right bit shifted 27 then multiply by a constant */
92    return z ^ (z >> 31);                      /* return the variable xored with itself right bit shifted 31 */
93}
94
Note: See TracBrowser for help on using the repository browser.