ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo3.c
Revision: 3.30
Committed: Tue Feb 2 16:13:24 1999 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.29: +6 -10 lines
Log Message:
made chunkycmp global and added test for it in rholo()

File Contents

# User Rev Content
1 gwlarson 3.23 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7 gregl 3.1 /*
8     * Routines for tracking beam compuatations
9     */
10    
11     #include "rholo.h"
12 gwlarson 3.28 #include <sys/types.h>
13 gregl 3.1
14 gwlarson 3.27 #ifndef NFRAG2CHUNK
15     #define NFRAG2CHUNK 4096 /* number of fragments to start chunking */
16     #endif
17    
18     #ifndef abs
19 gregl 3.1 #define abs(x) ((x) > 0 ? (x) : -(x))
20 gwlarson 3.27 #endif
21     #ifndef sgn
22 gregl 3.1 #define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
23 gwlarson 3.27 #endif
24 gregl 3.1
25 gwlarson 3.27 #define rchunk(n) (((n)+(RPACKSIZ/2))/RPACKSIZ)
26    
27 gwlarson 3.28 extern time_t time();
28    
29 gwlarson 3.30 int chunkycmp = 0; /* clump beams together on disk */
30    
31 gregl 3.4 static PACKHEAD *complist=NULL; /* list of beams to compute */
32     static int complen=0; /* length of complist */
33     static int listpos=0; /* current list position for next_packet */
34     static int lastin= -1; /* last ordered position in list */
35 gregl 3.1
36    
37     int
38 gwlarson 3.23 beamcmp(b0, b1) /* comparison for compute order */
39 gregl 3.2 register PACKHEAD *b0, *b1;
40     {
41 gwlarson 3.27 BEAMI *bip0, *bip1;
42     register long c;
43     /* first check desired quantities */
44 gwlarson 3.30 if (chunkycmp)
45 gwlarson 3.28 c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
46     rchunk(b0->nr)*(rchunk(b1->nc)+1L);
47 gwlarson 3.27 else
48 gwlarson 3.28 c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L);
49     if (c > 0) return(1);
50     if (c < 0) return(-1);
51 gwlarson 3.27 /* only one file, so skip the following: */
52     #if 0
53     /* next, check file descriptors */
54     c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd;
55     if (c) return(c);
56     #endif
57     /* finally, check file positions */
58     bip0 = &hdlist[b0->hd]->bi[b0->bi];
59     bip1 = &hdlist[b1->hd]->bi[b1->bi];
60     /* put diskless beams last */
61     if (!bip0->nrd)
62     return(bip1->nrd > 0);
63     if (!bip1->nrd)
64     return(-1);
65     c = bip0->fo - bip1->fo;
66     return(c < 0 ? -1 : c > 0);
67 gregl 3.2 }
68    
69    
70 gregl 3.14 int
71 gwlarson 3.23 beamidcmp(b0, b1) /* comparison for beam searching */
72     register PACKHEAD *b0, *b1;
73     {
74     register int c = b0->hd - b1->hd;
75    
76     if (c) return(c);
77     return(b0->bi - b1->bi);
78     }
79    
80    
81     int
82     dispbeam(b, hb) /* display a holodeck beam */
83 gregl 3.14 register BEAM *b;
84 gwlarson 3.23 register HDBEAMI *hb;
85 gregl 3.14 {
86     static int n = 0;
87     static PACKHEAD *p = NULL;
88    
89     if (b == NULL)
90     return;
91     if (b->nrm > n) { /* (re)allocate packet holder */
92     n = b->nrm;
93     if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
94     else p = (PACKHEAD *)realloc((char *)p, packsiz(n));
95     if (p == NULL)
96     error(SYSTEM, "out of memory in dispbeam");
97     }
98     /* assign packet fields */
99     bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
100     p->nr = p->nc = b->nrm;
101 gwlarson 3.23 for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
102 gregl 3.14 if (hdlist[p->hd] == NULL)
103     error(CONSISTENCY, "unregistered holodeck in dispbeam");
104 gwlarson 3.23 p->bi = hb->b;
105 gregl 3.14 disp_packet(p); /* display it */
106 gwlarson 3.29 if (n >= 1024) { /* free ridiculous packets */
107     free((char *)p);
108     p = NULL; n = 0;
109     }
110 gregl 3.14 }
111    
112    
113 gregl 3.2 bundle_set(op, clist, nents) /* bundle set operation */
114     int op;
115 gwlarson 3.23 PACKHEAD *clist;
116 gregl 3.2 int nents;
117     {
118 gwlarson 3.23 int oldnr, n;
119     HDBEAMI *hbarr;
120     register PACKHEAD *csm;
121     register int i;
122     /* search for common members */
123     for (csm = clist+nents; csm-- > clist; )
124     csm->nc = -1;
125 gwlarson 3.25 qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
126 gwlarson 3.23 for (i = 0; i < complen; i++) {
127     csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist,
128     nents, sizeof(PACKHEAD), beamidcmp);
129     if (csm == NULL)
130     continue;
131     oldnr = complist[i].nr;
132     csm->nc = complist[i].nc;
133     switch (op) {
134     case BS_ADD: /* add to count */
135     complist[i].nr += csm->nr;
136     csm->nr = 0;
137     break;
138     case BS_ADJ: /* reset count */
139     complist[i].nr = csm->nr;
140     csm->nr = 0;
141     break;
142     case BS_DEL: /* delete count */
143     if (csm->nr == 0 || csm->nr >= complist[i].nr)
144     complist[i].nr = 0;
145     else
146     complist[i].nr -= csm->nr;
147     break;
148     }
149     if (complist[i].nr != oldnr)
150     lastin = -1; /* flag sort */
151 gregl 3.20 }
152 gwlarson 3.24 /* record computed rays for uncommon beams */
153 gwlarson 3.23 for (csm = clist+nents; csm-- > clist; )
154     if (csm->nc < 0)
155     csm->nc = bnrays(hdlist[csm->hd], csm->bi);
156 gregl 3.20 /* complete list operations */
157 gregl 3.2 switch (op) {
158     case BS_NEW: /* new computation set */
159 gwlarson 3.21 listpos = 0; lastin = -1;
160 gregl 3.20 if (complen) /* free old list */
161 gregl 3.2 free((char *)complist);
162 gregl 3.20 complist = NULL;
163     if (!(complen = nents))
164 gregl 3.2 return;
165     complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
166     if (complist == NULL)
167     goto memerr;
168     bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
169     break;
170     case BS_ADD: /* add to computation set */
171 gregl 3.11 case BS_ADJ: /* adjust set quantities */
172 gregl 3.2 if (nents <= 0)
173     return;
174 gregl 3.20 sortcomplist(); /* sort updated list & new entries */
175 gregl 3.2 qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
176     /* what can't we satisfy? */
177 gwlarson 3.23 for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
178 gregl 3.2 ;
179 gwlarson 3.23 n = csm - clist;
180 gwlarson 3.22 if (op == BS_ADJ) { /* don't regenerate adjusted beams */
181 gwlarson 3.24 for (++i; i-- && csm->nr > 0; csm++)
182 gwlarson 3.22 ;
183 gwlarson 3.24 nents = csm - clist;
184 gwlarson 3.22 }
185 gregl 3.2 if (n) { /* allocate space for merged list */
186     PACKHEAD *newlist;
187     newlist = (PACKHEAD *)malloc(
188     (complen+n)*sizeof(PACKHEAD) );
189     if (newlist == NULL)
190     goto memerr;
191     /* merge lists */
192     mergeclists(newlist, clist, n, complist, complen);
193     if (complen)
194     free((char *)complist);
195     complist = newlist;
196     complen += n;
197     }
198     listpos = 0;
199     lastin = complen-1; /* list is now sorted */
200     break;
201     case BS_DEL: /* delete from computation set */
202 gregl 3.20 return; /* already done */
203 gregl 3.2 default:
204     error(CONSISTENCY, "bundle_set called with unknown operation");
205     }
206 gwlarson 3.27 if (outdev == NULL || !nents) /* nothing to display? */
207 gwlarson 3.22 return;
208     /* load and display beams we have */
209 gwlarson 3.23 hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
210     for (i = nents; i--; ) {
211     hbarr[i].h = hdlist[clist[i].hd];
212     hbarr[i].b = clist[i].bi;
213 gregl 3.11 }
214 gwlarson 3.23 hdloadbeams(hbarr, nents, dispbeam);
215     free((char *)hbarr);
216 gwlarson 3.27 if (hdfragflags&FF_READ) {
217     listpos = 0;
218     lastin = -1; /* need to re-sort list */
219     }
220 gregl 3.2 return;
221     memerr:
222     error(SYSTEM, "out of memory in bundle_set");
223     }
224    
225    
226 gregl 3.13 double
227     beamvolume(hp, bi) /* compute approximate volume of a beam */
228 gregl 3.1 HOLO *hp;
229 gregl 3.13 int bi;
230 gregl 3.1 {
231 gregl 3.13 GCOORD gc[2];
232     FVECT cp[4], edgeA, edgeB, cent[2];
233     FVECT v, crossp[2], diffv;
234     double vol[2];
235     register int i;
236     /* get grid coordinates */
237     if (!hdbcoord(gc, hp, bi))
238     error(CONSISTENCY, "bad beam index in beamvolume");
239     for (i = 0; i < 2; i++) { /* compute cell area vectors */
240     hdcell(cp, hp, gc+i);
241     VSUM(edgeA, cp[1], cp[0], -1.0);
242     VSUM(edgeB, cp[3], cp[1], -1.0);
243     fcross(crossp[i], edgeA, edgeB);
244     /* compute center */
245     cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
246     cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
247     cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
248 gregl 3.1 }
249 gregl 3.13 /* compute difference vector */
250     VSUM(diffv, cent[1], cent[0], -1.0);
251     for (i = 0; i < 2; i++) { /* compute volume contributions */
252 gregl 3.15 vol[i] = 0.5*DOT(crossp[i], diffv);
253 gregl 3.13 if (vol[i] < 0.) vol[i] = -vol[i];
254     }
255     return(vol[0] + vol[1]); /* return total volume */
256 gregl 3.1 }
257    
258    
259     init_global() /* initialize global ray computation */
260     {
261     long wtotal = 0;
262     double frac;
263 gregl 3.13 int i;
264     register int j, k;
265 gregl 3.18 /* free old list and empty queue */
266     if (complen > 0) {
267 gregl 3.3 free((char *)complist);
268 gregl 3.18 done_packets(flush_queue());
269     }
270 gwlarson 3.28 /* reseed random number generator */
271     srandom(time(NULL));
272 gregl 3.1 /* allocate beam list */
273     complen = 0;
274     for (j = 0; hdlist[j] != NULL; j++)
275     complen += nbeams(hdlist[j]);
276     complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
277     if (complist == NULL)
278     error(SYSTEM, "out of memory in init_global");
279     /* compute beam weights */
280     k = 0;
281 gregl 3.13 for (j = 0; hdlist[j] != NULL; j++) {
282 gregl 3.19 frac = 512. * VLEN(hdlist[j]->wg[0]) *
283     VLEN(hdlist[j]->wg[1]) *
284     VLEN(hdlist[j]->wg[2]);
285 gregl 3.1 for (i = nbeams(hdlist[j]); i > 0; i--) {
286     complist[k].hd = j;
287     complist[k].bi = i;
288 gregl 3.13 complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
289 gregl 3.18 complist[k].nc = bnrays(hdlist[j], i);
290 gregl 3.1 wtotal += complist[k++].nr;
291     }
292 gregl 3.13 }
293 gregl 3.1 /* adjust weights */
294 gregl 3.12 if (vdef(DISKSPACE))
295 gregl 3.1 frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
296 gregl 3.12 else
297     frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
298     while (k--)
299 gwlarson 3.24 complist[k].nr = frac*complist[k].nr + 0.5;
300 gwlarson 3.21 listpos = 0; lastin = -1; /* perform initial sort */
301     sortcomplist();
302 gwlarson 3.25 /* no view vicinity */
303     myeye.rng = 0;
304 gregl 3.1 }
305    
306    
307     mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */
308 gregl 3.16 register PACKHEAD *cdest;
309     register PACKHEAD *cl1, *cl2;
310 gregl 3.1 int n1, n2;
311     {
312 gregl 3.16 register int cmp;
313 gregl 3.1
314     while (n1 | n2) {
315     if (!n1) cmp = 1;
316     else if (!n2) cmp = -1;
317     else cmp = beamcmp(cl1, cl2);
318     if (cmp > 0) {
319     copystruct(cdest, cl2);
320     cl2++; n2--;
321     } else {
322     copystruct(cdest, cl1);
323     cl1++; n1--;
324     }
325     cdest++;
326     }
327     }
328    
329    
330     sortcomplist() /* fix our list order */
331     {
332     PACKHEAD *list2;
333 gwlarson 3.27 int listlen;
334 gregl 3.2 register int i;
335    
336 gregl 3.3 if (complen <= 0) /* check to see if there is even a list */
337 gregl 3.2 return;
338 gwlarson 3.30 if (!chunkycmp) /* check to see if fragment list is full */
339 gwlarson 3.27 if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
340     #if NFRAG2CHUNK
341     || listlen >= NFRAG2CHUNK
342     #endif
343     ) {
344 gwlarson 3.30 chunkycmp++; /* use "chunky" comparison */
345     lastin = -1; /* need to re-sort list */
346 gwlarson 3.27 #ifdef DEBUG
347     error(WARNING, "using chunky comparison mode");
348     #endif
349     }
350 gregl 3.6 if (lastin < 0 || listpos*4 >= complen*3)
351 gregl 3.1 qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
352     else if (listpos) { /* else sort and merge sublist */
353     list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
354     if (list2 == NULL)
355     error(SYSTEM, "out of memory in sortcomplist");
356     bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
357     qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
358     mergeclists(complist, list2, listpos,
359     complist+listpos, complen-listpos);
360     free((char *)list2);
361     }
362 gregl 3.2 /* drop satisfied requests */
363 gregl 3.11 for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
364 gregl 3.2 ;
365 gregl 3.4 if (i < 0) {
366     free((char *)complist);
367     complist = NULL;
368     complen = 0;
369     } else if (i < complen-1) {
370 gregl 3.2 list2 = (PACKHEAD *)realloc((char *)complist,
371     (i+1)*sizeof(PACKHEAD));
372 gwlarson 3.26 if (list2 != NULL)
373 gregl 3.2 complist = list2;
374 gwlarson 3.26 complen = i+1;
375 gregl 3.2 }
376     listpos = 0; lastin = i;
377 gregl 3.1 }
378    
379    
380     /*
381     * The following routine works on the assumption that the bundle weights are
382     * more or less evenly distributed, such that computing a packet causes
383     * a given bundle to move way down in the computation order. We keep
384     * track of where the computed bundle with the highest priority would end
385 gwlarson 3.27 * up, and if we get further in our compute list than this, we re-sort the
386 gregl 3.11 * list and start again from the beginning. Since
387     * a merge sort is used, the sorting costs are minimal.
388 gregl 3.1 */
389 gregl 3.17 next_packet(p, n) /* prepare packet for computation */
390 gregl 3.1 register PACKET *p;
391 gregl 3.17 int n;
392 gregl 3.1 {
393     register int i;
394    
395 gregl 3.10 if (listpos > lastin) /* time to sort the list */
396     sortcomplist();
397 gregl 3.1 if (complen <= 0)
398     return(0);
399     p->hd = complist[listpos].hd;
400     p->bi = complist[listpos].bi;
401 gregl 3.11 p->nc = complist[listpos].nc;
402     p->nr = complist[listpos].nr - p->nc;
403 gregl 3.1 if (p->nr <= 0)
404     return(0);
405 gwlarson 3.28 DCHECK(n < 1 | n > RPACKSIZ,
406     CONSISTENCY, "next_packet called with bad n value");
407 gregl 3.17 if (p->nr > n)
408     p->nr = n;
409 gregl 3.11 complist[listpos].nc += p->nr; /* find where this one would go */
410 gwlarson 3.27 if (hdgetbeam(hdlist[p->hd], p->bi) != NULL)
411     hdfreefrag(hdlist[p->hd], p->bi);
412 gregl 3.11 while (lastin > listpos &&
413     beamcmp(complist+lastin, complist+listpos) > 0)
414 gregl 3.1 lastin--;
415     listpos++;
416     return(1);
417     }