-
Notifications
You must be signed in to change notification settings - Fork 0
/
STMbst.cpp
246 lines (210 loc) · 5.03 KB
/
STMbst.cpp
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
#include <iostream>
#include <climits>
#include <vector>
#include <thread>
using namespace std;
const int inf1 = INT_MAX-1;
const int inf2 = INT_MAX;
class BST
{
public:
struct tree_node
{
tree_node* left;
tree_node* right;
bool isLeaf;
int data;
};
tree_node* root;
BST()
{
root = NULL;
}
void print_preorder();
void preorder(tree_node*);
vector<tree_node*> search(int);
void insert(int);
void initializetree();
};
typedef BST::tree_node* tnp;
void BST::initializetree(){
tree_node* t = new tree_node;
tree_node* rootleft = new tree_node;
tree_node* rootright = new tree_node;
rootleft->data = inf1;
rootleft->left = NULL;
rootleft->right = NULL;
rootleft->isLeaf = true;
rootright->data = inf2;
rootright->left = NULL;
rootright->right = NULL;
rootright->isLeaf = true;
t->data = inf2;
t->left = rootleft;
t->right = rootright;
t->isLeaf = false;
root = t;
}
vector<tnp> __attribute__((transaction_pure)) BST::search(int k)
{
vector<tnp> vec;
tnp gp = NULL;
tnp p = NULL;
tnp l = root;
__transaction_atomic{
do{
gp = p;
p = l;
if (k<l->data){
l = p->left;
}
else{
l = p->right;
}
}while(l->isLeaf != true);
vec.push_back(gp);
vec.push_back(p);
vec.push_back(l);
}
return vec;
}
void __attribute__((transaction_pure)) BST::insert(int k)
{
__transaction_atomic{
vector<tnp> vec = search(k);
tnp newinternal = vec[2];
if(newinternal->data == k){
return;
}
tnp newnode = new tree_node;
newnode->data = k;
newnode->left = NULL;
newnode->right = NULL;
newnode->isLeaf = true;
tnp newsibling = new tree_node;
newsibling->data = newinternal->data;
newsibling->left = NULL;
newsibling->right = NULL;
newsibling->isLeaf = true;
newinternal->data = max(k,newinternal->data);
newinternal->isLeaf = false;
if(k<newinternal->data){
newinternal->left = newnode;
newinternal->right = newsibling;
}
else{
newinternal->left = newsibling;
newinternal->right = newnode;
}
}
}
void __attribute__((transaction_pure)) BST::print_preorder()
{
preorder(root);
}
void __attribute__((transaction_pure)) BST::preorder(tree_node* p)
{
if(p != NULL)
{
// if(p->isLeaf){
cout<<" "<<p->data<<" ";
//}
if(p->left) preorder(p->left);
if(p->right) preorder(p->right);
}
else return;
}
void exec(BST b){
int j=10;//rand()%10+1;
for(int i=0;i<j;i++){
int k = rand() % 1000 + 1;
b.insert(k);
}
}
void testSearch(BST *b, int val, bool expected)
{
cout << " Looking for " << val << "." << endl;
cout << " (Expected to " << (expected ? "" : "not ") << "be in the tree.)" << endl;
vector<tnp> vec = b->search(val);
tnp newinternal = vec[2];
if(newinternal->data == val){
cout << " " << val << (expected ? " found." : " not found.") << endl << endl;
}
}
void populateSTMTree(BST *b)
{
int vals[6] = {6, 10, 7, 9, 14, 3};
for (int i = 0; i < 6; i++) {
b->insert(vals[i]);
cout << " Inserted " << vals[i] << endl;
}
}
void testSTM()
{
cout << "Testing STM implementation." << endl;
cout << "-----------------------------------" << endl;
cout << "Initializing new tree." << endl;
BST b;
b.initializetree();
b.print_preorder();
cout << "Testing nonblocking implementation." << endl;
testSearch(&b, INT_MAX, true);
cout << "Populating tree." << endl;
populateSTMTree(&b);
b.print_preorder();
cout << "Testing search." << endl;
testSearch(&b, 9, true);
testSearch(&b, 16, false);
cout << endl;
}
int test(int numthreads,int times){
float sum = 0;
clock_t begin_time,end_time;
for(int i=0;i<times;i++){
BST b;
b.initializetree();
vector<thread> myThreads;
begin_time = clock();
for (int i=0; i<numthreads; i++){
myThreads.push_back(thread(exec, b));
}
for (int i=0; i<numthreads; i++){
myThreads[i].join();
}
end_time = clock();
sum += float(end_time - begin_time);
}
sum /= times;
cout<<"Time taken with "<<numthreads<<" threads is "<<sum<<endl;
}
int main(int argc, char* argv[]) {
if (argc == 2) {
string s("test");
string t(argv[1]);
if (s != t) {
cout << "Usage: ./STMbst test" << endl
<< " or ./STMbst time <numThreads> <Times>" << endl;
return 1;
}
// Run BST STM test
testSTM();
}
else if (argc == 4) {
string s("time");
string t(argv[1]);
if (s != t) {
cout << "Usage: ./STMbst test" << endl
<< " or ./STMbst time <numThreads> <Times>" << endl;
return 1;
}
int numthreads = atoi(argv[2]);
int times = atoi(argv[3]);
test(numthreads,times);
}
else {
cout << "Usage: ./STMbst test" << endl
<< " or ./STMbst time <numThreads> <Times>" << endl;
return 1;
}
return 0;
}