ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfmesh.c
(Generate patch)

Comparing ray/src/cv/bsdfmesh.c (file contents):
Revision 2.10 by greg, Thu Sep 26 14:57:18 2013 UTC vs.
Revision 2.13 by greg, Fri Nov 1 18:23:50 2013 UTC

# Line 176 | Line 176 | price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, co
176              pm->prow = pricerow(pm,i);
177              srow = psortrow(pm,i);
178              for (j = to_rbf->nrbf; j--; ) {
179 <                double          dprod = DOT(vfrom, vto[j]);
180 <                pm->prow[j] = ((dprod >= 1.) ? .0 : acos(dprod)) +
181 <                                fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang);
179 >                double          d;              /* quadratic cost function */
180 >                d = DOT(vfrom, vto[j]);
181 >                d = (d >= 1.) ? .0 : acos(d);
182 >                pm->prow[j] = d*d;
183 >                d = R2ANG(to_rbf->rbfa[j].crad) - from_ang;
184 >                pm->prow[j] += d*d;    
185                  srow[j] = j;
186              }
187              qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp);
# Line 198 | Line 201 | free_routes(PRICEMAT *pm)
201   static double
202   min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s)
203   {
204 +        const short     *srow = psortrow(pm,s);
205 +        const float     *prow = pricerow(pm,s);
206          double          total_cost = 0;
207          int             j;
203
204        if (amt2move <= FTINY)                  /* pre-emptive check */
205                return(.0);
208                                                  /* move cheapest first */
209 <        for (j = 0; j < pm->ncols && amt2move > FTINY; j++) {
210 <                int     d = psortrow(pm,s)[j];
209 >        for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) {
210 >                int     d = srow[j];
211                  double  amt = (amt2move < avail[d]) ? amt2move : avail[d];
212  
213 <                total_cost += amt * pricerow(pm,s)[d];
213 >                total_cost += amt * prow[d];
214                  amt2move -= amt;
215          }
216          return(total_cost);
217   }
218  
219 < /* Take a step in migration by choosing optimal bucket to transfer */
219 > /* Compare entries by moving price */
220 > static int
221 > rmovcmp(void *b, const void *p1, const void *p2)
222 > {
223 >        PRICEMAT        *pm = (PRICEMAT *)b;
224 >        const short     *ij1 = (const short *)p1;
225 >        const short     *ij2 = (const short *)p2;
226 >        float           price_diff;
227 >
228 >        if (ij1[1] < 0) return(ij2[1] >= 0);
229 >        if (ij2[1] < 0) return(-1);
230 >        price_diff = pricerow(pm,ij1[0])[ij1[1]] - pricerow(pm,ij2[0])[ij2[1]];
231 >        if (price_diff > 0) return(1);
232 >        if (price_diff < 0) return(-1);
233 >        return(0);
234 > }
235 >
236 > /* Take a step in migration by choosing reasonable bucket to transfer */
237   static double
238 < migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm)
238 > migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm)
239   {
240 +        const int       max2check = 100;
241          const double    maxamt = 1./(double)pm->ncols;
242 <        const double    minamt = maxamt*5e-6;
242 >        const double    minamt = maxamt*1e-4;
243          double          *src_cost;
244 +        short           (*rord)[2];
245          struct {
246                  int     s, d;   /* source and destination */
247                  double  price;  /* price estimate per amount moved */
248                  double  amt;    /* amount we can move */
249          } cur, best;
250 <        int             i;
250 >        int             r2check, i, ri;
251 >        /*
252 >         * Check cheapest available routes only -- a higher adjusted
253 >         * destination price implies that another source is closer, so
254 >         * we can hold off considering more expensive options until
255 >         * some other (hopefully better) moves have been made.
256 >         */
257 >                                                /* most promising row order */
258 >        rord = (short (*)[2])malloc(sizeof(short)*2*pm->nrows);
259 >        if (rord == NULL)
260 >                goto memerr;
261 >        for (ri = pm->nrows; ri--; ) {
262 >            rord[ri][0] = ri;
263 >            rord[ri][1] = -1;
264 >            if (src_rem[ri] <= minamt)          /* enough source material? */
265 >                    continue;
266 >            for (i = 0; i < pm->ncols; i++)
267 >                if (dst_rem[ rord[ri][1] = psortrow(pm,ri)[i] ] > minamt)
268 >                        break;
269 >            if (i >= pm->ncols) {               /* moved all we can? */
270 >                free(rord);
271 >                return(.0);
272 >            }
273 >        }
274 >        if (pm->nrows > max2check)              /* sort if too many sources */
275 >                qsort_r(rord, pm->nrows, sizeof(short)*2, pm, &rmovcmp);
276                                                  /* allocate cost array */
277          src_cost = (double *)malloc(sizeof(double)*pm->nrows);
278 <        if (src_cost == NULL) {
279 <                fprintf(stderr, "%s: Out of memory in migration_step()\n",
234 <                                progname);
235 <                exit(1);
236 <        }
278 >        if (src_cost == NULL)
279 >                goto memerr;
280          for (i = pm->nrows; i--; )              /* starting costs for diff. */
281                  src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i);
239
282                                                  /* find best source & dest. */
283          best.s = best.d = -1; best.price = FHUGE; best.amt = 0;
284 <        for (cur.s = pm->nrows; cur.s--; ) {
284 >        if ((r2check = pm->nrows) > max2check)
285 >                r2check = max2check;            /* put a limit on search */
286 >        for (ri = 0; ri < r2check; ri++) {      /* check each source row */
287              double      cost_others = 0;
288 <
289 <            if (src_rem[cur.s] <= minamt)
290 <                    continue;
291 <                                                /* examine cheapest dest. */
292 <            for (i = 0; i < pm->ncols; i++)
293 <                if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt)
250 <                        break;
251 <            if (i >= pm->ncols)
252 <                break;
253 <            if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price)
254 <                continue;                       /* no point checking further */
288 >            cur.s = rord[ri][0];
289 >            if ((cur.d = rord[ri][1]) < 0 ||
290 >                        (cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) {
291 >                if (pm->nrows > max2check) break;       /* sorted end */
292 >                continue;                       /* else skip this one */
293 >            }
294              cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ?
295                                  src_rem[cur.s] : dst_rem[cur.d];
296 <            if (cur.amt > maxamt) cur.amt = maxamt;
297 <            dst_rem[cur.d] -= cur.amt;          /* add up differential costs */
296 >                                                /* don't just leave smidgen */
297 >            if (cur.amt > maxamt*1.02) cur.amt = maxamt;
298 >            dst_rem[cur.d] -= cur.amt;          /* add up opportunity costs */
299              for (i = pm->nrows; i--; )
300                  if (i != cur.s)
301 <                        cost_others += min_cost(src_rem[i], dst_rem, pm, i)
301 >                    cost_others += min_cost(src_rem[i], dst_rem, pm, i)
302                                          - src_cost[i];
303              dst_rem[cur.d] += cur.amt;          /* undo trial move */
304              cur.price += cost_others/cur.amt;   /* adjust effective price */
305              if (cur.price < best.price)         /* are we better than best? */
306 <                    best = cur;
306 >                best = cur;
307          }
308 <        free(src_cost);                         /* finish up */
309 <
308 >        free(src_cost);                         /* clean up */
309 >        free(rord);
310          if ((best.s < 0) | (best.d < 0))        /* nothing left to move? */
311                  return(.0);
312                                                  /* else make the actual move */
# Line 274 | Line 314 | migration_step(MIGRATION *mig, double *src_rem, double
314          src_rem[best.s] -= best.amt;
315          dst_rem[best.d] -= best.amt;
316          return(best.amt);
317 + memerr:
318 +        fprintf(stderr, "%s: Out of memory in migration_step()\n", progname);
319 +        exit(1);
320   }
321  
322   /* Compute and insert migration along directed edge (may fork child) */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines