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.22 by gwlarson, Wed Jul 8 17:59:58 1998 UTC vs.
Revision 3.33 by gwlarson, Mon Mar 8 17:31:49 1999 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1999 Silicon Graphics, Inc. */
2 +
3 + #ifndef lint
4 + static char SCCSid[] = "$SunId$ SGI";
5 + #endif
6 +
7   /*
8   * Routines for tracking beam compuatations
9   */
10  
11   #include "rholo.h"
12 + #include "view.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 14 | 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;
28 < int     bi;
84 > register HDBEAMI        *hb;
85   {
86          static int      n = 0;
87          static PACKHEAD *p = NULL;
# Line 36 | Line 92 | int    bi;
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)
40 <                        error(SYSTEM, "out of memory in dispbeam");
95 >                CHECK(p==NULL, SYSTEM, "out of memory in dispbeam");
96          }
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 +        if (n >= 1024) {                /* free ridiculous packets */
106 +                free((char *)p);
107 +                p = NULL; n = 0;
108 +        }
109   }
110  
111  
112   bundle_set(op, clist, nents)    /* bundle set operation */
113   int     op;
114 < register PACKHEAD       *clist;
114 > PACKHEAD        *clist;
115   int     nents;
116   {
117 <        int     oldnr;
118 <        register HDBEAMI        *hb;
119 <        register int    i, n;
120 <                                /* look for common members */
121 <        for (n = 0; n < nents; n++) {
122 <                for (i = 0; i < complen; i++)
123 <                        if (clist[n].bi == complist[i].bi &&
124 <                                        clist[n].hd == complist[i].hd) {
125 <                                oldnr = complist[i].nr;
126 <                                clist[n].nc = complist[i].nc;
127 <                                switch (op) {
128 <                                case BS_ADD:            /* add to count */
129 <                                        complist[i].nr += clist[n].nr;
130 <                                        clist[n].nr = 0;
131 <                                        break;
132 <                                case BS_ADJ:            /* reset count */
133 <                                        complist[i].nr = clist[n].nr;
134 <                                        clist[n].nr = 0;
135 <                                        break;
136 <                                case BS_DEL:            /* delete count */
137 <                                        if (clist[n].nr == 0 ||
138 <                                                clist[n].nr >= complist[i].nr)
139 <                                                complist[i].nr = 0;
140 <                                        else
141 <                                                complist[i].nr -= clist[n].nr;
142 <                                        break;
143 <                                }
144 <                                if (complist[i].nr != oldnr)
145 <                                        lastin = -1;    /* flag sort */
146 <                                break;
147 <                        }
148 <                if (i >= complen)
149 <                        clist[n].nc = bnrays(hdlist[clist[n].hd], clist[n].bi);
117 >        int     oldnr, n;
118 >        HDBEAMI *hbarr;
119 >        register PACKHEAD       *csm;
120 >        register int    i;
121 >                                        /* search for common members */
122 >        for (csm = clist+nents; csm-- > clist; )
123 >                csm->nc = -1;
124 >        qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
125 >        for (i = 0; i < complen; i++) {
126 >                csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist,
127 >                                nents, sizeof(PACKHEAD), beamidcmp);
128 >                if (csm == NULL)
129 >                        continue;
130 >                oldnr = complist[i].nr;
131 >                csm->nc = complist[i].nc;
132 >                switch (op) {
133 >                case BS_ADD:            /* add to count */
134 >                        complist[i].nr += csm->nr;
135 >                        csm->nr = 0;
136 >                        break;
137 >                case BS_MAX:            /* maximum of counts */
138 >                        if (csm->nr > complist[i].nr)
139 >                                complist[i].nr = csm->nr;
140 >                        csm->nr = 0;
141 >                        break;
142 >                case BS_ADJ:            /* reset count */
143 >                        complist[i].nr = csm->nr;
144 >                        csm->nr = 0;
145 >                        break;
146 >                case BS_DEL:            /* delete count */
147 >                        if (csm->nr == 0 || csm->nr >= complist[i].nr)
148 >                                complist[i].nr = 0;
149 >                        else
150 >                                complist[i].nr -= csm->nr;
151 >                        break;
152 >                }
153 >                if (complist[i].nr != oldnr)
154 >                        lastin = -1;    /* flag sort */
155          }
156 +                                /* record computed rays for uncommon beams */
157 +        for (csm = clist+nents; csm-- > clist; )
158 +                if (csm->nc < 0)
159 +                        csm->nc = bnrays(hdlist[csm->hd], csm->bi);
160                                  /* complete list operations */
161          switch (op) {
162          case BS_NEW:                    /* new computation set */
# Line 104 | Line 172 | int    nents;
172                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
173                  break;
174          case BS_ADD:                    /* add to computation set */
175 +        case BS_MAX:                    /* maximum of quantities */
176          case BS_ADJ:                    /* adjust set quantities */
177                  if (nents <= 0)
178                          return;
179                  sortcomplist();         /* sort updated list & new entries */
180                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
181                                          /* what can't we satisfy? */
182 <                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
182 >                for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
183                          ;
184 <                if (op == BS_ADJ) {     /* don't regenerate adjusted beams */
185 <                        for (i = n; i < nents && clist[i].nr > 0; i++)
184 >                n = csm - clist;
185 >                if (op != BS_ADD) {     /* don't regenerate adjusted beams */
186 >                        for (++i; i-- && csm->nr > 0; csm++)
187                                  ;
188 <                        nents = i;
188 >                        nents = csm - clist;
189                  }
190                  if (n) {                /* allocate space for merged list */
191                          PACKHEAD        *newlist;
# Line 138 | Line 208 | int    nents;
208          default:
209                  error(CONSISTENCY, "bundle_set called with unknown operation");
210          }
211 <        if (outdev == NULL)             /* nothing to display? */
211 >        if (outdev == NULL || !nents)   /* nothing to display? */
212                  return;
213                                          /* load and display beams we have */
214 <        hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
215 <        for (i = 0; i < nents; i++) {
216 <                hb[i].h = hdlist[clist[i].hd];
217 <                hb[i].b = clist[i].bi;
214 >        hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
215 >        for (i = nents; i--; ) {
216 >                hbarr[i].h = hdlist[clist[i].hd];
217 >                hbarr[i].b = clist[i].bi;
218          }
219 <        hdloadbeams(hb, nents, dispbeam);
220 <        free((char *)hb);
219 >        hdloadbeams(hbarr, nents, dispbeam);
220 >        free((char *)hbarr);
221 >        if (hdfragflags&FF_READ) {
222 >                listpos = 0;
223 >                lastin = -1;            /* need to re-sort list */
224 >        }
225          return;
226   memerr:
227          error(SYSTEM, "out of memory in bundle_set");
# Line 187 | Line 261 | int    bi;
261   }
262  
263  
264 < init_global()                   /* initialize global ray computation */
264 > ambient_list()                  /* compute ambient beam list */
265   {
266 <        long    wtotal = 0;
266 >        int4    wtotal, minrt;
267          double  frac;
268          int     i;
269          register int    j, k;
270 <                                        /* free old list and empty queue */
197 <        if (complen > 0) {
198 <                free((char *)complist);
199 <                done_packets(flush_queue());
200 <        }
201 <                                        /* allocate beam list */
270 >
271          complen = 0;
272          for (j = 0; hdlist[j] != NULL; j++)
273                  complen += nbeams(hdlist[j]);
274          complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
275 <        if (complist == NULL)
207 <                error(SYSTEM, "out of memory in init_global");
275 >        CHECK(complist==NULL, SYSTEM, "out of memory in ambient_list");
276                                          /* compute beam weights */
277 <        k = 0;
277 >        k = 0; wtotal = 0;
278          for (j = 0; hdlist[j] != NULL; j++) {
279                  frac = 512. * VLEN(hdlist[j]->wg[0]) *
280                                  VLEN(hdlist[j]->wg[1]) *
# Line 219 | Line 287 | init_global()                  /* initialize global ray computation *
287                          wtotal += complist[k++].nr;
288                  }
289          }
290 <                                        /* adjust weights */
290 >                                        /* adjust sample weights */
291          if (vdef(DISKSPACE))
292                  frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
293          else
294 <                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
295 <        while (k--)
296 <                complist[k].nr = frac * complist[k].nr;
297 <        listpos = 0; lastin = -1;       /* perform initial sort */
298 <        sortcomplist();
294 >                frac = 1024.*1024.*2048. / (wtotal*sizeof(RAYVAL));
295 >        minrt = .02*frac*wtotal/complen + .5;   /* heuristic mimimum */
296 >        if (minrt > RPACKSIZ)
297 >                minrt = RPACKSIZ;
298 >        for (k = complen; k--; )
299 >                if ((complist[k].nr = frac*complist[k].nr + 0.5) < minrt)
300 >                        complist[k].nr = minrt;
301 >        listpos = 0; lastin = -1;       /* flag initial sort */
302   }
303  
304  
305 + view_list(fp)                   /* assign beam priority from view list */
306 + FILE    *fp;
307 + {
308 +        double  pa = 1.;
309 +        VIEW    curview;
310 +        int     xr, yr;
311 +        char    *err;
312 +        BEAMLIST        blist;
313 +
314 +        copystruct(&curview, &stdview);
315 +        while (nextview(&curview, fp) != EOF) {
316 +                if ((err = setview(&curview)) != NULL) {
317 +                        error(WARNING, err);
318 +                        continue;
319 +                }
320 +                xr = yr = 1024;
321 +                normaspect(viewaspect(&curview), &pa, &xr, &yr);
322 +                viewbeams(&curview, xr, yr, &blist);
323 +                bundle_set(BS_MAX, blist.bl, blist.nb);
324 +                free((char *)blist.bl);
325 +        }
326 + }
327 +
328 +
329 + init_global()                   /* initialize global ray computation */
330 + {
331 +        register int    k;
332 +                                        /* free old list and empty queue */
333 +        if (complen > 0) {
334 +                free((char *)complist);
335 +                done_packets(flush_queue());
336 +        }
337 +                                        /* reseed random number generator */
338 +        srandom(time(NULL));
339 +                                        /* allocate beam list */
340 +        if (readinp)
341 +                view_list(stdin);
342 +        else
343 +                ambient_list();
344 +                                        /* no view vicinity */
345 +        myeye.rng = 0;
346 + }
347 +
348 +
349   mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
350   register PACKHEAD       *cdest;
351   register PACKHEAD       *cl1, *cl2;
# Line 257 | Line 372 | int    n1, n2;
372   sortcomplist()                  /* fix our list order */
373   {
374          PACKHEAD        *list2;
375 +        int     listlen;
376          register int    i;
377  
378          if (complen <= 0)       /* check to see if there is even a list */
379                  return;
380 +        if (!chunkycmp)         /* check to see if fragment list is full */
381 +                if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
382 + #if NFRAG2CHUNK
383 +                                || listlen >= NFRAG2CHUNK
384 + #endif
385 +                                ) {
386 +                        chunkycmp++;    /* use "chunky" comparison */
387 +                        lastin = -1;    /* need to re-sort list */
388 + #ifdef DEBUG
389 +                        error(WARNING, "using chunky comparison mode");
390 + #endif
391 +                }
392          if (lastin < 0 || listpos*4 >= complen*3)
393                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
394          else if (listpos) {     /* else sort and merge sublist */
395                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
396 <                if (list2 == NULL)
269 <                        error(SYSTEM, "out of memory in sortcomplist");
396 >                CHECK(list2==NULL, SYSTEM, "out of memory in sortcomplist");
397                  bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
398                  qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
399                  mergeclists(complist, list2, listpos,
# Line 283 | Line 410 | sortcomplist()                 /* fix our list order */
410          } else if (i < complen-1) {
411                  list2 = (PACKHEAD *)realloc((char *)complist,
412                                  (i+1)*sizeof(PACKHEAD));
413 <                if (list2 != NULL) {
413 >                if (list2 != NULL)
414                          complist = list2;
415 <                        complen = i+1;
289 <                }
415 >                complen = i+1;
416          }
417          listpos = 0; lastin = i;
418   }
# Line 297 | Line 423 | sortcomplist()                 /* fix our list order */
423   * more or less evenly distributed, such that computing a packet causes
424   * a given bundle to move way down in the computation order.  We keep
425   * track of where the computed bundle with the highest priority would end
426 < * up, and if we get further in our compute list than this, we resort the
426 > * up, and if we get further in our compute list than this, we re-sort the
427   * list and start again from the beginning.  Since
428   * a merge sort is used, the sorting costs are minimal.
429   */
# Line 317 | Line 443 | int    n;
443          p->nr = complist[listpos].nr - p->nc;
444          if (p->nr <= 0)
445                  return(0);
446 < #ifdef DEBUG
447 <        if (n < 1 | n > RPACKSIZ)
322 <                error(CONSISTENCY, "next_packet called with bad n value");
323 < #endif
446 >        DCHECK(n < 1 | n > RPACKSIZ,
447 >                        CONSISTENCY, "next_packet called with bad n value");
448          if (p->nr > n)
449                  p->nr = n;
450          complist[listpos].nc += p->nr;  /* find where this one would go */
451 +        if (hdgetbeam(hdlist[p->hd], p->bi) != NULL)
452 +                hdfreefrag(hdlist[p->hd], p->bi);
453          while (lastin > listpos &&
454                          beamcmp(complist+lastin, complist+listpos) > 0)
455                  lastin--;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines