-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg_a.h
More file actions
194 lines (174 loc) · 4.62 KB
/
alg_a.h
File metadata and controls
194 lines (174 loc) · 4.62 KB
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
#pragma once
#include "util.h"
#include <atomic>
#include <mutex>
#include <semaphore.h>
#include <immintrin.h>
#include <list>
using namespace std;
// #define MUTEX
#if defined(MUTEX)
class Lock {
mutex l;
public:
void lock() {
l.lock();
}
void unlock(){
l.unlock();
}
};
#elif defined(HYBRID_MUTEX)
class Lock {
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()) {
l.lock();
return;
}else {
maxSpin += maxSpin;
if (maxSpin > 10) {
this_thread::yield(); // sleep
}
}
}else {
l.lock();
return;
}
}
}
void unlock() {
lockInfo.fetch_sub(1, memory_order_relaxed);
l.unlock();
}
};
#else
class Lock {
pthread_spinlock_t l;
public:
Lock() {
pthread_spin_init(&l, PTHREAD_PROCESS_PRIVATE);
}
void lock() {
pthread_spin_lock(&l);
}
void unlock(){
pthread_spin_unlock(&l);
}
};
#endif
class AlgorithmA
{
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;
Lock l{};
char padding[PADDING_BYTES - sizeof(key) - sizeof(l)];
PaddedIntLocked(): key(NULL_VAL) {};
};
PaddedIntLocked *data;
public:
AlgorithmA(const int _numThreads, const int _capacity);
~AlgorithmA();
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)
*/
AlgorithmA::AlgorithmA(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.
AlgorithmA::~AlgorithmA()
{
delete[] data;
}
// semantics: try to insert key. return true if successful (if key doesn't already exist), and false otherwise
bool AlgorithmA::insertIfAbsent(const int tid, const int &key)
{
uint32_t hashedIndex = murmur3(key);
for (int i = 0; i < capacity; ++i)
{
uint32_t index = (hashedIndex + i) % capacity;
data[index].l.lock();
int found = data[index].key;
if (found == NULL_VAL)
{
data[index].key = key;
data[index].l.unlock();
return true;
}
else if (found == key)
{
data[index].l.unlock();
return false;
}
data[index].l.unlock();
}
return false;
}
// semantics: try to erase key. return true if successful, and false otherwise
bool AlgorithmA::erase(const int tid, const int &key)
{
uint32_t hashedIndex = murmur3(key);
for (int i = 0; i < capacity; ++i)
{
uint32_t index = (hashedIndex + i) % capacity;
data[index].l.lock();
int found = data[index].key;
if (found == NULL_VAL)
{
data[index].l.unlock();
return false;
}
else if (found == key)
{
data[index].key = TOMBSTONE;
data[index].l.unlock();
return true;
}
data[index].l.unlock();
}
return false;
}
// semantics: return the sum of all KEYS in the set
int64_t AlgorithmA::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 AlgorithmA::printDebuggingDetails()
{
}