| 13 |
|
|
| 14 |
|
/* Modifier contribution queue (results waiting to be output) */ |
| 15 |
|
typedef struct s_binq { |
| 16 |
< |
int ndx; /* index for this entry */ |
| 17 |
< |
int nadded; /* accumulated so far */ |
| 16 |
> |
RNUMBER ndx; /* index for this entry */ |
| 17 |
> |
RNUMBER nadded; /* accumulated so far */ |
| 18 |
|
struct s_binq *next; /* next in queue */ |
| 19 |
|
MODCONT *mca[1]; /* contrib. array (extends struct) */ |
| 20 |
|
} BINQ; |
| 22 |
|
static BINQ *out_bq = NULL; /* output bin queue */ |
| 23 |
|
static BINQ *free_bq = NULL; /* free queue entries */ |
| 24 |
|
|
| 25 |
< |
static SUBPROC kida[MAXPROCESS]; /* child processes */ |
| 26 |
< |
static FILE *inq_fp[MAXPROCESS]; /* input streams */ |
| 25 |
> |
static struct { |
| 26 |
> |
RNUMBER r1; /* assigned ray starting index */ |
| 27 |
> |
SUBPROC pr; /* PID, i/o descriptors */ |
| 28 |
> |
FILE *infp; /* file pointer to read from process */ |
| 29 |
> |
int nr; /* number rays to sum (0 if free) */ |
| 30 |
> |
} kida[MAXPROCESS]; /* our child processes */ |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
/* Get new bin queue entry */ |
| 41 |
|
bp = free_bq; |
| 42 |
|
free_bq = bp->next; |
| 43 |
|
bp->next = NULL; |
| 44 |
< |
bp->nadded = 1; |
| 44 |
> |
bp->nadded = 0; |
| 45 |
|
return(bp); |
| 46 |
|
} |
| 47 |
|
/* else allocate fresh */ |
| 58 |
|
/* memset(bp->mca[i]->cbin, 0, sizeof(DCOLOR)*mp->nbins); */ |
| 59 |
|
} |
| 60 |
|
bp->ndx = 0; |
| 61 |
< |
bp->nadded = 1; |
| 61 |
> |
bp->nadded = 0; |
| 62 |
|
bp->next = NULL; |
| 63 |
|
return(bp); |
| 64 |
|
memerr: |
| 235 |
|
for (i = nmods; i--; ) |
| 236 |
|
memset(bp->mca[i]->cbin, 0, sizeof(DCOLOR)*bp->mca[i]->nbins); |
| 237 |
|
bp->ndx = ndx; |
| 238 |
+ |
bp->nadded = 1; |
| 239 |
|
queue_output(bp); |
| 240 |
|
output_catchup(0); |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
|
| 244 |
+ |
/* Get results from child process and add to queue */ |
| 245 |
+ |
static void |
| 246 |
+ |
queue_results(int k) |
| 247 |
+ |
{ |
| 248 |
+ |
BINQ *bq = new_binq(); /* get results holder */ |
| 249 |
+ |
int j; |
| 250 |
+ |
|
| 251 |
+ |
bq->ndx = kida[k].r1; |
| 252 |
+ |
bq->nadded = kida[k].nr; |
| 253 |
+ |
/* read from child */ |
| 254 |
+ |
for (j = 0; j < nmods; j++) |
| 255 |
+ |
if (fread(bq->mca[j]->cbin, sizeof(DCOLOR), bq->mca[j]->nbins, |
| 256 |
+ |
kida[k].infp) != bq->mca[j]->nbins) |
| 257 |
+ |
error(SYSTEM, "read error from render process"); |
| 258 |
+ |
|
| 259 |
+ |
queue_output(bq); /* put results in output queue */ |
| 260 |
+ |
kida[k].nr = 0; /* mark child as available */ |
| 261 |
+ |
} |
| 262 |
+ |
|
| 263 |
+ |
|
| 264 |
|
/* callback to set output spec to NULL (stdout) */ |
| 265 |
|
static int |
| 266 |
|
set_stdout(const LUENT *le, void *p) |
| 293 |
|
lu_doall(&modconttab, set_stdout, NULL); |
| 294 |
|
lu_done(&ofiletab); |
| 295 |
|
while (nchild--) { /* don't share other pipes */ |
| 296 |
< |
close(kida[nchild].w); |
| 297 |
< |
fclose(inq_fp[nchild]); |
| 296 |
> |
close(kida[nchild].pr.w); |
| 297 |
> |
fclose(kida[nchild].infp); |
| 298 |
|
} |
| 299 |
|
dup2(p0[0], 0); close(p0[0]); |
| 300 |
|
dup2(p1[1], 1); close(p1[1]); |
| 301 |
|
inpfmt = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
| 302 |
|
outfmt = 'd'; |
| 303 |
|
header = 0; |
| 279 |
– |
waitflush = xres = 1; |
| 304 |
|
yres = 0; |
| 305 |
|
raysleft = 0; |
| 306 |
< |
account = accumulate = 1; |
| 306 |
> |
if (accumulate == 1) { |
| 307 |
> |
waitflush = xres = 1; |
| 308 |
> |
account = accumulate = 1; |
| 309 |
> |
} else { /* parent controls accumulation */ |
| 310 |
> |
waitflush = xres = 0; |
| 311 |
> |
account = accumulate = 0; |
| 312 |
> |
} |
| 313 |
|
return(1); /* child return value */ |
| 314 |
|
} |
| 315 |
|
if (pid < 0) |
| 316 |
|
error(SYSTEM, "fork() call failed!"); |
| 317 |
|
/* connect parent's pipes */ |
| 318 |
|
close(p0[0]); close(p1[1]); |
| 319 |
< |
kida[nchild].r = p1[0]; |
| 320 |
< |
kida[nchild].w = p0[1]; |
| 321 |
< |
kida[nchild].pid = pid; |
| 322 |
< |
kida[nchild].running = -1; |
| 323 |
< |
inq_fp[nchild] = fdopen(p1[0], "rb"); |
| 324 |
< |
if (inq_fp[nchild] == NULL) |
| 319 |
> |
kida[nchild].pr.r = p1[0]; |
| 320 |
> |
kida[nchild].pr.w = p0[1]; |
| 321 |
> |
kida[nchild].pr.pid = pid; |
| 322 |
> |
kida[nchild].pr.running = 1; |
| 323 |
> |
kida[nchild].infp = fdopen(p1[0], "rb"); |
| 324 |
> |
if (kida[nchild].infp == NULL) |
| 325 |
|
error(SYSTEM, "out of memory in in_rchild()"); |
| 326 |
|
#ifdef getc_unlocked |
| 327 |
< |
flockfile(inq_fp[nchild]); /* avoid mutex overhead */ |
| 327 |
> |
flockfile(kida[nchild].infp); /* avoid mutex overhead */ |
| 328 |
|
#endif |
| 329 |
< |
++nchild; |
| 329 |
> |
kida[nchild++].nr = 0; /* mark as available */ |
| 330 |
|
} |
| 331 |
|
return(0); /* parent return value */ |
| 332 |
|
#endif |
| 341 |
|
|
| 342 |
|
while (nchild > 0) { |
| 343 |
|
nchild--; |
| 344 |
< |
kida[nchild].r = -1; /* close(-1) error is ignored */ |
| 345 |
< |
if ((status = close_process(&kida[nchild])) > 0) { |
| 344 |
> |
fclose(kida[nchild].infp); |
| 345 |
> |
kida[nchild].pr.r = -1; /* close(-1) error is ignored */ |
| 346 |
> |
if ((status = close_process(&kida[nchild].pr)) > 0) { |
| 347 |
|
sprintf(errmsg, |
| 348 |
|
"rendering process returned bad status (%d)", |
| 349 |
|
status); |
| 350 |
|
error(WARNING, errmsg); |
| 351 |
|
} |
| 321 |
– |
fclose(inq_fp[nchild]); /* performs actual close() */ |
| 352 |
|
} |
| 353 |
|
} |
| 354 |
|
|
| 360 |
|
static struct timeval polling; |
| 361 |
|
struct timeval *pmode; |
| 362 |
|
fd_set readset, errset; |
| 363 |
< |
int i, j, n, nr, nqr; |
| 363 |
> |
int i, n, nr, nqr; |
| 364 |
|
|
| 365 |
|
if (!flushing) /* see if there's one free */ |
| 366 |
|
for (i = nchild; i--; ) |
| 367 |
< |
if (kida[i].running < 0) |
| 367 |
> |
if (!kida[i].nr) |
| 368 |
|
return(i); |
| 369 |
|
|
| 370 |
|
nqr = queue_ready(); /* choose blocking mode or polling */ |
| 382 |
|
FD_ZERO(&readset); FD_ZERO(&errset); |
| 383 |
|
n = nr = 0; |
| 384 |
|
for (i = nchild; i--; ) { |
| 385 |
< |
if (kida[i].running > 0) { |
| 386 |
< |
FD_SET(kida[i].r, &readset); |
| 385 |
> |
if (kida[i].nr) { |
| 386 |
> |
FD_SET(kida[i].pr.r, &readset); |
| 387 |
|
++nr; |
| 388 |
|
} |
| 389 |
< |
FD_SET(kida[i].r, &errset); |
| 390 |
< |
if (kida[i].r >= n) |
| 391 |
< |
n = kida[i].r + 1; |
| 389 |
> |
FD_SET(kida[i].pr.r, &errset); |
| 390 |
> |
if (kida[i].pr.r >= n) |
| 391 |
> |
n = kida[i].pr.r + 1; |
| 392 |
|
} |
| 393 |
|
if (!nr) /* nothing to wait for? */ |
| 394 |
|
return(-1); |
| 405 |
|
FD_ZERO(&errset); |
| 406 |
|
n = -1; /* read results from child(ren) */ |
| 407 |
|
for (i = nchild; i--; ) { |
| 408 |
< |
BINQ *bq; |
| 379 |
< |
if (FD_ISSET(kida[i].r, &errset)) |
| 408 |
> |
if (FD_ISSET(kida[i].pr.r, &errset)) |
| 409 |
|
error(USER, "rendering process died"); |
| 410 |
< |
if (!FD_ISSET(kida[i].r, &readset)) |
| 411 |
< |
continue; |
| 383 |
< |
bq = new_binq(); /* get results holder */ |
| 384 |
< |
bq->ndx = kida[i].running; |
| 385 |
< |
/* read from child */ |
| 386 |
< |
for (j = 0; j < nmods; j++) { |
| 387 |
< |
nr = bq->mca[j]->nbins; |
| 388 |
< |
if (fread(bq->mca[j]->cbin, sizeof(DCOLOR), nr, |
| 389 |
< |
inq_fp[i]) != nr) |
| 390 |
< |
error(SYSTEM, "read error from render process"); |
| 391 |
< |
} |
| 392 |
< |
queue_output(bq); /* add results to output queue */ |
| 393 |
< |
kida[i].running = -1; /* mark child as available */ |
| 394 |
< |
n = i; |
| 410 |
> |
if (FD_ISSET(kida[i].pr.r, &readset)) |
| 411 |
> |
queue_results(n = i); |
| 412 |
|
} |
| 413 |
|
return(n); /* first available child */ |
| 414 |
|
} |
| 418 |
|
void |
| 419 |
|
parental_loop() |
| 420 |
|
{ |
| 421 |
+ |
#define MAXIQ (int)(PIPE_BUF/(sizeof(FVECT)*2)) |
| 422 |
|
static int ignore_warning_given = 0; |
| 423 |
< |
FVECT orgdir[2]; |
| 423 |
> |
int qlimit = (accumulate == 1) ? 1 : MAXIQ-1; |
| 424 |
> |
int ninq = 0; |
| 425 |
> |
FVECT orgdir[2*MAXIQ]; |
| 426 |
|
double d; |
| 427 |
< |
int i; |
| 427 |
> |
int i, n; |
| 428 |
|
/* load rays from stdin & process */ |
| 429 |
|
#ifdef getc_unlocked |
| 430 |
|
flockfile(stdin); /* avoid lock/unlock overhead */ |
| 431 |
|
#endif |
| 432 |
< |
while (getvec(orgdir[0]) == 0 && getvec(orgdir[1]) == 0) { |
| 432 |
> |
while (getvec(orgdir[2*ninq]) == 0 && getvec(orgdir[2*ninq+1]) == 0) { |
| 433 |
|
d = normalize(orgdir[1]); |
| 434 |
< |
/* asking for flush? */ |
| 435 |
< |
if ((d == 0.0) & (accumulate != 1)) { |
| 436 |
< |
if (!ignore_warning_given++) |
| 437 |
< |
error(WARNING, |
| 434 |
> |
if (d == 0.0) { /* asking for flush? */ |
| 435 |
> |
if (accumulate != 1) { |
| 436 |
> |
if (!ignore_warning_given++) |
| 437 |
> |
error(WARNING, |
| 438 |
|
"dummy ray(s) ignored during accumulation\n"); |
| 439 |
< |
continue; |
| 440 |
< |
} |
| 421 |
< |
if ((d == 0.0) | (lastray+1 < lastray)) { |
| 439 |
> |
continue; |
| 440 |
> |
} |
| 441 |
|
while (next_child_nq(1) >= 0) |
| 442 |
|
; /* clear the queue */ |
| 443 |
|
lastdone = lastray = 0; |
| 425 |
– |
} |
| 426 |
– |
if (d == 0.0) { |
| 444 |
|
if ((yres <= 0) | (xres <= 0)) |
| 445 |
|
waitflush = 1; /* flush next */ |
| 446 |
|
put_zero_record(++lastray); |
| 447 |
< |
} else { /* else assign ray */ |
| 447 |
> |
} else if (++ninq >= qlimit || accumulate > 1 && |
| 448 |
> |
lastray/accumulate != (lastray+ninq)/accumulate) { |
| 449 |
|
i = next_child_nq(0); /* manages output */ |
| 450 |
< |
if (writebuf(kida[i].w, (char *)orgdir, |
| 451 |
< |
sizeof(orgdir)) != sizeof(orgdir)) |
| 450 |
> |
n = ninq; |
| 451 |
> |
if (accumulate != 1) /* request flush? */ |
| 452 |
> |
memset(orgdir[2*n++], 0, sizeof(FVECT)*2); |
| 453 |
> |
n *= sizeof(FVECT)*2; /* send assignment */ |
| 454 |
> |
if (writebuf(kida[i].pr.w, (char *)orgdir, n) != n) |
| 455 |
|
error(SYSTEM, "pipe write error"); |
| 456 |
< |
kida[i].running = ++lastray; /* busy now */ |
| 456 |
> |
kida[i].r1 = lastray+1; |
| 457 |
> |
lastray += kida[i].nr = ninq; /* mark as busy */ |
| 458 |
> |
ninq = 0; |
| 459 |
> |
if (lastray < lastdone) { /* RNUMBER wrapped? */ |
| 460 |
> |
while (next_child_nq(1) >= 0) |
| 461 |
> |
; |
| 462 |
> |
lastdone = lastray = 0; |
| 463 |
> |
} |
| 464 |
|
} |
| 465 |
|
if (raysleft && !--raysleft) |
| 466 |
|
break; /* preemptive EOI */ |
| 484 |
|
error(USER, "unexpected EOF on input"); |
| 485 |
|
free_binq(NULL); /* clean up */ |
| 486 |
|
lu_done(&ofiletab); |
| 487 |
+ |
#undef MAXIQ |
| 488 |
|
} |