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.28 by gwlarson, Sat Jan 9 09:17:10 1999 UTC vs.
Revision 3.36 by greg, Tue May 13 17:58:33 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for tracking beam compuatations
6   */
7  
8   #include "rholo.h"
9 < #include <sys/types.h>
9 > #include "view.h"
10  
11   #ifndef NFRAG2CHUNK
12   #define NFRAG2CHUNK     4096    /* number of fragments to start chunking */
# Line 26 | Line 23 | static char SCCSid[] = "$SunId$ SGI";
23  
24   extern time_t   time();
25  
26 + int     chunkycmp = 0;          /* clump beams together on disk */
27 +
28   static PACKHEAD *complist=NULL; /* list of beams to compute */
29   static int      complen=0;      /* length of complist */
30   static int      listpos=0;      /* current list position for next_packet */
31   static int      lastin= -1;     /* last ordered position in list */
33 static int      chunky=0;       /* clump beams together on disk */
32  
33  
34   int
# Line 40 | Line 38 | register PACKHEAD      *b0, *b1;
38          BEAMI   *bip0, *bip1;
39          register long   c;
40                                          /* first check desired quantities */
41 <        if (chunky)
41 >        if (chunkycmp)
42                  c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
43                                  rchunk(b0->nr)*(rchunk(b1->nc)+1L);
44          else
# Line 90 | Line 88 | register HDBEAMI       *hb;
88          if (b->nrm > n) {               /* (re)allocate packet holder */
89                  n = b->nrm;
90                  if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
91 <                else p = (PACKHEAD *)realloc((char *)p, packsiz(n));
92 <                if (p == NULL)
95 <                        error(SYSTEM, "out of memory in dispbeam");
91 >                else p = (PACKHEAD *)realloc((void *)p, packsiz(n));
92 >                CHECK(p==NULL, SYSTEM, "out of memory in dispbeam");
93          }
94                                          /* assign packet fields */
95 <        bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
95 >        bcopy((void *)hdbray(b), (void *)packra(p), b->nrm*sizeof(RAYVAL));
96          p->nr = p->nc = b->nrm;
97          for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
98                  if (hdlist[p->hd] == NULL)
99                          error(CONSISTENCY, "unregistered holodeck in dispbeam");
100          p->bi = hb->b;
101          disp_packet(p);                 /* display it */
102 +        if (n >= 1024) {                /* free ridiculous packets */
103 +                free((void *)p);
104 +                p = NULL; n = 0;
105 +        }
106   }
107  
108  
# Line 117 | Line 118 | int    nents;
118                                          /* search for common members */
119          for (csm = clist+nents; csm-- > clist; )
120                  csm->nc = -1;
121 <        qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp);
121 >        qsort((void *)clist, nents, sizeof(PACKHEAD), beamidcmp);
122          for (i = 0; i < complen; i++) {
123 <                csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist,
123 >                csm = (PACKHEAD *)bsearch((void *)(complist+i), (void *)clist,
124                                  nents, sizeof(PACKHEAD), beamidcmp);
125                  if (csm == NULL)
126                          continue;
# Line 130 | Line 131 | int    nents;
131                          complist[i].nr += csm->nr;
132                          csm->nr = 0;
133                          break;
134 +                case BS_MAX:            /* maximum of counts */
135 +                        if (csm->nr > complist[i].nr)
136 +                                complist[i].nr = csm->nr;
137 +                        csm->nr = 0;
138 +                        break;
139                  case BS_ADJ:            /* reset count */
140                          complist[i].nr = csm->nr;
141                          csm->nr = 0;
# Line 153 | Line 159 | int    nents;
159          case BS_NEW:                    /* new computation set */
160                  listpos = 0; lastin = -1;
161                  if (complen)            /* free old list */
162 <                        free((char *)complist);
162 >                        free((void *)complist);
163                  complist = NULL;
164                  if (!(complen = nents))
165                          return;
166                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
167                  if (complist == NULL)
168                          goto memerr;
169 <                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
169 >                bcopy((void *)clist, (void *)complist, nents*sizeof(PACKHEAD));
170                  break;
171          case BS_ADD:                    /* add to computation set */
172 +        case BS_MAX:                    /* maximum of quantities */
173          case BS_ADJ:                    /* adjust set quantities */
174                  if (nents <= 0)
175                          return;
176                  sortcomplist();         /* sort updated list & new entries */
177 <                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
177 >                qsort((void *)clist, nents, sizeof(PACKHEAD), beamcmp);
178                                          /* what can't we satisfy? */
179                  for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
180                          ;
181                  n = csm - clist;
182 <                if (op == BS_ADJ) {     /* don't regenerate adjusted beams */
182 >                if (op != BS_ADD) {     /* don't regenerate adjusted beams */
183                          for (++i; i-- && csm->nr > 0; csm++)
184                                  ;
185                          nents = csm - clist;
# Line 186 | Line 193 | int    nents;
193                                                  /* merge lists */
194                          mergeclists(newlist, clist, n, complist, complen);
195                          if (complen)
196 <                                free((char *)complist);
196 >                                free((void *)complist);
197                          complist = newlist;
198                          complen += n;
199                  }
# Line 207 | Line 214 | int    nents;
214                  hbarr[i].b = clist[i].bi;
215          }
216          hdloadbeams(hbarr, nents, dispbeam);
217 <        free((char *)hbarr);
217 >        free((void *)hbarr);
218          if (hdfragflags&FF_READ) {
219                  listpos = 0;
220                  lastin = -1;            /* need to re-sort list */
# Line 251 | Line 258 | int    bi;
258   }
259  
260  
261 < init_global()                   /* initialize global ray computation */
261 > ambient_list()                  /* compute ambient beam list */
262   {
263 <        long    wtotal = 0;
263 >        int4    wtotal, minrt;
264          double  frac;
265          int     i;
266          register int    j, k;
267 <                                        /* free old list and empty queue */
261 <        if (complen > 0) {
262 <                free((char *)complist);
263 <                done_packets(flush_queue());
264 <        }
265 <                                        /* reseed random number generator */
266 <        srandom(time(NULL));
267 <                                        /* allocate beam list */
267 >
268          complen = 0;
269          for (j = 0; hdlist[j] != NULL; j++)
270                  complen += nbeams(hdlist[j]);
271          complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
272 <        if (complist == NULL)
273 <                error(SYSTEM, "out of memory in init_global");
272 >        CHECK(complist==NULL, SYSTEM, "out of memory in ambient_list");
273                                          /* compute beam weights */
274 <        k = 0;
274 >        k = 0; wtotal = 0;
275          for (j = 0; hdlist[j] != NULL; j++) {
276                  frac = 512. * VLEN(hdlist[j]->wg[0]) *
277                                  VLEN(hdlist[j]->wg[1]) *
# Line 285 | Line 284 | init_global()                  /* initialize global ray computation *
284                          wtotal += complist[k++].nr;
285                  }
286          }
287 <                                        /* adjust weights */
287 >                                        /* adjust sample weights */
288          if (vdef(DISKSPACE))
289                  frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
290          else
291 <                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
292 <        while (k--)
293 <                complist[k].nr = frac*complist[k].nr + 0.5;
294 <        listpos = 0; lastin = -1;       /* perform initial sort */
295 <        sortcomplist();
291 >                frac = 1024.*1024.*2048. / (wtotal*sizeof(RAYVAL));
292 >        minrt = .02*frac*wtotal/complen + .5;   /* heuristic mimimum */
293 >        if (minrt > RPACKSIZ)
294 >                minrt = RPACKSIZ;
295 >        for (k = complen; k--; )
296 >                if ((complist[k].nr = frac*complist[k].nr + 0.5) < minrt)
297 >                        complist[k].nr = minrt;
298 >        listpos = 0; lastin = -1;       /* flag initial sort */
299 > }
300 >
301 >
302 > view_list(fp)                   /* assign beam priority from view list */
303 > FILE    *fp;
304 > {
305 >        double  pa = 1.;
306 >        VIEW    curview;
307 >        int     xr, yr;
308 >        char    *err;
309 >        BEAMLIST        blist;
310 >
311 >        copystruct(&curview, &stdview);
312 >        while (nextview(&curview, fp) != EOF) {
313 >                if ((err = setview(&curview)) != NULL) {
314 >                        error(WARNING, err);
315 >                        continue;
316 >                }
317 >                xr = yr = 1024;
318 >                normaspect(viewaspect(&curview), &pa, &xr, &yr);
319 >                viewbeams(&curview, xr, yr, &blist);
320 >                bundle_set(BS_MAX, blist.bl, blist.nb);
321 >                free((void *)blist.bl);
322 >        }
323 > }
324 >
325 >
326 > init_global()                   /* initialize global ray computation */
327 > {
328 >        register int    k;
329 >                                        /* free old list and empty queue */
330 >        if (complen > 0) {
331 >                free((void *)complist);
332 >                done_packets(flush_queue());
333 >        }
334 >                                        /* reseed random number generator */
335 >        srandom(time(NULL));
336 >                                        /* allocate beam list */
337 >        if (readinp)
338 >                view_list(stdin);
339 >        else
340 >                ambient_list();
341                                          /* no view vicinity */
342          myeye.rng = 0;
343   }
# Line 330 | Line 374 | sortcomplist()                 /* fix our list order */
374  
375          if (complen <= 0)       /* check to see if there is even a list */
376                  return;
377 <        if (!chunky)            /* check to see if fragment list is full */
377 >        if (!chunkycmp)         /* check to see if fragment list is full */
378                  if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
379   #if NFRAG2CHUNK
380                                  || listlen >= NFRAG2CHUNK
381   #endif
382                                  ) {
383 +                        chunkycmp++;    /* use "chunky" comparison */
384 +                        lastin = -1;    /* need to re-sort list */
385   #ifdef DEBUG
386                          error(WARNING, "using chunky comparison mode");
387   #endif
342                        chunky++;       /* use "chunky" comparison */
343                        lastin = -1;    /* need to re-sort list */
388                  }
345 #ifdef DEBUG
346                        else
347                                fprintf(stderr, "sortcomplist: %d fragments\n",
348                                                listlen);
349 #endif
389          if (lastin < 0 || listpos*4 >= complen*3)
390 <                qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
390 >                qsort((void *)complist, complen, sizeof(PACKHEAD), beamcmp);
391          else if (listpos) {     /* else sort and merge sublist */
392                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
393 <                if (list2 == NULL)
394 <                        error(SYSTEM, "out of memory in sortcomplist");
395 <                bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
357 <                qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
393 >                CHECK(list2==NULL, SYSTEM, "out of memory in sortcomplist");
394 >                bcopy((void *)complist,(void *)list2,listpos*sizeof(PACKHEAD));
395 >                qsort((void *)list2, listpos, sizeof(PACKHEAD), beamcmp);
396                  mergeclists(complist, list2, listpos,
397                                  complist+listpos, complen-listpos);
398 <                free((char *)list2);
398 >                free((void *)list2);
399          }
400                                          /* drop satisfied requests */
401          for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
402                  ;
403          if (i < 0) {
404 <                free((char *)complist);
404 >                free((void *)complist);
405                  complist = NULL;
406                  complen = 0;
407          } else if (i < complen-1) {
408 <                list2 = (PACKHEAD *)realloc((char *)complist,
408 >                list2 = (PACKHEAD *)realloc((void *)complist,
409                                  (i+1)*sizeof(PACKHEAD));
410                  if (list2 != NULL)
411                          complist = list2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines