--- ray/src/util/rcomb.c 2023/12/18 23:04:05 2.2 +++ ray/src/util/rcomb.c 2024/05/21 16:27:26 2.11 @@ -1,18 +1,16 @@ #ifndef lint -static const char RCSid[] = "$Id: rcomb.c,v 2.2 2023/12/18 23:04:05 greg Exp $"; +static const char RCSid[] = "$Id: rcomb.c,v 2.11 2024/05/21 16:27:26 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 @@ -186,6 +188,7 @@ checksymbolic(ROPMAT *rop) { const int nc = rop->imx.ncomp; const int dt = rop->imx.dtype; + double cf = 1; int i, j; /* check suffix => reference file */ if (strchr(rop->preop.csym, '.') > rop->preop.csym) @@ -206,28 +209,37 @@ checksymbolic(ROPMAT *rop) int comp = 0; switch (rop->preop.csym[j]) { case 'B': + case 'b': ++comp; /* fall through */ case 'G': + case 'g': ++comp; /* fall through */ case 'R': + case 'r': + if (rop->preop.csym[j] <= 'Z') + cf = 1./WHTEFFICACY; if (dt == DTxyze) { for (i = 3; i--; ) - rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY * - xyz2rgbmat[comp][i]; + rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i]; } else if (nc == 3) rop->preop.cmat[j*nc+comp] = 1.; else rgbrow(rop, j, comp); break; case 'Z': + case 'z': ++comp; /* fall through */ case 'Y': + case 'y': ++comp; /* fall through */ case 'X': + case 'x': + if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze)) + cf = WHTEFFICACY; if (dt == DTxyze) { rop->preop.cmat[j*nc+comp] = 1.; } else if (nc == 3) { @@ -239,20 +251,27 @@ checksymbolic(ROPMAT *rop) else xyzrow(rop, j, comp); - for (i = nc*(dt != DTxyze); i--; ) - rop->preop.cmat[j*nc+i] *= WHTEFFICACY; + for (i = nc*(cf != 1); i--; ) + rop->preop.cmat[j*nc+i] *= cf; break; case 'S': /* scotopic (il)luminance */ + cf = WHTSCOTOPIC; + /* fall through */ + case 's': sensrow(rop, j, scolor2scotopic); - for (i = nc; i--; ) - rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC; + for (i = nc*(cf != 1); i--; ) + rop->preop.cmat[j*nc+i] *= cf; break; case 'M': /* melanopic (il)luminance */ + cf = WHTMELANOPIC; + /* fall through */ + case 'm': sensrow(rop, j, scolor2melanopic); - for (i = nc; i--; ) - rop->preop.cmat[j*nc+i] *= WHTMELANOPIC; + for (i = nc*(cf != 1); i--; ) + rop->preop.cmat[j*nc+i] *= cf; break; case 'A': /* average component */ + case 'a': for (i = nc; i--; ) rop->preop.cmat[j*nc+i] = 1./(double)nc; break; @@ -267,10 +286,10 @@ checksymbolic(ROPMAT *rop) memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart)); rop->rmp->ncomp = rop->preop.clen / nc; /* decide on output type */ - if (!strcmp(rop->preop.csym, "XYZ")) { + if (!strcasecmp(rop->preop.csym, "XYZ")) { if (dt <= DTspec) rop->rmp->dtype = DTxyze; - } else if (!strcmp(rop->preop.csym, "RGB")) { + } else if (!strcasecmp(rop->preop.csym, "RGB")) { if (dt <= DTspec) rop->rmp->dtype = DTrgbe; } else if (rop->rmp->dtype == DTspec) @@ -374,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); @@ -523,15 +541,21 @@ 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 */ + 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; if (mop[i].rmp != &mop[i].imx) { @@ -540,22 +564,136 @@ combine_input(ROPMAT *res, FILE *fout) goto memerror; } } - /* 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 (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++) + 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) @@ -571,19 +709,21 @@ combine_input(ROPMAT *res, FILE *fout) } 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 (in_nrows <= 0) /* normal end? */ - goto loop_exit; + 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 (!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); @@ -620,26 +760,17 @@ combine_input(ROPMAT *res, FILE *fout) return(0); } rmx_free(mres); mres = NULL; + if (inchild >= 0) { + i = getc(stdin); /* child waits for turn to output */ + 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); @@ -663,7 +794,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); @@ -673,8 +806,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; } @@ -687,6 +820,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]; @@ -714,6 +848,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++; @@ -851,15 +990,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); }