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

Comparing ray/src/hd/rholo3.c (file contents):
Revision 3.18 by gregl, Mon Jan 5 16:46:34 1998 UTC vs.
Revision 3.28 by gwlarson, Sat Jan 9 09:17:10 1999 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 + #include <sys/types.h>
13  
14 + #ifndef NFRAG2CHUNK
15 + #define NFRAG2CHUNK     4096    /* number of fragments to start chunking */
16 + #endif
17 +
18 + #ifndef abs
19   #define abs(x)          ((x) > 0 ? (x) : -(x))
20 + #endif
21 + #ifndef sgn
22   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
23 + #endif
24  
25 + #define rchunk(n)       (((n)+(RPACKSIZ/2))/RPACKSIZ)
26 +
27 + extern time_t   time();
28 +
29   static PACKHEAD *complist=NULL; /* list of beams to compute */
30   static int      complen=0;      /* length of complist */
31   static int      listpos=0;      /* current list position for next_packet */
32   static int      lastin= -1;     /* last ordered position in list */
33 + static int      chunky=0;       /* clump beams together on disk */
34  
35  
36   int
37 < beamcmp(b0, b1)                 /* comparison for descending compute order */
37 > beamcmp(b0, b1)                         /* comparison for compute order */
38   register PACKHEAD       *b0, *b1;
39   {
40 <        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
40 >        BEAMI   *bip0, *bip1;
41 >        register long   c;
42 >                                        /* first check desired quantities */
43 >        if (chunky)
44 >                c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
45 >                                rchunk(b0->nr)*(rchunk(b1->nc)+1L);
46 >        else
47 >                c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L);
48 >        if (c > 0) return(1);
49 >        if (c < 0) return(-1);
50 >                                /* only one file, so skip the following: */
51 > #if 0
52 >                                        /* next, check file descriptors */
53 >        c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd;
54 >        if (c) return(c);
55 > #endif
56 >                                        /* finally, check file positions */
57 >        bip0 = &hdlist[b0->hd]->bi[b0->bi];
58 >        bip1 = &hdlist[b1->hd]->bi[b1->bi];
59 >                                        /* put diskless beams last */
60 >        if (!bip0->nrd)
61 >                return(bip1->nrd > 0);
62 >        if (!bip1->nrd)
63 >                return(-1);
64 >        c = bip0->fo - bip1->fo;
65 >        return(c < 0 ? -1 : c > 0);
66   }
67  
68  
69   int
70 < dispbeam(b, hp, bi)                     /* display a holodeck beam */
70 > beamidcmp(b0, b1)                       /* comparison for beam searching */
71 > register PACKHEAD       *b0, *b1;
72 > {
73 >        register int    c = b0->hd - b1->hd;
74 >
75 >        if (c) return(c);
76 >        return(b0->bi - b1->bi);
77 > }
78 >
79 >
80 > int
81 > dispbeam(b, hb)                         /* display a holodeck beam */
82   register BEAM   *b;
83 < HOLO    *hp;
34 < int     bi;
83 > register HDBEAMI        *hb;
84   {
85          static int      n = 0;
86          static PACKHEAD *p = NULL;
# Line 48 | Line 97 | int    bi;
97                                          /* assign packet fields */
98          bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
99          p->nr = p->nc = b->nrm;
100 <        for (p->hd = 0; hdlist[p->hd] != hp; p->hd++)
100 >        for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
101                  if (hdlist[p->hd] == NULL)
102                          error(CONSISTENCY, "unregistered holodeck in dispbeam");
103 <        p->bi = bi;
103 >        p->bi = hb->b;
104          disp_packet(p);                 /* display it */
105   }
106  
107  
108   bundle_set(op, clist, nents)    /* bundle set operation */
109   int     op;
110 < register PACKHEAD       *clist;
110 > PACKHEAD        *clist;
111   int     nents;
112   {
113 <        register int    i, n;
114 <
113 >        int     oldnr, n;
114 >        HDBEAMI *hbarr;
115 >        register PACKHEAD       *csm;
116 >        register int    i;
117 >                                        /* search for common members */
118 >        for (csm = clist+nents; csm-- > clist; )
119 >                csm->nc = -1;
120 >        qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
121 >        for (i = 0; i < complen; i++) {
122 >                csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist,
123 >                                nents, sizeof(PACKHEAD), beamidcmp);
124 >                if (csm == NULL)
125 >                        continue;
126 >                oldnr = complist[i].nr;
127 >                csm->nc = complist[i].nc;
128 >                switch (op) {
129 >                case BS_ADD:            /* add to count */
130 >                        complist[i].nr += csm->nr;
131 >                        csm->nr = 0;
132 >                        break;
133 >                case BS_ADJ:            /* reset count */
134 >                        complist[i].nr = csm->nr;
135 >                        csm->nr = 0;
136 >                        break;
137 >                case BS_DEL:            /* delete count */
138 >                        if (csm->nr == 0 || csm->nr >= complist[i].nr)
139 >                                complist[i].nr = 0;
140 >                        else
141 >                                complist[i].nr -= csm->nr;
142 >                        break;
143 >                }
144 >                if (complist[i].nr != oldnr)
145 >                        lastin = -1;    /* flag sort */
146 >        }
147 >                                /* record computed rays for uncommon beams */
148 >        for (csm = clist+nents; csm-- > clist; )
149 >                if (csm->nc < 0)
150 >                        csm->nc = bnrays(hdlist[csm->hd], csm->bi);
151 >                                /* complete list operations */
152          switch (op) {
153          case BS_NEW:                    /* new computation set */
154 <                if (complen)
154 >                listpos = 0; lastin = -1;
155 >                if (complen)            /* free old list */
156                          free((char *)complist);
157 <                if (nents <= 0) {
158 <                        complist = NULL;
72 <                        listpos = complen = 0;
73 <                        lastin = -1;
157 >                complist = NULL;
158 >                if (!(complen = nents))
159                          return;
75                }
160                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
161                  if (complist == NULL)
162                          goto memerr;
163                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
80                complen = nents;        /* finish initialization below */
164                  break;
165          case BS_ADD:                    /* add to computation set */
166          case BS_ADJ:                    /* adjust set quantities */
167                  if (nents <= 0)
168                          return;
169 <                                        /* merge any common members */
87 <                for (n = 0; n < nents; n++) {
88 <                        for (i = 0; i < complen; i++)
89 <                                if (clist[n].bi == complist[i].bi &&
90 <                                                clist[n].hd == complist[i].hd) {
91 <                                        int     oldnr = complist[i].nr;
92 <                                        if (op == BS_ADD)
93 <                                                complist[i].nr += clist[n].nr;
94 <                                        else /* op == BS_ADJ */
95 <                                                complist[i].nr = clist[n].nr;
96 <                                        clist[n].nr = 0;
97 <                                        clist[n].nc = complist[i].nc;
98 <                                        if (complist[i].nr != oldnr)
99 <                                                lastin = -1;    /* flag sort */
100 <                                        break;
101 <                                }
102 <                        if (i >= complen)
103 <                                clist[n].nc = bnrays(hdlist[clist[n].hd],
104 <                                                clist[n].bi);
105 <                }
106 <                                        /* sort updated list */
107 <                sortcomplist();
108 <                                        /* sort new entries */
169 >                sortcomplist();         /* sort updated list & new entries */
170                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
171                                          /* what can't we satisfy? */
172 <                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
172 >                for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
173                          ;
174 <                if (op == BS_ADJ)
175 <                        nents = n;
174 >                n = csm - clist;
175 >                if (op == BS_ADJ) {     /* don't regenerate adjusted beams */
176 >                        for (++i; i-- && csm->nr > 0; csm++)
177 >                                ;
178 >                        nents = csm - clist;
179 >                }
180                  if (n) {                /* allocate space for merged list */
181                          PACKHEAD        *newlist;
182                          newlist = (PACKHEAD *)malloc(
# Line 129 | Line 194 | int    nents;
194                  lastin = complen-1;     /* list is now sorted */
195                  break;
196          case BS_DEL:                    /* delete from computation set */
197 <                if (nents <= 0)
133 <                        return;
134 <                                        /* find each member */
135 <                for (i = 0; i < complen; i++)
136 <                        for (n = 0; n < nents; n++)
137 <                                if (clist[n].bi == complist[i].bi &&
138 <                                                clist[n].hd == complist[i].hd) {
139 <                                        if (clist[n].nr == 0 ||
140 <                                                clist[n].nr >= complist[i].nr)
141 <                                                complist[i].nr = 0;
142 <                                        else
143 <                                                complist[i].nr -= clist[n].nr;
144 <                                        lastin = -1;    /* flag full sort */
145 <                                        break;
146 <                                }
147 <                return;                 /* no display */
197 >                return;                 /* already done */
198          default:
199                  error(CONSISTENCY, "bundle_set called with unknown operation");
200          }
201 <        if (outdev != NULL) {           /* load and display beams we have */
202 <                register HDBEAMI        *hb;
203 <
204 <                hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
205 <                for (i = 0; i < nents; i++) {
206 <                        hb[i].h = hdlist[clist[i].hd];
207 <                        hb[i].b = clist[i].bi;
158 <                }
159 <                hdloadbeams(hb, nents, dispbeam);
160 <                free((char *)hb);
201 >        if (outdev == NULL || !nents)   /* nothing to display? */
202 >                return;
203 >                                        /* load and display beams we have */
204 >        hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
205 >        for (i = nents; i--; ) {
206 >                hbarr[i].h = hdlist[clist[i].hd];
207 >                hbarr[i].b = clist[i].bi;
208          }
209 <        if (op == BS_NEW) {
210 <                done_packets(flush_queue());    /* empty queue, so we can... */
211 <                for (i = 0; i < complen; i++)   /* ...get number computed */
165 <                        complist[i].nc = bnrays(hdlist[complist[i].hd],
166 <                                                complist[i].bi);
209 >        hdloadbeams(hbarr, nents, dispbeam);
210 >        free((char *)hbarr);
211 >        if (hdfragflags&FF_READ) {
212                  listpos = 0;
213 <                lastin = -1;            /* flag for initial sort */
213 >                lastin = -1;            /* need to re-sort list */
214          }
215          return;
216   memerr:
# Line 217 | Line 262 | init_global()                  /* initialize global ray computation *
262                  free((char *)complist);
263                  done_packets(flush_queue());
264          }
265 +                                        /* reseed random number generator */
266 +        srandom(time(NULL));
267                                          /* allocate beam list */
268          complen = 0;
269          for (j = 0; hdlist[j] != NULL; j++)
# Line 227 | Line 274 | init_global()                  /* initialize global ray computation *
274                                          /* compute beam weights */
275          k = 0;
276          for (j = 0; hdlist[j] != NULL; j++) {
277 <                frac = 512. * hdlist[j]->wg[0] *
278 <                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
279 <                if (frac < 0.) frac = -frac;
277 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
278 >                                VLEN(hdlist[j]->wg[1]) *
279 >                                VLEN(hdlist[j]->wg[2]);
280                  for (i = nbeams(hdlist[j]); i > 0; i--) {
281                          complist[k].hd = j;
282                          complist[k].bi = i;
# Line 244 | Line 291 | init_global()                  /* initialize global ray computation *
291          else
292                  frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
293          while (k--)
294 <                complist[k].nr = frac * complist[k].nr;
295 <        listpos = 0; lastin = -1;       /* flag initial sort */
294 >                complist[k].nr = frac*complist[k].nr + 0.5;
295 >        listpos = 0; lastin = -1;       /* perform initial sort */
296 >        sortcomplist();
297 >                                        /* no view vicinity */
298 >        myeye.rng = 0;
299   }
300  
301  
# Line 275 | Line 325 | int    n1, n2;
325   sortcomplist()                  /* fix our list order */
326   {
327          PACKHEAD        *list2;
328 +        int     listlen;
329          register int    i;
330  
331          if (complen <= 0)       /* check to see if there is even a list */
332                  return;
333 +        if (!chunky)            /* check to see if fragment list is full */
334 +                if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
335 + #if NFRAG2CHUNK
336 +                                || listlen >= NFRAG2CHUNK
337 + #endif
338 +                                ) {
339 + #ifdef DEBUG
340 +                        error(WARNING, "using chunky comparison mode");
341 + #endif
342 +                        chunky++;       /* use "chunky" comparison */
343 +                        lastin = -1;    /* need to re-sort list */
344 +                }
345 + #ifdef DEBUG
346 +                        else
347 +                                fprintf(stderr, "sortcomplist: %d fragments\n",
348 +                                                listlen);
349 + #endif
350          if (lastin < 0 || listpos*4 >= complen*3)
351                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
352          else if (listpos) {     /* else sort and merge sublist */
# Line 301 | Line 369 | sortcomplist()                 /* fix our list order */
369          } else if (i < complen-1) {
370                  list2 = (PACKHEAD *)realloc((char *)complist,
371                                  (i+1)*sizeof(PACKHEAD));
372 <                if (list2 != NULL) {
372 >                if (list2 != NULL)
373                          complist = list2;
374 <                        complen = i+1;
307 <                }
374 >                complen = i+1;
375          }
376          listpos = 0; lastin = i;
377   }
# Line 315 | Line 382 | sortcomplist()                 /* fix our list order */
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 < * up, and if we get further in our compute list than this, we resort the
385 > * up, and if we get further in our compute list than this, we re-sort the
386   * list and start again from the beginning.  Since
387   * a merge sort is used, the sorting costs are minimal.
388   */
# Line 335 | Line 402 | int    n;
402          p->nr = complist[listpos].nr - p->nc;
403          if (p->nr <= 0)
404                  return(0);
405 < #ifdef DEBUG
406 <        if (n < 1 | n > RPACKSIZ)
340 <                error(CONSISTENCY, "next_packet called with bad n value");
341 < #endif
405 >        DCHECK(n < 1 | n > RPACKSIZ,
406 >                        CONSISTENCY, "next_packet called with bad n value");
407          if (p->nr > n)
408                  p->nr = n;
409          complist[listpos].nc += p->nr;  /* find where this one would go */
410 +        if (hdgetbeam(hdlist[p->hd], p->bi) != NULL)
411 +                hdfreefrag(hdlist[p->hd], p->bi);
412          while (lastin > listpos &&
413                          beamcmp(complist+lastin, complist+listpos) > 0)
414                  lastin--;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines