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

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.26 by greg, Thu Apr 27 14:12:05 1995 UTC vs.
Revision 2.28 by greg, Sun Apr 30 13:24:36 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | typedef struct ambtree {
27  
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < extern char  *shm_boundary;     /* shared memory boundary */
30 > extern char  *shm_boundary;     /* memory sharing boundary */
31  
32   #define  MAXASET        511     /* maximum number of elements in ambient set */
33   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
# Line 42 | Line 42 | static int  nunflshed = 0;     /* number of unflushed ambi
42  
43   #ifndef SORT_THRESH
44   #ifdef BIGMEM
45 < #define SORT_THRESH     (6*(1L<<20)/sizeof(AMBVAL))
45 > #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
46   #else
47 < #define SORT_THRESH     (2*(1L<<20)/sizeof(AMBVAL))
47 > #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
48   #endif
49   #endif
50   #ifndef SORT_INTVL
51 < #define SORT_INTVL      (SORT_THRESH*2)
51 > #define SORT_INTVL      (SORT_THRESH*32)
52   #endif
53 + #ifndef MAX_SORT_INTVL
54 + #define MAX_SORT_INTVL  (SORT_INTVL<<8)
55 + #endif
56  
57 < static long  ambclock = 0;      /* ambient access clock */
58 < static int  nambvals = 0;       /* number of stored ambient values */
59 < static long  lastsort = 0;      /* time of last value sort */
57 > static unsigned long  ambclock = 0;     /* ambient access clock */
58 > static unsigned int  nambvals = 0;      /* number of stored ambient values */
59 > static unsigned long  lastsort = 0;     /* time of last value sort */
60   static long  sortintvl = SORT_INTVL;    /* time until next sort */
61  
62   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
63 +        /*
64 +         * Track access times unless we are sharing ambient values
65 +         * through memory on a multiprocessor, when we want to avoid
66 +         * claiming our own memory (copy on write).
67 +         */
68   #define tracktime       (shm_boundary == NULL || ambfp == NULL)
61 #define need2sort       (ambclock > lastsort+sortintvl && \
62                                nambvals > SORT_THRESH)
69  
70   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
71  
# Line 194 | Line 200 | register RAY  *r;
200                          goto dumbamb;
201                  return;
202          }
203 +                                                /* resort memory? */
204 +        sortambvals(0);
205                                                  /* get ambient value */
198        if (need2sort)
199                sortambvals(0);
206          setcolor(acol, 0.0, 0.0, 0.0);
207          d = sumambient(acol, r, rdepth,
208                          &atrunk, thescene.cuorg, thescene.cusize);
# Line 530 | Line 536 | static
536   av2list(av)
537   AMBVAL  *av;
538   {
539 + #ifdef DEBUG
540          if (i_avlist >= nambvals)
541                  error(CONSISTENCY, "too many ambient values in av2list1");
542 + #endif
543          avlist1[i_avlist] = avlist2[i_avlist] = av;
544          i_avlist++;
545   }
# Line 553 | Line 561 | AMBVAL **avp1, **avp2;
561   }
562  
563  
564 + #ifdef DEBUG
565 + static int
566 + avlmemi(avaddr)                         /* find list position from address */
567 + AMBVAL  *avaddr;
568 + {
569 +        register AMBVAL  **avlpp;
570 +
571 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
572 +                        nambvals, sizeof(AMBVAL *), aposcmp);
573 +        if (avlpp == NULL)
574 +                error(CONSISTENCY, "address not found in avlmemi");
575 +        return(avlpp - avlist2);
576 + }
577 + #else
578 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
579 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
580 + #endif
581 +
582 +
583   static
584   sortambvals(always)                     /* resort ambient values */
585   int     always;
# Line 560 | Line 587 | int    always;
587          AMBTREE  oldatrunk;
588          AMBVAL  tav, *tap, *pnext;
589          register int    i, j;
590 +                                        /* see if it's time yet */
591 +        if (!always && (ambclock < lastsort+sortintvl ||
592 +                        nambvals < SORT_THRESH))
593 +                return;
594          /*
595           * The idea here is to minimize memory thrashing
596           * in VM systems by improving reference locality.
# Line 575 | Line 606 | int    always;
606           * the "always" parameter set to 1 so that the ambient
607           * tree will be rebuilt with the new accuracy parameter.
608           */
609 <        if (tracktime) {
609 >        if (tracktime) {                /* allocate pointer arrays to sort */
610                  avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
611                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
612          } else
613                  avlist1 = avlist2 = NULL;
614 <        if (avlist2 == NULL) {          /* rebuild tree? */
614 >        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
615                  if (avlist1 != NULL)
616                          free((char *)avlist1);
617 <                if (!always)
618 <                        return;
619 <                copystruct(&oldatrunk, &atrunk);
620 <                atrunk.alist = NULL;
621 <                atrunk.kid = NULL;
622 <                unloadatree(&oldatrunk, avinsert);
617 >                if (always) {           /* rebuild without sorting */
618 >                        copystruct(&oldatrunk, &atrunk);
619 >                        atrunk.alist = NULL;
620 >                        atrunk.kid = NULL;
621 >                        unloadatree(&oldatrunk, avinsert);
622 >                }
623          } else {                        /* sort memory by last access time */
593                i_avlist = 0;
594                unloadatree(&atrunk, av2list);  /* empty current tree */
624                  /*
625                   * Sorting memory is tricky because it isn't contiguous.
626                   * We have to sort an array of pointers by MRA and also
627                   * by memory position.  We then copy values in "loops"
628                   * to minimize memory hits.  Nevertheless, we will visit
629 <                 * everyone at least once, and this is an expensive process
629 >                 * everyone at least twice, and this is an expensive process
630                   * when we're thrashing, which is when we need to do it.
631                   */
632 + #ifdef DEBUG
633 +                sprintf(errmsg, "sorting %u ambient values...", nambvals);
634 +                eputs(errmsg);
635 + #endif
636 +                i_avlist = 0;
637 +                unloadatree(&atrunk, av2list);  /* empty current tree */
638 + #ifdef DEBUG
639 +                if (i_avlist < nambvals)
640 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
641 + #endif
642                  qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
643                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
644                  for (i = 0; i < nambvals; i++) {
645 <                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
645 >                        if (avlist1[i] == NULL)
646                                  continue;
647                          tap = avlist2[i];
648                          copystruct(&tav, tap);
649                          for (j = i; (pnext = avlist1[j]) != tap;
650 <                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 <                                                (char *)(avlist2+i),nambvals-i,
613 <                                                sizeof(AMBVAL *),aposcmp) -
614 <                                                avlist2) {
650 >                                        j = avlmemi(pnext)) {
651                                  copystruct(avlist2[j], pnext);
652                                  avinsert(avlist2[j]);
653                                  avlist1[j] = NULL;
# Line 622 | Line 658 | int    always;
658                  }
659                  free((char *)avlist1);
660                  free((char *)avlist2);
661 <                if (sortintvl < MAXACLOCK/4)
661 >                                                /* compute new sort interval */
662 >                sortintvl = ambclock - lastsort;
663 >                if (sortintvl > MAX_SORT_INTVL)
664 >                        sortintvl = MAX_SORT_INTVL;
665 >                else
666                          sortintvl <<= 1;        /* wait twice as long next */
667 + #ifdef DEBUG
668 +                eputs("done\n");
669 + #endif
670          }
671          if (ambclock >= MAXACLOCK)
672                  ambclock = MAXACLOCK/2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines