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.30 by gwlarson, Tue Feb 2 16:13:24 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 + int     chunkycmp = 0;          /* clump beams together on disk */
30 +
31   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 */
# Line 20 | Line 35 | static int     lastin= -1;     /* last ordered position in lis
35  
36  
37   int
38 < beamcmp(b0, b1)                 /* comparison for descending compute order */
38 > beamcmp(b0, b1)                         /* comparison for compute order */
39   register PACKHEAD       *b0, *b1;
40   {
41 <        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
41 >        BEAMI   *bip0, *bip1;
42 >        register long   c;
43 >                                        /* first check desired quantities */
44 >        if (chunkycmp)
45 >                c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
46 >                                rchunk(b0->nr)*(rchunk(b1->nc)+1L);
47 >        else
48 >                c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L);
49 >        if (c > 0) return(1);
50 >        if (c < 0) return(-1);
51 >                                /* 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   }
68  
69  
70   int
71 < dispbeam(b, hp, bi)                     /* display a holodeck beam */
71 > 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   register BEAM   *b;
84 < HOLO    *hp;
34 < int     bi;
84 > register HDBEAMI        *hb;
85   {
86          static int      n = 0;
87          static PACKHEAD *p = NULL;
# Line 48 | Line 98 | int    bi;
98                                          /* assign packet fields */
99          bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
100          p->nr = p->nc = b->nrm;
101 <        for (p->hd = 0; hdlist[p->hd] != hp; p->hd++)
101 >        for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
102                  if (hdlist[p->hd] == NULL)
103                          error(CONSISTENCY, "unregistered holodeck in dispbeam");
104 <        p->bi = bi;
104 >        p->bi = hb->b;
105          disp_packet(p);                 /* display it */
106 +        if (n >= 1024) {                /* free ridiculous packets */
107 +                free((char *)p);
108 +                p = NULL; n = 0;
109 +        }
110   }
111  
112  
113   bundle_set(op, clist, nents)    /* bundle set operation */
114   int     op;
115 < register PACKHEAD       *clist;
115 > PACKHEAD        *clist;
116   int     nents;
117   {
118 <        register int    i, n;
119 <
118 >        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 >        qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
126 >        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 >        }
152 >                                /* record computed rays for uncommon beams */
153 >        for (csm = clist+nents; csm-- > clist; )
154 >                if (csm->nc < 0)
155 >                        csm->nc = bnrays(hdlist[csm->hd], csm->bi);
156 >                                /* complete list operations */
157          switch (op) {
158          case BS_NEW:                    /* new computation set */
159 <                if (complen)
159 >                listpos = 0; lastin = -1;
160 >                if (complen)            /* free old list */
161                          free((char *)complist);
162 <                if (nents <= 0) {
163 <                        complist = NULL;
72 <                        listpos = complen = 0;
73 <                        lastin = -1;
162 >                complist = NULL;
163 >                if (!(complen = nents))
164                          return;
75                }
165                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
166                  if (complist == NULL)
167                          goto memerr;
168                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
80                complen = nents;        /* finish initialization below */
169                  break;
170          case BS_ADD:                    /* add to computation set */
171          case BS_ADJ:                    /* adjust set quantities */
172                  if (nents <= 0)
173                          return;
174 <                                        /* 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 */
174 >                sortcomplist();         /* sort updated list & new entries */
175                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
176                                          /* what can't we satisfy? */
177 <                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
177 >                for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
178                          ;
179 <                if (op == BS_ADJ)
180 <                        nents = n;
179 >                n = csm - clist;
180 >                if (op == BS_ADJ) {     /* don't regenerate adjusted beams */
181 >                        for (++i; i-- && csm->nr > 0; csm++)
182 >                                ;
183 >                        nents = csm - clist;
184 >                }
185                  if (n) {                /* allocate space for merged list */
186                          PACKHEAD        *newlist;
187                          newlist = (PACKHEAD *)malloc(
# Line 129 | Line 199 | int    nents;
199                  lastin = complen-1;     /* list is now sorted */
200                  break;
201          case BS_DEL:                    /* delete from computation set */
202 <                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 */
202 >                return;                 /* already done */
203          default:
204                  error(CONSISTENCY, "bundle_set called with unknown operation");
205          }
206 <        if (outdev != NULL) {           /* load and display beams we have */
207 <                register HDBEAMI        *hb;
208 <
209 <                hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
210 <                for (i = 0; i < nents; i++) {
211 <                        hb[i].h = hdlist[clist[i].hd];
212 <                        hb[i].b = clist[i].bi;
158 <                }
159 <                hdloadbeams(hb, nents, dispbeam);
160 <                free((char *)hb);
206 >        if (outdev == NULL || !nents)   /* nothing to display? */
207 >                return;
208 >                                        /* load and display beams we have */
209 >        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          }
214 <        if (op == BS_NEW) {
215 <                done_packets(flush_queue());    /* empty queue, so we can... */
216 <                for (i = 0; i < complen; i++)   /* ...get number computed */
165 <                        complist[i].nc = bnrays(hdlist[complist[i].hd],
166 <                                                complist[i].bi);
214 >        hdloadbeams(hbarr, nents, dispbeam);
215 >        free((char *)hbarr);
216 >        if (hdfragflags&FF_READ) {
217                  listpos = 0;
218 <                lastin = -1;            /* flag for initial sort */
218 >                lastin = -1;            /* need to re-sort list */
219          }
220          return;
221   memerr:
# Line 217 | Line 267 | init_global()                  /* initialize global ray computation *
267                  free((char *)complist);
268                  done_packets(flush_queue());
269          }
270 +                                        /* reseed random number generator */
271 +        srandom(time(NULL));
272                                          /* allocate beam list */
273          complen = 0;
274          for (j = 0; hdlist[j] != NULL; j++)
# Line 227 | Line 279 | init_global()                  /* initialize global ray computation *
279                                          /* compute beam weights */
280          k = 0;
281          for (j = 0; hdlist[j] != NULL; j++) {
282 <                frac = 512. * hdlist[j]->wg[0] *
283 <                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
284 <                if (frac < 0.) frac = -frac;
282 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
283 >                                VLEN(hdlist[j]->wg[1]) *
284 >                                VLEN(hdlist[j]->wg[2]);
285                  for (i = nbeams(hdlist[j]); i > 0; i--) {
286                          complist[k].hd = j;
287                          complist[k].bi = i;
# Line 244 | Line 296 | init_global()                  /* initialize global ray computation *
296          else
297                  frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
298          while (k--)
299 <                complist[k].nr = frac * complist[k].nr;
300 <        listpos = 0; lastin = -1;       /* flag initial sort */
299 >                complist[k].nr = frac*complist[k].nr + 0.5;
300 >        listpos = 0; lastin = -1;       /* perform initial sort */
301 >        sortcomplist();
302 >                                        /* no view vicinity */
303 >        myeye.rng = 0;
304   }
305  
306  
# Line 275 | Line 330 | int    n1, n2;
330   sortcomplist()                  /* fix our list order */
331   {
332          PACKHEAD        *list2;
333 +        int     listlen;
334          register int    i;
335  
336          if (complen <= 0)       /* check to see if there is even a list */
337                  return;
338 +        if (!chunkycmp)         /* check to see if fragment list is full */
339 +                if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
340 + #if NFRAG2CHUNK
341 +                                || listlen >= NFRAG2CHUNK
342 + #endif
343 +                                ) {
344 +                        chunkycmp++;    /* use "chunky" comparison */
345 +                        lastin = -1;    /* need to re-sort list */
346 + #ifdef DEBUG
347 +                        error(WARNING, "using chunky comparison mode");
348 + #endif
349 +                }
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