-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2Dd-queue.c
311 lines (268 loc) · 6.19 KB
/
2Dd-queue.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "2Dd-queue.h"
#include "2Dd-window.c"
#ifdef RELAXATION_ANALYSIS
#include "relaxation_analysis_queue.c"
#endif
RETRY_STATS_VARS;
#include "latency.h"
#if LATENCY_PARSING == 1
__thread size_t lat_parsing_get = 0;
__thread size_t lat_parsing_put = 0;
__thread size_t lat_parsing_rem = 0;
#endif /* LATENCY_PARSING == 1 */
void free_node(node_t* node)
{
#if GC == 1
ssmem_free(alloc, (void*) node);
#endif
}
node_t* create_node(skey_t key, sval_t val, node_t* next)
{
#if GC == 1
node_t* node = ssmem_alloc(alloc, sizeof(node_t));
#else
node_t* node = ssalloc(sizeof(node_t));
#endif
node->key = key;
node->val = val;
node->next = next;
#ifdef __tile__
MEM_BARRIER;
#endif
return node;
}
mqueue_t* create_queue(size_t num_threads, uint32_t width, uint64_t depth, uint8_t k_mode, uint64_t relaxation_bound)
{
// Creates the data structure, including windows
mqueue_t *set;
/**** calculate width and depth using the relaxation bound ****/
if(k_mode == 3)
{
//maximum width is fixed as a multiple of number of threads
width = num_threads * width;
if(width < 2 )
{
width = 1;
depth = relaxation_bound;
relaxation_bound = 0;
}
else
{
depth = relaxation_bound / (width - 1);
if(depth<1)
{
depth = 1;
width = (relaxation_bound / depth) + 1;
}
}
}
else if(k_mode == 2)
{
//maximum depth is fixed
width = (relaxation_bound / depth) + 1;
if(width<1)
{
width = 1;
depth = relaxation_bound;
relaxation_bound = 0;
}
}
else if(k_mode == 1)
{
//width parameter is fixed
if(width < 2 )
{
width = 1;
depth = relaxation_bound;
relaxation_bound = 0;
}
else
{
depth = relaxation_bound / (width - 1);
if(depth<1)
{
depth = 1;
width = (relaxation_bound / depth) + 1;
}
}
}
else if(k_mode == 0)
{
relaxation_bound = depth * (width -1);
}
/*************************************************************/
if ((set = (mqueue_t*) ssalloc_aligned(CACHE_LINE_SIZE, sizeof(mqueue_t))) == NULL)
{
perror("malloc");
exit(1);
}
// Initialize all descriptors to zero (empty)
set->get_array = (volatile index_t*)ssalloc_aligned(CACHE_LINE_SIZE, width*sizeof(index_t)); //ssalloc(width);
set->put_array = (volatile index_t*)ssalloc_aligned(CACHE_LINE_SIZE, width*sizeof(index_t));//ssalloc(width);
set->random_hops = 2;
set->depth = depth;
set->width = width;
set->k_mode = k_mode;
set->relaxation_bound = relaxation_bound;
// Initlialize the window variables
initialize_global_window(depth);
uint64_t i;
node_t *node;
for(i = 0; i < width; i++)
{
//node = create_node(0, 0, NULL); // This thread isn't properly initialized
node = (node_t*) ssalloc_aligned(CACHE_LINE_SIZE, sizeof(node_t));
if (node == NULL)
printf("ERROR: Memory ran out when allocating queue");
node->next = NULL;
set->put_array[i].descriptor.node = set->get_array[i].descriptor.node = node;
set->put_array[i].descriptor.put_count = 0;
set->get_array[i].descriptor.get_count = 0;
}
return set;
}
static int enq_cas(node_t** next_loc, node_t* new_node)
{
#ifdef RELAXATION_ANALYSIS
lock_relaxation_lists();
if (CAS(next_loc, NULL, new_node))
{
new_node->val = gen_relaxation_count();
add_linear(new_node->val, 0);
unlock_relaxation_lists();
return true;
}
else {
unlock_relaxation_lists();
return false;
}
#else
return CAS(next_loc, NULL, new_node);
#endif
}
static int deq_cae(descriptor_t* des_loc, descriptor_t* read_des_loc, descriptor_t* new_des_loc)
{
#ifdef RELAXATION_ANALYSIS
lock_relaxation_lists();
if (CAE(des_loc, read_des_loc, new_des_loc))
{
remove_linear(new_des_loc->node->val);
unlock_relaxation_lists();
return true;
}
else {
unlock_relaxation_lists();
return false;
}
#else
return CAE(des_loc, read_des_loc, new_des_loc);
#endif
}
int enqueue(mqueue_t* set, skey_t key, sval_t val)
{
node_t* tail;
uint8_t contention = 0;
descriptor_t descriptor, new_descriptor;
node_t* new_node = create_node(key, val, NULL);
while(1)
{
descriptor = put_window(set,contention);
tail = descriptor.node;
new_descriptor.node = new_node;
new_descriptor.put_count = descriptor.put_count + 1;
if(tail->next == NULL)
{
if (set->put_array[thread_index].descriptor.put_count >= thread_PWindow.max) {
// Double check needed as the descriptor is not read atomically, and the node is written without ensuring it is read correctly
continue;
}
if(enq_cas(&tail->next, new_node))
{
break;
}
else
{
contention = 1;
}
}
else
{
//Try helping pending enqueue
new_descriptor.node = tail->next;
if(!CAE(&set->put_array[thread_index].descriptor,&descriptor,&new_descriptor))
{
contention = 1;
}
}
my_put_cas_fail_count+=1;
}
if(!CAE(&set->put_array[thread_index].descriptor,&descriptor,&new_descriptor));
{
contention = 1;
}
return 1;
}
sval_t dequeue(mqueue_t* set)
{
sval_t val;
node_t *head, *tail;
uint8_t contention = 0;
descriptor_t enq_descriptor, new_enq_descriptor, deq_descriptor, new_deq_descriptor;
while (1)
{
deq_descriptor = get_window(set,contention);
head = deq_descriptor.node;
enq_descriptor = set->put_array[thread_index].descriptor;
tail = enq_descriptor.node;
if (unlikely(head == tail))
{
if(head->next == NULL)
{
my_null_count+=1;
return 0;
}
else
{
//Try helping pending enqueue
new_enq_descriptor.node = tail->next;
new_enq_descriptor.put_count = enq_descriptor.put_count + 1;
if(!CAE(&set->put_array[thread_index].descriptor,&enq_descriptor,&new_enq_descriptor))
{
contention = 1;
}
}
}
else
{
new_deq_descriptor.node = head->next;
new_deq_descriptor.get_count = deq_descriptor.get_count + 1;
if(deq_cae(&set->get_array[thread_index].descriptor, &deq_descriptor, &new_deq_descriptor))
{
val = head->next->val;
free_node(head);
return val;
}
else
{
contention = 1;
}
}
my_get_cas_fail_count+=1;
}
}
size_t queue_size(mqueue_t *set)
{
size_t size = 0;
node_t *head, *tail;
for(int q = 0; q < set->width; q++)
{
head = set->get_array[q].descriptor.node;
tail = set->put_array[q].descriptor.node;
while (head != tail)
{
head = head->next;
size+=1;
}
}
return size;
}