ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcomb.c
(Generate patch)

Comparing ray/src/util/rcomb.c (file contents):
Revision 2.11 by greg, Tue May 21 16:27:26 2024 UTC vs.
Revision 2.28 by greg, Fri Mar 28 00:06:36 2025 UTC

# Line 3 | Line 3 | static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   * General component matrix combiner, operating on a row at a time.
6 + *
7 + * Multi-processing mode under Unix creates children that each work
8 + * on one input row at a time, fed by the original process.  Final conversion
9 + * and output to stdout is sorted by last child while its siblings send it
10 + * their record calculations.
11   */
12  
13   #include <math.h>
# Line 16 | Line 21 | static const char RCSid[] = "$Id$";
21   #define M_PI    3.14159265358979323846
22   #endif
23  
24 + #ifndef MAXCOMP
25   #define MAXCOMP         MAXCSAMP        /* #components we support */
26 + #endif
27  
28   /* Unary matrix operation(s) */
29   typedef struct {
# Line 57 | Line 64 | SUBPROC                *cproc = NULL;                  /* child process array */
64   int             nchildren = 0;                  /* # of child processes */
65   int             inchild = -1;                   /* our child ID (-1: parent) */
66  
67 < static int      checksymbolic(ROPMAT *rop);
67 > extern int      checksymbolic(ROPMAT *rop);
68  
69 < static int
69 > int
70   split_input(ROPMAT *rop)
71   {
72          if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) {
# Line 71 | Line 78 | split_input(ROPMAT *rop)
78   }
79  
80   /* Check/set transform based on a reference input file */
81 < static int
81 > int
82   checkreffile(ROPMAT *rop)
83   {
84          static const char       *curRF = NULL;
# Line 134 | Line 141 | checkreffile(ROPMAT *rop)
141   }
142  
143   /* Compute conversion row from spectrum to one channel of RGB */
144 < static void
144 > void
145   rgbrow(ROPMAT *rop, int r, int p)
146   {
147          const int       nc = rop->imx.ncomp;
# Line 151 | Line 158 | rgbrow(ROPMAT *rop, int r, int p)
158   }
159  
160   /* Compute conversion row from spectrum to one channel of XYZ */
161 < static void
161 > void
162   xyzrow(ROPMAT *rop, int r, int p)
163   {
164          const int       nc = rop->imx.ncomp;
# Line 168 | Line 175 | xyzrow(ROPMAT *rop, int r, int p)
175   }
176  
177   /* Use the spectral sensitivity function to compute matrix coefficients */
178 < static void
179 < sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
178 > void
179 > sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4]))
180   {
181          const int       nc = rop->imx.ncomp;
182          int             i;
# Line 183 | Line 190 | sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in
190   }
191  
192   /* Check/set symbolic transform */
193 < static int
193 > int
194   checksymbolic(ROPMAT *rop)
195   {
196          const int       nc = rop->imx.ncomp;
# Line 297 | Line 304 | checksymbolic(ROPMAT *rop)
304          return(1);
305   }
306  
307 < static int
307 > int
308   get_component_xfm(ROPMAT *rop)
309   {
310          int     i, j;
# Line 380 | Line 387 | get_component_xfm(ROPMAT *rop)
387          return(1);
388   }
389  
390 < static int
390 > int
391   apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro)
392   {
393          if (ro->clen > 0) {
# Line 399 | Line 406 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
406          return(1);
407   }
408  
409 < static int
409 > int
410   open_input(ROPMAT *rop)
411   {
412          int     outtype;
# Line 421 | Line 428 | open_input(ROPMAT *rop)
428   }
429  
430   /* Return nominal wavelength associated with input component (return nm) */
431 < static double
431 > double
432   l_wavelength(char *nam)
433   {
434          double  comp = argument(1);
# Line 445 | Line 452 | l_wavelength(char *nam)
452   }
453  
454   /* Return ith input with optional channel selector */
455 < static double
455 > double
456   l_chanin(char *nam)
457   {
458          double  inp = argument(1);
# Line 470 | Line 477 | l_chanin(char *nam)
477          return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]);
478   }
479  
480 < static int
480 > int
481   initialize(RMATRIX *imp)
482   {
483          int     i;
# Line 483 | Line 490 | initialize(RMATRIX *imp)
490                  restype = mop[i].rmp->dtype;
491                  if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0)
492                          imp->dtype = restype;
493 <                else
493 >                else if (!nowarn)
494                          fprintf(stderr, "%s: warning - data type mismatch\n",
495                                          mop[i].inspec);
496                  if (!i) {
# Line 517 | Line 524 | initialize(RMATRIX *imp)
524          return(1);
525   }
526  
527 < static void
527 > void
528   output_headinfo(FILE *fp)
529   {
530          int     i;
# Line 540 | Line 547 | output_headinfo(FILE *fp)
547          }
548   }
549  
550 < static int
550 > int
551   spawned_children(int np)
552   {
553          int     i, rv;
554  
555   #if defined(_WIN32) || defined(_WIN64)
556          if (np > 1) {
557 <                fputs("Warning: only one process under Windows\n", stderr);
557 >                if (!nowarn)
558 >                        fputs("Warning: only one process under Windows\n", stderr);
559                  np = 1;
560          } else
561   #endif
562 <        if ((in_nrows > 0) & (np > in_nrows))
563 <                np = in_nrows;
562 >        if ((in_nrows > 0) & (np*4 > in_nrows))
563 >                np = in_nrows/4;
564                                  /* we'll be doing a row at a time */
565          for (i = 0; i < nmats; i++) {
566                  mop[i].imx.nrows = 1;
# Line 564 | Line 572 | spawned_children(int np)
572                                  goto memerror;
573                  }
574          }
575 <                                /* prep output row buffer */
576 <        if (mcat || mop[nmats].preop.clen > 0) {
575 >                                /* prep output row buffer(s) */
576 >        if (mop[nmats].preop.clen > 0) {
577                  if (!split_input(&mop[nmats]))  /* need separate buffer */
570                        return(0);
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))
578                          goto memerror;
579 +                mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
580 +                                        mop[nmats].imx.ncomp;
581          }
582          mop[nmats].imx.nrows = 1;
583          if (!rmx_prepare(&mop[nmats].imx))
584                  goto memerror;
585 <        if (np <= 1) {          /* single process return point */
585 >        if (mop[nmats].rmp != &mop[nmats].imx) {
586 >                mop[nmats].rmp->nrows = 1;
587 >                if (!rmx_prepare(mop[nmats].rmp))
588 >                        goto memerror;
589 >        }
590 >        if (np <= 1) {          /* single process return */
591   #ifdef getc_unlocked
592                  for (i = 0; i < nmats; i++)
593                          flockfile(mop[i].infp);
# Line 587 | Line 596 | spawned_children(int np)
596                  return(0);
597          }
598          fflush(stdout);         /* flush header & spawn children */
599 <        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np);
599 >        nchildren = np + 1;     /* extra child to sequence output */
600 >        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*nchildren);
601          if (!cproc)
602                  goto memerror;
603 <        nchildren = np;
604 <        for (i = 0; i < np; i++) {
605 <                cproc[i].flags = PF_FILT_OUT;
606 <                cproc[i].w = dup(1);
607 <                cproc[i].r = 0;
608 <                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 */
603 >        for (i = nchildren; i--; ) cproc[i] = sp_inactive;
604 >        cproc[nchildren-1].flags |= PF_FILT_OUT;
605 >                                /* start each child from parent */
606 >        for (i = 0; i < nchildren; i++)
607 >                if ((rv = open_process(&cproc[i], NULL)) <= 0)
608 >                        break;  /* child breaks here */
609          if (rv < 0) {
610 <                perror("fork");
610 >                perror("fork"); /* WTH? */
611 >                close_processes(cproc, i);
612                  exit(1);
613          }
614 <        inchild = i;            /* our child index */
615 <        while (i-- > 0)         /* don't share siblings' pipes */
614 >        if (i != nchildren-1) { /* last child is sole reader */
615 >                int     j = i;
616 >                while (j-- > 0) {
617 >                        close(cproc[j].r);
618 >                        cproc[j].r = -1;
619 >                }
620 >        }
621 >        if (rv > 0)
622 >                return(1);      /* parent return value */
623 >
624 >        inchild = i;            /* else set our child index */
625 >        while (i-- > 0)         /* only parent writes siblings */
626                  close(cproc[i].w);
627 <        fpurge(stdin);          /* discard previous matrix input */
627 >
628 >        i = nmats;              /* close matrix streams (carefully) */
629 >        while (i-- > 0) {
630 >                if (mop[i].infp != stdin) {
631 >                        close(fileno(mop[i].infp));     /* avoid lseek() */
632 >                        fclose(mop[i].infp);            /* ! pclose() */
633 >                }
634 >                mop[i].infp = NULL;
635 >        }
636 >        fpurge(stdin);          /* discard previously buffered input */
637 >
638 >        if (inchild == nchildren-1)
639 >                return(-1);     /* output process return value */
640 >
641 >        i = nmats;              /* get matrix rows from parent */
642 >        while (i-- > 0) {
643 >                mop[i].infp = stdin;
644 >                mop[i].imx.dtype = DTrmx_native;
645 >                mop[i].imx.pflags &= ~RMF_SWAPIN;
646 >        }
647   #ifdef getc_unlocked
648          flockfile(stdin);
649   #endif
650 <        for (i = 0; i < nmats; i++) {
651 <                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 */
650 >        mop[nmats].rmp->dtype = DTrmx_native;
651 >        return(0);              /* worker child return value */
652   memerror:
653          fputs("Out of memory in spawned_children()\n", stderr);
654          exit(1);
655   }
656  
657 < static int
658 < parent_loop()
657 > int
658 > parent_loop(void)
659   {
630        FILE    **outfp = (FILE **)malloc(nchildren*sizeof(FILE *));
660          int     i;
661  
662 <        if (!outfp) goto memerror;
663 <        for (i = 0; i < nchildren; i++) {
664 <                outfp[i] = fdopen(cproc[i].w, "w");
665 <                if (!outfp[i]) goto memerror;
637 < #ifdef getc_unlocked
638 <                flockfile(outfp[i]);
639 < #endif
662 >        rmx_reset(&mop[nmats].imx);             /* not touching output side */
663 >        if (mop[nmats].rmp != &mop[nmats].imx) {
664 >                rmx_free(mop[nmats].rmp);
665 >                mop[nmats].rmp = &mop[nmats].imx;
666          }
667   #ifdef getc_unlocked
668 <        for (i = 0; i < nmats; i++)
668 >        for (i = 0; i < nmats; i++)             /* we handle matrix inputs */
669                  flockfile(mop[i].infp);
670   #endif
671 +                                                /* load & send rows to kids */
672          for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
673 <            FILE        *ofp = outfp[cur_row % nchildren];
673 >            int         wfd = cproc[cur_row % (nchildren-1)].w;
674              for (i = 0; i < nmats; i++)
675                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
676                          if (cur_row > in_nrows) /* unknown #input rows? */
677                                  break;
678 <                        fprintf(stderr, "%s: read error at row %d\n",
678 >                        fprintf(stderr, "%s: load error at row %d\n",
679                                          mop[i].inspec, cur_row);
680                          return(0);
681                  }
682              if (i < nmats)
683                  break;
684              for (i = 0; i < nmats; i++)
685 <                if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp,
686 <                                mop[i].imx.ncols, DTdouble, ofp))
687 <                        return(0);
688 <            if (fflush(ofp) == EOF)
689 <                return(0);
685 >                if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx))
686 >                                        != rmx_array_size(&mop[i].imx)) {
687 >                        fprintf(stderr, "%s: write error at row %d\n",
688 >                                        mop[i].inspec, cur_row);
689 >                        return(0);
690 >                }
691          }
692 <        for (i = 0; i < nchildren; i++)
693 <                fclose(outfp[i]);
666 <        free(outfp);
667 <        i = close_processes(cproc, nchildren);
668 <        free(cproc); cproc = NULL;
692 >        i = close_processes(cproc, nchildren);  /* collect family */
693 >        free(cproc); cproc = NULL; nchildren = 0;
694          if (i < 0) {
695 <                fputs("Warning: missing child in parent_loop()\n", stderr);
695 >                if (!nowarn)
696 >                        fputs("Warning: lost child process\n", stderr);
697                  return(1);
698          }
699          if (i > 0) {
700                  fprintf(stderr, "Child exited with status %d\n", i);
701                  return(0);
702          }
703 <        return(1);
678 < memerror:
679 <        fputs("Out of memory in parent_loop()\n", stderr);
680 <        exit(1);
703 >        return(1);                              /* return success! */
704   }
705  
706 < static int
707 < combine_input()
706 > int
707 > combine_input(void)
708   {
709          const int       row0 = (inchild >= 0)*inchild;
710 <        const int       rstep = nchildren + !nchildren;
710 >        const int       rstep = nchildren ? nchildren-1 : 1;
711          ROPMAT          *res = &mop[nmats];
712          int             set_r, set_c;
713          RMATRIX         *tmp = NULL;
714          int             co_set;
715          int             i;
716  
717 <        if (mcat && mcat_last &&
718 <                        !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
719 <                goto memerror;
717 >        if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) {
718 >                fputs("Out of buffer space in combine_input()\n", stderr);
719 >                return(0);
720 >        }
721                                          /* figure out what the user set */
722          co_set = fundefined("co");
723          if (!co_set)
# Line 715 | Line 739 | combine_input()
739                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
740                          if (cur_row > in_nrows) /* unknown #input rows? */
741                                  break;
742 <                        fprintf(stderr, "%s: read error at row %d\n",
742 >                        fprintf(stderr, "%s: load error at row %d\n",
743                                          mop[i].inspec, cur_row);
744                          return(0);
745                  }
# Line 760 | Line 784 | combine_input()
784                          return(0);
785              }
786              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            }
787              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
788 <                                res->rmp->ncols, res->rmp->dtype, stdout))
788 >                                res->rmp->ncols, res->rmp->dtype, stdout) ||
789 >                                 (inchild >= 0 && fflush(stdout) == EOF)) {
790 >                fprintf(stderr, "Conversion/write error at row %d\n",
791 >                                cur_row);
792                  return(0);
793 <            if (inchild >= 0 && fflush(stdout) == EOF)
771 <                return(0);
793 >            }
794          }
795          return(inchild >= 0 || fflush(stdout) != EOF);
774 memerror:
775        fputs("Out of buffer space in combine_input()\n", stderr);
776        return(0);
796   multerror:
797 <        fputs("Unexpected matrix multiply error in combine_input()\n", stderr);
797 >        fputs("Unexpected matrix multiply error\n", stderr);
798          return(0);
799   }
800  
801 < static int
801 > int
802 > output_loop(void)
803 > {
804 >        const size_t    row_size = rmx_array_size(mop[nmats].rmp);
805 >        int             cur_child = 0;
806 >        int             i = nmats;
807 >
808 >        while (i-- > 0) {                               /* free input buffers */
809 >                rmx_reset(&mop[i].imx);
810 >                if (mop[i].rmp != &mop[i].imx) {
811 >                        rmx_free(mop[i].rmp);
812 >                        mop[i].rmp = &mop[i].imx;
813 >                }
814 >        }
815 >        if (mop[nmats].rmp != &mop[nmats].imx)          /* output is split? */
816 >                rmx_reset(&mop[nmats].imx);
817 > #ifdef getc_unlocked
818 >        flockfile(stdout);                              /* we own this, now */
819 > #endif
820 >        for ( ; ; ) {                                   /* loop until no more */
821 >                ssize_t         rv;
822 >                rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size);
823 >                if (!rv)                                /* out of rows? */
824 >                        break;
825 >                if (rv != row_size) {
826 >                        fputs("Read error\n", stderr);
827 >                        return(0);
828 >                }                                       /* do final conversion */
829 >                if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp,
830 >                                mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) {
831 >                        fputs("Conversion/write error\n", stderr);
832 >                        return(0);
833 >                }
834 >                cur_child++;
835 >                cur_child *= (cur_child < inchild);     /* loop over workers */
836 >        }
837 >        return(fflush(stdout) != EOF);
838 > }
839 >
840 > int
841   get_factors(double da[], int n, char *av[])
842   {
843          int     ac;
# Line 789 | Line 847 | get_factors(double da[], int n, char *av[])
847          return(ac);
848   }
849  
850 < static void
850 > void
851   resize_inparr(int n2alloc)
852   {
853          int     i;
854  
855          if (n2alloc == nall)
856                  return;
857 <        for (i = nall; i > n2alloc; i--) {
857 >        for (i = nall; i-- > n2alloc; ) {
858                  rmx_reset(&mop[i].imx);
859                  if (mop[i].rmp != &mop[i].imx)
860                          rmx_free(mop[i].rmp);
# Line 891 | Line 949 | main(int argc, char *argv[])
949                                  }
950                                  break;
951                          case 'C':
952 +                                mcat_last = 0;
953                                  if (!n || isflt(argv[i+1]))
954                                          goto userr;
955                                  defCsym = mop[nmats].preop.csym = argv[++i];
956                                  mop[nmats].preop.clen = 0;
898                                mcat_last = 0;
957                                  break;
958                          case 'c':
959 +                                mcat_last = 0;
960                                  if (n && !isflt(argv[i+1])) {
961                                          mop[nmats].preop.csym = argv[++i];
962                                          mop[nmats].preop.clen = 0;
# Line 913 | Line 972 | main(int argc, char *argv[])
972                                          goto userr;
973                                  }
974                                  mop[nmats].preop.csym = NULL;
916                                mcat_last = 0;
975                                  break;
976                          case 'm':
977 +                                mcat_last = 1;
978                                  if (!n) goto userr;
979                                  if (argv[++i][0] == '-' && !argv[i][1]) {
980                                          if (stdin_used++) goto stdin_error;
981                                          mcat_spec = stdin_name;
982                                  } else
983                                          mcat_spec = argv[i];
925                                mcat_last = 1;
984                                  break;
985                          default:
986                                  fprintf(stderr, "%s: unknown option '%s'\n",
# Line 991 | Line 1049 | main(int argc, char *argv[])
1049                  return(1);
1050          }
1051          doptimize(1);                   /* optimize definitions */
1052 <        if (spawned_children(nproc))    /* running in parent process? */
1052 >        i = spawned_children(nproc);    /* create multiple processes if requested */
1053 >        if (i > 0)                      /* running in parent process? */
1054                  return(parent_loop() ? 0 : 1);
1055 <                                        /* process & write rows */
1055 >        if (i < 0)                      /* running in output process? */
1056 >                return(output_loop() ? 0 : 1);
1057 >                                        /* else we are a worker process */
1058          return(combine_input() ? 0 : 1);
1059   stdin_error:
1060          fprintf(stderr, "%s: %s used for more than one input\n",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines