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.4 by gregl, Tue Nov 4 11:52:21 1997 UTC vs.
Revision 3.41 by schorsch, Thu Jan 1 11:21:55 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 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 <time.h>
9 + #include <string.h>
10 + #include <stdio.h>
11 +
12 + #include "view.h"
13   #include "rholo.h"
14  
15 + #ifndef NFRAG2CHUNK
16 + #define NFRAG2CHUNK     4096    /* number of fragments to start chunking */
17 + #endif
18  
19 + #ifndef abs
20   #define abs(x)          ((x) > 0 ? (x) : -(x))
21 + #endif
22 + #ifndef sgn
23   #define sgn(x)          ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
24 + #endif
25  
26 + #define rchunk(n)       (((n)+(RPACKSIZ/2))/RPACKSIZ)
27  
28 + int     chunkycmp = 0;          /* clump beams together on disk */
29 +
30   static PACKHEAD *complist=NULL; /* list of beams to compute */
31   static int      complen=0;      /* length of complist */
32   static int      listpos=0;      /* current list position for next_packet */
33   static int      lastin= -1;     /* last ordered position in list */
34  
35 + static void sortcomplist(void);
36 + static void mergeclists(PACKHEAD *cdest, PACKHEAD *cl1, int n1, PACKHEAD *cl2, int n2);
37 + static void view_list(FILE      *fp);
38 + static void ambient_list(void);
39 + static double beamvolume(HOLO   *hp, int        bi);
40 + static void dispbeam(BEAM       *b, HDBEAMI     *hb);
41  
42 +
43 +
44 + static int
45 + beamcmp(b0, b1)                         /* comparison for compute order */
46 + register PACKHEAD       *b0, *b1;
47 + {
48 +        BEAMI   *bip0, *bip1;
49 +        register long   c;
50 +                                        /* first check desired quantities */
51 +        if (chunkycmp)
52 +                c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) -
53 +                                rchunk(b0->nr)*(rchunk(b1->nc)+1L);
54 +        else
55 +                c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L);
56 +        if (c > 0) return(1);
57 +        if (c < 0) return(-1);
58 +                                /* only one file, so skip the following: */
59 + #if 0
60 +                                        /* next, check file descriptors */
61 +        c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd;
62 +        if (c) return(c);
63 + #endif
64 +                                        /* finally, check file positions */
65 +        bip0 = &hdlist[b0->hd]->bi[b0->bi];
66 +        bip1 = &hdlist[b1->hd]->bi[b1->bi];
67 +                                        /* put diskless beams last */
68 +        if (!bip0->nrd)
69 +                return(bip1->nrd > 0);
70 +        if (!bip1->nrd)
71 +                return(-1);
72 +        c = bip0->fo - bip1->fo;
73 +        return(c < 0 ? -1 : c > 0);
74 + }
75 +
76 +
77   int
78 < beamcmp(b0, b1)                 /* comparison for descending compute order */
78 > beamidcmp(b0, b1)                       /* comparison for beam searching */
79   register PACKHEAD       *b0, *b1;
80   {
81 <        return( b1->nr*(bnrays(hdlist[b0->hd],b0->bi)+1) -
82 <                b0->nr*(bnrays(hdlist[b1->hd],b1->bi)+1) );
81 >        register int    c = b0->hd - b1->hd;
82 >
83 >        if (c) return(c);
84 >        return(b0->bi - b1->bi);
85   }
86  
87  
88 < bundle_set(op, clist, nents)    /* bundle set operation */
89 < int     op;
90 < PACKHEAD        *clist;
91 < int     nents;
88 > static void
89 > dispbeam(                               /* display a holodeck beam */
90 >        register BEAM   *b,
91 >        register HDBEAMI        *hb
92 > )
93   {
94 <        BEAM    *b;
95 <        PACKHEAD        *p;
40 <        register int    i, n;
94 >        static int      n = 0;
95 >        static PACKHEAD *p = NULL;
96  
97 +        if (b == NULL)
98 +                return;
99 +        if (b->nrm > n) {               /* (re)allocate packet holder */
100 +                n = b->nrm;
101 +                if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
102 +                else p = (PACKHEAD *)realloc((void *)p, packsiz(n));
103 +                CHECK(p==NULL, SYSTEM, "out of memory in dispbeam");
104 +        }
105 +                                        /* assign packet fields */
106 +        memcpy((void *)packra(p), (void *)hdbray(b), b->nrm*sizeof(RAYVAL));
107 +        p->nr = p->nc = b->nrm;
108 +        for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++)
109 +                if (hdlist[p->hd] == NULL)
110 +                        error(CONSISTENCY, "unregistered holodeck in dispbeam");
111 +        p->bi = hb->b;
112 +        disp_packet(p);                 /* display it */
113 +        if (n >= 1024) {                /* free ridiculous packets */
114 +                free((void *)p);
115 +                p = NULL; n = 0;
116 +        }
117 + }
118 +
119 +
120 + extern void
121 + bundle_set(     /* bundle set operation */
122 +        int     op,
123 +        PACKHEAD        *clist,
124 +        int     nents
125 + )
126 + {
127 +        int     oldnr, n;
128 +        HDBEAMI *hbarr;
129 +        register PACKHEAD       *csm;
130 +        register int    i;
131 +                                        /* search for common members */
132 +        for (csm = clist+nents; csm-- > clist; )
133 +                csm->nc = -1;
134 +        qsort((void *)clist, nents, sizeof(PACKHEAD), beamidcmp);
135 +        for (i = 0; i < complen; i++) {
136 +                csm = (PACKHEAD *)bsearch((void *)(complist+i), (void *)clist,
137 +                                nents, sizeof(PACKHEAD), beamidcmp);
138 +                if (csm == NULL)
139 +                        continue;
140 +                oldnr = complist[i].nr;
141 +                csm->nc = complist[i].nc;
142 +                switch (op) {
143 +                case BS_ADD:            /* add to count */
144 +                        complist[i].nr += csm->nr;
145 +                        csm->nr = 0;
146 +                        break;
147 +                case BS_MAX:            /* maximum of counts */
148 +                        if (csm->nr > complist[i].nr)
149 +                                complist[i].nr = csm->nr;
150 +                        csm->nr = 0;
151 +                        break;
152 +                case BS_ADJ:            /* reset count */
153 +                        complist[i].nr = csm->nr;
154 +                        csm->nr = 0;
155 +                        break;
156 +                case BS_DEL:            /* delete count */
157 +                        if (csm->nr == 0 || csm->nr >= complist[i].nr)
158 +                                complist[i].nr = 0;
159 +                        else
160 +                                complist[i].nr -= csm->nr;
161 +                        break;
162 +                }
163 +                if (complist[i].nr != oldnr)
164 +                        lastin = -1;    /* flag sort */
165 +        }
166 +                                /* record computed rays for uncommon beams */
167 +        for (csm = clist+nents; csm-- > clist; )
168 +                if (csm->nc < 0)
169 +                        csm->nc = bnrays(hdlist[csm->hd], csm->bi);
170 +                                /* complete list operations */
171          switch (op) {
172          case BS_NEW:                    /* new computation set */
173 <                if (complen)
174 <                        free((char *)complist);
175 <                if (nents <= 0) {
176 <                        complist = NULL;
177 <                        listpos = complen = 0;
49 <                        lastin = -1;
173 >                listpos = 0; lastin = -1;
174 >                if (complen)            /* free old list */
175 >                        free((void *)complist);
176 >                complist = NULL;
177 >                if (!(complen = nents))
178                          return;
51                }
179                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
180                  if (complist == NULL)
181                          goto memerr;
182 <                bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
56 <                complen = nents;
57 <                listpos = 0;
58 <                lastin = -1;            /* flag for initial sort */
182 >                memcpy((void *)complist, (void *)clist, nents*sizeof(PACKHEAD));
183                  break;
184          case BS_ADD:                    /* add to computation set */
185 +        case BS_MAX:                    /* maximum of quantities */
186 +        case BS_ADJ:                    /* adjust set quantities */
187                  if (nents <= 0)
188                          return;
189 <                                        /* merge any common members */
190 <                for (i = 0; i < complen; i++)
65 <                        for (n = 0; n < nents; n++)
66 <                                if (clist[n].bi == complist[i].bi &&
67 <                                                clist[n].hd == complist[i].hd) {
68 <                                        complist[i].nr += clist[n].nr;
69 <                                        clist[n].nr = 0;
70 <                                        lastin = -1;    /* flag full sort */
71 <                                        break;
72 <                                }
73 <                                        /* sort updated list */
74 <                sortcomplist();
75 <                                        /* sort new entries */
76 <                qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
189 >                sortcomplist();         /* sort updated list & new entries */
190 >                qsort((void *)clist, nents, sizeof(PACKHEAD), beamcmp);
191                                          /* what can't we satisfy? */
192 <                for (n = 0; n < nents && clist[n].nr >
79 <                                bnrays(hdlist[clist[n].hd],clist[n].bi); n++)
192 >                for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++)
193                          ;
194 +                n = csm - clist;
195 +                if (op != BS_ADD) {     /* don't regenerate adjusted beams */
196 +                        for (++i; i-- && csm->nr > 0; csm++)
197 +                                ;
198 +                        nents = csm - clist;
199 +                }
200                  if (n) {                /* allocate space for merged list */
201                          PACKHEAD        *newlist;
202                          newlist = (PACKHEAD *)malloc(
# Line 87 | Line 206 | int    nents;
206                                                  /* merge lists */
207                          mergeclists(newlist, clist, n, complist, complen);
208                          if (complen)
209 <                                free((char *)complist);
209 >                                free((void *)complist);
210                          complist = newlist;
211                          complen += n;
212                  }
# Line 95 | Line 214 | int    nents;
214                  lastin = complen-1;     /* list is now sorted */
215                  break;
216          case BS_DEL:                    /* delete from computation set */
217 <                if (nents <= 0)
99 <                        return;
100 <                                        /* find each member */
101 <                for (i = 0; i < complen; i++)
102 <                        for (n = 0; n < nents; n++)
103 <                                if (clist[n].bi == complist[i].bi &&
104 <                                                clist[n].hd == complist[i].hd) {
105 <                                        if (clist[n].nr == 0 ||
106 <                                                clist[n].nr >= complist[i].nr)
107 <                                                complist[i].nr = 0;
108 <                                        else
109 <                                                complist[i].nr -= clist[n].nr;
110 <                                        lastin = -1;    /* flag full sort */
111 <                                        break;
112 <                                }
113 <                if (lastin < 0)         /* sort updated list */
114 <                        sortcomplist();
115 <                return;                 /* no display */
217 >                return;                 /* already done */
218          default:
219                  error(CONSISTENCY, "bundle_set called with unknown operation");
220          }
221 <        n = 0;                          /* allocate packet holder */
222 <        for (i = 0; i < nents; i++)
223 <                if (clist[i].nr > n)
224 <                        n = clist[i].nr;
225 <        p = (PACKHEAD *)malloc(sizeof(PACKHEAD) + n*sizeof(RAYVAL));
226 <        if (p == NULL)
227 <                goto memerr;
228 <                                        /* display what we have */
229 <        for (i = 0; i < nents; i++)
230 <                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
231 <                        bcopy((char *)hdbray(b), (char *)(p+1),
232 <                                        (p->nr=b->nrm)*sizeof(RAYVAL));
233 <                        disp_packet((PACKET *)p);
234 <                }
133 <        free((char *)p);                /* clean up */
221 >        if (outdev == NULL || !nents)   /* nothing to display? */
222 >                return;
223 >                                        /* load and display beams we have */
224 >        hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
225 >        for (i = nents; i--; ) {
226 >                hbarr[i].h = hdlist[clist[i].hd];
227 >                hbarr[i].b = clist[i].bi;
228 >        }
229 >        hdloadbeams(hbarr, nents, dispbeam);
230 >        free((void *)hbarr);
231 >        if (hdfragflags&FF_READ) {
232 >                listpos = 0;
233 >                lastin = -1;            /* need to re-sort list */
234 >        }
235          return;
236   memerr:
237          error(SYSTEM, "out of memory in bundle_set");
238   }
239  
240  
241 < int
242 < weightf(hp, x0, x1, x2)         /* voxel weighting function */
243 < register HOLO   *hp;
244 < register int    x0, x1, x2;
241 > static double
242 > beamvolume(     /* compute approximate volume of a beam */
243 >        HOLO    *hp,
244 >        int     bi
245 > )
246   {
247 <        switch (vlet(OCCUPANCY)) {
248 <        case 'U':               /* uniform weighting */
249 <                return(1);
250 <        case 'C':               /* center weighting (crude) */
251 <                x0 += x0 - hp->grid[0] + 1;
252 <                x0 = abs(x0)*hp->grid[1]*hp->grid[2];
253 <                x1 += x1 - hp->grid[1] + 1;
254 <                x1 = abs(x1)*hp->grid[0]*hp->grid[2];
255 <                x2 += x2 - hp->grid[2] + 1;
256 <                x2 = abs(x2)*hp->grid[0]*hp->grid[1];
257 <                return(hp->grid[0]*hp->grid[1]*hp->grid[2] -
258 <                                (x0+x1+x2)/3);
259 <        default:
260 <                badvalue(OCCUPANCY);
247 >        GCOORD  gc[2];
248 >        FVECT   cp[4], edgeA, edgeB, cent[2];
249 >        FVECT   crossp[2], diffv;
250 >        double  vol[2];
251 >        register int    i;
252 >                                        /* get grid coordinates */
253 >        if (!hdbcoord(gc, hp, bi))
254 >                error(CONSISTENCY, "bad beam index in beamvolume");
255 >        for (i = 0; i < 2; i++) {       /* compute cell area vectors */
256 >                hdcell(cp, hp, gc+i);
257 >                VSUM(edgeA, cp[1], cp[0], -1.0);
258 >                VSUM(edgeB, cp[3], cp[1], -1.0);
259 >                fcross(crossp[i], edgeA, edgeB);
260 >                                        /* compute center */
261 >                cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
262 >                cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
263 >                cent[i][2] = 0.5*(cp[0][2] + cp[2][2]);
264          }
265 < }
266 <
267 <
268 < /* The following is by Daniel Cohen, taken from Graphics Gems IV, p. 368 */
269 < long
165 < lineweight(hp, x, y, z, dx, dy, dz)     /* compute weights along a line */
166 < HOLO    *hp;
167 < int     x, y, z, dx, dy, dz;
168 < {
169 <        long    wres = 0;
170 <        int     n, sx, sy, sz, exy, exz, ezy, ax, ay, az, bx, by, bz;
171 <
172 <        sx = sgn(dx);   sy = sgn(dy);   sz = sgn(dz);
173 <        ax = abs(dx);   ay = abs(dy);   az = abs(dz);
174 <        bx = 2*ax;      by = 2*ay;      bz = 2*az;
175 <        exy = ay-ax;    exz = az-ax;    ezy = ay-az;
176 <        n = ax+ay+az + 1;               /* added increment to visit last */
177 <        while (n--) {
178 <                wres += weightf(hp, x, y, z);
179 <                if (exy < 0) {
180 <                        if (exz < 0) {
181 <                                x += sx;
182 <                                exy += by; exz += bz;
183 <                        } else {
184 <                                z += sz;
185 <                                exz -= bx; ezy += by;
186 <                        }
187 <                } else {
188 <                        if (ezy < 0) {
189 <                                z += sz;
190 <                                exz -= bx; ezy += by;
191 <                        } else {
192 <                                y += sy;
193 <                                exy -= bx; ezy -= bz;
194 <                        }
195 <                }
265 >                                        /* compute difference vector */
266 >        VSUM(diffv, cent[1], cent[0], -1.0);
267 >        for (i = 0; i < 2; i++) {       /* compute volume contributions */
268 >                vol[i] = 0.5*DOT(crossp[i], diffv);
269 >                if (vol[i] < 0.) vol[i] = -vol[i];
270          }
271 <        return(wres);
271 >        return(vol[0] + vol[1]);        /* return total volume */
272   }
273  
274  
275 < init_global()                   /* initialize global ray computation */
275 > static void
276 > ambient_list(void)                      /* compute ambient beam list */
277   {
278 <        long    wtotal = 0;
204 <        int     i, j;
205 <        int     lseg[2][3];
278 >        int32   wtotal, minrt;
279          double  frac;
280 <        register int    k;
281 <                                        /* free old list */
282 <        if (complen > 0)
210 <                free((char *)complist);
211 <                                        /* allocate beam list */
280 >        int     i;
281 >        register int    j, k;
282 >
283          complen = 0;
284          for (j = 0; hdlist[j] != NULL; j++)
285                  complen += nbeams(hdlist[j]);
286          complist = (PACKHEAD *)malloc(complen*sizeof(PACKHEAD));
287 <        if (complist == NULL)
217 <                error(SYSTEM, "out of memory in init_global");
287 >        CHECK(complist==NULL, SYSTEM, "out of memory in ambient_list");
288                                          /* compute beam weights */
289 <        k = 0;
290 <        for (j = 0; hdlist[j] != NULL; j++)
289 >        k = 0; wtotal = 0;
290 >        for (j = 0; hdlist[j] != NULL; j++) {
291 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
292 >                                VLEN(hdlist[j]->wg[1]) *
293 >                                VLEN(hdlist[j]->wg[2]);
294                  for (i = nbeams(hdlist[j]); i > 0; i--) {
222                        hdlseg(lseg, hdlist[j], i);
295                          complist[k].hd = j;
296                          complist[k].bi = i;
297 <                        complist[k].nr = lineweight( hdlist[j],
298 <                                        lseg[0][0], lseg[0][1], lseg[0][2],
227 <                                        lseg[1][0] - lseg[0][0],
228 <                                        lseg[1][1] - lseg[0][1],
229 <                                        lseg[1][2] - lseg[0][2] );
297 >                        complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
298 >                        complist[k].nc = bnrays(hdlist[j], i);
299                          wtotal += complist[k++].nr;
300                  }
232                                        /* adjust weights */
233        if (vdef(DISKSPACE)) {
234                frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
235                if (frac < 0.95 | frac > 1.05)
236                        while (k--)
237                                complist[k].nr = frac * complist[k].nr;
301          }
302 +                                        /* adjust sample weights */
303 +        if (vdef(DISKSPACE))
304 +                frac = 1024.*1024.*vflt(DISKSPACE) / (wtotal*sizeof(RAYVAL));
305 +        else
306 +                frac = 1024.*1024.*2048. / (wtotal*sizeof(RAYVAL));
307 +        minrt = .02*frac*wtotal/complen + .5;   /* heuristic mimimum */
308 +        if (minrt > RPACKSIZ)
309 +                minrt = RPACKSIZ;
310 +        for (k = complen; k--; )
311 +                if ((complist[k].nr = frac*complist[k].nr + 0.5) < minrt)
312 +                        complist[k].nr = minrt;
313          listpos = 0; lastin = -1;       /* flag initial sort */
314   }
315  
316  
317 < mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
318 < PACKHEAD        *cdest;
319 < PACKHEAD        *cl1, *cl2;
320 < int     n1, n2;
317 > static void
318 > view_list(                      /* assign beam priority from view list */
319 >        FILE    *fp
320 > )
321   {
322 <        int     cmp;
322 >        double  pa = 1.;
323 >        VIEW    curview;
324 >        int     xr, yr;
325 >        char    *err;
326 >        BEAMLIST        blist;
327  
328 +        curview = stdview;
329 +        while (nextview(&curview, fp) != EOF) {
330 +                if ((err = setview(&curview)) != NULL) {
331 +                        error(WARNING, err);
332 +                        continue;
333 +                }
334 +                xr = yr = 1024;
335 +                normaspect(viewaspect(&curview), &pa, &xr, &yr);
336 +                viewbeams(&curview, xr, yr, &blist);
337 +                bundle_set(BS_MAX, blist.bl, blist.nb);
338 +                free((void *)blist.bl);
339 +        }
340 + }
341 +
342 +
343 + extern void
344 + init_global(void)                       /* initialize global ray computation */
345 + {
346 +                                        /* free old list and empty queue */
347 +        if (complen > 0) {
348 +                free((void *)complist);
349 +                done_packets(flush_queue());
350 +        }
351 +                                        /* reseed random number generator */
352 +        srandom(time(NULL));
353 +                                        /* allocate beam list */
354 +        if (readinp)
355 +                view_list(stdin);
356 +        else
357 +                ambient_list();
358 +                                        /* no view vicinity */
359 +        myeye.rng = 0;
360 + }
361 +
362 +
363 + static void
364 + mergeclists(    /* merge two sorted lists */
365 +        register PACKHEAD       *cdest,
366 +        register PACKHEAD       *cl1,
367 +        int     n1,
368 +        register PACKHEAD       *cl2,
369 +        int     n2
370 + )
371 + {
372 +        register int    cmp;
373 +
374          while (n1 | n2) {
375                  if (!n1) cmp = 1;
376                  else if (!n2) cmp = -1;
377                  else cmp = beamcmp(cl1, cl2);
378                  if (cmp > 0) {
379 <                        copystruct(cdest, cl2);
379 >                        *cdest = *cl2;
380                          cl2++; n2--;
381                  } else {
382 <                        copystruct(cdest, cl1);
382 >                        *cdest = *cl1;
383                          cl1++; n1--;
384                  }
385                  cdest++;
# Line 263 | Line 387 | int    n1, n2;
387   }
388  
389  
390 < sortcomplist()                  /* fix our list order */
390 > static void
391 > sortcomplist(void)                      /* fix our list order */
392   {
393          PACKHEAD        *list2;
394 +        int     listlen;
395          register int    i;
396  
271                                /* empty queue */
272        done_packets(flush_queue());
397          if (complen <= 0)       /* check to see if there is even a list */
398                  return;
399 <        if (lastin < 0)         /* flag to sort entire list */
400 <                qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp);
399 >        if (!chunkycmp)         /* check to see if fragment list is full */
400 >                if (!hdfragOK(hdlist[0]->fd, &listlen, NULL)
401 > #if NFRAG2CHUNK
402 >                                || listlen >= NFRAG2CHUNK
403 > #endif
404 >                                ) {
405 >                        chunkycmp++;    /* use "chunky" comparison */
406 >                        lastin = -1;    /* need to re-sort list */
407 > #ifdef DEBUG
408 >                        error(WARNING, "using chunky comparison mode");
409 > #endif
410 >                }
411 >        if (lastin < 0 || listpos*4 >= complen*3)
412 >                qsort((void *)complist, complen, sizeof(PACKHEAD), beamcmp);
413          else if (listpos) {     /* else sort and merge sublist */
414                  list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD));
415 <                if (list2 == NULL)
416 <                        error(SYSTEM, "out of memory in sortcomplist");
417 <                bcopy((char *)complist,(char *)list2,listpos*sizeof(PACKHEAD));
282 <                qsort((char *)list2, listpos, sizeof(PACKHEAD), beamcmp);
415 >                CHECK(list2==NULL, SYSTEM, "out of memory in sortcomplist");
416 >                memcpy((void *)list2,(void *)complist,listpos*sizeof(PACKHEAD));
417 >                qsort((void *)list2, listpos, sizeof(PACKHEAD), beamcmp);
418                  mergeclists(complist, list2, listpos,
419                                  complist+listpos, complen-listpos);
420 <                free((char *)list2);
420 >                free((void *)list2);
421          }
287                                        /* check for all finished */
288        if (complist[0].nr <= bnrays(hdlist[complist[0].hd],complist[0].bi)) {
289                free((char *)complist);
290                complist = NULL;
291                complen = 0;
292        }
422                                          /* drop satisfied requests */
423 <        for (i = complen; i-- && complist[i].nr <=
295 <                        bnrays(hdlist[complist[i].hd],complist[i].bi); )
423 >        for (i = complen; i-- && complist[i].nr <= complist[i].nc; )
424                  ;
425          if (i < 0) {
426 <                free((char *)complist);
426 >                free((void *)complist);
427                  complist = NULL;
428                  complen = 0;
429          } else if (i < complen-1) {
430 <                list2 = (PACKHEAD *)realloc((char *)complist,
430 >                list2 = (PACKHEAD *)realloc((void *)complist,
431                                  (i+1)*sizeof(PACKHEAD));
432 <                if (list2 != NULL) {
432 >                if (list2 != NULL)
433                          complist = list2;
434 <                        complen = i+1;
307 <                }
434 >                complen = i+1;
435          }
436          listpos = 0; lastin = i;
437   }
# Line 315 | Line 442 | sortcomplist()                 /* fix our list order */
442   * more or less evenly distributed, such that computing a packet causes
443   * a given bundle to move way down in the computation order.  We keep
444   * track of where the computed bundle with the highest priority would end
445 < * up, and if we get further in our compute list than this, we resort the
446 < * list and start again from the beginning.  We have to flush the queue
447 < * each time we sort, to ensure that we are not disturbing the order.
321 < *      If our major assumption is violated, and we have a very steep
322 < * descent in our weights, then we will end up resorting much more often
323 < * than necessary, resulting in frequent flushing of the queue.  Since
324 < * a merge sort is used, the sorting costs will be minimal.
445 > * up, and if we get further in our compute list than this, we re-sort the
446 > * list and start again from the beginning.  Since
447 > * a merge sort is used, the sorting costs are minimal.
448   */
449 < next_packet(p)                  /* prepare packet for computation */
450 < register PACKET *p;
449 > extern int
450 > next_packet(            /* prepare packet for computation */
451 >        register PACKET *p,
452 >        int     n
453 > )
454   {
329        int     ncomp;
330        register int    i;
331
332        if (complen <= 0)
333                return(0);
455          if (listpos > lastin)           /* time to sort the list */
456                  sortcomplist();
457 +        if (complen <= 0)
458 +                return(0);
459          p->hd = complist[listpos].hd;
460          p->bi = complist[listpos].bi;
461 <        ncomp = bnrays(hdlist[p->hd],p->bi);
462 <        p->nr = complist[listpos].nr - ncomp;
461 >        p->nc = complist[listpos].nc;
462 >        p->nr = complist[listpos].nr - p->nc;
463          if (p->nr <= 0)
464                  return(0);
465 <        if (p->nr > RPACKSIZ)
466 <                p->nr = RPACKSIZ;
467 <        ncomp += p->nr;                 /* find where this one would go */
468 <        while (lastin > listpos && complist[listpos].nr *
469 <                (bnrays(hdlist[complist[lastin].hd],complist[lastin].bi)+1)
470 <                        > complist[lastin].nr * (ncomp+1))
465 >        DCHECK(n < 1 | n > RPACKSIZ,
466 >                        CONSISTENCY, "next_packet called with bad n value");
467 >        if (p->nr > n)
468 >                p->nr = n;
469 >        complist[listpos].nc += p->nr;  /* find where this one would go */
470 >        if (hdgetbeam(hdlist[p->hd], p->bi) != NULL)
471 >                hdfreefrag(hdlist[p->hd], p->bi);
472 >        while (lastin > listpos &&
473 >                        beamcmp(complist+lastin, complist+listpos) > 0)
474                  lastin--;
475          listpos++;
476          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines