| 3 |
|
#endif |
| 4 |
|
/* |
| 5 |
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
+ |
* |
| 7 |
+ |
* Multi-processing mode under Unix creates children that each work |
| 8 |
+ |
* on one input row at a time, fed by the single parent. Output to |
| 9 |
+ |
* a shared stdout is assured because each child waits for parent |
| 10 |
+ |
* to be ready with next input row before it outputs the previous result. |
| 11 |
+ |
* The assumption (checked in spawn_children()) is that the parent |
| 12 |
+ |
* will remain blocked until the child it is feeding has finished |
| 13 |
+ |
* flushing the previous row to stdout. The final output row is |
| 14 |
+ |
* less guaranteed to be in order, so the parent sleeps for a few seconds |
| 15 |
+ |
* between each child pipe closure, in hopes that this allows enough |
| 16 |
+ |
* time for row output to finish in each process. |
| 17 |
|
*/ |
| 18 |
|
|
| 19 |
|
#include <math.h> |
| 554 |
|
static int |
| 555 |
|
spawned_children(int np) |
| 556 |
|
{ |
| 557 |
< |
long inpwidth = 0; |
| 557 |
> |
size_t recsize = 0; |
| 558 |
|
int i, rv; |
| 559 |
|
|
| 560 |
|
#if defined(_WIN32) || defined(_WIN64) |
| 563 |
|
np = 1; |
| 564 |
|
} else |
| 565 |
|
#endif |
| 566 |
< |
if ((in_nrows > 0) & (np > in_nrows)) |
| 567 |
< |
np = in_nrows; |
| 566 |
> |
if ((in_nrows > 0) & (np*4 > in_nrows)) |
| 567 |
> |
np = in_nrows/4; |
| 568 |
|
/* we'll be doing a row at a time */ |
| 569 |
|
for (i = 0; i < nmats; i++) { |
| 570 |
|
mop[i].imx.nrows = 1; |
| 571 |
|
if (!rmx_prepare(&mop[i].imx)) |
| 572 |
|
goto memerror; |
| 573 |
< |
inpwidth += rmx_array_size(&mop[i].imx); |
| 573 |
> |
recsize += rmx_array_size(&mop[i].imx); |
| 574 |
|
if (mop[i].rmp != &mop[i].imx) { |
| 575 |
|
mop[i].rmp->nrows = 1; |
| 576 |
|
if (!rmx_prepare(mop[i].rmp)) |
| 577 |
|
goto memerror; |
| 567 |
– |
inpwidth += rmx_array_size(mop[i].rmp); |
| 578 |
|
} |
| 579 |
|
} |
| 580 |
|
/* prep output row buffer */ |
| 591 |
|
mop[nmats].imx.nrows = 1; |
| 592 |
|
if (!rmx_prepare(&mop[nmats].imx)) |
| 593 |
|
goto memerror; |
| 594 |
< |
if (np <= 1) { /* single process return point */ |
| 594 |
> |
if (np <= 1) { /* single process return */ |
| 595 |
|
#ifdef getc_unlocked |
| 596 |
|
for (i = 0; i < nmats; i++) |
| 597 |
|
flockfile(mop[i].infp); |
| 611 |
|
cproc[i].pid = -1; |
| 612 |
|
rv = open_process(&cproc[i], NULL); |
| 613 |
|
if (rv <= 0) break; |
| 614 |
< |
if (!i && 2*rv >= inpwidth) { |
| 614 |
> |
if (!i && 2*rv >= recsize) { |
| 615 |
|
fputs("Problem too small for multi-processing\n", |
| 616 |
|
stderr); |
| 617 |
+ |
close_processes(cproc, 1); |
| 618 |
|
exit(1); |
| 619 |
|
} |
| 620 |
|
} |
| 610 |
– |
if (rv > 0) |
| 611 |
– |
return(1); /* parent return value */ |
| 621 |
|
if (rv < 0) { |
| 622 |
|
perror("fork"); |
| 623 |
+ |
close_processes(cproc, i); |
| 624 |
|
exit(1); |
| 625 |
|
} |
| 626 |
+ |
if (rv > 0) /* parent return? */ |
| 627 |
+ |
return(1); |
| 628 |
|
inchild = i; /* our child index */ |
| 629 |
|
while (i-- > 0) /* don't share siblings' pipes */ |
| 630 |
|
close(cproc[i].w); |
| 637 |
|
fclose(mop[i].infp); /* ! pclose() */ |
| 638 |
|
mop[i].infp = stdin; |
| 639 |
|
mop[i].imx.dtype = DTdouble; |
| 640 |
+ |
mop[i].imx.pflags &= ~RMF_SWAPIN; |
| 641 |
|
} |
| 642 |
|
return(0); /* child return */ |
| 643 |
|
memerror: |
| 682 |
|
if (fflush(ofp) == EOF) |
| 683 |
|
return(0); |
| 684 |
|
} |
| 685 |
< |
for (i = 0; i < nchildren; i++) { |
| 686 |
< |
sleep(2); /* try to maintain order */ |
| 685 |
> |
for (i = 0; i < nchildren; i++) { /* maintain output order */ |
| 686 |
> |
sleep(1+(mop[nmats].rmp->ncols*mop[nmats].rmp->ncomp >> 15)); |
| 687 |
|
fclose(outfp[i]); |
| 688 |
|
} |
| 689 |
|
free(outfp); |