--- ray/src/util/rcomb.c 2023/12/19 20:38:38 2.5 +++ ray/src/util/rcomb.c 2024/05/23 15:48:44 2.16 @@ -1,18 +1,16 @@ #ifndef lint -static const char RCSid[] = "$Id: rcomb.c,v 2.5 2023/12/19 20:38:38 greg Exp $"; +static const char RCSid[] = "$Id: rcomb.c,v 2.16 2024/05/23 15:48:44 greg Exp $"; #endif /* * General component matrix combiner, operating on a row at a time. */ -#include #include #include "platform.h" +#include "rtprocess.h" #include "rtio.h" -#include "resolu.h" #include "rmatrix.h" #include "calcomp.h" -#include "paths.h" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -55,6 +53,10 @@ int cur_row; /* current input/output row */ int cur_col; /* current input/output column */ int cur_chan; /* if we're looping channels */ +SUBPROC *cproc = NULL; /* child process array */ +int nchildren = 0; /* # of child processes */ +int inchild = -1; /* our child ID (-1: parent) */ + static int checksymbolic(ROPMAT *rop); static int @@ -391,8 +393,7 @@ apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR return(0); rmx_free(res); } else if (dst != src) - memcpy(dst->mtx, src->mtx, - sizeof(double)*dst->ncomp*dst->ncols*dst->nrows); + memcpy(dst->mtx, src->mtx, rmx_array_size(dst)); if (ro->nsf == dst->ncomp) rmx_scale(dst, ro->sca); return(1); @@ -540,39 +541,169 @@ output_headinfo(FILE *fp) } static int -combine_input(ROPMAT *res, FILE *fout) +spawned_children(int np) { - int set_r, set_c; - RMATRIX *tmp = NULL; - int co_set; - int i; - /* allocate input row buffers */ + long inpwidth = 0; + int i, rv; + +#if defined(_WIN32) || defined(_WIN64) + if (np > 1) { + fputs("Warning: only one process under Windows\n", stderr); + np = 1; + } else +#endif + if ((in_nrows > 0) & (np > in_nrows)) + np = in_nrows; + /* we'll be doing a row at a time */ for (i = 0; i < nmats; i++) { - mop[i].imx.nrows = 1; /* we'll be doing a row at a time */ + mop[i].imx.nrows = 1; if (!rmx_prepare(&mop[i].imx)) goto memerror; + inpwidth += rmx_array_size(&mop[i].imx); if (mop[i].rmp != &mop[i].imx) { mop[i].rmp->nrows = 1; if (!rmx_prepare(mop[i].rmp)) goto memerror; + inpwidth += rmx_array_size(mop[i].rmp); } } - /* prep output row buffers */ - if (mcat || res->preop.clen > 0) { - if (!split_input(res)) /* need separate buffer */ + /* prep output row buffer */ + if (mcat || mop[nmats].preop.clen > 0) { + if (!split_input(&mop[nmats])) /* need separate buffer */ return(0); - if (res->preop.clen > 0) - res->rmp->ncomp = res->preop.clen / res->imx.ncomp; - res->rmp->nrows = 1; - if (!mcat | !mcat_last && !rmx_prepare(res->rmp)) + if (mop[nmats].preop.clen > 0) + mop[nmats].rmp->ncomp = mop[nmats].preop.clen / + mop[nmats].imx.ncomp; + mop[nmats].rmp->nrows = 1; + if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp)) goto memerror; } + mop[nmats].imx.nrows = 1; + if (!rmx_prepare(&mop[nmats].imx)) + goto memerror; + if (np <= 1) { /* single process return point */ +#ifdef getc_unlocked + for (i = 0; i < nmats; i++) + flockfile(mop[i].infp); + flockfile(stdout); +#endif + return(0); + } + fflush(stdout); /* flush header & spawn children */ + cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np); + if (!cproc) + goto memerror; + nchildren = np; + for (i = 0; i < np; i++) { + cproc[i].flags = PF_FILT_OUT; + cproc[i].w = dup(1); + cproc[i].r = 0; + cproc[i].pid = -1; + rv = open_process(&cproc[i], NULL); + if (rv <= 0) break; + if (!i && 2*rv >= inpwidth) { + fputs("Problem too small for multi-processing\n", + stderr); + exit(1); + } + } + if (rv > 0) + return(1); /* parent return value */ + if (rv < 0) { + perror("fork"); + exit(1); + } + inchild = i; /* our child index */ + while (i-- > 0) /* don't share siblings' pipes */ + close(cproc[i].w); + fpurge(stdin); /* discard previous matrix input */ +#ifdef getc_unlocked + flockfile(stdin); +#endif + for (i = 0; i < nmats; i++) { + if (mop[i].infp != stdin) + fclose(mop[i].infp); /* ! pclose() */ + mop[i].infp = stdin; + mop[i].imx.dtype = DTdouble; + } + return(0); /* child return */ +memerror: + fputs("Out of memory in spawned_children()\n", stderr); + exit(1); +} + +static int +parent_loop() +{ + FILE **outfp = (FILE **)malloc(nchildren*sizeof(FILE *)); + int i; + + if (!outfp) goto memerror; + for (i = 0; i < nchildren; i++) { + outfp[i] = fdopen(cproc[i].w, "w"); + if (!outfp[i]) goto memerror; +#ifdef getc_unlocked + flockfile(outfp[i]); +#endif + } +#ifdef getc_unlocked + for (i = 0; i < nmats; i++) + flockfile(mop[i].infp); +#endif + for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { + FILE *ofp = outfp[cur_row % nchildren]; + for (i = 0; i < nmats; i++) + if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { + if (cur_row > in_nrows) /* unknown #input rows? */ + break; + fprintf(stderr, "%s: read error at row %d\n", + mop[i].inspec, cur_row); + return(0); + } + if (i < nmats) + break; + for (i = 0; i < nmats; i++) + if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp, + mop[i].imx.ncols, DTdouble, ofp)) + return(0); + if (fflush(ofp) == EOF) + return(0); + } + for (i = 0; i < nchildren; i++) { + sleep(2); /* try to maintain order */ + fclose(outfp[i]); + } + free(outfp); + i = close_processes(cproc, nchildren); + free(cproc); cproc = NULL; + if (i < 0) { + fputs("Warning: missing child in parent_loop()\n", stderr); + return(1); + } + if (i > 0) { + fprintf(stderr, "Child exited with status %d\n", i); + return(0); + } + return(1); +memerror: + fputs("Out of memory in parent_loop()\n", stderr); + exit(1); +} + +static int +combine_input() +{ + const int row0 = (inchild >= 0)*inchild; + const int rstep = nchildren + !nchildren; + ROPMAT *res = &mop[nmats]; + int set_r, set_c; + RMATRIX *tmp = NULL; + int co_set; + int i; + if (mcat && mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) goto memerror; - res->imx.nrows = 1; - if (!rmx_prepare(&res->imx)) - goto memerror; /* figure out what the user set */ co_set = fundefined("co"); if (!co_set) @@ -587,20 +718,23 @@ combine_input(ROPMAT *res, FILE *fout) set_c = varlookup("c") != NULL && !vardefined("c"); } else /* save a little time */ set_r = set_c = 0; + /* read/process row-by-row */ - for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { + for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { RMATRIX *mres = NULL; - for (i = 0; i < nmats; i++) { + for (i = 0; i < nmats; i++) if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { if (cur_row > in_nrows) /* unknown #input rows? */ - goto loop_exit; + break; fprintf(stderr, "%s: read error at row %d\n", mop[i].inspec, cur_row); return(0); } + if (i < nmats) + break; + for (i = 0; i < nmats; i++) if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop)) return(0); - } if (set_r) varset("r", '=', cur_row); for (cur_col = 0; cur_col < in_ncols; cur_col++) { if (set_c) varset("c", '=', cur_col); @@ -637,26 +771,17 @@ combine_input(ROPMAT *res, FILE *fout) return(0); } rmx_free(mres); mres = NULL; + if (inchild >= 0) { /* children share stdout */ + i = getc(stdin); /* signals it's our turn */ + if (i != EOF) ungetc(i, stdin); + } if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, - res->rmp->ncols, res->rmp->dtype, fout)) + res->rmp->ncols, res->rmp->dtype, stdout)) return(0); + if (inchild >= 0 && fflush(stdout) == EOF) + return(0); } -loop_exit: -#if 0 /* we're about to exit, so who cares? */ - rmx_free(tmp); /* clean up */ - rmx_reset(res->rmp); - rmx_reset(&res->imx); - for (i = 0; i < nmats; i++) { - rmx_reset(mop[i].rmp); - rmx_reset(&mop[i].imx); - if (mop[i].inspec[0] == '!') - pclose(mop[i].infp); - else if (mop[i].inspec != stdin_name) - fclose(mop[i].infp); - mop[i].infp = NULL; - } -#endif - return(fflush(fout) != EOF); + return(inchild >= 0 || fflush(stdout) != EOF); memerror: fputs("Out of buffer space in combine_input()\n", stderr); return(0); @@ -680,7 +805,9 @@ resize_inparr(int n2alloc) { int i; - for (i = nmats; i > n2alloc; i--) { + if (n2alloc == nall) + return; + for (i = nall; i > n2alloc; i--) { rmx_reset(&mop[i].imx); if (mop[i].rmp != &mop[i].imx) rmx_free(mop[i].rmp); @@ -690,8 +817,8 @@ resize_inparr(int n2alloc) fputs("Out of memory in resize_inparr()\n", stderr); exit(1); } - if (n2alloc > nmats) - memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT)); + if (n2alloc > nall) + memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT)); nall = n2alloc; } @@ -704,6 +831,7 @@ main(int argc, char *argv[]) const char *defCsym = NULL; int echoheader = 1; int stdin_used = 0; + int nproc = 1; const char *mcat_spec = NULL; int n2comp = 0; uby8 comp_ndx[128]; @@ -731,6 +859,11 @@ main(int argc, char *argv[]) case 'h': echoheader = !echoheader; break; + case 'n': + nproc = atoi(argv[++i]); + if (nproc <= 0) + goto userr; + break; case 'e': if (!n) goto userr; comp_ndx[n2comp++] = i++; @@ -779,6 +912,7 @@ main(int argc, char *argv[]) if (n && !isflt(argv[i+1])) { mop[nmats].preop.csym = argv[++i]; mop[nmats].preop.clen = 0; + mcat_last = 0; break; } if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; @@ -868,15 +1002,18 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: unsupported output format\n", argv[0]); return(1); } + doptimize(1); /* optimize definitions */ + if (spawned_children(nproc)) /* running in parent process? */ + return(parent_loop() ? 0 : 1); /* process & write rows */ - return(combine_input(&mop[nmats], stdout) ? 0 : 1); + return(combine_input() ? 0 : 1); stdin_error: fprintf(stderr, "%s: %s used for more than one input\n", argv[0], stdin_name); return(1); userr: fprintf(stderr, - "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", + "Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", argv[0]); return(1); }