| 5 |
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
– |
#include <signal.h> |
| 8 |
|
#include <math.h> |
| 9 |
|
#include "platform.h" |
| 10 |
|
#include "rtprocess.h" |
| 56 |
|
SUBPROC *cproc = NULL; /* child process array */ |
| 57 |
|
int nchildren = 0; /* # of child processes */ |
| 58 |
|
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 */ |
| 59 |
|
|
| 60 |
|
static int checksymbolic(ROPMAT *rop); |
| 61 |
|
|
| 65 |
– |
static void |
| 66 |
– |
on_sigio(int dummy) |
| 67 |
– |
{ |
| 68 |
– |
nr_out++; /* happens when child outputs row */ |
| 69 |
– |
} |
| 70 |
– |
|
| 62 |
|
static int |
| 63 |
|
split_input(ROPMAT *rop) |
| 64 |
|
{ |
| 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 */ |
| 589 |
|
#endif |
| 590 |
|
return(0); |
| 591 |
|
} |
| 598 |
– |
pgid = setpgrp(); /* set process group ID */ |
| 599 |
– |
signal(SIGIO, on_sigio); |
| 592 |
|
fflush(stdout); /* flush header & spawn children */ |
| 593 |
|
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); |
| 594 |
|
if (!cproc) |
| 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 */ |
| 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; |
| 699 |
|
int set_r, set_c; |
| 700 |
|
RMATRIX *tmp = NULL; |
| 701 |
|
int co_set; |
| 703 |
– |
sigset_t iomask; |
| 702 |
|
int i; |
| 703 |
|
|
| 704 |
|
if (mcat && mcat_last && |
| 719 |
|
} else /* save a little time */ |
| 720 |
|
set_r = set_c = 0; |
| 721 |
|
|
| 722 |
< |
sigemptyset(&iomask); /* read/process row-by-row */ |
| 725 |
< |
sigaddset(&iomask, SIGIO); |
| 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; |
| 728 |
– |
if (inchild >= 0) sigprocmask(SIG_BLOCK, &iomask, NULL); |
| 725 |
|
for (i = 0; i < nmats; i++) |
| 726 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 727 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 730 |
|
mop[i].inspec, cur_row); |
| 731 |
|
return(0); |
| 732 |
|
} |
| 737 |
– |
if (inchild >= 0) sigprocmask(SIG_UNBLOCK, &iomask, NULL); |
| 733 |
|
if (i < nmats) |
| 734 |
|
break; |
| 735 |
|
for (i = 0; i < nmats; i++) |
| 772 |
|
} |
| 773 |
|
rmx_free(mres); mres = NULL; |
| 774 |
|
if (inchild >= 0) { /* children share stdout */ |
| 775 |
< |
while (nr_out < cur_row) |
| 776 |
< |
pause(); /* wait for our turn */ |
| 782 |
< |
sigprocmask(SIG_BLOCK, &iomask, NULL); |
| 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, |
| 779 |
|
res->rmp->ncols, res->rmp->dtype, stdout)) |
| 780 |
|
return(0); |
| 781 |
< |
if (inchild >= 0) { /* flush and notify group */ |
| 782 |
< |
if (fflush(stdout) == EOF) |
| 789 |
< |
return(0); |
| 790 |
< |
sigprocmask(SIG_UNBLOCK, &iomask, NULL); |
| 791 |
< |
killpg(pgid, SIGIO); /* increments everyone's nr_out */ |
| 792 |
< |
} |
| 781 |
> |
if (inchild >= 0 && fflush(stdout) == EOF) |
| 782 |
> |
return(0); |
| 783 |
|
} |
| 784 |
|
return(inchild >= 0 || fflush(stdout) != EOF); |
| 785 |
|
memerror: |