--- ray/src/util/rcomb.c 2024/06/04 21:23:11 2.20 +++ ray/src/util/rcomb.c 2024/11/08 17:52:26 2.26 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcomb.c,v 2.20 2024/06/04 21:23:11 greg Exp $"; +static const char RCSid[] = "$Id: rcomb.c,v 2.26 2024/11/08 17:52:26 greg Exp $"; #endif /* * General component matrix combiner, operating on a row at a time. @@ -62,9 +62,9 @@ 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); +extern int checksymbolic(ROPMAT *rop); -static int +int split_input(ROPMAT *rop) { if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) { @@ -76,7 +76,7 @@ split_input(ROPMAT *rop) } /* Check/set transform based on a reference input file */ -static int +int checkreffile(ROPMAT *rop) { static const char *curRF = NULL; @@ -139,7 +139,7 @@ checkreffile(ROPMAT *rop) } /* Compute conversion row from spectrum to one channel of RGB */ -static void +void rgbrow(ROPMAT *rop, int r, int p) { const int nc = rop->imx.ncomp; @@ -156,7 +156,7 @@ rgbrow(ROPMAT *rop, int r, int p) } /* Compute conversion row from spectrum to one channel of XYZ */ -static void +void xyzrow(ROPMAT *rop, int r, int p) { const int nc = rop->imx.ncomp; @@ -173,8 +173,8 @@ xyzrow(ROPMAT *rop, int r, int p) } /* Use the spectral sensitivity function to compute matrix coefficients */ -static void -sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4])) +void +sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4])) { const int nc = rop->imx.ncomp; int i; @@ -188,7 +188,7 @@ sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in } /* Check/set symbolic transform */ -static int +int checksymbolic(ROPMAT *rop) { const int nc = rop->imx.ncomp; @@ -302,7 +302,7 @@ checksymbolic(ROPMAT *rop) return(1); } -static int +int get_component_xfm(ROPMAT *rop) { int i, j; @@ -385,7 +385,7 @@ get_component_xfm(ROPMAT *rop) return(1); } -static int +int apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro) { if (ro->clen > 0) { @@ -404,7 +404,7 @@ apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR return(1); } -static int +int open_input(ROPMAT *rop) { int outtype; @@ -426,7 +426,7 @@ open_input(ROPMAT *rop) } /* Return nominal wavelength associated with input component (return nm) */ -static double +double l_wavelength(char *nam) { double comp = argument(1); @@ -450,7 +450,7 @@ l_wavelength(char *nam) } /* Return ith input with optional channel selector */ -static double +double l_chanin(char *nam) { double inp = argument(1); @@ -475,7 +475,7 @@ l_chanin(char *nam) return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]); } -static int +int initialize(RMATRIX *imp) { int i; @@ -488,7 +488,7 @@ initialize(RMATRIX *imp) restype = mop[i].rmp->dtype; if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0) imp->dtype = restype; - else + else if (!nowarn) fprintf(stderr, "%s: warning - data type mismatch\n", mop[i].inspec); if (!i) { @@ -522,7 +522,7 @@ initialize(RMATRIX *imp) return(1); } -static void +void output_headinfo(FILE *fp) { int i; @@ -545,55 +545,15 @@ output_headinfo(FILE *fp) } } -static int -output_loop(void) -{ - const size_t row_size = rmx_array_size(mop[nmats].rmp); - int i = nmats; - int cur_child = 0; - - if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ - rmx_reset(&mop[nmats].imx); - while (i-- > 0) { /* close input matrices */ - fclose(mop[i].infp); /* ! pclose() */ - mop[i].infp = NULL; - rmx_reset(&mop[i].imx); - if (mop[i].rmp != &mop[i].imx) { - rmx_free(mop[i].rmp); - mop[i].rmp = &mop[i].imx; - } - } -#ifdef getc_unlocked - flockfile(stdout); /* we own this, now */ -#endif - for ( ; ; ) { /* loop until no more */ - ssize_t rv; - rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size); - if (!rv) /* out of rows? */ - break; - if (rv != row_size) { - fputs("Read error in output loop\n", stderr); - return(0); - } /* do final conversion */ - if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, - mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { - fputs("Conversion/write error in output loop\n", stderr); - return(0); - } - cur_child++; - cur_child *= (cur_child < inchild); - } - return(fflush(stdout) != EOF); -} - -static int +int spawned_children(int np) { int i, rv; #if defined(_WIN32) || defined(_WIN64) if (np > 1) { - fputs("Warning: only one process under Windows\n", stderr); + if (!nowarn) + fputs("Warning: only one process under Windows\n", stderr); np = 1; } else #endif @@ -611,12 +571,11 @@ spawned_children(int np) } } /* prep output row buffer(s) */ - if (mcat || mop[nmats].preop.clen > 0) { + if (mop[nmats].preop.clen > 0) { if (!split_input(&mop[nmats])) /* need separate buffer */ - return(0); - if (mop[nmats].preop.clen > 0) - mop[nmats].rmp->ncomp = mop[nmats].preop.clen / - mop[nmats].imx.ncomp; + goto memerror; + mop[nmats].rmp->ncomp = mop[nmats].preop.clen / + mop[nmats].imx.ncomp; } mop[nmats].imx.nrows = 1; if (!rmx_prepare(&mop[nmats].imx)) @@ -641,41 +600,48 @@ spawned_children(int np) goto memerror; for (i = nchildren; i--; ) cproc[i] = sp_inactive; cproc[nchildren-1].flags |= PF_FILT_OUT; - /* start each child */ - for (i = 0; i < nchildren; i++) { - rv = open_process(&cproc[i], NULL); - if (rv <= 0) break; - } + /* start each child from parent */ + for (i = 0; i < nchildren; i++) + if ((rv = open_process(&cproc[i], NULL)) <= 0) + break; /* child breaks here */ if (rv < 0) { - perror("fork"); + perror("fork"); /* WTH? */ close_processes(cproc, i); exit(1); } - if (rv) { /* are we the parent? */ - i = nchildren-1; /* last child is sole reader */ - while (i-- > 0) - close(cproc[i].r); - return(1); /* parent return value */ + if (i != nchildren-1) { /* last child is sole reader */ + int j = i; + while (j-- > 0) { + close(cproc[j].r); + cproc[j].r = -1; + } } - inchild = i; /* our child index */ + if (rv > 0) + return(1); /* parent return value */ + + inchild = i; /* else set our child index */ while (i-- > 0) /* only parent writes siblings */ close(cproc[i].w); - if (inchild == nchildren-1) /* last child sequences output */ - exit(output_loop() ? 0 : 1); + i = nmats; /* close matrix streams (carefully) */ + while (i-- > 0) { + if (mop[i].infp != stdin) { + close(fileno(mop[i].infp)); /* avoid lseek() */ + fclose(mop[i].infp); /* ! pclose() */ + } + mop[i].infp = NULL; + } + fpurge(stdin); /* discard previously buffered input */ - i = inchild; /* won't read from siblings */ - while (i-- > 0) - close(cproc[i].r); - i = nmats; /* close input matrix streams */ + if (inchild == nchildren-1) + return(-1); /* output process return value */ + + i = nmats; /* get matrix rows from parent */ while (i-- > 0) { - if (mop[i].infp != stdin) - fclose(mop[i].infp); /* ! pclose() */ mop[i].infp = stdin; mop[i].imx.dtype = DTrmx_native; mop[i].imx.pflags &= ~RMF_SWAPIN; } - fpurge(stdin); /* discard any previous matrix input */ #ifdef getc_unlocked flockfile(stdin); #endif @@ -686,7 +652,7 @@ memerror: exit(1); } -static int +int parent_loop(void) { int i; @@ -707,7 +673,7 @@ parent_loop(void) 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: parent read error at row %d\n", + fprintf(stderr, "%s: parent_loop() load error at row %d\n", mop[i].inspec, cur_row); return(0); } @@ -715,13 +681,17 @@ parent_loop(void) break; for (i = 0; i < nmats; i++) if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx)) - != rmx_array_size(&mop[i].imx)) + != rmx_array_size(&mop[i].imx)) { + fprintf(stderr, "%s: parent_loop() write error at row %d\n", + mop[i].inspec, cur_row); return(0); + } } - i = close_processes(cproc, nchildren); + i = close_processes(cproc, nchildren); /* collect family */ free(cproc); cproc = NULL; nchildren = 0; if (i < 0) { - fputs("Warning: lost child in parent_loop()\n", stderr); + if (!nowarn) + fputs("Warning: lost child in parent_loop()\n", stderr); return(1); } if (i > 0) { @@ -729,12 +699,9 @@ parent_loop(void) return(0); } return(1); /* return success! */ -memerror: - fputs("Out of memory in parent_loop()\n", stderr); - exit(1); } -static int +int combine_input(void) { const int row0 = (inchild >= 0)*inchild; @@ -745,9 +712,10 @@ combine_input(void) int co_set; int i; - if (mcat && mcat_last && - !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) - goto memerror; + if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) { + fputs("Out of buffer space in combine_input()\n", stderr); + return(0); + } /* figure out what the user set */ co_set = fundefined("co"); if (!co_set) @@ -769,7 +737,7 @@ combine_input(void) 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", + fprintf(stderr, "%s: combine_input() load error at row %d\n", mop[i].inspec, cur_row); return(0); } @@ -815,21 +783,59 @@ combine_input(void) } rmx_free(mres); mres = NULL; if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, - res->rmp->ncols, res->rmp->dtype, stdout)) + res->rmp->ncols, res->rmp->dtype, stdout) || + (inchild >= 0 && fflush(stdout) == EOF)) { + fprintf(stderr, "Conversion/write error at row %d in combine_input()\n", + cur_row); return(0); - if (inchild >= 0 && fflush(stdout) == EOF) - return(0); + } } return(inchild >= 0 || fflush(stdout) != EOF); -memerror: - fputs("Out of buffer space in combine_input()\n", stderr); - return(0); multerror: fputs("Unexpected matrix multiply error in combine_input()\n", stderr); return(0); } -static int +int +output_loop(void) +{ + const size_t row_size = rmx_array_size(mop[nmats].rmp); + int cur_child = 0; + int i = nmats; + + while (i-- > 0) { /* free input buffers */ + rmx_reset(&mop[i].imx); + if (mop[i].rmp != &mop[i].imx) { + rmx_free(mop[i].rmp); + mop[i].rmp = &mop[i].imx; + } + } + if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ + rmx_reset(&mop[nmats].imx); +#ifdef getc_unlocked + flockfile(stdout); /* we own this, now */ +#endif + for ( ; ; ) { /* loop until no more */ + ssize_t rv; + rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size); + if (!rv) /* out of rows? */ + break; + if (rv != row_size) { + fputs("Read error in output_loop()\n", stderr); + return(0); + } /* do final conversion */ + if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, + mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { + fputs("Conversion/write error in output_loop()\n", stderr); + return(0); + } + cur_child++; + cur_child *= (cur_child < inchild); /* loop over workers */ + } + return(fflush(stdout) != EOF); +} + +int get_factors(double da[], int n, char *av[]) { int ac; @@ -839,14 +845,14 @@ get_factors(double da[], int n, char *av[]) return(ac); } -static void +void resize_inparr(int n2alloc) { int i; if (n2alloc == nall) return; - for (i = nall; i > n2alloc; i--) { + for (i = nall; i-- > n2alloc; ) { rmx_reset(&mop[i].imx); if (mop[i].rmp != &mop[i].imx) rmx_free(mop[i].rmp); @@ -941,17 +947,17 @@ main(int argc, char *argv[]) } break; case 'C': + mcat_last = 0; if (!n || isflt(argv[i+1])) goto userr; defCsym = mop[nmats].preop.csym = argv[++i]; mop[nmats].preop.clen = 0; - mcat_last = 0; break; case 'c': + mcat_last = 0; 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; @@ -964,16 +970,15 @@ main(int argc, char *argv[]) goto userr; } mop[nmats].preop.csym = NULL; - mcat_last = 0; break; case 'm': + mcat_last = 1; if (!n) goto userr; if (argv[++i][0] == '-' && !argv[i][1]) { if (stdin_used++) goto stdin_error; mcat_spec = stdin_name; } else mcat_spec = argv[i]; - mcat_last = 1; break; default: fprintf(stderr, "%s: unknown option '%s'\n", @@ -1042,9 +1047,12 @@ main(int argc, char *argv[]) return(1); } doptimize(1); /* optimize definitions */ - if (spawned_children(nproc)) /* running in parent process? */ + i = spawned_children(nproc); /* create multiple processes if requested */ + if (i > 0) /* running in parent process? */ return(parent_loop() ? 0 : 1); - /* process & write rows */ + if (i < 0) /* running in output process? */ + return(output_loop() ? 0 : 1); + /* else we are a worker process */ return(combine_input() ? 0 : 1); stdin_error: fprintf(stderr, "%s: %s used for more than one input\n",