--- ray/src/hd/rholo3.c 1997/12/19 09:55:36 3.15 +++ ray/src/hd/rholo3.c 1998/07/08 17:59:58 3.22 @@ -1,9 +1,3 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - -#ifndef lint -static char SCCSid[] = "$SunId$ SGI"; -#endif - /* * Routines for tracking beam compuatations */ @@ -61,57 +55,68 @@ int op; register PACKHEAD *clist; int nents; { + int oldnr; + register HDBEAMI *hb; register int i, n; - + /* look for 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) { + oldnr = complist[i].nr; + clist[n].nc = complist[i].nc; + switch (op) { + case BS_ADD: /* add to count */ + complist[i].nr += clist[n].nr; + clist[n].nr = 0; + break; + case BS_ADJ: /* reset count */ + complist[i].nr = clist[n].nr; + clist[n].nr = 0; + break; + case BS_DEL: /* delete count */ + if (clist[n].nr == 0 || + clist[n].nr >= complist[i].nr) + complist[i].nr = 0; + else + complist[i].nr -= clist[n].nr; + break; + } + if (complist[i].nr != oldnr) + lastin = -1; /* flag sort */ + break; + } + if (i >= complen) + clist[n].nc = bnrays(hdlist[clist[n].hd], clist[n].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++) ; - if (op == BS_ADJ) - nents = n; + if (op == BS_ADJ) { /* don't regenerate adjusted beams */ + for (i = n; i < nents && clist[i].nr > 0; i++) + ; + nents = i; + } if (n) { /* allocate space for merged list */ PACKHEAD *newlist; newlist = (PACKHEAD *)malloc( @@ -129,44 +134,20 @@ 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) { /* load and display beams we have */ - register HDBEAMI *hb; - - hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); - for (i = 0; i < nents; i++) { - hb[i].h = hdlist[clist[i].hd]; - hb[i].b = clist[i].bi; - } - hdloadbeams(hb, nents, dispbeam); - free((char *)hb); + if (outdev == NULL) /* nothing to display? */ + return; + /* load and display beams we have */ + hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); + for (i = 0; i < nents; i++) { + hb[i].h = hdlist[clist[i].hd]; + hb[i].b = clist[i].bi; } - 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); - listpos = 0; - lastin = -1; /* flag for initial sort */ - } + hdloadbeams(hb, nents, dispbeam); + free((char *)hb); return; memerr: error(SYSTEM, "out of memory in bundle_set"); @@ -212,9 +193,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++) @@ -225,13 +208,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]; - if (frac < 0.) frac = -frac; + 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; } } @@ -242,16 +226,17 @@ init_global() /* initialize global ray computation * frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL)); while (k--) complist[k].nr = frac * complist[k].nr; - listpos = 0; lastin = -1; /* flag initial sort */ + listpos = 0; lastin = -1; /* perform initial sort */ + sortcomplist(); } 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; @@ -316,8 +301,9 @@ sortcomplist() /* fix our list order */ * 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; @@ -331,8 +317,12 @@ 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 */ while (lastin > listpos && beamcmp(complist+lastin, complist+listpos) > 0)