14 |
|
/* Modifier contribution queue (results waiting to be output) */ |
15 |
|
typedef struct s_binq { |
16 |
|
int ndx; /* index for this entry */ |
17 |
< |
int n2add; /* number left to add */ |
17 |
> |
int nadded; /* accumulated so far */ |
18 |
|
struct s_binq *next; /* next in queue */ |
19 |
|
MODCONT *mca[1]; /* contrib. array (extends struct) */ |
20 |
|
} BINQ; |
26 |
|
static FILE *inq_fp[MAXPROCESS]; /* input streams */ |
27 |
|
|
28 |
|
|
29 |
< |
/* Get new (empty) bin queue entry */ |
29 |
> |
/* Get new bin queue entry */ |
30 |
|
static BINQ * |
31 |
|
new_binq() |
32 |
|
{ |
33 |
< |
BINQ *bp = free_bq; |
33 |
> |
BINQ *bp; |
34 |
|
int i; |
35 |
|
|
36 |
< |
if (bp != NULL) { /* something already available? */ |
36 |
> |
if (free_bq != NULL) { /* something already available? */ |
37 |
> |
bp = free_bq; |
38 |
|
free_bq = bp->next; |
39 |
|
bp->next = NULL; |
40 |
< |
bp->n2add = accumulate-1; |
40 |
> |
bp->nadded = 1; |
41 |
|
return(bp); |
42 |
|
} |
43 |
|
/* else allocate fresh */ |
44 |
< |
bp = (BINQ *)malloc(sizeof(BINQ)+(nmods-1)*sizeof(MODCONT *)); |
44 |
> |
bp = (BINQ *)malloc(sizeof(BINQ) + sizeof(MODCONT *)*(nmods-1)); |
45 |
|
if (bp == NULL) |
46 |
|
goto memerr; |
47 |
|
for (i = nmods; i--; ) { |
54 |
|
/* memset(bp->mca[i]->cbin, 0, sizeof(DCOLOR)*mp->nbins); */ |
55 |
|
} |
56 |
|
bp->ndx = 0; |
57 |
< |
bp->n2add = accumulate-1; |
57 |
> |
bp->nadded = 1; |
58 |
|
bp->next = NULL; |
59 |
|
return(bp); |
60 |
|
memerr: |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
< |
/* Sum one modifier record into another (doesn't update n2add) */ |
112 |
> |
/* Sum one modifier record into another (updates nadded) */ |
113 |
|
static void |
114 |
|
add_modbin(BINQ *dst, BINQ *src) |
115 |
|
{ |
121 |
|
for (j = mpout->nbins; j--; ) |
122 |
|
addcolor(mpout->cbin[j], mpin->cbin[j]); |
123 |
|
} |
124 |
+ |
dst->nadded += src->nadded; |
125 |
|
} |
126 |
|
|
127 |
|
|
145 |
|
for (b_cur = out_bq; b_cur != NULL && b_cur->ndx < bp->ndx; |
146 |
|
b_cur = b_cur->next) |
147 |
|
b_last = b_cur; |
148 |
+ |
|
149 |
|
if (b_last != NULL) { |
150 |
|
bp->next = b_cur; |
151 |
|
b_last->next = bp; |
157 |
|
return; |
158 |
|
b_cur = out_bq; /* else merge accumulation entries */ |
159 |
|
while (b_cur->next != NULL) { |
160 |
< |
if (b_cur->n2add <= 0 || |
160 |
> |
if (b_cur->nadded >= accumulate || |
161 |
|
(b_cur->ndx-1)/accumulate != |
162 |
|
(b_cur->next->ndx-1)/accumulate) { |
163 |
|
b_cur = b_cur->next; |
164 |
|
continue; |
165 |
|
} |
166 |
|
add_modbin(b_cur, b_cur->next); |
164 |
– |
b_cur->n2add--; |
167 |
|
b_last = b_cur->next; |
168 |
|
b_cur->next = b_last->next; |
169 |
|
b_last->next = NULL; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
< |
/* Get current with output queue by producing ready results */ |
175 |
> |
/* Count number of records ready for output */ |
176 |
|
static int |
177 |
< |
output_catchup() |
177 |
> |
queue_ready() |
178 |
|
{ |
179 |
+ |
int nready = 0; |
180 |
+ |
BINQ *bp; |
181 |
+ |
|
182 |
+ |
if (accumulate <= 0) /* just accumulating? */ |
183 |
+ |
return(0); |
184 |
+ |
|
185 |
+ |
for (bp = out_bq; bp != NULL && bp->nadded >= accumulate && |
186 |
+ |
bp->ndx == lastdone+nready*accumulate+1; |
187 |
+ |
bp = bp->next) |
188 |
+ |
++nready; |
189 |
+ |
|
190 |
+ |
return(nready); |
191 |
+ |
} |
192 |
+ |
|
193 |
+ |
|
194 |
+ |
/* Catch up with output queue by producing ready results */ |
195 |
+ |
static int |
196 |
+ |
output_catchup(int nmax) |
197 |
+ |
{ |
198 |
|
int nout = 0; |
199 |
|
BINQ *bp; |
200 |
|
int i; |
202 |
|
if (accumulate <= 0) /* just accumulating? */ |
203 |
|
return(0); |
204 |
|
/* else output ready results */ |
205 |
< |
while (out_bq != NULL && (out_bq->ndx == lastdone+1) & !out_bq->n2add) { |
205 |
> |
while (out_bq != NULL && out_bq->nadded >= accumulate |
206 |
> |
&& out_bq->ndx == lastdone+1) { |
207 |
> |
if ((nmax > 0) & (nout >= nmax)) |
208 |
> |
break; |
209 |
|
bp = out_bq; /* pop off first entry */ |
210 |
|
out_bq = bp->next; |
211 |
|
bp->next = NULL; |
231 |
|
memset(bp->mca[i]->cbin, 0, sizeof(DCOLOR)*bp->mca[i]->nbins); |
232 |
|
bp->ndx = ndx; |
233 |
|
queue_output(bp); |
234 |
< |
output_catchup(); |
234 |
> |
output_catchup(0); |
235 |
|
} |
236 |
|
|
237 |
|
|
264 |
|
pid = fork(); /* fork parent process */ |
265 |
|
if (pid == 0) { /* if in child, set up & return true */ |
266 |
|
close(p0[1]); close(p1[0]); |
267 |
+ |
lu_doall(&modconttab, set_stdout, NULL); |
268 |
+ |
lu_done(&ofiletab); |
269 |
+ |
while (nchild--) { |
270 |
+ |
close(kida[nchild].w); |
271 |
+ |
fclose(inq_fp[nchild]); |
272 |
+ |
} |
273 |
|
dup2(p0[0], 0); close(p0[0]); |
274 |
|
dup2(p1[1], 1); close(p1[1]); |
275 |
|
inpfmt = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
279 |
|
yres = 0; |
280 |
|
raysleft = 0; |
281 |
|
account = accumulate = 1; |
252 |
– |
lu_doall(&modconttab, set_stdout, NULL); |
253 |
– |
nchild = -1; |
282 |
|
return(1); /* child return value */ |
283 |
|
} |
284 |
|
if (pid < 0) |
326 |
|
next_child_nq(int force_wait) |
327 |
|
{ |
328 |
|
static struct timeval polling; |
329 |
< |
struct timeval *pmode = force_wait | (accumulate <= 0) ? |
302 |
< |
(struct timeval *)NULL : &polling; |
329 |
> |
struct timeval *pmode; |
330 |
|
fd_set readset, errset; |
331 |
< |
int i, j, n, nr; |
331 |
> |
int i, j, n, nr, nqr; |
332 |
|
|
333 |
|
if (!force_wait) /* see if there's one free */ |
334 |
|
for (i = nchild; i--; ) |
335 |
|
if (kida[i].running < 0) |
336 |
|
return(i); |
337 |
+ |
|
338 |
+ |
nqr = queue_ready(); /* wait mode or polling? */ |
339 |
+ |
if (!nqr | force_wait | (accumulate <= 0)) |
340 |
+ |
pmode = NULL; |
341 |
+ |
else |
342 |
+ |
pmode = &polling; |
343 |
+ |
tryagain: |
344 |
+ |
n = 0; /* catch up with output? */ |
345 |
+ |
if ((pmode == &polling) & (nqr > nchild)) |
346 |
+ |
n = nqr - nchild; |
347 |
+ |
if ((pmode == NULL) & (nqr > 0) | (n > 0)) |
348 |
+ |
nqr -= output_catchup(n); |
349 |
|
/* prepare select() call */ |
350 |
|
FD_ZERO(&readset); FD_ZERO(&errset); |
351 |
|
n = nr = 0; |
358 |
|
if (kida[i].r >= n) |
359 |
|
n = kida[i].r + 1; |
360 |
|
} |
322 |
– |
tryagain: |
323 |
– |
if (pmode == NULL) /* catch up in case we block */ |
324 |
– |
output_catchup(); |
361 |
|
if (!nr) /* nothing to wait for? */ |
362 |
|
return(-1); |
363 |
|
if ((nr > 1) | (pmode == &polling)) { |