5 |
|
* General component matrix combiner, operating on a row at a time. |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <errno.h> |
8 |
|
#include <math.h> |
9 |
|
#include "platform.h" |
10 |
+ |
#include "rtprocess.h" |
11 |
|
#include "rtio.h" |
12 |
– |
#include "resolu.h" |
12 |
|
#include "rmatrix.h" |
13 |
|
#include "calcomp.h" |
15 |
– |
#include "paths.h" |
14 |
|
|
15 |
|
#ifndef M_PI |
16 |
|
#define M_PI 3.14159265358979323846 |
53 |
|
int cur_col; /* current input/output column */ |
54 |
|
int cur_chan; /* if we're looping channels */ |
55 |
|
|
56 |
+ |
SUBPROC *cproc = NULL; /* child process array */ |
57 |
+ |
int nchildren = 0; /* # of child processes */ |
58 |
+ |
int inchild = -1; /* our child ID (-1: parent) */ |
59 |
+ |
|
60 |
|
static int checksymbolic(ROPMAT *rop); |
61 |
|
|
62 |
|
static int |
393 |
|
return(0); |
394 |
|
rmx_free(res); |
395 |
|
} else if (dst != src) |
396 |
< |
memcpy(dst->mtx, src->mtx, |
395 |
< |
sizeof(double)*dst->ncomp*dst->ncols*dst->nrows); |
396 |
> |
memcpy(dst->mtx, src->mtx, rmx_array_size(dst)); |
397 |
|
if (ro->nsf == dst->ncomp) |
398 |
|
rmx_scale(dst, ro->sca); |
399 |
|
return(1); |
541 |
|
} |
542 |
|
|
543 |
|
static int |
544 |
< |
combine_input(ROPMAT *res, FILE *fout) |
544 |
> |
spawned_children(int np) |
545 |
|
{ |
546 |
< |
int set_r, set_c; |
547 |
< |
RMATRIX *tmp = NULL; |
548 |
< |
int co_set; |
549 |
< |
int i; |
550 |
< |
/* allocate input row buffers */ |
546 |
> |
int i, rv; |
547 |
> |
|
548 |
> |
#if defined(_WIN32) || defined(_WIN64) |
549 |
> |
if (np > 1) { |
550 |
> |
fputs("Warning: only one process under Windows\n", stderr); |
551 |
> |
np = 1; |
552 |
> |
} else |
553 |
> |
#endif |
554 |
> |
if ((in_nrows > 0) & (np > in_nrows)) |
555 |
> |
np = in_nrows; |
556 |
> |
/* we'll be doing a row at a time */ |
557 |
|
for (i = 0; i < nmats; i++) { |
558 |
< |
mop[i].imx.nrows = 1; /* we'll be doing a row at a time */ |
558 |
> |
mop[i].imx.nrows = 1; |
559 |
|
if (!rmx_prepare(&mop[i].imx)) |
560 |
|
goto memerror; |
561 |
|
if (mop[i].rmp != &mop[i].imx) { |
564 |
|
goto memerror; |
565 |
|
} |
566 |
|
} |
567 |
< |
/* prep output row buffers */ |
568 |
< |
if (mcat || res->preop.clen > 0) { |
569 |
< |
if (!split_input(res)) /* need separate buffer */ |
567 |
> |
/* prep output row buffer */ |
568 |
> |
if (mcat || mop[nmats].preop.clen > 0) { |
569 |
> |
if (!split_input(&mop[nmats])) /* need separate buffer */ |
570 |
|
return(0); |
571 |
< |
if (res->preop.clen > 0) |
572 |
< |
res->rmp->ncomp = res->preop.clen / res->imx.ncomp; |
573 |
< |
res->rmp->nrows = 1; |
574 |
< |
if (!mcat | !mcat_last && !rmx_prepare(res->rmp)) |
571 |
> |
if (mop[nmats].preop.clen > 0) |
572 |
> |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
573 |
> |
mop[nmats].imx.ncomp; |
574 |
> |
mop[nmats].rmp->nrows = 1; |
575 |
> |
if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp)) |
576 |
|
goto memerror; |
577 |
|
} |
578 |
+ |
mop[nmats].imx.nrows = 1; |
579 |
+ |
if (!rmx_prepare(&mop[nmats].imx)) |
580 |
+ |
goto memerror; |
581 |
+ |
if (np <= 1) { /* single process return point */ |
582 |
+ |
#ifdef getc_unlocked |
583 |
+ |
for (i = 0; i < nmats; i++) |
584 |
+ |
flockfile(mop[i].infp); |
585 |
+ |
flockfile(stdout); |
586 |
+ |
#endif |
587 |
+ |
return(0); |
588 |
+ |
} |
589 |
+ |
fflush(stdout); /* flush header & spawn children */ |
590 |
+ |
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); |
591 |
+ |
if (!cproc) |
592 |
+ |
goto memerror; |
593 |
+ |
nchildren = np; |
594 |
+ |
for (i = 0; i < np; i++) { |
595 |
+ |
cproc[i].flags = PF_FILT_OUT; |
596 |
+ |
cproc[i].w = dup(1); |
597 |
+ |
cproc[i].r = 0; |
598 |
+ |
cproc[i].pid = -1; |
599 |
+ |
rv = open_process(&cproc[i], NULL); |
600 |
+ |
if (rv <= 0) break; |
601 |
+ |
} |
602 |
+ |
if (rv > 0) |
603 |
+ |
return(1); /* parent return value */ |
604 |
+ |
if (rv < 0) { |
605 |
+ |
perror("fork"); |
606 |
+ |
exit(1); |
607 |
+ |
} |
608 |
+ |
inchild = i; /* our child index */ |
609 |
+ |
while (i-- > 0) /* don't share siblings' pipes */ |
610 |
+ |
close(cproc[i].w); |
611 |
+ |
fpurge(stdin); /* discard previous matrix input */ |
612 |
+ |
#ifdef getc_unlocked |
613 |
+ |
flockfile(stdin); |
614 |
+ |
#endif |
615 |
+ |
for (i = 0; i < nmats; i++) { |
616 |
+ |
if (mop[i].infp != stdin) |
617 |
+ |
fclose(mop[i].infp); /* ! pclose() */ |
618 |
+ |
mop[i].infp = stdin; |
619 |
+ |
mop[i].imx.dtype = DTdouble; |
620 |
+ |
} |
621 |
+ |
return(0); /* child return */ |
622 |
+ |
memerror: |
623 |
+ |
fputs("Out of memory in spawned_children()\n", stderr); |
624 |
+ |
exit(1); |
625 |
+ |
} |
626 |
+ |
|
627 |
+ |
static int |
628 |
+ |
parent_loop() |
629 |
+ |
{ |
630 |
+ |
FILE **outfp = (FILE **)malloc(nchildren*sizeof(FILE *)); |
631 |
+ |
int i; |
632 |
+ |
|
633 |
+ |
if (!outfp) goto memerror; |
634 |
+ |
for (i = 0; i < nchildren; i++) { |
635 |
+ |
outfp[i] = fdopen(cproc[i].w, "w"); |
636 |
+ |
if (!outfp[i]) goto memerror; |
637 |
+ |
#ifdef getc_unlocked |
638 |
+ |
flockfile(outfp[i]); |
639 |
+ |
#endif |
640 |
+ |
} |
641 |
+ |
#ifdef getc_unlocked |
642 |
+ |
for (i = 0; i < nmats; i++) |
643 |
+ |
flockfile(mop[i].infp); |
644 |
+ |
#endif |
645 |
+ |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
646 |
+ |
FILE *ofp = outfp[cur_row % nchildren]; |
647 |
+ |
for (i = 0; i < nmats; i++) |
648 |
+ |
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
649 |
+ |
if (cur_row > in_nrows) /* unknown #input rows? */ |
650 |
+ |
break; |
651 |
+ |
fprintf(stderr, "%s: read error at row %d\n", |
652 |
+ |
mop[i].inspec, cur_row); |
653 |
+ |
return(0); |
654 |
+ |
} |
655 |
+ |
if (i < nmats) |
656 |
+ |
break; |
657 |
+ |
for (i = 0; i < nmats; i++) |
658 |
+ |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
659 |
+ |
mop[i].imx.ncols, DTdouble, ofp)) |
660 |
+ |
return(0); |
661 |
+ |
if (fflush(ofp) == EOF) |
662 |
+ |
return(0); |
663 |
+ |
} |
664 |
+ |
for (i = 0; i < nchildren; i++) |
665 |
+ |
fclose(outfp[i]); |
666 |
+ |
free(outfp); |
667 |
+ |
i = close_processes(cproc, nchildren); |
668 |
+ |
free(cproc); cproc = NULL; |
669 |
+ |
if (i < 0) { |
670 |
+ |
fputs("Warning: missing child in parent_loop()\n", stderr); |
671 |
+ |
return(1); |
672 |
+ |
} |
673 |
+ |
if (i > 0) { |
674 |
+ |
fprintf(stderr, "Child exited with status %d\n", i); |
675 |
+ |
return(0); |
676 |
+ |
} |
677 |
+ |
return(1); |
678 |
+ |
memerror: |
679 |
+ |
fputs("Out of memory in parent_loop()\n", stderr); |
680 |
+ |
exit(1); |
681 |
+ |
} |
682 |
+ |
|
683 |
+ |
static int |
684 |
+ |
combine_input() |
685 |
+ |
{ |
686 |
+ |
const int row0 = (inchild >= 0)*inchild; |
687 |
+ |
const int rstep = nchildren + !nchildren; |
688 |
+ |
ROPMAT *res = &mop[nmats]; |
689 |
+ |
int set_r, set_c; |
690 |
+ |
RMATRIX *tmp = NULL; |
691 |
+ |
int co_set; |
692 |
+ |
int i; |
693 |
+ |
|
694 |
|
if (mcat && mcat_last && |
695 |
|
!(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) |
696 |
|
goto memerror; |
573 |
– |
res->imx.nrows = 1; |
574 |
– |
if (!rmx_prepare(&res->imx)) |
575 |
– |
goto memerror; |
697 |
|
/* figure out what the user set */ |
698 |
|
co_set = fundefined("co"); |
699 |
|
if (!co_set) |
709 |
|
} else /* save a little time */ |
710 |
|
set_r = set_c = 0; |
711 |
|
/* read/process row-by-row */ |
712 |
< |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
712 |
> |
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
713 |
|
RMATRIX *mres = NULL; |
714 |
< |
for (i = 0; i < nmats; i++) { |
714 |
> |
for (i = 0; i < nmats; i++) |
715 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
716 |
< |
if (in_nrows <= 0) /* normal end? */ |
717 |
< |
goto loop_exit; |
716 |
> |
if (cur_row > in_nrows) /* unknown #input rows? */ |
717 |
> |
break; |
718 |
|
fprintf(stderr, "%s: read error at row %d\n", |
719 |
|
mop[i].inspec, cur_row); |
720 |
|
return(0); |
721 |
|
} |
722 |
+ |
if (i < nmats) |
723 |
+ |
break; |
724 |
+ |
for (i = 0; i < nmats; i++) |
725 |
|
if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop)) |
726 |
|
return(0); |
603 |
– |
} |
727 |
|
if (set_r) varset("r", '=', cur_row); |
728 |
|
for (cur_col = 0; cur_col < in_ncols; cur_col++) { |
729 |
|
if (set_c) varset("c", '=', cur_col); |
760 |
|
return(0); |
761 |
|
} |
762 |
|
rmx_free(mres); mres = NULL; |
763 |
+ |
if (inchild >= 0) { |
764 |
+ |
i = getc(stdin); /* child waits for turn to output */ |
765 |
+ |
if (i != EOF) ungetc(i, stdin); |
766 |
+ |
} |
767 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
768 |
< |
res->rmp->ncols, res->rmp->dtype, fout)) |
768 |
> |
res->rmp->ncols, res->rmp->dtype, stdout)) |
769 |
|
return(0); |
770 |
+ |
if (inchild >= 0 && fflush(stdout) == EOF) |
771 |
+ |
return(0); |
772 |
|
} |
773 |
< |
loop_exit: |
645 |
< |
#if 0 /* we're about to exit, so who cares? */ |
646 |
< |
rmx_free(tmp); /* clean up */ |
647 |
< |
rmx_reset(res->rmp); |
648 |
< |
rmx_reset(&res->imx); |
649 |
< |
for (i = 0; i < nmats; i++) { |
650 |
< |
rmx_reset(mop[i].rmp); |
651 |
< |
rmx_reset(&mop[i].imx); |
652 |
< |
if (mop[i].inspec[0] == '!') |
653 |
< |
pclose(mop[i].infp); |
654 |
< |
else if (mop[i].inspec != stdin_name) |
655 |
< |
fclose(mop[i].infp); |
656 |
< |
mop[i].infp = NULL; |
657 |
< |
} |
658 |
< |
#endif |
659 |
< |
return(fflush(fout) != EOF); |
773 |
> |
return(inchild >= 0 || fflush(stdout) != EOF); |
774 |
|
memerror: |
775 |
|
fputs("Out of buffer space in combine_input()\n", stderr); |
776 |
|
return(0); |
794 |
|
{ |
795 |
|
int i; |
796 |
|
|
797 |
< |
for (i = nmats; i > n2alloc; i--) { |
797 |
> |
if (n2alloc == nall) |
798 |
> |
return; |
799 |
> |
for (i = nall; i > n2alloc; i--) { |
800 |
|
rmx_reset(&mop[i].imx); |
801 |
|
if (mop[i].rmp != &mop[i].imx) |
802 |
|
rmx_free(mop[i].rmp); |
806 |
|
fputs("Out of memory in resize_inparr()\n", stderr); |
807 |
|
exit(1); |
808 |
|
} |
809 |
< |
if (n2alloc > nmats) |
810 |
< |
memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT)); |
809 |
> |
if (n2alloc > nall) |
810 |
> |
memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT)); |
811 |
|
nall = n2alloc; |
812 |
|
} |
813 |
|
|
820 |
|
const char *defCsym = NULL; |
821 |
|
int echoheader = 1; |
822 |
|
int stdin_used = 0; |
823 |
+ |
int nproc = 1; |
824 |
|
const char *mcat_spec = NULL; |
825 |
|
int n2comp = 0; |
826 |
|
uby8 comp_ndx[128]; |
848 |
|
case 'h': |
849 |
|
echoheader = !echoheader; |
850 |
|
break; |
851 |
+ |
case 'n': |
852 |
+ |
nproc = atoi(argv[++i]); |
853 |
+ |
if (nproc <= 0) |
854 |
+ |
goto userr; |
855 |
+ |
break; |
856 |
|
case 'e': |
857 |
|
if (!n) goto userr; |
858 |
|
comp_ndx[n2comp++] = i++; |
990 |
|
fprintf(stderr, "%s: unsupported output format\n", argv[0]); |
991 |
|
return(1); |
992 |
|
} |
993 |
+ |
doptimize(1); /* optimize definitions */ |
994 |
+ |
if (spawned_children(nproc)) /* running in parent process? */ |
995 |
+ |
return(parent_loop() ? 0 : 1); |
996 |
|
/* process & write rows */ |
997 |
< |
return(combine_input(&mop[nmats], stdout) ? 0 : 1); |
997 |
> |
return(combine_input() ? 0 : 1); |
998 |
|
stdin_error: |
999 |
|
fprintf(stderr, "%s: %s used for more than one input\n", |
1000 |
|
argv[0], stdin_name); |
1001 |
|
return(1); |
1002 |
|
userr: |
1003 |
|
fprintf(stderr, |
1004 |
< |
"Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
1004 |
> |
"Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
1005 |
|
argv[0]); |
1006 |
|
return(1); |
1007 |
|
} |