splitting.c
7.72 KB
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
312
313
314
315
316
317
#include <string.h>
#include "fdc_int.h"
#include "variables.h"
#include "values.h"
#include "store.h"
#ifndef COMPACT_DOMAINS
#error "only works with COMPACT_DOMAINS"
#endif
#ifndef USE_STORE
#error "must USE_STORE"
#endif
//#define MAX_AGENTS 256
//static fd_int *_fd_copies[MAX_AGENTS];
extern int tid; // XXX: team ID, just for debugging
/*
Problem partitioning functions use the domains from `store' and save
the N subproblems created in the STORES given in their 2nd argument.
*/
int (*fd__split_problem_f)(int n, _fd_store stores[]);
#ifdef SPLITGO_MPI
int (*fd__split_team_problem_f)(int n, _fd_store stores[]);
#endif
static int fd__split_eager(int n, _fd_store stores[])
{
int np, minv, maxv;
int v;
np = 1;
for (v = 0; v < fd__label_vars_count && np < n; v++)
{
int cp = np;
int vi = fd__label_vars[v]->index;
int p;
for (p = 0; p < cp && np < n; ++p)
{
fd_iterator iterator = _fd_val_iterator(DOMAIN(fd__label_vars[v]));
minv = _fd_val_next_element(iterator);
_fd_init_domain(SVALUE(stores[p][vi]), minv, minv);
while (_fd_val_has_next(iterator) && np < n)
{
int i = _fd_val_next_element(iterator);
int j;
for (j = 0; j < v; ++j)
_fd_copy_value(SVALUE(stores[np][fd__label_vars[j]->index]),
SVALUE(stores[p][fd__label_vars[j]->index]));
_fd_init_domain(SVALUE(stores[np][vi]), i, i);
np++;
}
_fd_val_iterator_dispose(iterator);
// XXX: this reassigns a domain to the last variable assigned,
// which is a waste (although it only does that once?)
if (np == n)
{
int value;
_fd_val_single(SVALUE(stores[np - 1][vi]), &value);
_fd_copy_value(SVALUE(stores[np - 1][vi]), DOMAIN(fd__label_vars[v]));
_fd_val_del_lt(value, &stores[np - 1][vi]); // XXX
}
}
for (; p < cp; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
}
for (; v < fd__label_vars_count; ++v)
{
int vi = fd__label_vars[v]->index;
int p;
for (p = 0; p < n; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
}
return np;
}
static int split_even(int n, _fd_store stores[], int first_var)
{
int v, vi, p;
int ds;
int g;
fd_iterator iterator;
v = first_var;
while (v < fd__label_vars_count &&
(ds = _fd_val_size(DOMAIN(fd__label_vars[v]))) == 1)
{
vi = fd__label_vars[v]->index;
for (p = 0; p < n; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
++v;
}
if (v == fd__label_vars_count)
return 1; // all domains are singletons
if (ds >= n)
{
// divide the v-th variable domain between the n stores
iterator = _fd_val_iterator(DOMAIN(fd__label_vars[v]));
vi = fd__label_vars[v]->index;
for (p = 0; p < n; ++p)
{
// this is all maybe a little wasteful
int minv, maxv;
int i;
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
minv = maxv = _fd_val_next_element(iterator);
// find the last element of the new domain
for (i = ds / n + (p >= n - ds % n); i > 1; --i)
maxv = _fd_val_next_element(iterator);
(void) _fd_val_del_lt(minv, (DOMAIN_REF_T()) &stores[p][vi]); // XXX
(void) _fd_val_del_gt(maxv, (DOMAIN_REF_T()) &stores[p][vi]); // XXX
}
// copy the domains of the remaining variables to all the stores
for (v++; v < fd__label_vars_count; v++)
{
vi = fd__label_vars[v]->index;
for (p = 0; p < n; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
}
return n;
}
/*
the domain of the variable has less than n elements, some stores
will have to share a value:
- the first n % ds elements will be shared by n / ds + 1 stores
- the remaining ds - n % ds elements will be shared by n / ds
stores
*/
iterator = _fd_val_iterator(DOMAIN(fd__label_vars[v]));
vi = fd__label_vars[v]->index;
p = 0;
for (g = 0; g < ds; ++g)
{
int e = _fd_val_next_element(iterator);
int gs = n / ds + (g < n % ds);
int j;
for (j = 0; j < gs; ++j)
_fd_init_domain(SVALUE(stores[p + j][vi]), e, e);
// count the stores really created
p += split_even(gs, stores + p, v + 1);
}
return p;
}
static int fd__split_even(int n, _fd_store stores[])
{
return split_even(n, stores, 0);
}
/*
Split the store evenly on the first variable whose domain has at
least N elements. If no such variable exists, use split-even.
*/
static int fd__split_even_one(int n, _fd_store stores[])
{
int v, vi, p;
int ds;
fd_iterator iterator;
for (v = 0; v < fd__label_vars_count; ++v)
{
// see if the v-th variable domain can be split n-ways
if ((ds = _fd_val_size(DOMAIN(fd__label_vars[v]))) >= n)
break;
// as it can't, copy it to all the stores
vi = fd__label_vars[v]->index;
for (p = 0; p < n; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
}
if (v == fd__label_vars_count)
{
_fd_debug("[%d] cannot split %d-ways evenly, resorting to even "
"splitting\n", tid, n);
return fd__split_even(n, stores);
}
// divide the v-th variable domain between the n stores
iterator = _fd_val_iterator(DOMAIN(fd__label_vars[v]));
for (p = 0; p < n; ++p)
{
// this is all maybe a little wasteful
int minv, maxv;
int i;
vi = fd__label_vars[v]->index;
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
minv = maxv = _fd_val_next_element(iterator);
// find the last element of the new domain
for (i = ds / n + (p >= n - ds % n); i > 1; --i)
maxv = _fd_val_next_element(iterator);
(void) _fd_val_del_lt(minv, (DOMAIN_REF_T()) &stores[p][vi]); // XXX
(void) _fd_val_del_gt(maxv, (DOMAIN_REF_T()) &stores[p][vi]); // XXX
}
// copy the domains of the remaining variables to all the stores
for (v++; v < fd__label_vars_count; v++)
{
vi = fd__label_vars[v]->index;
for (p = 0; p < n; ++p)
_fd_copy_value(SVALUE(stores[p][vi]), DOMAIN(fd__label_vars[v]));
}
return n;
}
int fd__split_problem(int n, _fd_store stores[],
int (*splitter)(int, _fd_store[]))
{
int parts;
int v, p;
#ifdef DEBUG_SPLITTING
_fd_output("before splitting: "); _fd_cprint3(store);
#endif
parts = splitter(n, stores);
// copy the domains of the non-labelled variables to the sub-search
// spaces
if (fd__label_vars_count != fd_variables_count)
for (v = 0; v < fd_variables_count; ++v)
if (!fd__var_labelled[v])
for (p = 0; p < parts; ++p)
_fd_copy_value(SVALUE(stores[p][v]), DOMAIN(_fd_variables[v]));
#ifdef DEBUG_SPLITTING
_fd_output("split into %d out of %d stores\n", np, n);
for (p = 0; p < n; ++p)
_fd_output("store %d: ", p), _fd_cprint3(stores[p]);
#endif
return parts;
}
void fd__init_splitgo(int *argc, char *argv[])
{
int args = *argc;
int i, j;
fd__split_problem_f = fd__split_eager;
#ifdef SPLITGO_MPI
fd__split_team_problem_f = 0;
#endif
for (i = j = 1; i < args; i++)
if (!strcmp(argv[i], "--split-even"))
fd__split_problem_f = fd__split_even;
else if (!strcmp(argv[i], "--split-even1"))
fd__split_problem_f = fd__split_even_one;
else if (!strcmp(argv[i], "--split-eager"))
fd__split_problem_f = fd__split_eager;
#ifdef SPLITGO_MPI
else if (!strcmp(argv[i], "--team-split-even"))
fd__split_team_problem_f = fd__split_even;
else if (!strcmp(argv[i], "--team-split-even1"))
fd__split_team_problem_f = fd__split_even_one;
else if (!strcmp(argv[i], "--team-split-eager"))
fd__split_team_problem_f = fd__split_eager;
#endif
else
argv[j++] = argv[i];
*argc = j;
#ifdef SPLITGO_MPI
if (!fd__split_team_problem_f)
fd__split_team_problem_f = fd__split_problem_f;
#endif
}