| 570 |
|
} |
| 571 |
|
} |
| 572 |
|
/* prep output row buffer(s) */ |
| 573 |
< |
if (mcat || mop[nmats].preop.clen > 0) { |
| 573 |
> |
if (mop[nmats].preop.clen > 0) { |
| 574 |
|
if (!split_input(&mop[nmats])) /* need separate buffer */ |
| 575 |
< |
return(0); |
| 576 |
< |
if (mop[nmats].preop.clen > 0) |
| 577 |
< |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
| 578 |
< |
mop[nmats].imx.ncomp; |
| 575 |
> |
goto memerror; |
| 576 |
> |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
| 577 |
> |
mop[nmats].imx.ncomp; |
| 578 |
|
} |
| 579 |
|
mop[nmats].imx.nrows = 1; |
| 580 |
|
if (!rmx_prepare(&mop[nmats].imx)) |
| 599 |
|
goto memerror; |
| 600 |
|
for (i = nchildren; i--; ) cproc[i] = sp_inactive; |
| 601 |
|
cproc[nchildren-1].flags |= PF_FILT_OUT; |
| 602 |
< |
/* start each child */ |
| 603 |
< |
for (i = 0; i < nchildren; i++) { |
| 604 |
< |
rv = open_process(&cproc[i], NULL); |
| 605 |
< |
if (rv <= 0) break; |
| 607 |
< |
} |
| 602 |
> |
/* start each child from parent */ |
| 603 |
> |
for (i = 0; i < nchildren; i++) |
| 604 |
> |
if ((rv = open_process(&cproc[i], NULL)) <= 0) |
| 605 |
> |
break; /* child breaks here */ |
| 606 |
|
if (rv < 0) { |
| 607 |
< |
perror("fork"); |
| 607 |
> |
perror("fork"); /* WTH? */ |
| 608 |
|
close_processes(cproc, i); |
| 609 |
|
exit(1); |
| 610 |
|
} |
| 611 |
< |
if (rv) { /* are we the parent? */ |
| 612 |
< |
i = nchildren-1; /* last child is sole reader */ |
| 613 |
< |
while (i-- > 0) { |
| 614 |
< |
close(cproc[i].r); |
| 615 |
< |
cproc[i].r = -1; |
| 611 |
> |
if (i != nchildren-1) { /* last child is sole reader */ |
| 612 |
> |
int j = i; |
| 613 |
> |
while (j-- > 0) { |
| 614 |
> |
close(cproc[j].r); |
| 615 |
> |
cproc[j].r = -1; |
| 616 |
|
} |
| 619 |
– |
return(1); /* parent return value */ |
| 617 |
|
} |
| 618 |
< |
inchild = i; /* our child index */ |
| 618 |
> |
if (rv > 0) |
| 619 |
> |
return(1); /* parent return value */ |
| 620 |
> |
|
| 621 |
> |
inchild = i; /* else set our child index */ |
| 622 |
|
while (i-- > 0) /* only parent writes siblings */ |
| 623 |
|
close(cproc[i].w); |
| 624 |
|
|
| 625 |
+ |
i = nmats; /* close matrix streams (carefully) */ |
| 626 |
+ |
while (i-- > 0) { |
| 627 |
+ |
if (mop[i].infp != stdin) { |
| 628 |
+ |
close(fileno(mop[i].infp)); /* avoid lseek() */ |
| 629 |
+ |
fclose(mop[i].infp); /* ! pclose() */ |
| 630 |
+ |
} |
| 631 |
+ |
mop[i].infp = NULL; |
| 632 |
+ |
} |
| 633 |
+ |
fpurge(stdin); /* discard previously buffered input */ |
| 634 |
+ |
|
| 635 |
|
if (inchild == nchildren-1) |
| 636 |
|
return(-1); /* output process return value */ |
| 637 |
|
|
| 638 |
< |
i = inchild; /* won't read from siblings */ |
| 629 |
< |
while (i-- > 0) |
| 630 |
< |
close(cproc[i].r); |
| 631 |
< |
i = nmats; /* redirect input matrix streams */ |
| 638 |
> |
i = nmats; /* get matrix rows from parent */ |
| 639 |
|
while (i-- > 0) { |
| 633 |
– |
if (mop[i].infp != stdin) |
| 634 |
– |
fclose(mop[i].infp); /* ! pclose() */ |
| 640 |
|
mop[i].infp = stdin; |
| 641 |
|
mop[i].imx.dtype = DTrmx_native; |
| 642 |
|
mop[i].imx.pflags &= ~RMF_SWAPIN; |
| 643 |
|
} |
| 639 |
– |
fpurge(stdin); /* discard any previous matrix input */ |
| 644 |
|
#ifdef getc_unlocked |
| 645 |
|
flockfile(stdin); |
| 646 |
|
#endif |
| 672 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 673 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 674 |
|
break; |
| 675 |
< |
fprintf(stderr, "%s: parent read error at row %d\n", |
| 675 |
> |
fprintf(stderr, "%s: parent_loop() load error at row %d\n", |
| 676 |
|
mop[i].inspec, cur_row); |
| 677 |
|
return(0); |
| 678 |
|
} |
| 680 |
|
break; |
| 681 |
|
for (i = 0; i < nmats; i++) |
| 682 |
|
if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx)) |
| 683 |
< |
!= rmx_array_size(&mop[i].imx)) |
| 683 |
> |
!= rmx_array_size(&mop[i].imx)) { |
| 684 |
> |
fprintf(stderr, "%s: parent_loop() write error at row %d\n", |
| 685 |
> |
mop[i].inspec, cur_row); |
| 686 |
|
return(0); |
| 687 |
+ |
} |
| 688 |
|
} |
| 689 |
< |
i = close_processes(cproc, nchildren); |
| 689 |
> |
i = close_processes(cproc, nchildren); /* collect family */ |
| 690 |
|
free(cproc); cproc = NULL; nchildren = 0; |
| 691 |
|
if (i < 0) { |
| 692 |
|
fputs("Warning: lost child in parent_loop()\n", stderr); |
| 697 |
|
return(0); |
| 698 |
|
} |
| 699 |
|
return(1); /* return success! */ |
| 693 |
– |
memerror: |
| 694 |
– |
fputs("Out of memory in parent_loop()\n", stderr); |
| 695 |
– |
exit(1); |
| 700 |
|
} |
| 701 |
|
|
| 702 |
|
int |
| 710 |
|
int co_set; |
| 711 |
|
int i; |
| 712 |
|
|
| 713 |
< |
if (mcat && mcat_last && |
| 714 |
< |
!(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) |
| 715 |
< |
goto memerror; |
| 713 |
> |
if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) { |
| 714 |
> |
fputs("Out of buffer space in combine_input()\n", stderr); |
| 715 |
> |
return(0); |
| 716 |
> |
} |
| 717 |
|
/* figure out what the user set */ |
| 718 |
|
co_set = fundefined("co"); |
| 719 |
|
if (!co_set) |
| 735 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 736 |
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 737 |
|
break; |
| 738 |
< |
fprintf(stderr, "%s: read error at row %d\n", |
| 738 |
> |
fprintf(stderr, "%s: combine_input() load error at row %d\n", |
| 739 |
|
mop[i].inspec, cur_row); |
| 740 |
|
return(0); |
| 741 |
|
} |
| 781 |
|
} |
| 782 |
|
rmx_free(mres); mres = NULL; |
| 783 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
| 784 |
< |
res->rmp->ncols, res->rmp->dtype, stdout)) |
| 784 |
> |
res->rmp->ncols, res->rmp->dtype, stdout) || |
| 785 |
> |
(inchild >= 0 && fflush(stdout) == EOF)) { |
| 786 |
> |
fprintf(stderr, "Conversion/write error at row %d in combine_input()\n", |
| 787 |
> |
cur_row); |
| 788 |
|
return(0); |
| 789 |
< |
if (inchild >= 0 && fflush(stdout) == EOF) |
| 782 |
< |
return(0); |
| 789 |
> |
} |
| 790 |
|
} |
| 791 |
|
return(inchild >= 0 || fflush(stdout) != EOF); |
| 785 |
– |
memerror: |
| 786 |
– |
fputs("Out of buffer space in combine_input()\n", stderr); |
| 787 |
– |
return(0); |
| 792 |
|
multerror: |
| 793 |
|
fputs("Unexpected matrix multiply error in combine_input()\n", stderr); |
| 794 |
|
return(0); |
| 798 |
|
output_loop(void) |
| 799 |
|
{ |
| 800 |
|
const size_t row_size = rmx_array_size(mop[nmats].rmp); |
| 797 |
– |
int i = nmats; |
| 801 |
|
int cur_child = 0; |
| 802 |
+ |
int i = nmats; |
| 803 |
|
|
| 804 |
< |
if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ |
| 801 |
< |
rmx_reset(&mop[nmats].imx); |
| 802 |
< |
while (i-- > 0) { /* close input matrices */ |
| 803 |
< |
fclose(mop[i].infp); /* ! pclose() */ |
| 804 |
< |
mop[i].infp = NULL; |
| 804 |
> |
while (i-- > 0) { /* free input buffers */ |
| 805 |
|
rmx_reset(&mop[i].imx); |
| 806 |
|
if (mop[i].rmp != &mop[i].imx) { |
| 807 |
|
rmx_free(mop[i].rmp); |
| 808 |
|
mop[i].rmp = &mop[i].imx; |
| 809 |
|
} |
| 810 |
|
} |
| 811 |
+ |
if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ |
| 812 |
+ |
rmx_reset(&mop[nmats].imx); |
| 813 |
|
#ifdef getc_unlocked |
| 814 |
|
flockfile(stdout); /* we own this, now */ |
| 815 |
|
#endif |
| 819 |
|
if (!rv) /* out of rows? */ |
| 820 |
|
break; |
| 821 |
|
if (rv != row_size) { |
| 822 |
< |
fputs("Read error in output loop\n", stderr); |
| 822 |
> |
fputs("Read error in output_loop()\n", stderr); |
| 823 |
|
return(0); |
| 824 |
|
} /* do final conversion */ |
| 825 |
|
if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, |
| 826 |
|
mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { |
| 827 |
< |
fputs("Conversion/write error in output loop\n", stderr); |
| 827 |
> |
fputs("Conversion/write error in output_loop()\n", stderr); |
| 828 |
|
return(0); |
| 829 |
|
} |
| 830 |
|
cur_child++; |
| 831 |
< |
cur_child *= (cur_child < inchild); |
| 831 |
> |
cur_child *= (cur_child < inchild); /* loop over workers */ |
| 832 |
|
} |
| 833 |
|
return(fflush(stdout) != EOF); |
| 834 |
|
} |
| 945 |
|
} |
| 946 |
|
break; |
| 947 |
|
case 'C': |
| 948 |
+ |
mcat_last = 0; |
| 949 |
|
if (!n || isflt(argv[i+1])) |
| 950 |
|
goto userr; |
| 951 |
|
defCsym = mop[nmats].preop.csym = argv[++i]; |
| 952 |
|
mop[nmats].preop.clen = 0; |
| 950 |
– |
mcat_last = 0; |
| 953 |
|
break; |
| 954 |
|
case 'c': |
| 955 |
+ |
mcat_last = 0; |
| 956 |
|
if (n && !isflt(argv[i+1])) { |
| 957 |
|
mop[nmats].preop.csym = argv[++i]; |
| 958 |
|
mop[nmats].preop.clen = 0; |
| 956 |
– |
mcat_last = 0; |
| 959 |
|
break; |
| 960 |
|
} |
| 961 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
| 968 |
|
goto userr; |
| 969 |
|
} |
| 970 |
|
mop[nmats].preop.csym = NULL; |
| 969 |
– |
mcat_last = 0; |
| 971 |
|
break; |
| 972 |
|
case 'm': |
| 973 |
+ |
mcat_last = 1; |
| 974 |
|
if (!n) goto userr; |
| 975 |
|
if (argv[++i][0] == '-' && !argv[i][1]) { |
| 976 |
|
if (stdin_used++) goto stdin_error; |
| 977 |
|
mcat_spec = stdin_name; |
| 978 |
|
} else |
| 979 |
|
mcat_spec = argv[i]; |
| 978 |
– |
mcat_last = 1; |
| 980 |
|
break; |
| 981 |
|
default: |
| 982 |
|
fprintf(stderr, "%s: unknown option '%s'\n", |