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.27 by greg, Sat Apr 29 10:51:28 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*8)
52   #endif
53  
54 < static long  ambclock = 0;      /* ambient access clock */
55 < static int  nambvals = 0;       /* number of stored ambient values */
56 < static long  lastsort = 0;      /* time of last value sort */
54 > static unsigned long  ambclock = 0;     /* ambient access clock */
55 > static unsigned int  nambvals = 0;      /* number of stored ambient values */
56 > static unsigned long  lastsort = 0;     /* time of last value sort */
57   static long  sortintvl = SORT_INTVL;    /* time until next sort */
58  
59   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
60 +        /*
61 +         * Track access times unless we are sharing ambient values
62 +         * through memory on a multiprocessor, when we want to avoid
63 +         * claiming our own memory (copy on write).
64 +         */
65   #define tracktime       (shm_boundary == NULL || ambfp == NULL)
66 < #define need2sort       (ambclock > lastsort+sortintvl && \
67 <                                nambvals > SORT_THRESH)
66 > #define checksort()     if (ambclock > lastsort+sortintvl && \
67 >                        nambvals > SORT_THRESH) sortambvals(0)
68  
69   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
70  
# Line 194 | Line 199 | register RAY  *r;
199                          goto dumbamb;
200                  return;
201          }
202 +                                                /* resort memory? */
203 +        checksort();
204                                                  /* get ambient value */
198        if (need2sort)
199                sortambvals(0);
205          setcolor(acol, 0.0, 0.0, 0.0);
206          d = sumambient(acol, r, rdepth,
207                          &atrunk, thescene.cuorg, thescene.cusize);
# Line 530 | Line 535 | static
535   av2list(av)
536   AMBVAL  *av;
537   {
538 + #ifdef DEBUG
539          if (i_avlist >= nambvals)
540                  error(CONSISTENCY, "too many ambient values in av2list1");
541 + #endif
542          avlist1[i_avlist] = avlist2[i_avlist] = av;
543          i_avlist++;
544   }
# Line 553 | Line 560 | AMBVAL **avp1, **avp2;
560   }
561  
562  
563 + #ifdef DEBUG
564 + static int
565 + avlmemi(avaddr)                         /* find list position from address */
566 + AMBVAL  *avaddr;
567 + {
568 +        register AMBVAL  **avlpp;
569 +
570 +        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
571 +                        nambvals, sizeof(AMBVAL *), aposcmp);
572 +        if (avlpp == NULL)
573 +                error(CONSISTENCY, "address not found in avlmemi");
574 +        return(avlpp - avlist2);
575 + }
576 + #else
577 + #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
578 +                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
579 + #endif
580 +
581 +
582   static
583   sortambvals(always)                     /* resort ambient values */
584   int     always;
# Line 575 | Line 601 | int    always;
601           * the "always" parameter set to 1 so that the ambient
602           * tree will be rebuilt with the new accuracy parameter.
603           */
604 <        if (tracktime) {
604 >        if (tracktime) {                /* allocate pointer arrays to sort */
605                  avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
606                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
607          } else
608                  avlist1 = avlist2 = NULL;
609 <        if (avlist2 == NULL) {          /* rebuild tree? */
609 >        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
610                  if (avlist1 != NULL)
611                          free((char *)avlist1);
612 <                if (!always)
613 <                        return;
614 <                copystruct(&oldatrunk, &atrunk);
615 <                atrunk.alist = NULL;
616 <                atrunk.kid = NULL;
617 <                unloadatree(&oldatrunk, avinsert);
612 >                if (always) {           /* rebuild without sorting */
613 >                        copystruct(&oldatrunk, &atrunk);
614 >                        atrunk.alist = NULL;
615 >                        atrunk.kid = NULL;
616 >                        unloadatree(&oldatrunk, avinsert);
617 >                }
618          } else {                        /* sort memory by last access time */
593                i_avlist = 0;
594                unloadatree(&atrunk, av2list);  /* empty current tree */
619                  /*
620                   * Sorting memory is tricky because it isn't contiguous.
621                   * We have to sort an array of pointers by MRA and also
622                   * by memory position.  We then copy values in "loops"
623                   * to minimize memory hits.  Nevertheless, we will visit
624 <                 * everyone at least once, and this is an expensive process
624 >                 * everyone at least twice, and this is an expensive process
625                   * when we're thrashing, which is when we need to do it.
626                   */
627 + #ifdef DEBUG
628 +                sprintf(errmsg, "sorting %u ambient values...", nambvals);
629 +                eputs(errmsg);
630 + #endif
631 +                i_avlist = 0;
632 +                unloadatree(&atrunk, av2list);  /* empty current tree */
633 + #ifdef DEBUG
634 +                if (i_avlist < nambvals)
635 +                        error(CONSISTENCY, "missing ambient values in sortambvals");
636 + #endif
637                  qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
638                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
639                  for (i = 0; i < nambvals; i++) {
640 <                        if (avlist1[i] == NULL || avlist1[i] == avlist2[i])
640 >                        if (avlist1[i] == NULL)
641                                  continue;
642                          tap = avlist2[i];
643                          copystruct(&tav, tap);
644                          for (j = i; (pnext = avlist1[j]) != tap;
645 <                                        j = (AMBVAL **)bsearch((char *)&pnext,
612 <                                                (char *)(avlist2+i),nambvals-i,
613 <                                                sizeof(AMBVAL *),aposcmp) -
614 <                                                avlist2) {
645 >                                        j = avlmemi(pnext)) {
646                                  copystruct(avlist2[j], pnext);
647                                  avinsert(avlist2[j]);
648                                  avlist1[j] = NULL;
# Line 622 | Line 653 | int    always;
653                  }
654                  free((char *)avlist1);
655                  free((char *)avlist2);
656 <                if (sortintvl < MAXACLOCK/4)
656 >                if (sortintvl < SORT_INTVL<<6)
657                          sortintvl <<= 1;        /* wait twice as long next */
658 + #ifdef DEBUG
659 +                eputs("done\n");
660 + #endif
661          }
662          if (ambclock >= MAXACLOCK)
663                  ambclock = MAXACLOCK/2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines