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.2 by greg, Sat Oct 20 07:02:00 2012 UTC vs.
Revision 2.10 by greg, Thu Sep 26 14:57:18 2013 UTC

# Line 23 | Line 23 | int                    nprocs = 1;
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 +        float           *prow;          /* current price row */
31 + } PRICEMAT;                     /* sorted pricing matrix */
32 +
33 + #define pricerow(p,i)   ((p)->price + (i)*(p)->ncols)
34 + #define psortrow(p,i)   ((p)->sord + (i)*(p)->ncols)
35 +
36   /* Create a new migration holder (sharing memory for multiprocessing) */
37   static MIGRATION *
38   new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
# Line 110 | Line 120 | run_subprocess(void)
120                  if (pid < 0) {
121                          fprintf(stderr, "%s: cannot fork subprocess\n",
122                                          progname);
123 +                        await_children(nchild);
124                          exit(1);
125                  }
126                  ++nchild;                       /* subprocess started */
# Line 124 | Line 135 | run_subprocess(void)
135  
136   #endif  /* ! _WIN32 */
137  
138 + /* Comparison routine needed for sorting price row */
139 + static int
140 + msrt_cmp(void *b, const void *p1, const void *p2)
141 + {
142 +        PRICEMAT        *pm = (PRICEMAT *)b;
143 +        float           c1 = pm->prow[*(const short *)p1];
144 +        float           c2 = pm->prow[*(const short *)p2];
145 +
146 +        if (c1 > c2) return(1);
147 +        if (c1 < c2) return(-1);
148 +        return(0);
149 + }
150 +
151   /* Compute (and allocate) migration price matrix for optimization */
152 < static float *
153 < price_routes(const RBFNODE *from_rbf, const RBFNODE *to_rbf)
152 > static void
153 > price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf)
154   {
131        float   *pmtx = (float *)malloc(sizeof(float) *
132                                        from_rbf->nrbf * to_rbf->nrbf);
155          FVECT   *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf);
156          int     i, j;
157  
158 <        if ((pmtx == NULL) | (vto == NULL)) {
158 >        pm->nrows = from_rbf->nrbf;
159 >        pm->ncols = to_rbf->nrbf;
160 >        pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols);
161 >        pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols);
162 >        
163 >        if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) {
164                  fprintf(stderr, "%s: Out of memory in migration_costs()\n",
165                                  progname);
166                  exit(1);
# Line 144 | Line 171 | price_routes(const RBFNODE *from_rbf, const RBFNODE *t
171          for (i = from_rbf->nrbf; i--; ) {
172              const double        from_ang = R2ANG(from_rbf->rbfa[i].crad);
173              FVECT               vfrom;
174 +            short               *srow;
175              ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy);
176 <            for (j = to_rbf->nrbf; j--; )
177 <                pmtx[i*to_rbf->nrbf + j] = acos(DOT(vfrom, vto[j])) +
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);
182 +                srow[j] = j;
183 +            }
184 +            qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp);
185          }
186          free(vto);
153        return(pmtx);
187   }
188  
189 < /* Comparison routine needed for sorting price row */
190 < static const float      *price_arr;
191 < static int
159 < msrt_cmp(const void *p1, const void *p2)
189 > /* Free price matrix */
190 > static void
191 > free_routes(PRICEMAT *pm)
192   {
193 <        float   c1 = price_arr[*(const int *)p1];
194 <        float   c2 = price_arr[*(const int *)p2];
163 <
164 <        if (c1 > c2) return(1);
165 <        if (c1 < c2) return(-1);
166 <        return(0);
193 >        free(pm->price); pm->price = NULL;
194 >        free(pm->sord); pm->sord = NULL;
195   }
196  
197   /* Compute minimum (optimistic) cost for moving the given source material */
198   static double
199 < min_cost(double amt2move, const double *avail, const float *price, int n)
199 > min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s)
200   {
173        static int      *price_sort = NULL;
174        static int      n_alloc = 0;
201          double          total_cost = 0;
202 <        int             i;
202 >        int             j;
203  
204          if (amt2move <= FTINY)                  /* pre-emptive check */
205 <                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);
205 >                return(.0);
206                                                  /* move cheapest first */
207 <        for (i = 0; i < n && amt2move > FTINY; i++) {
208 <                int     d = price_sort[i];
207 >        for (j = 0; j < pm->ncols && amt2move > FTINY; j++) {
208 >                int     d = psortrow(pm,s)[j];
209                  double  amt = (amt2move < avail[d]) ? amt2move : avail[d];
210  
211 <                total_cost += amt * price[d];
211 >                total_cost += amt * pricerow(pm,s)[d];
212                  amt2move -= amt;
213          }
214          return(total_cost);
# Line 204 | Line 216 | min_cost(double amt2move, const double *avail, const f
216  
217   /* Take a step in migration by choosing optimal bucket to transfer */
218   static double
219 < migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const float *pmtx)
219 > migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm)
220   {
221 <        const double    maxamt = .1;
221 >        const double    maxamt = 1./(double)pm->ncols;
222          const double    minamt = maxamt*5e-6;
223 <        static double   *src_cost = NULL;
212 <        static int      n_alloc = 0;
223 >        double          *src_cost;
224          struct {
225                  int     s, d;   /* source and destination */
226                  double  price;  /* price estimate per amount moved */
227                  double  amt;    /* amount we can move */
228          } cur, best;
229          int             i;
230 <
231 <        if (mtx_nrows(mig) > n_alloc) {         /* allocate cost array */
232 <                if (n_alloc)
233 <                        free(src_cost);
234 <                src_cost = (double *)malloc(sizeof(double)*mtx_nrows(mig));
235 <                if (src_cost == NULL) {
225 <                        fprintf(stderr, "%s: Out of memory in migration_step()\n",
226 <                                        progname);
227 <                        exit(1);
228 <                }
229 <                n_alloc = mtx_nrows(mig);
230 >                                                /* allocate cost array */
231 >        src_cost = (double *)malloc(sizeof(double)*pm->nrows);
232 >        if (src_cost == NULL) {
233 >                fprintf(stderr, "%s: Out of memory in migration_step()\n",
234 >                                progname);
235 >                exit(1);
236          }
237 <        for (i = mtx_nrows(mig); i--; )         /* starting costs for diff. */
238 <                src_cost[i] = min_cost(src_rem[i], dst_rem,
233 <                                        pmtx+i*mtx_ncols(mig), mtx_ncols(mig));
237 >        for (i = pm->nrows; i--; )              /* starting costs for diff. */
238 >                src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i);
239  
240                                                  /* find best source & dest. */
241          best.s = best.d = -1; best.price = FHUGE; best.amt = 0;
242 <        for (cur.s = mtx_nrows(mig); cur.s--; ) {
238 <            const float *price = pmtx + cur.s*mtx_ncols(mig);
242 >        for (cur.s = pm->nrows; cur.s--; ) {
243              double      cost_others = 0;
244 +
245              if (src_rem[cur.s] <= minamt)
246                      continue;
247 <            cur.d = -1;                         /* examine cheapest dest. */
248 <            for (i = mtx_ncols(mig); i--; )
249 <                if (dst_rem[i] > minamt &&
250 <                                (cur.d < 0 || price[i] < price[cur.d]))
251 <                        cur.d = i;
252 <            if (cur.d < 0)
253 <                    return(.0);
254 <            if ((cur.price = price[cur.d]) >= best.price)
250 <                    continue;                   /* no point checking further */
247 >                                                /* examine cheapest dest. */
248 >            for (i = 0; i < pm->ncols; i++)
249 >                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 */
255              cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ?
256                                  src_rem[cur.s] : dst_rem[cur.d];
257              if (cur.amt > maxamt) cur.amt = maxamt;
258              dst_rem[cur.d] -= cur.amt;          /* add up differential costs */
259 <            for (i = mtx_nrows(mig); i--; )
259 >            for (i = pm->nrows; i--; )
260                  if (i != cur.s)
261 <                        cost_others += min_cost(src_rem[i], dst_rem,
258 <                                                price, mtx_ncols(mig))
261 >                        cost_others += min_cost(src_rem[i], dst_rem, pm, i)
262                                          - src_cost[i];
263              dst_rem[cur.d] += cur.amt;          /* undo trial move */
264              cur.price += cost_others/cur.amt;   /* adjust effective price */
265              if (cur.price < best.price)         /* are we better than best? */
266                      best = cur;
267          }
268 <        if ((best.s < 0) | (best.d < 0))
268 >        free(src_cost);                         /* finish up */
269 >
270 >        if ((best.s < 0) | (best.d < 0))        /* nothing left to move? */
271                  return(.0);
272 <                                                /* make the actual move */
272 >                                                /* else make the actual move */
273          mtx_coef(mig,best.s,best.d) += best.amt;
274          src_rem[best.s] -= best.amt;
275          dst_rem[best.d] -= best.amt;
276          return(best.amt);
277   }
278  
274 #ifdef DEBUG
275 static char *
276 thetaphi(const FVECT v)
277 {
278        static char     buf[128];
279        double          theta, phi;
280
281        theta = 180./M_PI*acos(v[2]);
282        phi = 180./M_PI*atan2(v[1],v[0]);
283        sprintf(buf, "(%.0f,%.0f)", theta, phi);
284
285        return(buf);
286 }
287 #endif
288
279   /* Compute and insert migration along directed edge (may fork child) */
280   static MIGRATION *
281   create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
282   {
283          const double    end_thresh = 5e-6;
284 <        float           *pmtx;
284 >        PRICEMAT        pmtx;
285          MIGRATION       *newmig;
286          double          *src_rem, *dst_rem;
287          double          total_rem = 1., move_amt;
288 <        int             i;
288 >        int             i, j;
289                                                  /* check if exists already */
290          for (newmig = from_rbf->ejl; newmig != NULL;
291                          newmig = nextedge(from_rbf,newmig))
292                  if (newmig->rbfv[1] == to_rbf)
293                          return(NULL);
294                                                  /* else allocate */
295 + #ifdef DEBUG
296 +        fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ",
297 +                        get_theta180(from_rbf->invec),
298 +                        get_phi360(from_rbf->invec));
299 +        fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n",
300 +                        get_theta180(to_rbf->invec),
301 +                        get_phi360(to_rbf->invec),
302 +                        from_rbf->nrbf, to_rbf->nrbf);
303 + #endif
304          newmig = new_migration(from_rbf, to_rbf);
305          if (run_subprocess())
306                  return(newmig);                 /* child continues */
307 <        pmtx = price_routes(from_rbf, to_rbf);
307 >        price_routes(&pmtx, from_rbf, to_rbf);
308          src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf);
309          dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf);
310          if ((src_rem == NULL) | (dst_rem == NULL)) {
# Line 313 | Line 312 | create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
312                                  progname);
313                  exit(1);
314          }
316 #ifdef DEBUG
317        fprintf(stderr, "Building path from (theta,phi) %s ",
318                        thetaphi(from_rbf->invec));
319        fprintf(stderr, "to %s with %d x %d matrix\n",
320                        thetaphi(to_rbf->invec),
321                        from_rbf->nrbf, to_rbf->nrbf);
322 #endif
315                                                  /* starting quantities */
316          memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf);
317          for (i = from_rbf->nrbf; i--; )
318                  src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal;
319 <        for (i = to_rbf->nrbf; i--; )
320 <                dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal;
319 >        for (j = to_rbf->nrbf; j--; )
320 >                dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal;
321 >
322          do {                                    /* move a bit at a time */
323 <                move_amt = migration_step(newmig, src_rem, dst_rem, pmtx);
323 >                move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx);
324                  total_rem -= move_amt;
332 #ifdef DEBUG
333                if (!nchild)
334                        fprintf(stderr, "\r%.9f remaining...", total_rem);
335 #endif
325          } while ((total_rem > end_thresh) & (move_amt > 0));
326 < #ifdef DEBUG
338 <        if (!nchild) fputs("done.\n", stderr);
339 <        else fprintf(stderr, "finished with %.9f remaining\n", total_rem);
340 < #endif
326 >
327          for (i = from_rbf->nrbf; i--; ) {       /* normalize final matrix */
328 <            float       nf = rbf_volume(&from_rbf->rbfa[i]);
343 <            int         j;
328 >            double      nf = rbf_volume(&from_rbf->rbfa[i]);
329              if (nf <= FTINY) continue;
330              nf = from_rbf->vtotal / nf;
331              for (j = to_rbf->nrbf; j--; )
332 <                mtx_coef(newmig,i,j) *= nf;
332 >                mtx_coef(newmig,i,j) *= nf;     /* row now sums to 1.0 */
333          }
334          end_subprocess();                       /* exit here if subprocess */
335 <        free(pmtx);                             /* free working arrays */
335 >        free_routes(&pmtx);                     /* free working arrays */
336          free(src_rem);
337          free(dst_rem);
338          return(newmig);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines