source: doc/theses/thierry_delisle_PhD/code/utils.hpp @ 8f4f3e0

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8f4f3e0 was 33e62f1b, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added simple SNZI implementation for the relaxed list

  • Property mode set to 100644
File size: 4.8 KB
Line 
1#pragma once
2
3#include <cassert>
4#include <cstddef>
5#include <atomic>
6#include <chrono>
7#include <fstream>
8#include <iostream>
9
10#include <unistd.h>
11#include <sys/sysinfo.h>
12
13#include <x86intrin.h>
14
15// Barrier from
16class barrier_t {
17public:
18        barrier_t(size_t total)
19                : waiting(0)
20                , total(total)
21        {}
22
23        void wait(unsigned) {
24                size_t target = waiting++;
25                target = (target - (target % total)) + total;
26                while(waiting < target)
27                        asm volatile("pause");
28
29                assert(waiting < (1ul << 60));
30        }
31
32private:
33        std::atomic<size_t> waiting;
34        size_t total;
35};
36
37class Random {
38private:
39        unsigned int seed;
40public:
41        Random(int seed) {
42                this->seed = seed;
43        }
44
45        /** returns pseudorandom x satisfying 0 <= x < n. **/
46        unsigned int next() {
47                seed ^= seed << 6;
48                seed ^= seed >> 21;
49                seed ^= seed << 7;
50                return seed;
51        }
52};
53
54static inline long long rdtscl(void) {
55    unsigned int lo, hi;
56    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
57    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
58}
59
60static inline void affinity(int tid) {
61        static int cpus = get_nprocs();
62
63        cpu_set_t  mask;
64        CPU_ZERO(&mask);
65        int cpu = cpus - tid;  // Set CPU affinity to tid, starting from the end
66        CPU_SET(cpu, &mask);
67        auto result = sched_setaffinity(0, sizeof(mask), &mask);
68        if(result != 0) {
69                std::cerr << "Affinity set failed with " << result<< ", wanted " << cpu << std::endl;
70        }
71}
72
73static const constexpr std::size_t cache_line_size = 64;
74static inline void check_cache_line_size() {
75        std::cout << "Checking cache line size" << std::endl;
76        const std::string cache_file = "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size";
77
78        std::ifstream ifs (cache_file, std::ifstream::in);
79
80        if(!ifs.good()) {
81                std::cerr << "Could not open file to check cache line size" << std::endl;
82                std::cerr << "Looking for: " << cache_file << std::endl;
83                std::exit(2);
84        }
85
86        size_t got;
87        ifs >> got;
88
89        ifs.close();
90
91        if(cache_line_size != got) {
92                std::cerr << "Cache line has incorrect size : " << got << std::endl;
93                std::exit(1);
94        }
95
96        std::cout << "Done" << std::endl;
97}
98
99using Clock = std::chrono::high_resolution_clock;
100using duration_t = std::chrono::duration<double>;
101using std::chrono::nanoseconds;
102
103template<typename Ratio, typename T>
104T duration_cast(T seconds) {
105        return std::chrono::duration_cast<std::chrono::duration<T, Ratio>>(std::chrono::duration<T>(seconds)).count();
106}
107
108static inline unsigned rand_bit(unsigned rnum, size_t mask) {
109        unsigned bit = mask ? rnum % __builtin_popcountl(mask) : 0;
110#if !defined(__BMI2__)
111        uint64_t v = mask;   // Input value to find position with rank r.
112        unsigned int r = bit + 1;// Input: bit's desired rank [1-64].
113        unsigned int s;      // Output: Resulting position of bit with rank r [1-64]
114        uint64_t a, b, c, d; // Intermediate temporaries for bit count.
115        unsigned int t;      // Bit count temporary.
116
117        // Do a normal parallel bit count for a 64-bit integer,
118        // but store all intermediate steps.
119        a =  v - ((v >> 1) & ~0UL/3);
120        b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5);
121        c = (b + (b >> 4)) & ~0UL/0x11;
122        d = (c + (c >> 8)) & ~0UL/0x101;
123
124
125        t = (d >> 32) + (d >> 48);
126        // Now do branchless select!
127        s  = 64;
128        s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8));
129        t  = (d >> (s - 16)) & 0xff;
130        s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8));
131        t  = (c >> (s - 8)) & 0xf;
132        s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8));
133        t  = (b >> (s - 4)) & 0x7;
134        s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8));
135        t  = (a >> (s - 2)) & 0x3;
136        s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8));
137        t  = (v >> (s - 1)) & 0x1;
138        s -= ((t - r) & 256) >> 8;
139        return s - 1;
140#else
141        uint64_t picked = _pdep_u64(1ul << bit, mask);
142        return picked ? __builtin_ctzl(picked) : 0;
143#endif
144}
145
146struct spinlock_t {
147        std::atomic_bool ll = { false };
148
149        inline void lock() {
150                while( __builtin_expect(ll.exchange(true),false) ) {
151                        while(ll.load(std::memory_order_relaxed))
152                                asm volatile("pause");
153                }
154        }
155
156        inline bool try_lock() {
157                return false == ll.exchange(true);
158        }
159
160        inline void unlock() {
161                ll.store(false, std::memory_order_release);
162        }
163
164        inline explicit operator bool() {
165                return ll.load(std::memory_order_relaxed);
166        }
167};
168
169static inline bool bts(std::atomic_size_t & target, size_t bit ) {
170        //*
171        int result = 0;
172        asm volatile(
173                "LOCK btsq %[bit], %[target]\n\t"
174                :"=@ccc" (result)
175                : [target] "m" (target), [bit] "r" (bit)
176        );
177        return result != 0;
178        /*/
179        size_t mask = 1ul << bit;
180        size_t ret = target.fetch_or(mask, std::memory_order_relaxed);
181        return (ret & mask) != 0;
182        //*/
183}
184
185static inline bool btr(std::atomic_size_t & target, size_t bit ) {
186        //*
187        int result = 0;
188        asm volatile(
189                "LOCK btrq %[bit], %[target]\n\t"
190                :"=@ccc" (result)
191                : [target] "m" (target), [bit] "r" (bit)
192        );
193        return result != 0;
194        /*/
195        size_t mask = 1ul << bit;
196        size_t ret = target.fetch_and(~mask, std::memory_order_relaxed);
197        return (ret & mask) != 0;
198        //*/
199}
Note: See TracBrowser for help on using the repository browser.