-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_b.h
164 lines (146 loc) · 4.43 KB
/
alg_b.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
#pragma once
#include "util.h"
#include <atomic>
#include <mutex>
using namespace std;
class AlgorithmB
{
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
{
volatile int key;
std::mutex _lock;
// pthread_spinlock_t _lock;
char padding[PADDING_BYTES - sizeof(key) - sizeof(_lock)];
PaddedIntLocked()
{
key = NULL_VAL;
// pthread_spin_init(&_lock, PTHREAD_PROCESS_PRIVATE);
}
void lockL() {
_lock.lock();
// pthread_spin_lock(&data[index]._lock);
}
void unLock() {
_lock.unlock();
// pthread_spin_unlock(&data[index]._lock);
}
};
private:
PaddedIntLocked *data;
public:
AlgorithmB(const int _numThreads, const int _capacity);
~AlgorithmB();
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)
*/
AlgorithmB::AlgorithmB(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.
AlgorithmB::~AlgorithmB()
{
delete[] data;
}
// semantics: try to insert key. return true if successful (if key doesn't already exist), and false otherwise
bool AlgorithmB::insertIfAbsent(const int tid, const int &key)
{
uint32_t hashedIndex = murmur3(key);
for (int i = 0; i < capacity; i++)
{
uint32_t index = (hashedIndex + i) % (uint32_t)capacity;
int found = data[index].key;
if (found == NULL_VAL)
{
data[index].lockL();
// pthread_spin_lock(&data[index]._lock);
found = data[index].key;
if (found == NULL_VAL)
{
data[index].key = key;
data[index].unLock();
// pthread_spin_unlock(&data[index]._lock);
return true;
}
else if (found == key)
{
data[index].unLock();
// pthread_spin_unlock(&data[index]._lock);
return false;
}
data[index].unLock();
}
else if (found == key)
{
return false;
}
}
return false;
}
// semantics: try to erase key. return true if successful, and false otherwise
bool AlgorithmB::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;
int found = data[index].key;
if (found == NULL_VAL)
{
return false;
}
else if (found == key)
{
data[index].lockL();
found = data[index].key;
if (found == key)
{
data[index].key = TOMBSTONE;
data[index].unLock();
// pthread_spin_unlock(&data[index]._lock);
return true;
}else if(found == NULL_VAL) {
data[index].unLock();
// pthread_spin_unlock(&data[index]._lock);
return false;
}
data[index].unLock();
// pthread_spin_unlock(&data[index]._lock);
}
}
return false;
}
// semantics: return the sum of all KEYS in the set
int64_t AlgorithmB::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 AlgorithmB::printDebuggingDetails()
{
}