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.1 by gregl, Fri Oct 31 10:23:29 1997 UTC vs.
Revision 3.12 by gregl, Mon Dec 1 16:35:02 1997 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include "rholo.h"
12  
13
13   #define abs(x)          ((x) > 0 ? (x) : -(x))
14   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
15  
16 + static PACKHEAD *complist=NULL; /* list of beams to compute */
17 + static int      complen=0;      /* length of complist */
18 + static int      listpos=0;      /* current list position for next_packet */
19 + static int      lastin= -1;     /* last ordered position in list */
20  
18 static PACKHEAD *complist;      /* list of beams to compute */
19 static int      complen;        /* length of complist */
20 static int      listpos;        /* current list position for next_pack */
21 static int      lastin;         /* last ordered position in list */
21  
22 + int
23 + beamcmp(b0, b1)                 /* comparison for descending compute order */
24 + register PACKHEAD       *b0, *b1;
25 + {
26 +        return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) );
27 + }
28  
29 +
30 + bundle_set(op, clist, nents)    /* bundle set operation */
31 + int     op;
32 + PACKHEAD        *clist;
33 + int     nents;
34 + {
35 +        BEAM    *b;
36 +        PACKHEAD        *p;
37 +        register int    i, n;
38 +
39 +        switch (op) {
40 +        case BS_NEW:                    /* new computation set */
41 +                if (complen)
42 +                        free((char *)complist);
43 +                if (nents <= 0) {
44 +                        complist = NULL;
45 +                        listpos = complen = 0;
46 +                        lastin = -1;
47 +                        return;
48 +                }
49 +                complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
50 +                if (complist == NULL)
51 +                        goto memerr;
52 +                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
53 +                complen = nents;        /* finish initialization below */
54 +                break;
55 +        case BS_ADD:                    /* add to computation set */
56 +        case BS_ADJ:                    /* adjust set quantities */
57 +                if (nents <= 0)
58 +                        return;
59 +                                        /* merge any common members */
60 +                for (n = 0; n < nents; n++) {
61 +                        for (i = 0; i < complen; i++)
62 +                                if (clist[n].bi == complist[i].bi &&
63 +                                                clist[n].hd == complist[i].hd) {
64 +                                        int     oldnr = complist[i].nr;
65 +                                        if (op == BS_ADD)
66 +                                                complist[i].nr += clist[n].nr;
67 +                                        else /* op == BS_ADJ */
68 +                                                complist[i].nr = clist[n].nr;
69 +                                        clist[n].nr = 0;
70 +                                        clist[n].nc = complist[i].nc;
71 +                                        if (complist[i].nr != oldnr)
72 +                                                lastin = -1;    /* flag sort */
73 +                                        break;
74 +                                }
75 +                        if (i >= complen)
76 +                                clist[n].nc = bnrays(hdlist[clist[n].hd],
77 +                                                clist[n].bi);
78 +                }
79 +                                        /* sort updated list */
80 +                sortcomplist();
81 +                                        /* sort new entries */
82 +                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
83 +                                        /* what can't we satisfy? */
84 +                for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
85 +                        ;
86 +                if (op == BS_ADJ)
87 +                        nents = n;
88 +                if (n) {                /* allocate space for merged list */
89 +                        PACKHEAD        *newlist;
90 +                        newlist = (PACKHEAD *)malloc(
91 +                                        (complen+n)*sizeof(PACKHEAD) );
92 +                        if (newlist == NULL)
93 +                                goto memerr;
94 +                                                /* merge lists */
95 +                        mergeclists(newlist, clist, n, complist, complen);
96 +                        if (complen)
97 +                                free((char *)complist);
98 +                        complist = newlist;
99 +                        complen += n;
100 +                }
101 +                listpos = 0;
102 +                lastin = complen-1;     /* list is now sorted */
103 +                break;
104 +        case BS_DEL:                    /* delete from computation set */
105 +                if (nents <= 0)
106 +                        return;
107 +                                        /* find each member */
108 +                for (i = 0; i < complen; i++)
109 +                        for (n = 0; n < nents; n++)
110 +                                if (clist[n].bi == complist[i].bi &&
111 +                                                clist[n].hd == complist[i].hd) {
112 +                                        if (clist[n].nr == 0 ||
113 +                                                clist[n].nr >= complist[i].nr)
114 +                                                complist[i].nr = 0;
115 +                                        else
116 +                                                complist[i].nr -= clist[n].nr;
117 +                                        lastin = -1;    /* flag full sort */
118 +                                        break;
119 +                                }
120 +                return;                 /* no display */
121 +        default:
122 +                error(CONSISTENCY, "bundle_set called with unknown operation");
123 +        }
124 +        if (outdev == NULL)
125 +                return;
126 +        n = 32*RPACKSIZ;                /* allocate packet holder */
127 +        p = (PACKHEAD *)malloc(packsiz(n));
128 +        if (p == NULL)
129 +                goto memerr;
130 +                                        /* display what we have */
131 +        for (i = 0; i < nents; i++)
132 +                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
133 +                        if (b->nrm > n) {
134 +                                n = b->nrm;
135 +                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
136 +                                if (p == NULL)
137 +                                        goto memerr;
138 +                        }
139 +                        bcopy((char *)hdbray(b), (char *)packra(p),
140 +                                        b->nrm*sizeof(RAYVAL));
141 +                        p->hd = clist[i].hd;
142 +                        p->bi = clist[i].bi;
143 +                        p->nr = p->nc = b->nrm;
144 +                        disp_packet(p);
145 +                }
146 +        free((char *)p);                /* clean up */
147 +        if (op == BS_NEW) {
148 +                done_packets(flush_queue());    /* empty queue, so we can... */
149 +                for (i = 0; i < complen; i++)   /* ...get number computed */
150 +                        complist[i].nc = bnrays(hdlist[complist[i].hd],
151 +                                                complist[i].bi);
152 +                listpos = 0;
153 +                lastin = -1;            /* flag for initial sort */
154 +        }
155 +        return;
156 + memerr:
157 +        error(SYSTEM, "out of memory in bundle_set");
158 + }
159 +
160 +
161   int
162   weightf(hp, x0, x1, x2)         /* voxel weighting function */
163   register HOLO   *hp;
# Line 82 | Line 219 | int    x, y, z, dx, dy, dz;
219   }
220  
221  
85 int
86 beamcmp(b0, b1)                 /* comparison for descending compute order */
87 register PACKHEAD       *b0, *b1;
88 {
89        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
90                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
91 }
92
93
222   init_global()                   /* initialize global ray computation */
223   {
224          long    wtotal = 0;
# Line 98 | Line 226 | init_global()                  /* initialize global ray computation *
226          int     lseg[2][3];
227          double  frac;
228          register int    k;
229 +                                        /* free old list */
230 +        if (complen > 0)
231 +                free((char *)complist);
232                                          /* allocate beam list */
233          complen = 0;
234          for (j = 0; hdlist[j] != NULL; j++)
# Line 120 | Line 251 | init_global()                  /* initialize global ray computation *
251                          wtotal += complist[k++].nr;
252                  }
253                                          /* adjust weights */
254 <        if (vdef(DISKSPACE)) {
254 >        if (vdef(DISKSPACE))
255                  frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
256 <                if (frac < 0.95 | frac > 1.05)
257 <                        while (k--)
258 <                                complist[k].nr = frac * complist[k].nr;
259 <        }
260 <        listpos = 0; lastin = -1;
256 >        else
257 >                frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
258 >        while (k--)
259 >                complist[k].nr = frac * complist[k].nr;
260 >        listpos = 0; lastin = -1;       /* flag initial sort */
261   }
262  
263  
# Line 156 | Line 287 | int    n1, n2;
287   sortcomplist()                  /* fix our list order */
288   {
289          PACKHEAD        *list2;
290 <                                /* empty queue */
291 <        done_packets(flush_queue());
292 <        if (lastin < 0)         /* flag to sort entire list */
290 >        register int    i;
291 >
292 >        if (complen <= 0)       /* check to see if there is even a list */
293 >                return;
294 >        if (lastin < 0 || listpos*4 >= complen*3)
295                  qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
296          else if (listpos) {     /* else sort and merge sublist */
297                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
# Line 170 | Line 303 | sortcomplist()                 /* fix our list order */
303                                  complist+listpos, complen-listpos);
304                  free((char *)list2);
305          }
306 <        listpos = 0; lastin = complen-1;
306 >                                        /* drop satisfied requests */
307 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
308 >                ;
309 >        if (i < 0) {
310 >                free((char *)complist);
311 >                complist = NULL;
312 >                complen = 0;
313 >        } else if (i < complen-1) {
314 >                list2 = (PACKHEAD *)realloc((char *)complist,
315 >                                (i+1)*sizeof(PACKHEAD));
316 >                if (list2 != NULL) {
317 >                        complist = list2;
318 >                        complen = i+1;
319 >                }
320 >        }
321 >        listpos = 0; lastin = i;
322   }
323  
324  
# Line 180 | Line 328 | sortcomplist()                 /* fix our list order */
328   * a given bundle to move way down in the computation order.  We keep
329   * track of where the computed bundle with the highest priority would end
330   * up, and if we get further in our compute list than this, we resort the
331 < * list and start again from the beginning.  We have to flush the queue
332 < * each time we sort, to ensure that we are not disturbing the order.
185 < *      If our major assumption is violated, and we have a very steep
186 < * descent in our weights, then we will end up resorting much more often
187 < * than necessary, resulting in frequent flushing of the queue.  Since
188 < * a merge sort is used, the sorting costs will be minimal.
331 > * list and start again from the beginning.  Since
332 > * a merge sort is used, the sorting costs are minimal.
333   */
334   next_packet(p)                  /* prepare packet for computation */
335   register PACKET *p;
336   {
193        int     ncomp;
337          register int    i;
338  
196        if (complen <= 0)
197                return(0);
339          if (listpos > lastin)           /* time to sort the list */
340                  sortcomplist();
341 +        if (complen <= 0)
342 +                return(0);
343          p->hd = complist[listpos].hd;
344          p->bi = complist[listpos].bi;
345 <        ncomp = bnrays(hdlist[p->hd],p->bi);
346 <        p->nr = complist[listpos].nr - ncomp;
345 >        p->nc = complist[listpos].nc;
346 >        p->nr = complist[listpos].nr - p->nc;
347          if (p->nr <= 0)
348                  return(0);
349          if (p->nr > RPACKSIZ)
350                  p->nr = RPACKSIZ;
351 <        ncomp += p->nr;                 /* find where this one would go */
352 <        while (lastin > listpos && complist[listpos].nr *
353 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
211 <                        > complist[lastin].nr * (ncomp+1))
351 >        complist[listpos].nc += p->nr;  /* find where this one would go */
352 >        while (lastin > listpos &&
353 >                        beamcmp(complist+lastin, complist+listpos) > 0)
354                  lastin--;
355          listpos++;
356          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines