-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg_aa.h
257 lines (226 loc) · 6.17 KB
/
alg_aa.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#include "util.h"
#include <atomic>
#include <mutex>
#include <semaphore.h>
#include <immintrin.h>
#include <list>
#include <algorithm>
using namespace std;
// #define MUTEX
// #define HYBRID_SPIN_LOCK
#if defined(MUTEX)
class Lock2 {
mutex l;
public:
void lock() {
l.lock();
}
void unlock(){
l.unlock();
}
};
#elif defined(HYBRID_FUTEX)
class Lock {
atomic<int> lockInfo{0};
mutex l;
public:
void lock() {
int spinStart = 4;
int endSpin = 1024;
for(;;) {
int exp = 0;
if (lockInfo.compare_exchange_strong(exp, 1)) return;
if(exp == 2) break;
for(int i = 0; i < spinStart; i++) _mm_pause();
if(spinStart < endSpin) spinStart += spinStart;
else break;
}
int ycnt = 0;
int scnt = 0;
int spin = 4;
bool flag = false;
for (;;) {
if (lockInfo.exchange(2) == 0) {
flag = true;
break;
}
if (scnt < 16) {
for (int i = 0; i < spin; i += 1) _mm_pause();
if (spin < endSpin) spin += spin;
else scnt += 1;
} else if (ycnt < 0) {
ycnt += 1;
// this_thread::yield();
} else {
break;
}
}
if (flag) {
return;
}else {
l.lock(); // sleep
lock();
}
}
void unlock() {
lockInfo = 0;
l.unlock();
}
};
#elif defined(HYBRID_SPIN_LOCK)
class Lock2 {
atomic<int> l;
int maxSpin = 5;
public:
void lock() {
int expected = 0;
int mSpin = maxSpin;
while (!l.compare_exchange_strong(expected, 1, memory_order_acquire)) {
expected = 0;
for(int i = 0; i < mSpin; i++)
_mm_pause();
mSpin += mSpin;
}
}
void unlock() {
l.store(0, memory_order_release);
}
};
#elif defined(HYBRID_MUTEX)
class Lock2 {
atomic<int> lockInfo{0};
mutex l;
int maxSpin = 2;
// I HAVE A PAD IN my struct.
public:
void lock() {
lockInfo.fetch_add(1, memory_order_relaxed);
while(1) {
if (lockInfo >= 2){ // contented Case
for(int i = 0; i < maxSpin; i++) {
_mm_pause();
}
if(l.try_lock()) {
return;
}else {
maxSpin += maxSpin;
if (maxSpin > 1024) {
this_thread::yield(); // sleep
}
}
}else {
l.lock();
return;
}
}
}
void unlock() {
lockInfo.fetch_sub(1, memory_order_relaxed);
l.unlock();
}
};
#else
class Lock2 {
pthread_spinlock_t l;
public:
Lock2() {
pthread_spin_init(&l, PTHREAD_PROCESS_PRIVATE);
}
void lock() {
pthread_spin_lock(&l);
}
void unlock(){
pthread_spin_unlock(&l);
}
};
#endif
class AlgorithmAA
{
public:
static constexpr int TOMBSTONE = -1;
static constexpr int NULL_VAL = -2;
char padding0[PADDING_BYTES];
const int numThreads;
int capacity;
char padding2[PADDING_BYTES];
struct PaddedIntLocked
{
int key;
Lock2 l{};
list<int> *ll;
char padding[PADDING_BYTES - sizeof(key) - sizeof(l) - sizeof(ll)];
PaddedIntLocked(): key(NULL_VAL) {
ll = new list<int>;
};
};
PaddedIntLocked *data;
public:
AlgorithmAA(const int _numThreads, const int _capacity);
~AlgorithmAA();
bool insertIfAbsent(const int tid, const int &key);
bool erase(const int tid, const int &key);
long getSumOfKeys();
void printDebuggingDetails();
};
/**
* constructor: initialize the hash table's internals
*
* @param _numThreads maximum number of threads that will ever use the hash table (i.e., at least tid+1, where tid is the largest thread ID passed to any function of this class)
* @param _capacity is the INITIAL size of the hash table (maximum number of elements it can contain WITHOUT expansion)
*/
AlgorithmAA::AlgorithmAA(const int _numThreads, const int _capacity)
: numThreads(_numThreads), capacity(_capacity)
{
data = new PaddedIntLocked[capacity];
for (int i = 0; i < capacity; i++)
data[i].key = NULL_VAL;
}
// destructor: clean up any allocated memory, etc.
AlgorithmAA::~AlgorithmAA()
{
delete[] data;
}
// semantics: try to insert key. return true if successful (if key doesn't already exist), and false otherwise
bool AlgorithmAA::insertIfAbsent(const int tid, const int &key)
{
uint32_t hashedIndex = murmur3(key);
// for (int i = 0; i < capacity; ++i)
// {
uint32_t index = (hashedIndex) % capacity;
data[index].l.lock();
int found = key;
auto it = find(data[index].ll->begin(), data[index].ll->end(), found);
if( it == data[index].ll->end())
data[index].ll->push_back(key);
data[index].l.unlock();
// }
return false;
}
// semantics: try to erase key. return true if successful, and false otherwise
bool AlgorithmAA::erase(const int tid, const int &key)
{
uint32_t hashedIndex = murmur3(key);
uint32_t index = (hashedIndex) % capacity;
data[index].l.lock();
int found = key;
auto it = find(data[index].ll->begin(), data[index].ll->end(), found);
if (it != data[index].ll->end())
data[index].ll->erase(it);
data[index].l.unlock();
return false;
}
// semantics: return the sum of all KEYS in the set
int64_t AlgorithmAA::getSumOfKeys()
{
// because this function is called at the end of threads' work.
// I have not guard it with a lock.
int64_t keySummation = 0;
for (int i = 0; i < capacity; i++)
keySummation += ((data[i].key == NULL_VAL || data[i].key == TOMBSTONE) ? 0 : data[i].key);
return keySummation;
}
// print any debugging details you want at the end of a trial in this function
void AlgorithmAA::printDebuggingDetails()
{
}