| 5 |
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
+ |
#include <signal.h> |
| 9 |
|
#include <math.h> |
| 10 |
|
#include "platform.h" |
| 11 |
|
#include "rtprocess.h" |
| 57 |
|
SUBPROC *cproc = NULL; /* child process array */ |
| 58 |
|
int nchildren = 0; /* # of child processes */ |
| 59 |
|
int inchild = -1; /* our child ID (-1: parent) */ |
| 60 |
+ |
int pgid = -1; /* process group ID */ |
| 61 |
+ |
int nr_out = 0; /* # of rows output by kids */ |
| 62 |
|
|
| 63 |
|
static int checksymbolic(ROPMAT *rop); |
| 64 |
|
|
| 65 |
+ |
static void |
| 66 |
+ |
on_sigio(int dummy) |
| 67 |
+ |
{ |
| 68 |
+ |
nr_out++; /* happens when child outputs row */ |
| 69 |
+ |
} |
| 70 |
+ |
|
| 71 |
|
static int |
| 72 |
|
split_input(ROPMAT *rop) |
| 73 |
|
{ |
| 595 |
|
#endif |
| 596 |
|
return(0); |
| 597 |
|
} |
| 598 |
+ |
pgid = setpgrp(); /* set process group ID */ |
| 599 |
+ |
signal(SIGIO, on_sigio); |
| 600 |
|
fflush(stdout); /* flush header & spawn children */ |
| 601 |
|
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); |
| 602 |
|
if (!cproc) |
| 616 |
|
perror("fork"); |
| 617 |
|
exit(1); |
| 618 |
|
} |
| 619 |
< |
inchild = i; /* child index */ |
| 619 |
> |
inchild = i; /* our child index */ |
| 620 |
> |
while (i-- > 0) /* don't share siblings' pipes */ |
| 621 |
> |
close(cproc[i].w); |
| 622 |
|
fpurge(stdin); /* discard previous matrix input */ |
| 623 |
|
#ifdef getc_unlocked |
| 624 |
|
flockfile(stdin); |
| 625 |
|
#endif |
| 626 |
|
for (i = 0; i < nmats; i++) { |
| 627 |
|
if (mop[i].infp != stdin) |
| 628 |
< |
fclose(mop[i].infp); |
| 628 |
> |
fclose(mop[i].infp); /* ! pclose() */ |
| 629 |
|
mop[i].infp = stdin; |
| 630 |
|
mop[i].imx.dtype = DTdouble; |
| 631 |
|
} |
| 655 |
|
#endif |
| 656 |
|
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
| 657 |
|
FILE *ofp = outfp[cur_row % nchildren]; |
| 658 |
< |
for (i = 0; i < nmats; i++) { |
| 658 |
> |
for (i = 0; i < nmats; i++) |
| 659 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 660 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 661 |
|
break; |
| 663 |
|
mop[i].inspec, cur_row); |
| 664 |
|
return(0); |
| 665 |
|
} |
| 653 |
– |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
| 654 |
– |
mop[i].imx.ncols, DTdouble, ofp)) |
| 655 |
– |
return(0); |
| 656 |
– |
} |
| 666 |
|
if (i < nmats) |
| 667 |
|
break; |
| 668 |
+ |
for (i = 0; i < nmats; i++) |
| 669 |
+ |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
| 670 |
+ |
mop[i].imx.ncols, DTdouble, ofp)) |
| 671 |
+ |
return(0); |
| 672 |
|
if (fflush(ofp) == EOF) |
| 673 |
|
return(0); |
| 674 |
|
} |
| 700 |
|
int set_r, set_c; |
| 701 |
|
RMATRIX *tmp = NULL; |
| 702 |
|
int co_set; |
| 703 |
+ |
sigset_t iomask; |
| 704 |
|
int i; |
| 705 |
|
|
| 706 |
|
if (mcat && mcat_last && |
| 720 |
|
set_c = varlookup("c") != NULL && !vardefined("c"); |
| 721 |
|
} else /* save a little time */ |
| 722 |
|
set_r = set_c = 0; |
| 723 |
< |
/* read/process row-by-row */ |
| 723 |
> |
|
| 724 |
> |
sigemptyset(&iomask); /* read/process row-by-row */ |
| 725 |
> |
sigaddset(&iomask, SIGIO); |
| 726 |
|
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
| 727 |
|
RMATRIX *mres = NULL; |
| 728 |
+ |
if (inchild >= 0) sigprocmask(SIG_BLOCK, &iomask, NULL); |
| 729 |
|
for (i = 0; i < nmats; i++) |
| 730 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 731 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 734 |
|
mop[i].inspec, cur_row); |
| 735 |
|
return(0); |
| 736 |
|
} |
| 737 |
+ |
if (inchild >= 0) sigprocmask(SIG_UNBLOCK, &iomask, NULL); |
| 738 |
|
if (i < nmats) |
| 739 |
|
break; |
| 740 |
|
for (i = 0; i < nmats; i++) |
| 776 |
|
return(0); |
| 777 |
|
} |
| 778 |
|
rmx_free(mres); mres = NULL; |
| 779 |
< |
if (inchild >= 0) { |
| 780 |
< |
i = getc(stdin); /* child waits for turn to output */ |
| 781 |
< |
if (i != EOF) ungetc(i, stdin); |
| 779 |
> |
if (inchild >= 0) { /* children share stdout */ |
| 780 |
> |
while (nr_out < cur_row) |
| 781 |
> |
pause(); /* wait for our turn */ |
| 782 |
> |
sigprocmask(SIG_BLOCK, &iomask, NULL); |
| 783 |
|
} |
| 784 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
| 785 |
|
res->rmp->ncols, res->rmp->dtype, stdout)) |
| 786 |
|
return(0); |
| 787 |
< |
if (inchild >= 0 && fflush(stdout) == EOF) |
| 788 |
< |
return(0); |
| 787 |
> |
if (inchild >= 0) { /* flush and notify group */ |
| 788 |
> |
if (fflush(stdout) == EOF) |
| 789 |
> |
return(0); |
| 790 |
> |
sigprocmask(SIG_UNBLOCK, &iomask, NULL); |
| 791 |
> |
killpg(pgid, SIGIO); /* increments everyone's nr_out */ |
| 792 |
> |
} |
| 793 |
|
} |
| 794 |
|
return(inchild >= 0 || fflush(stdout) != EOF); |
| 795 |
|
memerror: |
| 922 |
|
if (n && !isflt(argv[i+1])) { |
| 923 |
|
mop[nmats].preop.csym = argv[++i]; |
| 924 |
|
mop[nmats].preop.clen = 0; |
| 925 |
+ |
mcat_last = 0; |
| 926 |
|
break; |
| 927 |
|
} |
| 928 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
| 1013 |
|
return(1); |
| 1014 |
|
} |
| 1015 |
|
doptimize(1); /* optimize definitions */ |
| 1016 |
< |
if (spawned_children(nproc)) /* running in children? */ |
| 1016 |
> |
if (spawned_children(nproc)) /* running in parent process? */ |
| 1017 |
|
return(parent_loop() ? 0 : 1); |
| 1018 |
|
/* process & write rows */ |
| 1019 |
|
return(combine_input() ? 0 : 1); |