ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfmesh.c
Revision: 2.11
Committed: Thu Oct 24 16:11:37 2013 UTC (10 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +74 -34 lines
Log Message:
Sped up convergence on large systems by almost 3x

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: bsdfmesh.c,v 2.10 2013/09/26 14:57:18 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Create BSDF advection mesh from radial basis functions.
6     *
7     * G. Ward
8     */
9    
10     #ifndef _WIN32
11     #include <unistd.h>
12     #include <sys/wait.h>
13     #include <sys/mman.h>
14     #endif
15     #define _USE_MATH_DEFINES
16     #include <stdio.h>
17     #include <stdlib.h>
18     #include <string.h>
19     #include <math.h>
20     #include "bsdfrep.h"
21     /* number of processes to run */
22     int nprocs = 1;
23     /* number of children (-1 in child) */
24     static int nchild = 0;
25    
26 greg 2.3 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 greg 2.10 float *prow; /* current price row */
31 greg 2.3 } 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 greg 2.2 /* Create a new migration holder (sharing memory for multiprocessing) */
37     static MIGRATION *
38     new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
39     {
40     size_t memlen = sizeof(MIGRATION) +
41     sizeof(float)*(from_rbf->nrbf*to_rbf->nrbf - 1);
42     MIGRATION *newmig;
43     #ifdef _WIN32
44     if (nprocs > 1)
45     fprintf(stderr, "%s: warning - multiprocessing not supported\n",
46     progname);
47     nprocs = 1;
48     newmig = (MIGRATION *)malloc(memlen);
49     #else
50     if (nprocs <= 1) { /* single process? */
51     newmig = (MIGRATION *)malloc(memlen);
52     } else { /* else need to share memory */
53     newmig = (MIGRATION *)mmap(NULL, memlen, PROT_READ|PROT_WRITE,
54     MAP_ANON|MAP_SHARED, -1, 0);
55     if ((void *)newmig == MAP_FAILED)
56     newmig = NULL;
57     }
58     #endif
59     if (newmig == NULL) {
60     fprintf(stderr, "%s: cannot allocate new migration\n", progname);
61     exit(1);
62     }
63     newmig->rbfv[0] = from_rbf;
64     newmig->rbfv[1] = to_rbf;
65     /* insert in edge lists */
66     newmig->enxt[0] = from_rbf->ejl;
67     from_rbf->ejl = newmig;
68     newmig->enxt[1] = to_rbf->ejl;
69     to_rbf->ejl = newmig;
70     newmig->next = mig_list; /* push onto global list */
71     return(mig_list = newmig);
72     }
73    
74     #ifdef _WIN32
75     #define await_children(n) (void)(n)
76     #define run_subprocess() 0
77     #define end_subprocess() (void)0
78     #else
79    
80     /* Wait for the specified number of child processes to complete */
81     static void
82     await_children(int n)
83     {
84     int exit_status = 0;
85    
86     if (n > nchild)
87     n = nchild;
88     while (n-- > 0) {
89     int status;
90     if (wait(&status) < 0) {
91     fprintf(stderr, "%s: missing child(ren)!\n", progname);
92     nchild = 0;
93     break;
94     }
95     --nchild;
96     if (status) { /* something wrong */
97     if ((status = WEXITSTATUS(status)))
98     exit_status = status;
99     else
100     exit_status += !exit_status;
101     fprintf(stderr, "%s: subprocess died\n", progname);
102     n = nchild; /* wait for the rest */
103     }
104     }
105     if (exit_status)
106     exit(exit_status);
107     }
108    
109     /* Start child process if multiprocessing selected */
110     static pid_t
111     run_subprocess(void)
112     {
113     int status;
114     pid_t pid;
115    
116     if (nprocs <= 1) /* any children requested? */
117     return(0);
118     await_children(nchild + 1 - nprocs); /* free up child process */
119     if ((pid = fork())) {
120     if (pid < 0) {
121     fprintf(stderr, "%s: cannot fork subprocess\n",
122     progname);
123 greg 2.6 await_children(nchild);
124 greg 2.2 exit(1);
125     }
126     ++nchild; /* subprocess started */
127     return(pid);
128     }
129     nchild = -1;
130     return(0); /* put child to work */
131     }
132    
133     /* If we are in subprocess, call exit */
134     #define end_subprocess() if (nchild < 0) _exit(0); else
135    
136     #endif /* ! _WIN32 */
137    
138 greg 2.3 /* 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 greg 2.10 float c1 = pm->prow[*(const short *)p1];
144     float c2 = pm->prow[*(const short *)p2];
145 greg 2.3
146     if (c1 > c2) return(1);
147     if (c1 < c2) return(-1);
148     return(0);
149     }
150    
151 greg 2.1 /* Compute (and allocate) migration price matrix for optimization */
152 greg 2.3 static void
153     price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf)
154 greg 2.1 {
155     FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf);
156     int i, j;
157    
158 greg 2.3 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 greg 2.1 fprintf(stderr, "%s: Out of memory in migration_costs()\n",
165     progname);
166     exit(1);
167     }
168     for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */
169     ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy);
170    
171     for (i = from_rbf->nrbf; i--; ) {
172     const double from_ang = R2ANG(from_rbf->rbfa[i].crad);
173     FVECT vfrom;
174 greg 2.10 short *srow;
175 greg 2.1 ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy);
176 greg 2.10 pm->prow = pricerow(pm,i);
177     srow = psortrow(pm,i);
178 greg 2.3 for (j = to_rbf->nrbf; j--; ) {
179 greg 2.7 double dprod = DOT(vfrom, vto[j]);
180 greg 2.10 pm->prow[j] = ((dprod >= 1.) ? .0 : acos(dprod)) +
181 greg 2.1 fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang);
182 greg 2.10 srow[j] = j;
183 greg 2.3 }
184 greg 2.10 qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp);
185 greg 2.1 }
186     free(vto);
187     }
188    
189 greg 2.3 /* Free price matrix */
190     static void
191     free_routes(PRICEMAT *pm)
192 greg 2.1 {
193 greg 2.3 free(pm->price); pm->price = NULL;
194     free(pm->sord); pm->sord = NULL;
195 greg 2.1 }
196    
197     /* Compute minimum (optimistic) cost for moving the given source material */
198     static double
199 greg 2.3 min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s)
200 greg 2.1 {
201 greg 2.11 const short *srow = psortrow(pm,s);
202     const float *prow = pricerow(pm,s);
203 greg 2.1 double total_cost = 0;
204 greg 2.3 int j;
205 greg 2.1 /* move cheapest first */
206 greg 2.11 for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) {
207     int d = srow[j];
208 greg 2.1 double amt = (amt2move < avail[d]) ? amt2move : avail[d];
209    
210 greg 2.11 total_cost += amt * prow[d];
211 greg 2.1 amt2move -= amt;
212     }
213     return(total_cost);
214     }
215    
216 greg 2.11 /* Compare entries by moving price */
217     static int
218     rmovcmp(void *b, const void *p1, const void *p2)
219     {
220     PRICEMAT *pm = (PRICEMAT *)b;
221     const short *ij1 = (const short *)p1;
222     const short *ij2 = (const short *)p2;
223     float price_diff;
224    
225     if (ij1[1] < 0) return(ij2[1] >= 0);
226     if (ij2[1] < 0) return(-1);
227     price_diff = pricerow(pm,ij1[0])[ij1[1]] - pricerow(pm,ij2[0])[ij2[1]];
228     if (price_diff > 0) return(1);
229     if (price_diff < 0) return(-1);
230     return(0);
231     }
232    
233     /* Take a step in migration by choosing reasonable bucket to transfer */
234 greg 2.1 static double
235 greg 2.11 migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm)
236 greg 2.1 {
237 greg 2.11 const int max2check = 100;
238 greg 2.4 const double maxamt = 1./(double)pm->ncols;
239 greg 2.2 const double minamt = maxamt*5e-6;
240 greg 2.5 double *src_cost;
241 greg 2.11 short (*rord)[2];
242 greg 2.1 struct {
243     int s, d; /* source and destination */
244     double price; /* price estimate per amount moved */
245     double amt; /* amount we can move */
246     } cur, best;
247 greg 2.11 int r2check, i, ri;
248     /*
249     * Check cheapest available routes only -- a higher adjusted
250     * destination price implies that another source is closer, so
251     * we can hold off considering more expensive options until
252     * some other (hopefully better) moves have been made.
253     */
254     /* most promising row order */
255     rord = (short (*)[2])malloc(sizeof(short)*2*pm->nrows);
256     if (rord == NULL)
257     goto memerr;
258     for (ri = pm->nrows; ri--; ) {
259     rord[ri][0] = ri;
260     rord[ri][1] = -1;
261     if (src_rem[ri] <= minamt) /* enough source material? */
262     continue;
263     for (i = 0; i < pm->ncols; i++)
264     if (dst_rem[ rord[ri][1] = psortrow(pm,ri)[i] ] > minamt)
265     break;
266     if (i >= pm->ncols) { /* moved all we can? */
267     free(rord);
268     return(.0);
269     }
270     }
271     if (pm->nrows > max2check) /* sort if too many sources */
272     qsort_r(rord, pm->nrows, sizeof(short)*2, pm, &rmovcmp);
273 greg 2.5 /* allocate cost array */
274     src_cost = (double *)malloc(sizeof(double)*pm->nrows);
275 greg 2.11 if (src_cost == NULL)
276     goto memerr;
277 greg 2.3 for (i = pm->nrows; i--; ) /* starting costs for diff. */
278     src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i);
279 greg 2.1 /* find best source & dest. */
280     best.s = best.d = -1; best.price = FHUGE; best.amt = 0;
281 greg 2.11 if ((r2check = pm->nrows) > max2check)
282     r2check = max2check; /* put a limit on search */
283     for (ri = 0; ri < r2check; ri++) { /* check each source row */
284 greg 2.1 double cost_others = 0;
285 greg 2.11 cur.s = rord[ri][0];
286     if ((cur.d = rord[ri][1]) < 0 ||
287     (cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) {
288     if (pm->nrows > max2check) break; /* sorted end */
289     continue; /* else skip this one */
290     }
291 greg 2.1 cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ?
292     src_rem[cur.s] : dst_rem[cur.d];
293 greg 2.11 /* don't just leave smidgen */
294     if (cur.amt > maxamt*1.02) cur.amt = maxamt;
295     dst_rem[cur.d] -= cur.amt; /* add up opportunity costs */
296 greg 2.3 for (i = pm->nrows; i--; )
297 greg 2.1 if (i != cur.s)
298 greg 2.11 cost_others += min_cost(src_rem[i], dst_rem, pm, i)
299 greg 2.1 - src_cost[i];
300     dst_rem[cur.d] += cur.amt; /* undo trial move */
301     cur.price += cost_others/cur.amt; /* adjust effective price */
302     if (cur.price < best.price) /* are we better than best? */
303 greg 2.11 best = cur;
304 greg 2.1 }
305 greg 2.11 free(src_cost); /* clean up */
306     free(rord);
307 greg 2.5 if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */
308 greg 2.1 return(.0);
309 greg 2.5 /* else make the actual move */
310 greg 2.2 mtx_coef(mig,best.s,best.d) += best.amt;
311 greg 2.1 src_rem[best.s] -= best.amt;
312     dst_rem[best.d] -= best.amt;
313     return(best.amt);
314 greg 2.11 memerr:
315     fprintf(stderr, "%s: Out of memory in migration_step()\n", progname);
316     exit(1);
317 greg 2.1 }
318    
319     /* Compute and insert migration along directed edge (may fork child) */
320     static MIGRATION *
321     create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf)
322     {
323 greg 2.2 const double end_thresh = 5e-6;
324 greg 2.3 PRICEMAT pmtx;
325 greg 2.1 MIGRATION *newmig;
326     double *src_rem, *dst_rem;
327     double total_rem = 1., move_amt;
328 greg 2.6 int i, j;
329 greg 2.1 /* check if exists already */
330     for (newmig = from_rbf->ejl; newmig != NULL;
331     newmig = nextedge(from_rbf,newmig))
332     if (newmig->rbfv[1] == to_rbf)
333     return(NULL);
334     /* else allocate */
335 greg 2.7 #ifdef DEBUG
336     fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ",
337     get_theta180(from_rbf->invec),
338     get_phi360(from_rbf->invec));
339     fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n",
340     get_theta180(to_rbf->invec),
341     get_phi360(to_rbf->invec),
342     from_rbf->nrbf, to_rbf->nrbf);
343     #endif
344 greg 2.1 newmig = new_migration(from_rbf, to_rbf);
345     if (run_subprocess())
346     return(newmig); /* child continues */
347 greg 2.3 price_routes(&pmtx, from_rbf, to_rbf);
348 greg 2.1 src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf);
349     dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf);
350     if ((src_rem == NULL) | (dst_rem == NULL)) {
351     fprintf(stderr, "%s: Out of memory in create_migration()\n",
352     progname);
353     exit(1);
354     }
355     /* starting quantities */
356     memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf);
357     for (i = from_rbf->nrbf; i--; )
358     src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal;
359 greg 2.6 for (j = to_rbf->nrbf; j--; )
360     dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal;
361    
362 greg 2.1 do { /* move a bit at a time */
363 greg 2.3 move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx);
364 greg 2.1 total_rem -= move_amt;
365 greg 2.2 } while ((total_rem > end_thresh) & (move_amt > 0));
366 greg 2.6
367 greg 2.1 for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */
368 greg 2.6 double nf = rbf_volume(&from_rbf->rbfa[i]);
369 greg 2.1 if (nf <= FTINY) continue;
370     nf = from_rbf->vtotal / nf;
371     for (j = to_rbf->nrbf; j--; )
372 greg 2.6 mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */
373 greg 2.1 }
374     end_subprocess(); /* exit here if subprocess */
375 greg 2.3 free_routes(&pmtx); /* free working arrays */
376 greg 2.1 free(src_rem);
377     free(dst_rem);
378     return(newmig);
379     }
380    
381     /* Check if prospective vertex would create overlapping triangle */
382     static int
383     overlaps_tri(const RBFNODE *bv0, const RBFNODE *bv1, const RBFNODE *pv)
384     {
385     const MIGRATION *ej;
386     RBFNODE *vother[2];
387     int im_rev;
388     /* find shared edge in mesh */
389     for (ej = pv->ejl; ej != NULL; ej = nextedge(pv,ej)) {
390     const RBFNODE *tv = opp_rbf(pv,ej);
391     if (tv == bv0) {
392     im_rev = is_rev_tri(ej->rbfv[0]->invec,
393     ej->rbfv[1]->invec, bv1->invec);
394     break;
395     }
396     if (tv == bv1) {
397     im_rev = is_rev_tri(ej->rbfv[0]->invec,
398     ej->rbfv[1]->invec, bv0->invec);
399     break;
400     }
401     }
402     if (!get_triangles(vother, ej)) /* triangle on same side? */
403     return(0);
404     return(vother[im_rev] != NULL);
405     }
406    
407     /* Find context hull vertex to complete triangle (oriented call) */
408     static RBFNODE *
409     find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1)
410     {
411     FVECT vmid, vejn, vp;
412     RBFNODE *rbf, *rbfbest = NULL;
413     double dprod, area2, bestarea2 = FHUGE, bestdprod = -.5;
414    
415     VSUB(vejn, rbf1->invec, rbf0->invec);
416     VADD(vmid, rbf0->invec, rbf1->invec);
417     if (normalize(vejn) == 0 || normalize(vmid) == 0)
418     return(NULL);
419     /* XXX exhaustive search */
420     /* Find triangle with minimum rotation from perpendicular */
421     for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
422     if ((rbf == rbf0) | (rbf == rbf1))
423     continue;
424     tri_orient(vp, rbf0->invec, rbf1->invec, rbf->invec);
425     if (DOT(vp, vmid) <= FTINY)
426     continue; /* wrong orientation */
427     area2 = .25*DOT(vp,vp);
428     VSUB(vp, rbf->invec, rbf0->invec);
429     dprod = -DOT(vp, vejn);
430     VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */
431     dprod = DOT(vp, vmid) / VLEN(vp);
432     if (dprod <= bestdprod + FTINY*(1 - 2*(area2 < bestarea2)))
433     continue; /* found better already */
434     if (overlaps_tri(rbf0, rbf1, rbf))
435     continue; /* overlaps another triangle */
436     rbfbest = rbf;
437     bestdprod = dprod; /* new one to beat */
438     bestarea2 = area2;
439     }
440     return(rbfbest);
441     }
442    
443     /* Create new migration edge and grow mesh recursively around it */
444     static void
445     mesh_from_edge(MIGRATION *edge)
446     {
447     MIGRATION *ej0, *ej1;
448     RBFNODE *tvert[2];
449    
450     if (edge == NULL)
451     return;
452     /* triangle on either side? */
453     get_triangles(tvert, edge);
454     if (tvert[0] == NULL) { /* grow mesh on right */
455     tvert[0] = find_chull_vert(edge->rbfv[0], edge->rbfv[1]);
456     if (tvert[0] != NULL) {
457     if (tvert[0]->ord > edge->rbfv[0]->ord)
458     ej0 = create_migration(edge->rbfv[0], tvert[0]);
459     else
460     ej0 = create_migration(tvert[0], edge->rbfv[0]);
461     if (tvert[0]->ord > edge->rbfv[1]->ord)
462     ej1 = create_migration(edge->rbfv[1], tvert[0]);
463     else
464     ej1 = create_migration(tvert[0], edge->rbfv[1]);
465     mesh_from_edge(ej0);
466     mesh_from_edge(ej1);
467     }
468     } else if (tvert[1] == NULL) { /* grow mesh on left */
469     tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]);
470     if (tvert[1] != NULL) {
471     if (tvert[1]->ord > edge->rbfv[0]->ord)
472     ej0 = create_migration(edge->rbfv[0], tvert[1]);
473     else
474     ej0 = create_migration(tvert[1], edge->rbfv[0]);
475     if (tvert[1]->ord > edge->rbfv[1]->ord)
476     ej1 = create_migration(edge->rbfv[1], tvert[1]);
477     else
478     ej1 = create_migration(tvert[1], edge->rbfv[1]);
479     mesh_from_edge(ej0);
480     mesh_from_edge(ej1);
481     }
482     }
483     }
484    
485     /* Build our triangle mesh from recorded RBFs */
486     void
487     build_mesh(void)
488     {
489     double best2 = M_PI*M_PI;
490     RBFNODE *shrt_edj[2];
491     RBFNODE *rbf0, *rbf1;
492     /* check if isotropic */
493     if (single_plane_incident) {
494     for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next)
495     if (rbf0->next != NULL)
496     create_migration(rbf0, rbf0->next);
497     await_children(nchild);
498     return;
499     }
500     shrt_edj[0] = shrt_edj[1] = NULL; /* start w/ shortest edge */
501     for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next)
502     for (rbf1 = rbf0->next; rbf1 != NULL; rbf1 = rbf1->next) {
503     double dist2 = 2. - 2.*DOT(rbf0->invec,rbf1->invec);
504     if (dist2 < best2) {
505     shrt_edj[0] = rbf0;
506     shrt_edj[1] = rbf1;
507     best2 = dist2;
508     }
509     }
510     if (shrt_edj[0] == NULL) {
511     fprintf(stderr, "%s: Cannot find shortest edge\n", progname);
512     exit(1);
513     }
514     /* build mesh from this edge */
515     if (shrt_edj[0]->ord < shrt_edj[1]->ord)
516     mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1]));
517     else
518     mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0]));
519     /* complete migrations */
520     await_children(nchild);
521     }