--- ray/src/hd/rholo3.c 1997/12/15 20:44:28 3.13 +++ ray/src/hd/rholo3.c 1999/01/07 22:04:49 3.27 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -10,81 +10,169 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rholo.h" +#ifndef NFRAG2CHUNK +#define NFRAG2CHUNK 4096 /* number of fragments to start chunking */ +#endif + +#ifndef abs #define abs(x) ((x) > 0 ? (x) : -(x)) +#endif +#ifndef sgn #define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) +#endif +#define rchunk(n) (((n)+(RPACKSIZ/2))/RPACKSIZ) + static PACKHEAD *complist=NULL; /* list of beams to compute */ static int complen=0; /* length of complist */ static int listpos=0; /* current list position for next_packet */ static int lastin= -1; /* last ordered position in list */ +static int chunky=0; /* clump beams together on disk */ int -beamcmp(b0, b1) /* comparison for descending compute order */ +beamcmp(b0, b1) /* comparison for compute order */ register PACKHEAD *b0, *b1; { - return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) ); + BEAMI *bip0, *bip1; + register long c; + /* first check desired quantities */ + if (chunky) + c = rchunk(b1->nr)*(rchunk(b0->nc)+1) - + rchunk(b0->nr)*(rchunk(b1->nc)+1); + else + c = b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1); + if (c) return(c); + /* only one file, so skip the following: */ +#if 0 + /* next, check file descriptors */ + c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd; + if (c) return(c); +#endif + /* finally, check file positions */ + bip0 = &hdlist[b0->hd]->bi[b0->bi]; + bip1 = &hdlist[b1->hd]->bi[b1->bi]; + /* put diskless beams last */ + if (!bip0->nrd) + return(bip1->nrd > 0); + if (!bip1->nrd) + return(-1); + c = bip0->fo - bip1->fo; + return(c < 0 ? -1 : c > 0); } +int +beamidcmp(b0, b1) /* comparison for beam searching */ +register PACKHEAD *b0, *b1; +{ + register int c = b0->hd - b1->hd; + + if (c) return(c); + return(b0->bi - b1->bi); +} + + +int +dispbeam(b, hb) /* display a holodeck beam */ +register BEAM *b; +register HDBEAMI *hb; +{ + static int n = 0; + static PACKHEAD *p = NULL; + + if (b == NULL) + return; + if (b->nrm > n) { /* (re)allocate packet holder */ + n = b->nrm; + if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n)); + else p = (PACKHEAD *)realloc((char *)p, packsiz(n)); + if (p == NULL) + error(SYSTEM, "out of memory in dispbeam"); + } + /* assign packet fields */ + bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL)); + p->nr = p->nc = b->nrm; + for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++) + if (hdlist[p->hd] == NULL) + error(CONSISTENCY, "unregistered holodeck in dispbeam"); + p->bi = hb->b; + disp_packet(p); /* display it */ +} + + bundle_set(op, clist, nents) /* bundle set operation */ int op; PACKHEAD *clist; int nents; { - BEAM *b; - PACKHEAD *p; - register int i, n; - + int oldnr, n; + HDBEAMI *hbarr; + register PACKHEAD *csm; + register int i; + /* search for common members */ + for (csm = clist+nents; csm-- > clist; ) + csm->nc = -1; + qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp); + for (i = 0; i < complen; i++) { + csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist, + nents, sizeof(PACKHEAD), beamidcmp); + if (csm == NULL) + continue; + oldnr = complist[i].nr; + csm->nc = complist[i].nc; + switch (op) { + case BS_ADD: /* add to count */ + complist[i].nr += csm->nr; + csm->nr = 0; + break; + case BS_ADJ: /* reset count */ + complist[i].nr = csm->nr; + csm->nr = 0; + break; + case BS_DEL: /* delete count */ + if (csm->nr == 0 || csm->nr >= complist[i].nr) + complist[i].nr = 0; + else + complist[i].nr -= csm->nr; + break; + } + if (complist[i].nr != oldnr) + lastin = -1; /* flag sort */ + } + /* record computed rays for uncommon beams */ + for (csm = clist+nents; csm-- > clist; ) + if (csm->nc < 0) + csm->nc = bnrays(hdlist[csm->hd], csm->bi); + /* complete list operations */ switch (op) { case BS_NEW: /* new computation set */ - if (complen) + listpos = 0; lastin = -1; + if (complen) /* free old list */ free((char *)complist); - if (nents <= 0) { - complist = NULL; - listpos = complen = 0; - lastin = -1; + complist = NULL; + if (!(complen = nents)) return; - } complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD)); if (complist == NULL) goto memerr; bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD)); - complen = nents; /* finish initialization below */ break; case BS_ADD: /* add to computation set */ case BS_ADJ: /* adjust set quantities */ if (nents <= 0) return; - /* merge any common members */ - for (n = 0; n < nents; n++) { - for (i = 0; i < complen; i++) - if (clist[n].bi == complist[i].bi && - clist[n].hd == complist[i].hd) { - int oldnr = complist[i].nr; - if (op == BS_ADD) - complist[i].nr += clist[n].nr; - else /* op == BS_ADJ */ - complist[i].nr = clist[n].nr; - clist[n].nr = 0; - clist[n].nc = complist[i].nc; - if (complist[i].nr != oldnr) - lastin = -1; /* flag sort */ - break; - } - if (i >= complen) - clist[n].nc = bnrays(hdlist[clist[n].hd], - clist[n].bi); - } - /* sort updated list */ - sortcomplist(); - /* sort new entries */ + sortcomplist(); /* sort updated list & new entries */ qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp); /* what can't we satisfy? */ - for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++) + for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++) ; - if (op == BS_ADJ) - nents = n; + n = csm - clist; + if (op == BS_ADJ) { /* don't regenerate adjusted beams */ + for (++i; i-- && csm->nr > 0; csm++) + ; + nents = csm - clist; + } if (n) { /* allocate space for merged list */ PACKHEAD *newlist; newlist = (PACKHEAD *)malloc( @@ -102,55 +190,23 @@ int nents; lastin = complen-1; /* list is now sorted */ break; case BS_DEL: /* delete from computation set */ - if (nents <= 0) - return; - /* find each member */ - for (i = 0; i < complen; i++) - for (n = 0; n < nents; n++) - if (clist[n].bi == complist[i].bi && - clist[n].hd == complist[i].hd) { - if (clist[n].nr == 0 || - clist[n].nr >= complist[i].nr) - complist[i].nr = 0; - else - complist[i].nr -= clist[n].nr; - lastin = -1; /* flag full sort */ - break; - } - return; /* no display */ + return; /* already done */ default: error(CONSISTENCY, "bundle_set called with unknown operation"); } - if (outdev == NULL) + if (outdev == NULL || !nents) /* nothing to display? */ return; - n = 32*RPACKSIZ; /* allocate packet holder */ - p = (PACKHEAD *)malloc(packsiz(n)); - if (p == NULL) - goto memerr; - /* display what we have */ - for (i = 0; i < nents; i++) - if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) { - if (b->nrm > n) { - n = b->nrm; - p = (PACKHEAD *)realloc((char *)p, packsiz(n)); - if (p == NULL) - goto memerr; - } - bcopy((char *)hdbray(b), (char *)packra(p), - b->nrm*sizeof(RAYVAL)); - p->hd = clist[i].hd; - p->bi = clist[i].bi; - p->nr = p->nc = b->nrm; - disp_packet(p); - } - free((char *)p); /* clean up */ - if (op == BS_NEW) { - done_packets(flush_queue()); /* empty queue, so we can... */ - for (i = 0; i < complen; i++) /* ...get number computed */ - complist[i].nc = bnrays(hdlist[complist[i].hd], - complist[i].bi); + /* load and display beams we have */ + hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); + for (i = nents; i--; ) { + hbarr[i].h = hdlist[clist[i].hd]; + hbarr[i].b = clist[i].bi; + } + hdloadbeams(hbarr, nents, dispbeam); + free((char *)hbarr); + if (hdfragflags&FF_READ) { listpos = 0; - lastin = -1; /* flag for initial sort */ + lastin = -1; /* need to re-sort list */ } return; memerr: @@ -176,10 +232,6 @@ int bi; VSUM(edgeA, cp[1], cp[0], -1.0); VSUM(edgeB, cp[3], cp[1], -1.0); fcross(crossp[i], edgeA, edgeB); - VSUM(edgeA, cp[2], cp[3], -1.0); - VSUM(edgeB, cp[0], cp[2], -1.0); - fcross(v, edgeA, edgeB); - VSUM(crossp[i], crossp[i], v, 1.0); /* compute center */ cent[i][0] = 0.5*(cp[0][0] + cp[2][0]); cent[i][1] = 0.5*(cp[0][1] + cp[2][1]); @@ -188,7 +240,7 @@ int bi; /* compute difference vector */ VSUM(diffv, cent[1], cent[0], -1.0); for (i = 0; i < 2; i++) { /* compute volume contributions */ - vol[i] = 0.25*DOT(crossp[i], diffv); + vol[i] = 0.5*DOT(crossp[i], diffv); if (vol[i] < 0.) vol[i] = -vol[i]; } return(vol[0] + vol[1]); /* return total volume */ @@ -201,9 +253,11 @@ init_global() /* initialize global ray computation * double frac; int i; register int j, k; - /* free old list */ - if (complen > 0) + /* free old list and empty queue */ + if (complen > 0) { free((char *)complist); + done_packets(flush_queue()); + } /* allocate beam list */ complen = 0; for (j = 0; hdlist[j] != NULL; j++) @@ -214,12 +268,14 @@ init_global() /* initialize global ray computation * /* compute beam weights */ k = 0; for (j = 0; hdlist[j] != NULL; j++) { - frac = 512. * hdlist[j]->wg[0] * - hdlist[j]->wg[1] * hdlist[j]->wg[2]; + frac = 512. * VLEN(hdlist[j]->wg[0]) * + VLEN(hdlist[j]->wg[1]) * + VLEN(hdlist[j]->wg[2]); for (i = nbeams(hdlist[j]); i > 0; i--) { complist[k].hd = j; complist[k].bi = i; complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5; + complist[k].nc = bnrays(hdlist[j], i); wtotal += complist[k++].nr; } } @@ -229,17 +285,20 @@ init_global() /* initialize global ray computation * else frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL)); while (k--) - complist[k].nr = frac * complist[k].nr; - listpos = 0; lastin = -1; /* flag initial sort */ + complist[k].nr = frac*complist[k].nr + 0.5; + listpos = 0; lastin = -1; /* perform initial sort */ + sortcomplist(); + /* no view vicinity */ + myeye.rng = 0; } mergeclists(cdest, cl1, n1, cl2, n2) /* merge two sorted lists */ -PACKHEAD *cdest; -PACKHEAD *cl1, *cl2; +register PACKHEAD *cdest; +register PACKHEAD *cl1, *cl2; int n1, n2; { - int cmp; + register int cmp; while (n1 | n2) { if (!n1) cmp = 1; @@ -260,10 +319,23 @@ int n1, n2; sortcomplist() /* fix our list order */ { PACKHEAD *list2; + int listlen; register int i; if (complen <= 0) /* check to see if there is even a list */ return; + if (!chunky) /* check to see if fragment list is full */ + if (!hdfragOK(hdlist[0]->fd, &listlen, NULL) +#if NFRAG2CHUNK + || listlen >= NFRAG2CHUNK +#endif + ) { +#ifdef DEBUG + error(WARNING, "using chunky comparison mode"); +#endif + chunky++; /* use "chunky" comparison */ + lastin = -1; /* need to re-sort list */ + } if (lastin < 0 || listpos*4 >= complen*3) qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); else if (listpos) { /* else sort and merge sublist */ @@ -286,10 +358,9 @@ sortcomplist() /* fix our list order */ } else if (i < complen-1) { list2 = (PACKHEAD *)realloc((char *)complist, (i+1)*sizeof(PACKHEAD)); - if (list2 != NULL) { + if (list2 != NULL) complist = list2; - complen = i+1; - } + complen = i+1; } listpos = 0; lastin = i; } @@ -300,12 +371,13 @@ sortcomplist() /* fix our list order */ * more or less evenly distributed, such that computing a packet causes * a given bundle to move way down in the computation order. We keep * track of where the computed bundle with the highest priority would end - * up, and if we get further in our compute list than this, we resort the + * up, and if we get further in our compute list than this, we re-sort the * list and start again from the beginning. Since * a merge sort is used, the sorting costs are minimal. */ -next_packet(p) /* prepare packet for computation */ +next_packet(p, n) /* prepare packet for computation */ register PACKET *p; +int n; { register int i; @@ -319,9 +391,15 @@ register PACKET *p; p->nr = complist[listpos].nr - p->nc; if (p->nr <= 0) return(0); - if (p->nr > RPACKSIZ) - p->nr = RPACKSIZ; +#ifdef DEBUG + if (n < 1 | n > RPACKSIZ) + error(CONSISTENCY, "next_packet called with bad n value"); +#endif + if (p->nr > n) + p->nr = n; complist[listpos].nc += p->nr; /* find where this one would go */ + if (hdgetbeam(hdlist[p->hd], p->bi) != NULL) + hdfreefrag(hdlist[p->hd], p->bi); while (lastin > listpos && beamcmp(complist+lastin, complist+listpos) > 0) lastin--;