543 |
|
static int |
544 |
|
spawned_children(int np) |
545 |
|
{ |
546 |
+ |
long inpwidth = 0; |
547 |
|
int i, rv; |
548 |
|
|
549 |
|
#if defined(_WIN32) || defined(_WIN64) |
559 |
|
mop[i].imx.nrows = 1; |
560 |
|
if (!rmx_prepare(&mop[i].imx)) |
561 |
|
goto memerror; |
562 |
+ |
inpwidth += rmx_array_size(&mop[i].imx); |
563 |
|
if (mop[i].rmp != &mop[i].imx) { |
564 |
|
mop[i].rmp->nrows = 1; |
565 |
|
if (!rmx_prepare(mop[i].rmp)) |
566 |
|
goto memerror; |
567 |
+ |
inpwidth += rmx_array_size(mop[i].rmp); |
568 |
|
} |
569 |
|
} |
570 |
|
/* prep output row buffer */ |
601 |
|
cproc[i].pid = -1; |
602 |
|
rv = open_process(&cproc[i], NULL); |
603 |
|
if (rv <= 0) break; |
604 |
+ |
if (!i && 2*rv >= inpwidth) { |
605 |
+ |
fputs("Problem too small for multi-processing\n", |
606 |
+ |
stderr); |
607 |
+ |
exit(1); |
608 |
+ |
} |
609 |
|
} |
610 |
|
if (rv > 0) |
611 |
|
return(1); /* parent return value */ |
613 |
|
perror("fork"); |
614 |
|
exit(1); |
615 |
|
} |
616 |
< |
inchild = i; /* child index */ |
616 |
> |
inchild = i; /* our child index */ |
617 |
> |
while (i-- > 0) /* don't share siblings' pipes */ |
618 |
> |
close(cproc[i].w); |
619 |
|
fpurge(stdin); /* discard previous matrix input */ |
620 |
|
#ifdef getc_unlocked |
621 |
|
flockfile(stdin); |
622 |
|
#endif |
623 |
|
for (i = 0; i < nmats; i++) { |
624 |
|
if (mop[i].infp != stdin) |
625 |
< |
fclose(mop[i].infp); |
625 |
> |
fclose(mop[i].infp); /* ! pclose() */ |
626 |
|
mop[i].infp = stdin; |
627 |
|
mop[i].imx.dtype = DTdouble; |
628 |
|
} |
652 |
|
#endif |
653 |
|
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
654 |
|
FILE *ofp = outfp[cur_row % nchildren]; |
655 |
< |
for (i = 0; i < nmats; i++) { |
655 |
> |
for (i = 0; i < nmats; i++) |
656 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
657 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
658 |
|
break; |
660 |
|
mop[i].inspec, cur_row); |
661 |
|
return(0); |
662 |
|
} |
653 |
– |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
654 |
– |
mop[i].imx.ncols, DTdouble, ofp)) |
655 |
– |
return(0); |
656 |
– |
} |
663 |
|
if (i < nmats) |
664 |
|
break; |
665 |
+ |
for (i = 0; i < nmats; i++) |
666 |
+ |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
667 |
+ |
mop[i].imx.ncols, DTdouble, ofp)) |
668 |
+ |
return(0); |
669 |
|
if (fflush(ofp) == EOF) |
670 |
|
return(0); |
671 |
|
} |
672 |
< |
for (i = 0; i < nchildren; i++) |
672 |
> |
for (i = 0; i < nchildren; i++) { |
673 |
> |
sleep(2); /* try to maintain order */ |
674 |
|
fclose(outfp[i]); |
675 |
+ |
} |
676 |
|
free(outfp); |
677 |
|
i = close_processes(cproc, nchildren); |
678 |
|
free(cproc); cproc = NULL; |
718 |
|
set_c = varlookup("c") != NULL && !vardefined("c"); |
719 |
|
} else /* save a little time */ |
720 |
|
set_r = set_c = 0; |
721 |
+ |
|
722 |
|
/* read/process row-by-row */ |
723 |
|
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
724 |
|
RMATRIX *mres = NULL; |
771 |
|
return(0); |
772 |
|
} |
773 |
|
rmx_free(mres); mres = NULL; |
774 |
< |
if (inchild >= 0) { |
775 |
< |
i = getc(stdin); /* child waits for turn to output */ |
774 |
> |
if (inchild >= 0) { /* children share stdout */ |
775 |
> |
i = getc(stdin); /* signals it's our turn */ |
776 |
|
if (i != EOF) ungetc(i, stdin); |
777 |
|
} |
778 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
912 |
|
if (n && !isflt(argv[i+1])) { |
913 |
|
mop[nmats].preop.csym = argv[++i]; |
914 |
|
mop[nmats].preop.clen = 0; |
915 |
+ |
mcat_last = 0; |
916 |
|
break; |
917 |
|
} |
918 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
1003 |
|
return(1); |
1004 |
|
} |
1005 |
|
doptimize(1); /* optimize definitions */ |
1006 |
< |
if (spawned_children(nproc)) /* running in children? */ |
1006 |
> |
if (spawned_children(nproc)) /* running in parent process? */ |
1007 |
|
return(parent_loop() ? 0 : 1); |
1008 |
|
/* process & write rows */ |
1009 |
|
return(combine_input() ? 0 : 1); |