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 |
> |
size_t recsize = 0; |
547 |
> |
int i, rv; |
548 |
> |
|
549 |
> |
#if defined(_WIN32) || defined(_WIN64) |
550 |
> |
if (np > 1) { |
551 |
> |
fputs("Warning: only one process under Windows\n", stderr); |
552 |
> |
np = 1; |
553 |
> |
} else |
554 |
> |
#endif |
555 |
> |
if ((in_nrows > 0) & (np > in_nrows)) |
556 |
> |
np = in_nrows; |
557 |
> |
/* we'll be doing a row at a time */ |
558 |
|
for (i = 0; i < nmats; i++) { |
559 |
< |
mop[i].imx.nrows = 1; /* we'll be doing a row at a time */ |
559 |
> |
mop[i].imx.nrows = 1; |
560 |
|
if (!rmx_prepare(&mop[i].imx)) |
561 |
|
goto memerror; |
562 |
+ |
recsize += 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 |
|
} |
568 |
|
} |
569 |
< |
/* prep output row buffers */ |
570 |
< |
if (mcat || res->preop.clen > 0) { |
571 |
< |
if (!split_input(res)) /* need separate buffer */ |
569 |
> |
/* prep output row buffer */ |
570 |
> |
if (mcat || mop[nmats].preop.clen > 0) { |
571 |
> |
if (!split_input(&mop[nmats])) /* need separate buffer */ |
572 |
|
return(0); |
573 |
< |
if (res->preop.clen > 0) |
574 |
< |
res->rmp->ncomp = res->preop.clen / res->imx.ncomp; |
575 |
< |
res->rmp->nrows = 1; |
576 |
< |
if (!mcat | !mcat_last && !rmx_prepare(res->rmp)) |
573 |
> |
if (mop[nmats].preop.clen > 0) |
574 |
> |
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
575 |
> |
mop[nmats].imx.ncomp; |
576 |
> |
mop[nmats].rmp->nrows = 1; |
577 |
> |
if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp)) |
578 |
|
goto memerror; |
579 |
|
} |
580 |
+ |
mop[nmats].imx.nrows = 1; |
581 |
+ |
if (!rmx_prepare(&mop[nmats].imx)) |
582 |
+ |
goto memerror; |
583 |
+ |
if (np <= 1) { /* single process return point */ |
584 |
+ |
#ifdef getc_unlocked |
585 |
+ |
for (i = 0; i < nmats; i++) |
586 |
+ |
flockfile(mop[i].infp); |
587 |
+ |
flockfile(stdout); |
588 |
+ |
#endif |
589 |
+ |
return(0); |
590 |
+ |
} |
591 |
+ |
fflush(stdout); /* flush header & spawn children */ |
592 |
+ |
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); |
593 |
+ |
if (!cproc) |
594 |
+ |
goto memerror; |
595 |
+ |
nchildren = np; |
596 |
+ |
for (i = 0; i < np; i++) { |
597 |
+ |
cproc[i].flags = PF_FILT_OUT; |
598 |
+ |
cproc[i].w = dup(1); |
599 |
+ |
cproc[i].r = 0; |
600 |
+ |
cproc[i].pid = -1; |
601 |
+ |
rv = open_process(&cproc[i], NULL); |
602 |
+ |
if (rv <= 0) break; |
603 |
+ |
if (!i && 2*rv >= recsize) { |
604 |
+ |
fputs("Problem too small for multi-processing\n", |
605 |
+ |
stderr); |
606 |
+ |
close_processes(cproc, 1); |
607 |
+ |
exit(1); |
608 |
+ |
} |
609 |
+ |
} |
610 |
+ |
if (rv < 0) { |
611 |
+ |
perror("fork"); |
612 |
+ |
close_processes(cproc, i); |
613 |
+ |
exit(1); |
614 |
+ |
} |
615 |
+ |
if (rv > 0) /* parent return? */ |
616 |
+ |
return(1); |
617 |
+ |
inchild = i; /* our child index */ |
618 |
+ |
while (i-- > 0) /* don't share siblings' pipes */ |
619 |
+ |
close(cproc[i].w); |
620 |
+ |
fpurge(stdin); /* discard previous matrix input */ |
621 |
+ |
#ifdef getc_unlocked |
622 |
+ |
flockfile(stdin); |
623 |
+ |
#endif |
624 |
+ |
for (i = 0; i < nmats; i++) { |
625 |
+ |
if (mop[i].infp != stdin) |
626 |
+ |
fclose(mop[i].infp); /* ! pclose() */ |
627 |
+ |
mop[i].infp = stdin; |
628 |
+ |
mop[i].imx.dtype = DTdouble; |
629 |
+ |
} |
630 |
+ |
return(0); /* child return */ |
631 |
+ |
memerror: |
632 |
+ |
fputs("Out of memory in spawned_children()\n", stderr); |
633 |
+ |
exit(1); |
634 |
+ |
} |
635 |
+ |
|
636 |
+ |
static int |
637 |
+ |
parent_loop() |
638 |
+ |
{ |
639 |
+ |
FILE **outfp = (FILE **)malloc(nchildren*sizeof(FILE *)); |
640 |
+ |
int i; |
641 |
+ |
|
642 |
+ |
if (!outfp) goto memerror; |
643 |
+ |
for (i = 0; i < nchildren; i++) { |
644 |
+ |
outfp[i] = fdopen(cproc[i].w, "w"); |
645 |
+ |
if (!outfp[i]) goto memerror; |
646 |
+ |
#ifdef getc_unlocked |
647 |
+ |
flockfile(outfp[i]); |
648 |
+ |
#endif |
649 |
+ |
} |
650 |
+ |
#ifdef getc_unlocked |
651 |
+ |
for (i = 0; i < nmats; i++) |
652 |
+ |
flockfile(mop[i].infp); |
653 |
+ |
#endif |
654 |
+ |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
655 |
+ |
FILE *ofp = outfp[cur_row % nchildren]; |
656 |
+ |
for (i = 0; i < nmats; i++) |
657 |
+ |
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
658 |
+ |
if (cur_row > in_nrows) /* unknown #input rows? */ |
659 |
+ |
break; |
660 |
+ |
fprintf(stderr, "%s: read error at row %d\n", |
661 |
+ |
mop[i].inspec, cur_row); |
662 |
+ |
return(0); |
663 |
+ |
} |
664 |
+ |
if (i < nmats) |
665 |
+ |
break; |
666 |
+ |
for (i = 0; i < nmats; i++) |
667 |
+ |
if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, |
668 |
+ |
mop[i].imx.ncols, DTdouble, ofp)) |
669 |
+ |
return(0); |
670 |
+ |
if (fflush(ofp) == EOF) |
671 |
+ |
return(0); |
672 |
+ |
} |
673 |
+ |
for (i = 0; i < nchildren; i++) { |
674 |
+ |
sleep(2); /* try to maintain order */ |
675 |
+ |
fclose(outfp[i]); |
676 |
+ |
} |
677 |
+ |
free(outfp); |
678 |
+ |
i = close_processes(cproc, nchildren); |
679 |
+ |
free(cproc); cproc = NULL; |
680 |
+ |
if (i < 0) { |
681 |
+ |
fputs("Warning: missing child in parent_loop()\n", stderr); |
682 |
+ |
return(1); |
683 |
+ |
} |
684 |
+ |
if (i > 0) { |
685 |
+ |
fprintf(stderr, "Child exited with status %d\n", i); |
686 |
+ |
return(0); |
687 |
+ |
} |
688 |
+ |
return(1); |
689 |
+ |
memerror: |
690 |
+ |
fputs("Out of memory in parent_loop()\n", stderr); |
691 |
+ |
exit(1); |
692 |
+ |
} |
693 |
+ |
|
694 |
+ |
static int |
695 |
+ |
combine_input() |
696 |
+ |
{ |
697 |
+ |
const int row0 = (inchild >= 0)*inchild; |
698 |
+ |
const int rstep = nchildren + !nchildren; |
699 |
+ |
ROPMAT *res = &mop[nmats]; |
700 |
+ |
int set_r, set_c; |
701 |
+ |
RMATRIX *tmp = NULL; |
702 |
+ |
int co_set; |
703 |
+ |
int i; |
704 |
+ |
|
705 |
|
if (mcat && mcat_last && |
706 |
|
!(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) |
707 |
|
goto memerror; |
573 |
– |
res->imx.nrows = 1; |
574 |
– |
if (!rmx_prepare(&res->imx)) |
575 |
– |
goto memerror; |
708 |
|
/* figure out what the user set */ |
709 |
|
co_set = fundefined("co"); |
710 |
|
if (!co_set) |
719 |
|
set_c = varlookup("c") != NULL && !vardefined("c"); |
720 |
|
} else /* save a little time */ |
721 |
|
set_r = set_c = 0; |
722 |
+ |
|
723 |
|
/* read/process row-by-row */ |
724 |
< |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
724 |
> |
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
725 |
|
RMATRIX *mres = NULL; |
726 |
< |
for (i = 0; i < nmats; i++) { |
726 |
> |
for (i = 0; i < nmats; i++) |
727 |
|
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
728 |
< |
if (in_nrows <= 0) /* normal end? */ |
729 |
< |
goto loop_exit; |
728 |
> |
if (cur_row > in_nrows) /* unknown #input rows? */ |
729 |
> |
break; |
730 |
|
fprintf(stderr, "%s: read error at row %d\n", |
731 |
|
mop[i].inspec, cur_row); |
732 |
|
return(0); |
733 |
|
} |
734 |
+ |
if (i < nmats) |
735 |
+ |
break; |
736 |
+ |
for (i = 0; i < nmats; i++) |
737 |
|
if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop)) |
738 |
|
return(0); |
603 |
– |
} |
739 |
|
if (set_r) varset("r", '=', cur_row); |
740 |
|
for (cur_col = 0; cur_col < in_ncols; cur_col++) { |
741 |
|
if (set_c) varset("c", '=', cur_col); |
772 |
|
return(0); |
773 |
|
} |
774 |
|
rmx_free(mres); mres = NULL; |
775 |
+ |
if (inchild >= 0) { /* children share stdout */ |
776 |
+ |
i = getc(stdin); /* signals it's our turn */ |
777 |
+ |
if (i != EOF) ungetc(i, stdin); |
778 |
+ |
} |
779 |
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
780 |
< |
res->rmp->ncols, res->rmp->dtype, fout)) |
780 |
> |
res->rmp->ncols, res->rmp->dtype, stdout)) |
781 |
|
return(0); |
782 |
+ |
if (inchild >= 0 && fflush(stdout) == EOF) |
783 |
+ |
return(0); |
784 |
|
} |
785 |
< |
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); |
785 |
> |
return(inchild >= 0 || fflush(stdout) != EOF); |
786 |
|
memerror: |
787 |
|
fputs("Out of buffer space in combine_input()\n", stderr); |
788 |
|
return(0); |
806 |
|
{ |
807 |
|
int i; |
808 |
|
|
809 |
< |
for (i = nmats; i > n2alloc; i--) { |
809 |
> |
if (n2alloc == nall) |
810 |
> |
return; |
811 |
> |
for (i = nall; i > n2alloc; i--) { |
812 |
|
rmx_reset(&mop[i].imx); |
813 |
|
if (mop[i].rmp != &mop[i].imx) |
814 |
|
rmx_free(mop[i].rmp); |
818 |
|
fputs("Out of memory in resize_inparr()\n", stderr); |
819 |
|
exit(1); |
820 |
|
} |
821 |
< |
if (n2alloc > nmats) |
822 |
< |
memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT)); |
821 |
> |
if (n2alloc > nall) |
822 |
> |
memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT)); |
823 |
|
nall = n2alloc; |
824 |
|
} |
825 |
|
|
832 |
|
const char *defCsym = NULL; |
833 |
|
int echoheader = 1; |
834 |
|
int stdin_used = 0; |
835 |
+ |
int nproc = 1; |
836 |
|
const char *mcat_spec = NULL; |
837 |
|
int n2comp = 0; |
838 |
|
uby8 comp_ndx[128]; |
860 |
|
case 'h': |
861 |
|
echoheader = !echoheader; |
862 |
|
break; |
863 |
+ |
case 'n': |
864 |
+ |
nproc = atoi(argv[++i]); |
865 |
+ |
if (nproc <= 0) |
866 |
+ |
goto userr; |
867 |
+ |
break; |
868 |
|
case 'e': |
869 |
|
if (!n) goto userr; |
870 |
|
comp_ndx[n2comp++] = i++; |
913 |
|
if (n && !isflt(argv[i+1])) { |
914 |
|
mop[nmats].preop.csym = argv[++i]; |
915 |
|
mop[nmats].preop.clen = 0; |
916 |
+ |
mcat_last = 0; |
917 |
|
break; |
918 |
|
} |
919 |
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
1003 |
|
fprintf(stderr, "%s: unsupported output format\n", argv[0]); |
1004 |
|
return(1); |
1005 |
|
} |
1006 |
+ |
doptimize(1); /* optimize definitions */ |
1007 |
+ |
if (spawned_children(nproc)) /* running in parent process? */ |
1008 |
+ |
return(parent_loop() ? 0 : 1); |
1009 |
|
/* process & write rows */ |
1010 |
< |
return(combine_input(&mop[nmats], stdout) ? 0 : 1); |
1010 |
> |
return(combine_input() ? 0 : 1); |
1011 |
|
stdin_error: |
1012 |
|
fprintf(stderr, "%s: %s used for more than one input\n", |
1013 |
|
argv[0], stdin_name); |
1014 |
|
return(1); |
1015 |
|
userr: |
1016 |
|
fprintf(stderr, |
1017 |
< |
"Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
1017 |
> |
"Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
1018 |
|
argv[0]); |
1019 |
|
return(1); |
1020 |
|
} |