1 |
+ |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
+ |
|
3 |
+ |
#ifndef lint |
4 |
+ |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
+ |
#endif |
6 |
+ |
|
7 |
|
/* |
8 |
|
* Routines for tracking beam compuatations |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "rholo.h" |
12 |
|
|
13 |
+ |
#ifndef NFRAG2CHUNK |
14 |
+ |
#define NFRAG2CHUNK 4096 /* number of fragments to start chunking */ |
15 |
+ |
#endif |
16 |
+ |
|
17 |
+ |
#ifndef abs |
18 |
|
#define abs(x) ((x) > 0 ? (x) : -(x)) |
19 |
+ |
#endif |
20 |
+ |
#ifndef sgn |
21 |
|
#define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) |
22 |
+ |
#endif |
23 |
|
|
24 |
+ |
#define rchunk(n) (((n)+(RPACKSIZ/2))/RPACKSIZ) |
25 |
+ |
|
26 |
|
static PACKHEAD *complist=NULL; /* list of beams to compute */ |
27 |
|
static int complen=0; /* length of complist */ |
28 |
|
static int listpos=0; /* current list position for next_packet */ |
29 |
|
static int lastin= -1; /* last ordered position in list */ |
30 |
+ |
static int chunky=0; /* clump beams together on disk */ |
31 |
|
|
32 |
|
|
33 |
|
int |
34 |
< |
beamcmp(b0, b1) /* comparison for descending compute order */ |
34 |
> |
beamcmp(b0, b1) /* comparison for compute order */ |
35 |
|
register PACKHEAD *b0, *b1; |
36 |
|
{ |
37 |
< |
return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) ); |
37 |
> |
BEAMI *bip0, *bip1; |
38 |
> |
register long c; |
39 |
> |
/* first check desired quantities */ |
40 |
> |
if (chunky) |
41 |
> |
c = rchunk(b1->nr)*(rchunk(b0->nc)+1) - |
42 |
> |
rchunk(b0->nr)*(rchunk(b1->nc)+1); |
43 |
> |
else |
44 |
> |
c = b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1); |
45 |
> |
if (c) return(c); |
46 |
> |
/* only one file, so skip the following: */ |
47 |
> |
#if 0 |
48 |
> |
/* next, check file descriptors */ |
49 |
> |
c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd; |
50 |
> |
if (c) return(c); |
51 |
> |
#endif |
52 |
> |
/* finally, check file positions */ |
53 |
> |
bip0 = &hdlist[b0->hd]->bi[b0->bi]; |
54 |
> |
bip1 = &hdlist[b1->hd]->bi[b1->bi]; |
55 |
> |
/* put diskless beams last */ |
56 |
> |
if (!bip0->nrd) |
57 |
> |
return(bip1->nrd > 0); |
58 |
> |
if (!bip1->nrd) |
59 |
> |
return(-1); |
60 |
> |
c = bip0->fo - bip1->fo; |
61 |
> |
return(c < 0 ? -1 : c > 0); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
int |
66 |
< |
dispbeam(b, hp, bi) /* display a holodeck beam */ |
66 |
> |
beamidcmp(b0, b1) /* comparison for beam searching */ |
67 |
> |
register PACKHEAD *b0, *b1; |
68 |
> |
{ |
69 |
> |
register int c = b0->hd - b1->hd; |
70 |
> |
|
71 |
> |
if (c) return(c); |
72 |
> |
return(b0->bi - b1->bi); |
73 |
> |
} |
74 |
> |
|
75 |
> |
|
76 |
> |
int |
77 |
> |
dispbeam(b, hb) /* display a holodeck beam */ |
78 |
|
register BEAM *b; |
79 |
< |
HOLO *hp; |
28 |
< |
int bi; |
79 |
> |
register HDBEAMI *hb; |
80 |
|
{ |
81 |
|
static int n = 0; |
82 |
|
static PACKHEAD *p = NULL; |
93 |
|
/* assign packet fields */ |
94 |
|
bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL)); |
95 |
|
p->nr = p->nc = b->nrm; |
96 |
< |
for (p->hd = 0; hdlist[p->hd] != hp; p->hd++) |
96 |
> |
for (p->hd = 0; hdlist[p->hd] != hb->h; p->hd++) |
97 |
|
if (hdlist[p->hd] == NULL) |
98 |
|
error(CONSISTENCY, "unregistered holodeck in dispbeam"); |
99 |
< |
p->bi = bi; |
99 |
> |
p->bi = hb->b; |
100 |
|
disp_packet(p); /* display it */ |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
bundle_set(op, clist, nents) /* bundle set operation */ |
105 |
|
int op; |
106 |
< |
register PACKHEAD *clist; |
106 |
> |
PACKHEAD *clist; |
107 |
|
int nents; |
108 |
|
{ |
109 |
< |
int oldnr; |
110 |
< |
register HDBEAMI *hb; |
111 |
< |
register int i, n; |
112 |
< |
/* look for common members */ |
113 |
< |
for (n = 0; n < nents; n++) { |
114 |
< |
for (i = 0; i < complen; i++) |
115 |
< |
if (clist[n].bi == complist[i].bi && |
116 |
< |
clist[n].hd == complist[i].hd) { |
117 |
< |
oldnr = complist[i].nr; |
118 |
< |
clist[n].nc = complist[i].nc; |
119 |
< |
switch (op) { |
120 |
< |
case BS_ADD: /* add to count */ |
121 |
< |
complist[i].nr += clist[n].nr; |
122 |
< |
clist[n].nr = 0; |
123 |
< |
break; |
124 |
< |
case BS_ADJ: /* reset count */ |
125 |
< |
complist[i].nr = clist[n].nr; |
126 |
< |
clist[n].nr = 0; |
127 |
< |
break; |
128 |
< |
case BS_DEL: /* delete count */ |
129 |
< |
if (clist[n].nr == 0 || |
130 |
< |
clist[n].nr >= complist[i].nr) |
131 |
< |
complist[i].nr = 0; |
132 |
< |
else |
133 |
< |
complist[i].nr -= clist[n].nr; |
134 |
< |
break; |
135 |
< |
} |
136 |
< |
if (complist[i].nr != oldnr) |
137 |
< |
lastin = -1; /* flag sort */ |
138 |
< |
break; |
139 |
< |
} |
140 |
< |
if (i >= complen) |
141 |
< |
clist[n].nc = bnrays(hdlist[clist[n].hd], clist[n].bi); |
109 |
> |
int oldnr, n; |
110 |
> |
HDBEAMI *hbarr; |
111 |
> |
register PACKHEAD *csm; |
112 |
> |
register int i; |
113 |
> |
/* search for common members */ |
114 |
> |
for (csm = clist+nents; csm-- > clist; ) |
115 |
> |
csm->nc = -1; |
116 |
> |
qsort((char *)clist, nents, sizeof(PACKHEAD), beamidcmp); |
117 |
> |
for (i = 0; i < complen; i++) { |
118 |
> |
csm = (PACKHEAD *)bsearch((char *)(complist+i), (char *)clist, |
119 |
> |
nents, sizeof(PACKHEAD), beamidcmp); |
120 |
> |
if (csm == NULL) |
121 |
> |
continue; |
122 |
> |
oldnr = complist[i].nr; |
123 |
> |
csm->nc = complist[i].nc; |
124 |
> |
switch (op) { |
125 |
> |
case BS_ADD: /* add to count */ |
126 |
> |
complist[i].nr += csm->nr; |
127 |
> |
csm->nr = 0; |
128 |
> |
break; |
129 |
> |
case BS_ADJ: /* reset count */ |
130 |
> |
complist[i].nr = csm->nr; |
131 |
> |
csm->nr = 0; |
132 |
> |
break; |
133 |
> |
case BS_DEL: /* delete count */ |
134 |
> |
if (csm->nr == 0 || csm->nr >= complist[i].nr) |
135 |
> |
complist[i].nr = 0; |
136 |
> |
else |
137 |
> |
complist[i].nr -= csm->nr; |
138 |
> |
break; |
139 |
> |
} |
140 |
> |
if (complist[i].nr != oldnr) |
141 |
> |
lastin = -1; /* flag sort */ |
142 |
|
} |
143 |
+ |
/* record computed rays for uncommon beams */ |
144 |
+ |
for (csm = clist+nents; csm-- > clist; ) |
145 |
+ |
if (csm->nc < 0) |
146 |
+ |
csm->nc = bnrays(hdlist[csm->hd], csm->bi); |
147 |
|
/* complete list operations */ |
148 |
|
switch (op) { |
149 |
|
case BS_NEW: /* new computation set */ |
165 |
|
sortcomplist(); /* sort updated list & new entries */ |
166 |
|
qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp); |
167 |
|
/* what can't we satisfy? */ |
168 |
< |
for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++) |
168 |
> |
for (i = nents, csm = clist; i-- && csm->nr > csm->nc; csm++) |
169 |
|
; |
170 |
+ |
n = csm - clist; |
171 |
|
if (op == BS_ADJ) { /* don't regenerate adjusted beams */ |
172 |
< |
for (i = n; i < nents && clist[i].nr > 0; i++) |
172 |
> |
for (++i; i-- && csm->nr > 0; csm++) |
173 |
|
; |
174 |
< |
nents = i; |
174 |
> |
nents = csm - clist; |
175 |
|
} |
176 |
|
if (n) { /* allocate space for merged list */ |
177 |
|
PACKHEAD *newlist; |
194 |
|
default: |
195 |
|
error(CONSISTENCY, "bundle_set called with unknown operation"); |
196 |
|
} |
197 |
< |
if (outdev == NULL) /* nothing to display? */ |
197 |
> |
if (outdev == NULL || !nents) /* nothing to display? */ |
198 |
|
return; |
199 |
|
/* load and display beams we have */ |
200 |
< |
hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
201 |
< |
for (i = 0; i < nents; i++) { |
202 |
< |
hb[i].h = hdlist[clist[i].hd]; |
203 |
< |
hb[i].b = clist[i].bi; |
200 |
> |
hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
201 |
> |
for (i = nents; i--; ) { |
202 |
> |
hbarr[i].h = hdlist[clist[i].hd]; |
203 |
> |
hbarr[i].b = clist[i].bi; |
204 |
|
} |
205 |
< |
hdloadbeams(hb, nents, dispbeam); |
206 |
< |
free((char *)hb); |
205 |
> |
hdloadbeams(hbarr, nents, dispbeam); |
206 |
> |
free((char *)hbarr); |
207 |
> |
if (hdfragflags&FF_READ) { |
208 |
> |
listpos = 0; |
209 |
> |
lastin = -1; /* need to re-sort list */ |
210 |
> |
} |
211 |
|
return; |
212 |
|
memerr: |
213 |
|
error(SYSTEM, "out of memory in bundle_set"); |
285 |
|
else |
286 |
|
frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL)); |
287 |
|
while (k--) |
288 |
< |
complist[k].nr = frac * complist[k].nr; |
288 |
> |
complist[k].nr = frac*complist[k].nr + 0.5; |
289 |
|
listpos = 0; lastin = -1; /* perform initial sort */ |
290 |
|
sortcomplist(); |
291 |
+ |
/* no view vicinity */ |
292 |
+ |
myeye.rng = 0; |
293 |
|
} |
294 |
|
|
295 |
|
|
319 |
|
sortcomplist() /* fix our list order */ |
320 |
|
{ |
321 |
|
PACKHEAD *list2; |
322 |
+ |
int listlen; |
323 |
|
register int i; |
324 |
|
|
325 |
|
if (complen <= 0) /* check to see if there is even a list */ |
326 |
|
return; |
327 |
+ |
if (!chunky) /* check to see if fragment list is full */ |
328 |
+ |
if (!hdfragOK(hdlist[0]->fd, &listlen, NULL) |
329 |
+ |
#if NFRAG2CHUNK |
330 |
+ |
|| listlen >= NFRAG2CHUNK |
331 |
+ |
#endif |
332 |
+ |
) { |
333 |
+ |
#ifdef DEBUG |
334 |
+ |
error(WARNING, "using chunky comparison mode"); |
335 |
+ |
#endif |
336 |
+ |
chunky++; /* use "chunky" comparison */ |
337 |
+ |
lastin = -1; /* need to re-sort list */ |
338 |
+ |
} |
339 |
|
if (lastin < 0 || listpos*4 >= complen*3) |
340 |
|
qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); |
341 |
|
else if (listpos) { /* else sort and merge sublist */ |
358 |
|
} else if (i < complen-1) { |
359 |
|
list2 = (PACKHEAD *)realloc((char *)complist, |
360 |
|
(i+1)*sizeof(PACKHEAD)); |
361 |
< |
if (list2 != NULL) { |
361 |
> |
if (list2 != NULL) |
362 |
|
complist = list2; |
363 |
< |
complen = i+1; |
289 |
< |
} |
363 |
> |
complen = i+1; |
364 |
|
} |
365 |
|
listpos = 0; lastin = i; |
366 |
|
} |
371 |
|
* more or less evenly distributed, such that computing a packet causes |
372 |
|
* a given bundle to move way down in the computation order. We keep |
373 |
|
* track of where the computed bundle with the highest priority would end |
374 |
< |
* up, and if we get further in our compute list than this, we resort the |
374 |
> |
* up, and if we get further in our compute list than this, we re-sort the |
375 |
|
* list and start again from the beginning. Since |
376 |
|
* a merge sort is used, the sorting costs are minimal. |
377 |
|
*/ |
398 |
|
if (p->nr > n) |
399 |
|
p->nr = n; |
400 |
|
complist[listpos].nc += p->nr; /* find where this one would go */ |
401 |
+ |
if (hdgetbeam(hdlist[p->hd], p->bi) != NULL) |
402 |
+ |
hdfreefrag(hdlist[p->hd], p->bi); |
403 |
|
while (lastin > listpos && |
404 |
|
beamcmp(complist+lastin, complist+listpos) > 0) |
405 |
|
lastin--; |