-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqueue.c
186 lines (150 loc) · 4.58 KB
/
pqueue.c
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
#include "intset.h"
#define LOG3(n) floor_log_2(n)*floor_log_2(n)*floor_log_2(n)
#define LOG2(n) floor_log_2(n)*floor_log_2(n)
#define LOGLOG(n) floor_log_2(floor_log_2(n))
// SCANHEIGHT is what height to start spray at; must be >= 0
//#define SCANHEIGHT floor_log_2(n)+1
#define SCANHEIGHT floor_log_2(n)+1
// SCANMAX is scanlength at the top level; must be > 0
#define SCANMAX floor_log_2(n)+1
// SCANINC is the amount to increase scan length at each step; can be any integer
#define SCANINC 0
//SCANSKIP is # of levels to go down at each step; must be > 0
#define SCANSKIP 1
static int _old_MarsagliaXOR(int seed) {
const int a = 16807;
const int m = 2147483647;
const int q = 127773; /* m div a */
const int r = 2836; /* m mod a */
int hi = seed / q;
int lo = seed % q;
int test = a * lo - r * hi;
if (test > 0)
seed = test;
else
seed = test + m;
return seed;
}
static int _MarsagliaXOR(int seed) {
// const int a = 16807;
const int a = 123456789;
const int m = 2147483647;
const int q = 521288629; /* m div a */
const int r = 88675123; /* m mod a */
int hi = seed / q;
int lo = seed % q;
int test = a * lo - r * hi;
if (test > 0)
seed = test;
else
seed = test + m;
return seed;
}
int lotan_shavit_delete_min(sl_intset_t *set, val_t *val, thread_data_t *d) {
slkey_t key;
return lotan_shavit_delete_min_key(set, &key, val, d);
}
int lotan_shavit_delete_min_key(sl_intset_t *set, slkey_t *key, val_t *val, thread_data_t *d) {
sl_node_t *first;
int result;
first = set->head;
while(1) {
do {
first = (sl_node_t*)unset_mark((uintptr_t)first->next[0]);
} while(first->next[0] && first->deleted);
if (first->next[0] && ATOMIC_FETCH_AND_INC_FULL(&first->deleted) != 0) {
d->nb_collisions++;
} else {
break;
}
}
result = (first->next[0] != NULL);
if (!result) {
*key = -1;
return 0;
}
*val = (first->val);
*key = (first->key);
mark_node_ptrs(first);
// unsigned int *seed = &d->seed2;
// *seed = _MarsagliaXOR(*seed);
// if (*seed % (d->nb_threads) == 0) {
// fraser_search(set, first->val, NULL, NULL);
// }
// if (!first->next[0]->deleted)
fraser_search(set, first->key, NULL, NULL);
return result;
}
int spray_delete_min(sl_intset_t *set, val_t *val, thread_data_t *d) {
slkey_t key;
return spray_delete_min_key(set, &key, val, d);
}
int spray_delete_min_key(sl_intset_t *set, slkey_t *key, val_t *val, thread_data_t *d) {
unsigned int n = d->nb_threads;
unsigned int *seed = &d->seed2;
#ifndef DISTRIBUTION_EXPERIMENT
*seed = _MarsagliaXOR(*seed);
if (n == 1 || *seed % n/*/floor_log_2(n)*/ == 0) { // n == 1 is equivalent to Lotan-Shavit delete_min
d->nb_clean++;
return lotan_shavit_delete_min_key(set, key, val, d);
}
#endif
sl_node_t *cur;
int result;
int scanlen;
int height = SCANHEIGHT;
int scanmax = SCANMAX;
int scan_inc = SCANINC;
cur = set->head;
int i = height;
int dummy = 0;
while(1) {
*seed = _MarsagliaXOR(*seed);
scanlen = *seed % (scanmax+1);
while (dummy < n*floor_log_2(n)/2 && scanlen > 0) {
dummy += (1 << i);
scanlen--;
}
while (scanlen > 0 && cur->next[i]) { // Step right //here: cur->next[0], or cur->next[i]??
cur = (sl_node_t*)unset_mark((uintptr_t)cur->next[i]);
if (!cur->deleted) scanlen--;
}
// TODO: This is probably a reasonable condition to become a 'cleaner' since the list is so small
// We can also just ignore it since it shouldn't happen in benchmarks?
if (!cur->next[0]) {
*key = -1;
return 0; //got to end of list
}
scanmax += scan_inc;
if (i == 0) break;
if (i <= SCANSKIP) { i = 0; continue; } // need to guarantee bottom level gets scanned
i -= SCANSKIP;
}
if (cur == set->head) // still in dummy range
return 0; // TODO: clean instead? something else?
while (cur->deleted && cur->next[0]) {
cur = (sl_node_t*)unset_mark((uintptr_t)cur->next[0]); // Find first non-deleted node
}
if (!cur->next[0]) return 0;
#ifdef DISTRIBUTION_EXPERIMENT
*val = (cur->val);
*key = (cur->key);
return 1;
#endif
result = 1;
if (cur->deleted == 0 )
{
result = ATOMIC_FETCH_AND_INC_FULL(&cur->deleted);
}
if (result != 0) {
d->nb_collisions++;
return 0; // TODO: Retry and eventually 'clean'
}
*val = (cur->val);
*key = (cur->key);
mark_node_ptrs(cur);
// if (((*seed) & 0x10)) return 1;
// TODO: batch deletes (this method is somewhat inefficient)
fraser_search(set, cur->val, NULL, NULL);
return 1;
}