23 |
|
/* number of children (-1 in child) */ |
24 |
|
static int nchild = 0; |
25 |
|
|
26 |
+ |
typedef struct { |
27 |
+ |
int nrows, ncols; /* array size (matches migration) */ |
28 |
+ |
float *price; /* migration prices */ |
29 |
+ |
short *sord; /* sort for each row, low to high */ |
30 |
+ |
} PRICEMAT; /* sorted pricing matrix */ |
31 |
+ |
|
32 |
+ |
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
33 |
+ |
#define psortrow(p,i) ((p)->sord + (i)*(p)->ncols) |
34 |
+ |
|
35 |
|
/* Create a new migration holder (sharing memory for multiprocessing) */ |
36 |
|
static MIGRATION * |
37 |
|
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
133 |
|
|
134 |
|
#endif /* ! _WIN32 */ |
135 |
|
|
136 |
+ |
/* Comparison routine needed for sorting price row */ |
137 |
+ |
static int |
138 |
+ |
msrt_cmp(void *b, const void *p1, const void *p2) |
139 |
+ |
{ |
140 |
+ |
PRICEMAT *pm = (PRICEMAT *)b; |
141 |
+ |
int ri = ((const short *)p1 - pm->sord) / pm->ncols; |
142 |
+ |
float c1 = pricerow(pm,ri)[*(const short *)p1]; |
143 |
+ |
float c2 = pricerow(pm,ri)[*(const short *)p2]; |
144 |
+ |
|
145 |
+ |
if (c1 > c2) return(1); |
146 |
+ |
if (c1 < c2) return(-1); |
147 |
+ |
return(0); |
148 |
+ |
} |
149 |
+ |
|
150 |
|
/* Compute (and allocate) migration price matrix for optimization */ |
151 |
< |
static float * |
152 |
< |
price_routes(const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
151 |
> |
static void |
152 |
> |
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
153 |
|
{ |
131 |
– |
float *pmtx = (float *)malloc(sizeof(float) * |
132 |
– |
from_rbf->nrbf * to_rbf->nrbf); |
154 |
|
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
155 |
|
int i, j; |
156 |
|
|
157 |
< |
if ((pmtx == NULL) | (vto == NULL)) { |
157 |
> |
pm->nrows = from_rbf->nrbf; |
158 |
> |
pm->ncols = to_rbf->nrbf; |
159 |
> |
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
160 |
> |
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
161 |
> |
|
162 |
> |
if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { |
163 |
|
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
164 |
|
progname); |
165 |
|
exit(1); |
171 |
|
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
172 |
|
FVECT vfrom; |
173 |
|
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
174 |
< |
for (j = to_rbf->nrbf; j--; ) |
175 |
< |
pmtx[i*to_rbf->nrbf + j] = acos(DOT(vfrom, vto[j])) + |
174 |
> |
for (j = to_rbf->nrbf; j--; ) { |
175 |
> |
pricerow(pm,i)[j] = acos(DOT(vfrom, vto[j])) + |
176 |
|
fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); |
177 |
+ |
psortrow(pm,i)[j] = j; |
178 |
+ |
} |
179 |
+ |
qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); |
180 |
|
} |
181 |
|
free(vto); |
153 |
– |
return(pmtx); |
182 |
|
} |
183 |
|
|
184 |
< |
/* Comparison routine needed for sorting price row */ |
185 |
< |
static const float *price_arr; |
186 |
< |
static int |
159 |
< |
msrt_cmp(const void *p1, const void *p2) |
184 |
> |
/* Free price matrix */ |
185 |
> |
static void |
186 |
> |
free_routes(PRICEMAT *pm) |
187 |
|
{ |
188 |
< |
float c1 = price_arr[*(const int *)p1]; |
189 |
< |
float c2 = price_arr[*(const int *)p2]; |
163 |
< |
|
164 |
< |
if (c1 > c2) return(1); |
165 |
< |
if (c1 < c2) return(-1); |
166 |
< |
return(0); |
188 |
> |
free(pm->price); pm->price = NULL; |
189 |
> |
free(pm->sord); pm->sord = NULL; |
190 |
|
} |
191 |
|
|
192 |
|
/* Compute minimum (optimistic) cost for moving the given source material */ |
193 |
|
static double |
194 |
< |
min_cost(double amt2move, const double *avail, const float *price, int n) |
194 |
> |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
195 |
|
{ |
173 |
– |
static int *price_sort = NULL; |
174 |
– |
static int n_alloc = 0; |
196 |
|
double total_cost = 0; |
197 |
< |
int i; |
197 |
> |
int j; |
198 |
|
|
199 |
|
if (amt2move <= FTINY) /* pre-emptive check */ |
200 |
|
return(0.); |
180 |
– |
if (n > n_alloc) { /* (re)allocate sort array */ |
181 |
– |
if (n_alloc) free(price_sort); |
182 |
– |
price_sort = (int *)malloc(sizeof(int)*n); |
183 |
– |
if (price_sort == NULL) { |
184 |
– |
fprintf(stderr, "%s: Out of memory in min_cost()\n", |
185 |
– |
progname); |
186 |
– |
exit(1); |
187 |
– |
} |
188 |
– |
n_alloc = n; |
189 |
– |
} |
190 |
– |
for (i = n; i--; ) |
191 |
– |
price_sort[i] = i; |
192 |
– |
price_arr = price; |
193 |
– |
qsort(price_sort, n, sizeof(int), &msrt_cmp); |
201 |
|
/* move cheapest first */ |
202 |
< |
for (i = 0; i < n && amt2move > FTINY; i++) { |
203 |
< |
int d = price_sort[i]; |
202 |
> |
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
203 |
> |
int d = psortrow(pm,s)[j]; |
204 |
|
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
205 |
|
|
206 |
< |
total_cost += amt * price[d]; |
206 |
> |
total_cost += amt * pricerow(pm,s)[d]; |
207 |
|
amt2move -= amt; |
208 |
|
} |
209 |
|
return(total_cost); |
211 |
|
|
212 |
|
/* Take a step in migration by choosing optimal bucket to transfer */ |
213 |
|
static double |
214 |
< |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const float *pmtx) |
214 |
> |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
215 |
|
{ |
216 |
|
const double maxamt = .1; |
217 |
|
const double minamt = maxamt*5e-6; |
224 |
|
} cur, best; |
225 |
|
int i; |
226 |
|
|
227 |
< |
if (mtx_nrows(mig) > n_alloc) { /* allocate cost array */ |
227 |
> |
if (pm->nrows > n_alloc) { /* allocate cost array */ |
228 |
|
if (n_alloc) |
229 |
|
free(src_cost); |
230 |
< |
src_cost = (double *)malloc(sizeof(double)*mtx_nrows(mig)); |
230 |
> |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
231 |
|
if (src_cost == NULL) { |
232 |
|
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
233 |
|
progname); |
234 |
|
exit(1); |
235 |
|
} |
236 |
< |
n_alloc = mtx_nrows(mig); |
236 |
> |
n_alloc = pm->nrows; |
237 |
|
} |
238 |
< |
for (i = mtx_nrows(mig); i--; ) /* starting costs for diff. */ |
239 |
< |
src_cost[i] = min_cost(src_rem[i], dst_rem, |
233 |
< |
pmtx+i*mtx_ncols(mig), mtx_ncols(mig)); |
238 |
> |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
239 |
> |
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
240 |
|
|
241 |
|
/* find best source & dest. */ |
242 |
|
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
243 |
< |
for (cur.s = mtx_nrows(mig); cur.s--; ) { |
244 |
< |
const float *price = pmtx + cur.s*mtx_ncols(mig); |
243 |
> |
for (cur.s = pm->nrows; cur.s--; ) { |
244 |
> |
const float *price = pricerow(pm,cur.s); |
245 |
|
double cost_others = 0; |
246 |
|
if (src_rem[cur.s] <= minamt) |
247 |
|
continue; |
248 |
|
cur.d = -1; /* examine cheapest dest. */ |
249 |
< |
for (i = mtx_ncols(mig); i--; ) |
249 |
> |
for (i = pm->ncols; i--; ) |
250 |
|
if (dst_rem[i] > minamt && |
251 |
|
(cur.d < 0 || price[i] < price[cur.d])) |
252 |
|
cur.d = i; |
258 |
|
src_rem[cur.s] : dst_rem[cur.d]; |
259 |
|
if (cur.amt > maxamt) cur.amt = maxamt; |
260 |
|
dst_rem[cur.d] -= cur.amt; /* add up differential costs */ |
261 |
< |
for (i = mtx_nrows(mig); i--; ) |
261 |
> |
for (i = pm->nrows; i--; ) |
262 |
|
if (i != cur.s) |
263 |
< |
cost_others += min_cost(src_rem[i], dst_rem, |
258 |
< |
price, mtx_ncols(mig)) |
263 |
> |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
264 |
|
- src_cost[i]; |
265 |
|
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
266 |
|
cur.price += cost_others/cur.amt; /* adjust effective price */ |
296 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
297 |
|
{ |
298 |
|
const double end_thresh = 5e-6; |
299 |
< |
float *pmtx; |
299 |
> |
PRICEMAT pmtx; |
300 |
|
MIGRATION *newmig; |
301 |
|
double *src_rem, *dst_rem; |
302 |
|
double total_rem = 1., move_amt; |
310 |
|
newmig = new_migration(from_rbf, to_rbf); |
311 |
|
if (run_subprocess()) |
312 |
|
return(newmig); /* child continues */ |
313 |
< |
pmtx = price_routes(from_rbf, to_rbf); |
313 |
> |
price_routes(&pmtx, from_rbf, to_rbf); |
314 |
|
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
315 |
|
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
316 |
|
if ((src_rem == NULL) | (dst_rem == NULL)) { |
332 |
|
for (i = to_rbf->nrbf; i--; ) |
333 |
|
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
334 |
|
do { /* move a bit at a time */ |
335 |
< |
move_amt = migration_step(newmig, src_rem, dst_rem, pmtx); |
335 |
> |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
336 |
|
total_rem -= move_amt; |
337 |
|
#ifdef DEBUG |
338 |
|
if (!nchild) |
352 |
|
mtx_coef(newmig,i,j) *= nf; |
353 |
|
} |
354 |
|
end_subprocess(); /* exit here if subprocess */ |
355 |
< |
free(pmtx); /* free working arrays */ |
355 |
> |
free_routes(&pmtx); /* free working arrays */ |
356 |
|
free(src_rem); |
357 |
|
free(dst_rem); |
358 |
|
return(newmig); |