965dadaa
Salvador Abreu
initial commit fr...
|
1
2
3
4
5
6
7
8
9
10
11
12
|
// included from dsearch-sg.c
#define STORE_SIZE (fd_variables_count * sizeof(*split_stores))
#ifdef STORE_IN_POOL
static __thread _fd_store main_store; // to receive work and return solutions
static __thread _fd_store alt_store; // the other side of the split
#endif
static _fd_store *split_stores_;
#define split_stores split_stores_[agent]
|
965dadaa
Salvador Abreu
initial commit fr...
|
13
|
#ifndef INDEX_IN_POOL
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
14
15
|
static __thread fd_int *split_variable;
#else
|
965dadaa
Salvador Abreu
initial commit fr...
|
16
17
|
static int **split_variable_;
static __thread int *split_variable;
|
a2ec137b
Vasco Pedro
updated to PaCCS ...
|
18
|
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#ifdef GROWABLE_POOL
#define POOL_DEFAULT_SIZE 32
static __thread int pool_size;
#endif
#define POOL_INDEXES 2
static int **split_indexes;
static __thread int *local_split_indexes;
#define base_split_index local_split_indexes[0]
#define next_split_index local_split_indexes[1]
static int EMPTY_POOL[] = { 0, 0 };
|
a2ec137b
Vasco Pedro
updated to PaCCS ...
|
32
|
#ifndef STORE_IN_POOL
|
965dadaa
Salvador Abreu
initial commit fr...
|
33
|
#define STEAL_THRESHOLD 3 // an agent with less stores won't be stolen from
|
a2ec137b
Vasco Pedro
updated to PaCCS ...
|
34
35
|
#define INDEX_SAFE 3 // XXX: explain....
#else
|
965dadaa
Salvador Abreu
initial commit fr...
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// XXX: should STEAL_THRESHOLD and INDEX_SAFE be changed (because
// there's one extra store in the pool)?
// changing for now, to be able to compare performances
#define STEAL_THRESHOLD 4
#define INDEX_SAFE 4
#endif
static pthread_mutex_t *stores_mutexes;
static pthread_mutex_t thieves_mutex;
static int most_work; // agent which is likely to have the most work,
// and from which the others will try to steal
#ifdef RANDOM_VICTIM
#define MAX_ATTEMPTS 5
static pthread_spinlock_t starving_lock;
static volatile int agents_starving = 0;
#endif
#ifdef STATS_POOL
static __thread unsigned long pool_entries = 0;
|
a2ec137b
Vasco Pedro
updated to PaCCS ...
|
56
|
static __thread unsigned long pool_max_entries = 0;
|
965dadaa
Salvador Abreu
initial commit fr...
|
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
|
static __thread unsigned long pool_gets = 0;
#endif
#ifdef STATS_STEALS
#define MAX_AGENTS 256 // XXX: copied from agents-splitgo*.c
static unsigned long sts_attempts[MAX_AGENTS + 1], sts_done[MAX_AGENTS + 1];
#ifdef RANDOM_VICTIM
static unsigned long sts_checks[MAX_AGENTS + 1], sts_slept[MAX_AGENTS + 1];
#endif
#endif
void _fd_init_store_depository(int agents)
{
int i;
#ifdef NEW_ENTRANCE
agents_searching = agents_failed = 0;
#endif
#ifdef RANDOM_VICTIM
agents_starving = 0;
#endif
nagents = agents;
// most_work = agents - 1; // the last agent receives the remaining search space
most_work = -1;
if (split_stores_)
{
// depository already initialised, just try to avoid race
// conditions
for (i = 0; i < agents; ++i)
memset(split_indexes[i], 0, POOL_INDEXES * sizeof(**split_indexes));
return;
|
a2ec137b
Vasco Pedro
updated to PaCCS ...
|
93
94
95
96
|
}
// XXX: check for NULL's
split_stores_ = calloc(agents, sizeof(*split_stores_));
|
965dadaa
Salvador Abreu
initial commit fr...
|
97
98
99
|
split_indexes = calloc(agents, sizeof(*split_indexes));
// XXX: mitigate a race condition: another agent may try to access
// this before proper initialisation by the owning agent
|
965dadaa
Salvador Abreu
initial commit fr...
|
100
|
for (i = 0; i < agents; ++i)
|
965dadaa
Salvador Abreu
initial commit fr...
|
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
|
split_indexes[i] = EMPTY_POOL;
#ifdef INDEX_IN_POOL
split_variable_ = calloc(agents, sizeof(*split_variable_));
#endif
stores_mutexes = calloc(agents, sizeof(*stores_mutexes));
for (i = 0; i < agents; ++i)
pthread_mutex_init(&stores_mutexes[i], NULL);
pthread_mutex_init(&thieves_mutex, NULL);
#ifdef NEW_ENTRANCE
pthread_spin_init(&entrance_lock, 0);
#endif
#ifdef RANDOM_VICTIM
pthread_spin_init(&starving_lock, 0);
#endif
#ifdef STATS_STEALS
memset(sts_attempts, 0, sizeof(sts_attempts));
memset(sts_done, 0, sizeof(sts_done));
#ifdef RANDOM_VICTIM
memset(sts_checks, 0, sizeof(sts_done));
memset(sts_slept, 0, sizeof(sts_done));
#endif
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
128
|
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
129
130
131
132
133
|
}
static void _fd_init_local_depository()
{
if (split_stores)
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
134
135
136
|
{
#ifdef STORE_IN_POOL
// copy the new store to the pool
|
965dadaa
Salvador Abreu
initial commit fr...
|
137
138
139
140
141
142
143
|
store = split_stores;
memcpy(store, main_store, STORE_SIZE);
#ifdef INDEX_IN_POOL
split_variable[next_split_index] = -1;
#else
split_variable[next_split_index] = NULL;
#endif
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
144
|
++next_split_index;
|
965dadaa
Salvador Abreu
initial commit fr...
|
145
146
147
148
149
|
#endif /* STORE_IN_POOL */
return;
}
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
150
|
#ifndef GROWABLE_POOL
|
965dadaa
Salvador Abreu
initial commit fr...
|
151
152
153
154
|
#ifndef STORE_IN_POOL
_fd_debug("[%d.%d] stores size %d + %d\n", tid, agent,
fd_variables_count * STORE_SIZE,
fd_variables_count * sizeof(*split_variable));
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
155
|
if (posix_memalign((void **) &split_stores, sysconf(_SC_PAGESIZE),
|
965dadaa
Salvador Abreu
initial commit fr...
|
156
157
158
159
160
|
fd_variables_count * STORE_SIZE +
fd_variables_count * sizeof(*split_variable)))
_fd_fatal("unable to allocate pool memory");
// XXX: alignment problems possible
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
161
|
split_variable = ((void *) split_stores) + fd_variables_count * STORE_SIZE;
|
965dadaa
Salvador Abreu
initial commit fr...
|
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#else
_fd_debug("[%d.%d] stores size %d + %d\n", tid, agent,
(fd_variables_count + 1) * STORE_SIZE,
fd_variables_count * sizeof(*split_variable));
if (posix_memalign((void **) &split_stores, sysconf(_SC_PAGESIZE),
(fd_variables_count + 1) * STORE_SIZE +
fd_variables_count * sizeof(*split_variable)))
_fd_fatal("unable to allocate pool memory");
// XXX: alignment problems possible
split_variable = ((void *) split_stores) + (fd_variables_count + 1) * STORE_SIZE;
#endif /* STORE_IN_POOL */
#else /* GROWABLE_POOL */
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
175
|
#ifndef STORE_IN_POOL
|
965dadaa
Salvador Abreu
initial commit fr...
|
176
|
pool_size = (fd__label_vars_count < POOL_DEFAULT_SIZE) ?
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
177
|
fd__label_vars_count : POOL_DEFAULT_SIZE;
|
965dadaa
Salvador Abreu
initial commit fr...
|
178
179
|
#else
pool_size = (fd__label_vars_count + 1 < POOL_DEFAULT_SIZE) ?
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
180
|
fd__label_vars_count + 1 : POOL_DEFAULT_SIZE;
|
965dadaa
Salvador Abreu
initial commit fr...
|
181
182
183
|
#endif
_fd_debug("[%d.%d] stores size %d + %d\n", tid, agent, pool_size * STORE_SIZE,
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
184
|
fd_variables_count * sizeof(*split_variable));
|
965dadaa
Salvador Abreu
initial commit fr...
|
185
186
|
if (posix_memalign((void **) &split_stores, sysconf(_SC_PAGESIZE),
|
965dadaa
Salvador Abreu
initial commit fr...
|
187
|
pool_size * STORE_SIZE))
|
965dadaa
Salvador Abreu
initial commit fr...
|
188
189
190
191
192
193
194
195
196
|
_fd_fatal("unable to allocate pool memory");
if (posix_memalign((void **) &split_variable, sysconf(_SC_PAGESIZE),
fd_variables_count * sizeof(*split_variable)))
_fd_fatal("unable to allocate pool variable memory");
#endif /* GROWABLE_POOL */
#if defined(INDEX_IN_POOL) && !defined(split_variable)
split_variable_[agent] = split_variable;
|
965dadaa
Salvador Abreu
initial commit fr...
|
197
|
#endif
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
198
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
local_split_indexes = calloc(POOL_INDEXES, sizeof(*local_split_indexes));
split_indexes[agent] = local_split_indexes;
#ifdef STORE_IN_POOL
// install the received store in the pool
main_store = store;
store = split_stores;
memcpy(store, main_store, STORE_SIZE);
#ifdef INDEX_IN_POOL
split_variable[next_split_index] = -1;
#else
split_variable[next_split_index] = NULL;
#endif
++next_split_index;
#endif /* STORE_IN_POOL */
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
214
215
|
}
|
965dadaa
Salvador Abreu
initial commit fr...
|
216
217
|
#ifdef GROWABLE_POOL
static void _fd_grow_pool(void)
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
218
|
{
|
965dadaa
Salvador Abreu
initial commit fr...
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
int nsize;
_fd_store npool, opool;
nsize = pool_size * 2;
#ifndef STORE_IN_POOL
nsize = (fd_variables_count < nsize) ? fd_variables_count : nsize;
#else
nsize = (fd_variables_count + 1 < nsize) ? fd_variables_count + 1 : nsize;
#endif
_fd_debug("[%d.%d] new pool size is %d\n", tid, agent, nsize * STORE_SIZE);
if (posix_memalign((void **) &npool, sysconf(_SC_PAGESIZE),
nsize * STORE_SIZE))
_fd_fatal("unable to allocate pool memory");
memcpy(npool, split_stores, pool_size * STORE_SIZE);
// lock the pool
pthread_mutex_lock(&stores_mutexes[agent]);
opool = split_stores;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
240
|
split_stores = npool;
|
965dadaa
Salvador Abreu
initial commit fr...
|
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
|
// store now points to freed memory
store = split_stores + (store - opool);
// unlock the pool
pthread_mutex_unlock(&stores_mutexes[agent]);
free(opool);
pool_size = nsize;
}
#endif /* GROWABLE_POOL */
/*
saving a store to the pool is done in two steps:
1. a copy of the store which is about to be split is saved (to have
a place to put the result of splitting a domain);
2. the remaining data is updated (after the splitting), making the
store visible to the other agents.
*/
static void _fd_start_saving_store(_fd_store *dst)
{
#ifdef GROWABLE_POOL
if (next_split_index == pool_size)
_fd_grow_pool();
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
269
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
270
271
272
|
#ifndef STORE_IN_POOL
*dst = &split_stores[next_split_index * fd_variables_count];
memcpy(*dst, store, STORE_SIZE);
|
965dadaa
Salvador Abreu
initial commit fr...
|
273
|
#else
|
965dadaa
Salvador Abreu
initial commit fr...
|
274
275
|
*dst = store;
store = &split_stores[next_split_index * fd_variables_count];
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
276
|
memcpy(store, *dst, STORE_SIZE);
|
965dadaa
Salvador Abreu
initial commit fr...
|
277
278
279
|
#endif
}
|
965dadaa
Salvador Abreu
initial commit fr...
|
280
|
static void _fd_end_saving_store(fd_int variable)
|
965dadaa
Salvador Abreu
initial commit fr...
|
281
282
283
284
285
286
287
288
289
290
291
292
293
|
{
#ifndef STORE_IN_POOL
#ifndef INDEX_IN_POOL
split_variable[next_split_index] = variable;
#else
split_variable[next_split_index] = variable->index;
#endif
#else /* STORE_IN_POOL */
// here, the split also affects the store which is below the
// (about to be) current one
#ifndef INDEX_IN_POOL
split_variable[next_split_index - 1] = variable;
#else
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
294
|
split_variable[next_split_index - 1] = variable->index;
|
965dadaa
Salvador Abreu
initial commit fr...
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
#endif
#endif /* STORE_IN_POOL */
++next_split_index;
}
// try to steal a store from another agent
#ifndef INDEX_IN_POOL
bool _fd_steal_store(_fd_store store_, int agent, int retries)
#else
bool _fd_steal_store(_fd_store store_, fd_int *variable, int agent, int retries)
#endif
{
#ifndef RANDOM_VICTIM
int v; // the victim
int s; // the store
int stores; // how many stores the victim has
struct timespec delay = { 0, 2500000 }; // 2.5ms
#ifdef STATS_STEALS
sts_attempts[agent + 1]++;
#endif
#if defined(STORE_IN_POOL) && defined(DECREMENT_EARLY)
if (agent != -1)
--next_split_index;
#endif
// only let one agent in at a time
pthread_mutex_lock(&thieves_mutex);
v = most_work;
// if work has last been stolen by the main thread or by the agent
// itself, look for an agent to steal work from
if (v == -1 || v == agent
|| split_indexes[v][1] - split_indexes[v][0] < STEAL_THRESHOLD)
{
int most_stores = 1;
int candidate = -1;
bool retrying = false;
for (;;)
{
int active = 0;
int i;
// find an agent likely to own the largest search space,
// using the pool size as a heuristic
// (go through all agents, in case it is the controller who
// is trying to take a store)
#ifdef PREFER_NEXT
v = (agent + 1) % nagents;
#else
v = 0;
#endif
for (i = 0; i < nagents; ++i, v = (v + 1) % nagents)
{
if (v == agent)
continue;
if ((stores = split_indexes[v][1] - split_indexes[v][0]) > 0)
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
358
|
{
|
965dadaa
Salvador Abreu
initial commit fr...
|
359
360
361
362
363
364
365
366
367
368
|
++active;
#ifdef FIRST_CANDIDATE
if (stores >= STEAL_THRESHOLD)
{
candidate = v;
break;
}
#else /* FIRST_CANDIDATE */
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
369
|
if (stores > most_stores)
|
965dadaa
Salvador Abreu
initial commit fr...
|
370
371
|
{
most_stores = stores;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
372
|
candidate = v;
|
965dadaa
Salvador Abreu
initial commit fr...
|
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
}
#endif /* FIRST_CANDIDATE */
}
}
if (candidate != -1)
{
// have a candidate to steal from
v = candidate;
if (retrying)
_fd_debug("[%d.%d] %d tries left\n", tid, agent, retries);
break;
}
/* found no agent to steal from */
pthread_mutex_unlock(&thieves_mutex);
if (active == 0 || retries == 0)
{
_fd_debug("[%d.%d] no agent can supply work\n", tid, agent);
if (retrying)
_fd_debug("[%d.%d] %d tries left\n", tid, agent, retries);
return false;
}
/* there is still at least one active agent and we are
willing to try again */
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
406
|
if (retrying)
|
965dadaa
Salvador Abreu
initial commit fr...
|
407
408
409
410
411
|
{
// increase the delay
delay.tv_sec <<= 1;
delay.tv_nsec <<= 1;
if (delay.tv_nsec > 1000000000)
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
412
|
{
|
965dadaa
Salvador Abreu
initial commit fr...
|
413
414
415
416
417
418
419
420
421
|
delay.tv_sec += delay.tv_nsec / 1000000000;
delay.tv_nsec %= 1000000000;
}
}
else
retrying = true;
--retries;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
422
|
nanosleep(&delay, NULL);
|
965dadaa
Salvador Abreu
initial commit fr...
|
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
// re-lock and try again
pthread_mutex_lock(&thieves_mutex);
}
}
// lock access to victim's stores
pthread_mutex_lock(&stores_mutexes[v]);
#if DEBUG_STEALING > 1
_fd_debug("[%d.%d] locked %d stores' mutex\n", tid, agent, v);
#endif
stores = split_indexes[v][1] - split_indexes[v][0];
#if DEBUG_STEALING > 1
_fd_debug("[%d.%d] %d stores left at %d\n", tid, agent, stores, v);
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
440
441
442
443
444
445
|
// don't steal the last few stores from an agent
if (stores < STEAL_THRESHOLD)
{
pthread_mutex_unlock(&stores_mutexes[v]);
pthread_mutex_unlock(&thieves_mutex);
|
965dadaa
Salvador Abreu
initial commit fr...
|
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] not enough stores (%d) at %d\n", tid, agent, stores, v);
#endif
return false;
}
// steal the store
s = split_indexes[v][0]++;
#ifdef STORE_IN_POOL
// XXX: there should be no side effects on the value of `store'
if (agent != -1)
alt_store = 0,
store = store_ = split_stores;
#endif
memcpy(store_, &split_stores_[v][s * fd_variables_count], STORE_SIZE);
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
465
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
466
|
#ifdef INDEX_IN_POOL
|
965dadaa
Salvador Abreu
initial commit fr...
|
467
|
#ifndef STORE_IN_POOL
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
468
|
if (variable)
|
965dadaa
Salvador Abreu
initial commit fr...
|
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
#else
if (variable && split_variable_[v][s] != -1)
#endif
*variable = _fd_variables[split_variable_[v][s]];
#endif
// this agent will likely become the one with the largest search space
// (if work is being stolen by the controller, most_work will be set
// to -1 which will force the next agent who tries to steal a store
// to look for a suitable victim)
most_work = agent;
if (agent != -1)
{
// reset the indexes into the depository
#ifndef STORE_IN_POOL
next_split_index = 0;
#else
next_split_index = 1;
#endif
base_split_index = 0;
}
#ifdef DEBUG_STEALING
#ifdef INDEX_IN_POOL
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
494
|
_fd_debug("[%d.%d] stole work (%d/%d) from %d (var %d)\n", tid, agent, s,
|
965dadaa
Salvador Abreu
initial commit fr...
|
495
496
497
498
499
500
501
502
503
504
505
|
stores, v, split_variable_[v][s]);
#else
_fd_debug("[%d.%d] stole work (%d/%d) from %d\n", tid, agent, s, stores, v);
#endif
#if DEBUG_STEALING > 2
_fd_output("[%d.%d] ", tid, agent); _fd_cprint3(store_);
#endif
#endif
pthread_mutex_unlock(&stores_mutexes[v]);
pthread_mutex_unlock(&thieves_mutex);
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
506
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
#ifdef STATS_STEALS
sts_done[agent + 1]++;
#endif
return true;
#else /* RANDOM_VICTIM */
int v; // the victim
int s; // the store
int stores; // how many stores the victim has
struct timespec delay = { 0, 100000 }; // 100us
int attempts;
int as;
#ifdef STATS_STEALS
sts_attempts[agent + 1]++;
#endif
#if defined(STORE_IN_POOL) && defined(DECREMENT_EARLY)
if (agent != -1)
--next_split_index;
#endif
pthread_spin_lock(&starving_lock);
as = agents_starving;
if (agent != -1)
agents_starving = ++as;
pthread_spin_unlock(&starving_lock);
if (as == nagents)
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
536
|
{
|
965dadaa
Salvador Abreu
initial commit fr...
|
537
538
539
540
541
542
|
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] all agents starving...\n", tid, agent);
#endif
return false;
}
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
543
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
544
545
546
547
548
549
550
551
552
553
|
// only let one agent in at a time
pthread_mutex_lock(&thieves_mutex);
// check the most likely candidate first
v = most_work;
// if work has last been stolen by the main thread or by the agent
// itself, look for another agent to steal work from
while (v == -1 || v == agent)
v = random() % nagents;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
554
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
555
556
557
558
559
560
561
562
563
564
565
566
|
attempts = 0;
for (;;)
{
#ifdef STATS_STEALS
sts_checks[agent + 1]++;
#endif
if (split_indexes[v][1] - split_indexes[v][0] >= STEAL_THRESHOLD)
{
// lock access to victim's stores ...
pthread_mutex_lock(&stores_mutexes[v]);
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
567
|
#if DEBUG_STEALING > 1
|
965dadaa
Salvador Abreu
initial commit fr...
|
568
569
570
571
572
573
574
575
576
577
578
|
_fd_debug("[%d.%d] locked %d stores' mutex\n", tid, agent, v);
#endif
// ... and make sure that the victim has enough stores
stores = split_indexes[v][1] - split_indexes[v][0];
#if DEBUG_STEALING > 1
_fd_debug("[%d.%d] %d stores left at %d\n", tid, agent, stores, v);
#endif
// don't steal the last few stores from an agent
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
579
|
if (stores >= STEAL_THRESHOLD)
|
965dadaa
Salvador Abreu
initial commit fr...
|
580
581
582
583
584
585
586
587
588
589
|
break; // all is well, proceed to stealing
// won't steal from this agent, unlock its store
pthread_mutex_unlock(&stores_mutexes[v]);
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] not enough stores (%d) at %d\n", tid, agent,
stores, v);
#endif
}
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
590
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
591
592
593
594
595
596
597
598
599
600
601
|
if (++attempts == MAX_ATTEMPTS)
{
// take a break
pthread_mutex_unlock(&thieves_mutex);
// the main thread won't keep trying
if (agent == -1)
{
_fd_debug("[%d] giving up stealing\n", tid);
return false;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
602
|
}
|
965dadaa
Salvador Abreu
initial commit fr...
|
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
pthread_spin_lock(&starving_lock);
as = agents_starving;
pthread_spin_unlock(&starving_lock);
if (as == nagents)
{
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] all agents starving...\n", tid, agent);
#endif
return false;
}
#ifdef STATS_STEALS
sts_slept[agent + 1]++;
#endif
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] pausing...\n", tid, agent);
#endif
nanosleep(&delay, NULL);
pthread_spin_lock(&starving_lock);
as = agents_starving;
pthread_spin_unlock(&starving_lock);
if (as == nagents)
{
#ifdef DEBUG_STEALING
_fd_debug("[%d.%d] all agents starving...\n", tid, agent);
#endif
return false;
|
965dadaa
Salvador Abreu
initial commit fr...
|
638
639
640
641
642
643
|
}
// try again to find a victim
attempts = 0;
pthread_mutex_lock(&thieves_mutex);
|
965dadaa
Salvador Abreu
initial commit fr...
|
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
}
do
v = random() % nagents;
while (v == agent);
}
if (agent != -1)
{
pthread_spin_lock(&starving_lock);
agents_starving--;
pthread_spin_unlock(&starving_lock);
}
// steal the store
s = split_indexes[v][0]++;
#ifdef STORE_IN_POOL
// XXX: there should be no side effects on the value of `store'
if (agent != -1)
alt_store = 0,
store = store_ = split_stores;
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
666
|
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
667
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
668
|
memcpy(store_, &split_stores_[v][s * fd_variables_count], STORE_SIZE);
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
669
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
#ifdef INDEX_IN_POOL
#ifndef STORE_IN_POOL
if (variable)
#else
if (variable && split_variable_[v][s] != -1)
#endif
*variable = _fd_variables[split_variable_[v][s]];
#endif
// this agent will likely become the one with the largest search space
// (if work is being stolen by the controller, most_work will be set
// to -1 which will force the next agent who tries to steal a store
// to look for a suitable victim)
most_work = agent;
// everything needed has been copied out
pthread_mutex_unlock(&stores_mutexes[v]);
if (agent != -1)
{
// reset the indexes into the depository
#ifndef STORE_IN_POOL
next_split_index = 0;
#else
next_split_index = 1;
#endif
base_split_index = 0;
}
|
965dadaa
Salvador Abreu
initial commit fr...
|
698
|
|
965dadaa
Salvador Abreu
initial commit fr...
|
699
700
701
702
703
|
#if DEBUG_STEALING > 0
#ifdef INDEX_IN_POOL
_fd_debug("[%d.%d] stole work (%d/%d) from %d (var %d)\n", tid, agent, s,
stores, v, split_variable_[v][s]);
#else
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
704
705
|
_fd_debug("[%d.%d] stole work (%d/%d) from %d\n", tid, agent, s, stores, v);
#endif
|
965dadaa
Salvador Abreu
initial commit fr...
|
706
|
#if DEBUG_STEALING > 2
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
707
|
_fd_output("[%d.%d] ", tid, agent); _fd_cprint3(store_);
|
965dadaa
Salvador Abreu
initial commit fr...
|
708
709
710
|
#endif
#endif
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
711
|
pthread_mutex_unlock(&thieves_mutex);
|
965dadaa
Salvador Abreu
initial commit fr...
|
712
713
714
715
716
717
718
719
720
721
722
723
|
#ifdef STATS_STEALS
sts_done[agent + 1]++;
#endif
return true;
#endif /* RANDOM_VICTIM */
}
// copy the last saved store to the agent's store
static bool _fd_restore_store(fd_int *variable)
{
|
eef94371
Vasco Pedro
Update to PaCCS v...
|
724
|
bool locked = false;
|
965dadaa
Salvador Abreu
initial commit fr...
|
725
726
727
728
729
730
731
732
733
734
|
*variable = NULL;
#ifndef STORE_IN_POOL
if (next_split_index <= base_split_index)
#else
if (next_split_index <= base_split_index + 1)
#endif /* STORE_IN_POOL */
#if STEAL_WORK < 1
return false;
|
965dadaa
Salvador Abreu
initial commit fr...
|
735
|
#else /* STEAL_WORK < 1 */
|
965dadaa
Salvador Abreu
initial commit fr...
|
736
|
#ifndef INDEX_IN_POOL
|
965dadaa
Salvador Abreu
initial commit fr...
|
737
738
|
return _fd_steal_store(store, agent, 0);
#else
|
965dadaa
Salvador Abreu
initial commit fr...
|
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
return _fd_steal_store(store, variable, agent, 0);
#endif
#endif /* STEAL_WORK < 1 */
#if STEAL_WORK > 0
if (next_split_index - base_split_index < INDEX_SAFE)
{
//_fd_debug("[%d.%d] have %d stores\n", tid, agent, next_split_index - base_split_index);
#ifndef FAST
if (pthread_mutex_trylock(&stores_mutexes[agent]))
_fd_debug("[%d.%d] *** found stores locked [%d,%d[\n", tid, agent,
base_split_index, next_split_index),
#endif
pthread_mutex_lock(&stores_mutexes[agent]);
//_fd_debug("[%d.%d] locked stores\n", tid, agent);
locked = true;
}
#endif /* STEAL_WORK > 0 */
#ifdef STATS_POOL
if (next_split_index - base_split_index > pool_max_entries)
pool_max_entries = next_split_index - base_split_index;
pool_entries += next_split_index - base_split_index;
++pool_gets;
#endif
--next_split_index;
#ifndef STORE_IN_POOL
memcpy(store, &split_stores[next_split_index * fd_variables_count],
STORE_SIZE);
#else
store = alt_store;
alt_store = &split_stores[(next_split_index - 2) * fd_variables_count];
#endif
#ifndef STORE_IN_POOL
# ifndef INDEX_IN_POOL
*variable = split_variable[next_split_index];
# else
*variable = _fd_variables[split_variable[next_split_index]];
# endif /* INDEX_IN_POOL */
#else
# ifndef INDEX_IN_POOL
*variable = split_variable[next_split_index - 1];
# else
assert(split_variable[next_split_index - 1] != -1);
*variable = _fd_variables[split_variable[next_split_index - 1]];
# endif /* INDEX_IN_POOL */
|