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.12 by greg, Tue May 21 17:39:17 2024 UTC vs.
Revision 2.31 by greg, Fri Apr 18 23:59:03 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  
8 #include <signal.h>
13   #include <math.h>
14   #include "platform.h"
15   #include "rtprocess.h"
# Line 17 | Line 21 | static const char RCSid[] = "$Id$";
21   #define M_PI    3.14159265358979323846
22   #endif
23  
20 #define MAXCOMP         MAXCSAMP        /* #components we support */
21
24   /* Unary matrix operation(s) */
25   typedef struct {
26          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
# Line 57 | Line 59 | int            cur_chan;                       /* if we're looping channels */
59   SUBPROC         *cproc = NULL;                  /* child process array */
60   int             nchildren = 0;                  /* # of child processes */
61   int             inchild = -1;                   /* our child ID (-1: parent) */
60 int             gpid = -1;                      /* group process ID (parent) */
61 int             nr_out = 0;                     /* # of rows output by kids */
62  
63 < static int      checksymbolic(ROPMAT *rop);
63 > extern int      checksymbolic(ROPMAT *rop);
64  
65 < static void
66 < on_sigio(int dummy)
67 < {
68 <        nr_out++;                       /* happens when child outputs row */
69 <        signal(SIGIO, on_sigio);        /* reset to maximize portability */
70 < }
71 <
72 < static int
65 > int
66   split_input(ROPMAT *rop)
67   {
68          if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) {
# Line 81 | Line 74 | split_input(ROPMAT *rop)
74   }
75  
76   /* Check/set transform based on a reference input file */
77 < static int
77 > int
78   checkreffile(ROPMAT *rop)
79   {
80          static const char       *curRF = NULL;
# Line 144 | Line 137 | checkreffile(ROPMAT *rop)
137   }
138  
139   /* Compute conversion row from spectrum to one channel of RGB */
140 < static void
140 > void
141   rgbrow(ROPMAT *rop, int r, int p)
142   {
143          const int       nc = rop->imx.ncomp;
# Line 161 | Line 154 | rgbrow(ROPMAT *rop, int r, int p)
154   }
155  
156   /* Compute conversion row from spectrum to one channel of XYZ */
157 < static void
157 > void
158   xyzrow(ROPMAT *rop, int r, int p)
159   {
160          const int       nc = rop->imx.ncomp;
# Line 178 | Line 171 | xyzrow(ROPMAT *rop, int r, int p)
171   }
172  
173   /* Use the spectral sensitivity function to compute matrix coefficients */
174 < static void
175 < sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
174 > void
175 > sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4]))
176   {
177          const int       nc = rop->imx.ncomp;
178          int             i;
# Line 193 | Line 186 | sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, in
186   }
187  
188   /* Check/set symbolic transform */
189 < static int
189 > int
190   checksymbolic(ROPMAT *rop)
191   {
192          const int       nc = rop->imx.ncomp;
# Line 307 | Line 300 | checksymbolic(ROPMAT *rop)
300          return(1);
301   }
302  
303 < static int
303 > int
304   get_component_xfm(ROPMAT *rop)
305   {
306          int     i, j;
# Line 390 | Line 383 | get_component_xfm(ROPMAT *rop)
383          return(1);
384   }
385  
386 < static int
386 > int
387   apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro)
388   {
389          if (ro->clen > 0) {
# Line 409 | Line 402 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
402          return(1);
403   }
404  
405 < static int
405 > int
406   open_input(ROPMAT *rop)
407   {
408          int     outtype;
# Line 431 | Line 424 | open_input(ROPMAT *rop)
424   }
425  
426   /* Return nominal wavelength associated with input component (return nm) */
427 < static double
427 > double
428   l_wavelength(char *nam)
429   {
430          double  comp = argument(1);
# Line 455 | Line 448 | l_wavelength(char *nam)
448   }
449  
450   /* Return ith input with optional channel selector */
451 < static double
451 > double
452   l_chanin(char *nam)
453   {
454          double  inp = argument(1);
# Line 480 | Line 473 | l_chanin(char *nam)
473          return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]);
474   }
475  
476 < static int
476 > int
477   initialize(RMATRIX *imp)
478   {
479          int     i;
# Line 493 | Line 486 | initialize(RMATRIX *imp)
486                  restype = mop[i].rmp->dtype;
487                  if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0)
488                          imp->dtype = restype;
489 <                else
489 >                else if (!nowarn)
490                          fprintf(stderr, "%s: warning - data type mismatch\n",
491                                          mop[i].inspec);
492                  if (!i) {
# Line 527 | Line 520 | initialize(RMATRIX *imp)
520          return(1);
521   }
522  
523 < static void
523 > void
524   output_headinfo(FILE *fp)
525   {
526          int     i;
# Line 550 | Line 543 | output_headinfo(FILE *fp)
543          }
544   }
545  
546 < static int
546 > int
547   spawned_children(int np)
548   {
549          int     i, rv;
550  
551   #if defined(_WIN32) || defined(_WIN64)
552          if (np > 1) {
553 <                fputs("Warning: only one process under Windows\n", stderr);
553 >                if (!nowarn)
554 >                        fputs("Warning: only one process under Windows\n", stderr);
555                  np = 1;
556          } else
557   #endif
558 <        if ((in_nrows > 0) & (np > in_nrows))
559 <                np = in_nrows;
558 >        if ((in_nrows > 0) & (np*4 > in_nrows))
559 >                np = in_nrows/4;
560                                  /* we'll be doing a row at a time */
561          for (i = 0; i < nmats; i++) {
562                  mop[i].imx.nrows = 1;
# Line 574 | Line 568 | spawned_children(int np)
568                                  goto memerror;
569                  }
570          }
571 <                                /* prep output row buffer */
572 <        if (mcat || mop[nmats].preop.clen > 0) {
571 >                                /* prep output row buffer(s) */
572 >        if (mop[nmats].preop.clen > 0) {
573                  if (!split_input(&mop[nmats]))  /* need separate buffer */
580                        return(0);
581                if (mop[nmats].preop.clen > 0)
582                        mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
583                                                mop[nmats].imx.ncomp;
584                mop[nmats].rmp->nrows = 1;
585                if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp))
574                          goto memerror;
575 +                mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
576 +                                        mop[nmats].imx.ncomp;
577          }
578          mop[nmats].imx.nrows = 1;
579          if (!rmx_prepare(&mop[nmats].imx))
580                  goto memerror;
581 <        if (np <= 1) {          /* single process return point */
581 >        if (mop[nmats].rmp != &mop[nmats].imx) {
582 >                mop[nmats].rmp->nrows = 1;
583 >                if (!rmx_prepare(mop[nmats].rmp))
584 >                        goto memerror;
585 >        }
586 >        if (np <= 1) {          /* single process return */
587   #ifdef getc_unlocked
588                  for (i = 0; i < nmats; i++)
589                          flockfile(mop[i].infp);
# Line 596 | Line 591 | spawned_children(int np)
591   #endif
592                  return(0);
593          }
599        gpid = setpgrp();       /* set group process ID */
600        signal(SIGIO, on_sigio);
594          fflush(stdout);         /* flush header & spawn children */
595 <        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np);
595 >        nchildren = np + 1;     /* extra child to sequence output */
596 >        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*nchildren);
597          if (!cproc)
598                  goto memerror;
599 <        nchildren = np;
600 <        for (i = 0; i < np; i++) {
601 <                cproc[i].flags = PF_FILT_OUT;
602 <                cproc[i].w = dup(1);
603 <                cproc[i].r = 0;
604 <                cproc[i].pid = -1;
611 <                rv = open_process(&cproc[i], NULL);
612 <                if (rv <= 0) break;
613 <        }
614 <        if (rv > 0)
615 <                return(1);      /* parent return value */
599 >        for (i = nchildren; i--; ) cproc[i] = sp_inactive;
600 >        cproc[nchildren-1].flags |= PF_FILT_OUT;
601 >                                /* start each child from parent */
602 >        for (i = 0; i < nchildren; i++)
603 >                if ((rv = open_process(&cproc[i], NULL)) <= 0)
604 >                        break;  /* child breaks here */
605          if (rv < 0) {
606 <                perror("fork");
606 >                perror("fork"); /* WTH? */
607 >                close_processes(cproc, i);
608                  exit(1);
609          }
610 <        inchild = i;            /* our child index */
611 <        while (i-- > 0)         /* don't share siblings' pipes */
610 >        if (i != nchildren-1) { /* last child is sole reader */
611 >                int     j = i;
612 >                while (j-- > 0) {
613 >                        close(cproc[j].r);
614 >                        cproc[j].r = -1;
615 >                }
616 >        }
617 >        if (rv > 0)
618 >                return(1);      /* parent return value */
619 >
620 >        inchild = i;            /* else set our child index */
621 >        while (i-- > 0)         /* only parent writes siblings */
622                  close(cproc[i].w);
623 <        fpurge(stdin);          /* discard previous matrix input */
623 >
624 >        i = nmats;              /* close matrix streams (carefully) */
625 >        while (i-- > 0) {
626 >                if (mop[i].infp != stdin) {
627 >                        close(fileno(mop[i].infp));     /* avoid lseek() */
628 >                        fclose(mop[i].infp);            /* ! pclose() */
629 >                }
630 >                mop[i].infp = NULL;
631 >        }
632 >        fpurge(stdin);          /* discard previously buffered input */
633 >
634 >        if (inchild == nchildren-1)
635 >                return(-1);     /* output process return value */
636 >
637 >        i = nmats;              /* get matrix rows from parent */
638 >        while (i-- > 0) {
639 >                mop[i].infp = stdin;
640 >                mop[i].imx.dtype = DTrmx_native;
641 >                mop[i].imx.pflags &= ~RMF_SWAPIN;
642 >        }
643   #ifdef getc_unlocked
644          flockfile(stdin);
645   #endif
646 <        for (i = 0; i < nmats; i++) {
647 <                if (mop[i].infp != stdin)
629 <                        fclose(mop[i].infp);    /* ! pclose() */
630 <                mop[i].infp = stdin;
631 <                mop[i].imx.dtype = DTdouble;
632 <        }
633 <        return(0);              /* child return */
646 >        mop[nmats].rmp->dtype = DTrmx_native;
647 >        return(0);              /* worker child return value */
648   memerror:
649          fputs("Out of memory in spawned_children()\n", stderr);
650          exit(1);
651   }
652  
653 < static int
654 < parent_loop()
653 > int
654 > parent_loop(void)
655   {
642        FILE    **outfp = (FILE **)malloc(nchildren*sizeof(FILE *));
656          int     i;
657  
658 <        if (!outfp) goto memerror;
659 <        for (i = 0; i < nchildren; i++) {
660 <                outfp[i] = fdopen(cproc[i].w, "w");
661 <                if (!outfp[i]) goto memerror;
649 < #ifdef getc_unlocked
650 <                flockfile(outfp[i]);
651 < #endif
658 >        rmx_reset(&mop[nmats].imx);             /* not touching output side */
659 >        if (mop[nmats].rmp != &mop[nmats].imx) {
660 >                rmx_free(mop[nmats].rmp);
661 >                mop[nmats].rmp = &mop[nmats].imx;
662          }
663   #ifdef getc_unlocked
664 <        for (i = 0; i < nmats; i++)
664 >        for (i = 0; i < nmats; i++)             /* we handle matrix inputs */
665                  flockfile(mop[i].infp);
666   #endif
667 +                                                /* load & send rows to kids */
668          for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
669 <            FILE        *ofp = outfp[cur_row % nchildren];
669 >            int         wfd = cproc[cur_row % (nchildren-1)].w;
670              for (i = 0; i < nmats; i++)
671                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
672                          if (cur_row > in_nrows) /* unknown #input rows? */
673                                  break;
674 <                        fprintf(stderr, "%s: read error at row %d\n",
674 >                        fprintf(stderr, "%s: load error at row %d\n",
675                                          mop[i].inspec, cur_row);
676                          return(0);
677                  }
678              if (i < nmats)
679                  break;
680              for (i = 0; i < nmats; i++)
681 <                if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp,
682 <                                        mop[i].imx.ncols, DTdouble, ofp))
683 <                        return(0);
684 <            if (fflush(ofp) == EOF)
685 <                return(0);
681 >                if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx))
682 >                                        != rmx_array_size(&mop[i].imx)) {
683 >                        fprintf(stderr, "%s: write error at row %d\n",
684 >                                        mop[i].inspec, cur_row);
685 >                        return(0);
686 >                }
687          }
688 <        for (i = 0; i < nchildren; i++)
689 <                fclose(outfp[i]);
678 <        free(outfp);
679 <        i = close_processes(cproc, nchildren);
680 <        free(cproc); cproc = NULL;
688 >        i = close_processes(cproc, nchildren);  /* collect family */
689 >        free(cproc); cproc = NULL; nchildren = 0;
690          if (i < 0) {
691 <                fputs("Warning: missing child in parent_loop()\n", stderr);
691 >                if (!nowarn)
692 >                        fputs("Warning: lost child process\n", stderr);
693                  return(1);
694          }
695          if (i > 0) {
696                  fprintf(stderr, "Child exited with status %d\n", i);
697                  return(0);
698          }
699 <        return(1);
690 < memerror:
691 <        fputs("Out of memory in parent_loop()\n", stderr);
692 <        exit(1);
699 >        return(1);                              /* return success! */
700   }
701  
702 < static int
703 < combine_input()
702 > int
703 > combine_input(void)
704   {
705          const int       row0 = (inchild >= 0)*inchild;
706 <        const int       rstep = nchildren + !nchildren;
706 >        const int       rstep = nchildren ? nchildren-1 : 1;
707          ROPMAT          *res = &mop[nmats];
708          int             set_r, set_c;
709          RMATRIX         *tmp = NULL;
710          int             co_set;
711          int             i;
712  
713 <        if (mcat && mcat_last &&
714 <                        !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
715 <                goto memerror;
713 >        if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) {
714 >                fputs("Out of buffer space in combine_input()\n", stderr);
715 >                return(0);
716 >        }
717                                          /* figure out what the user set */
718          co_set = fundefined("co");
719          if (!co_set)
# Line 727 | Line 735 | combine_input()
735                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
736                          if (cur_row > in_nrows) /* unknown #input rows? */
737                                  break;
738 <                        fprintf(stderr, "%s: read error at row %d\n",
738 >                        fprintf(stderr, "%s: load error at row %d\n",
739                                          mop[i].inspec, cur_row);
740                          return(0);
741                  }
# Line 772 | Line 780 | combine_input()
780                          return(0);
781              }
782              rmx_free(mres); mres = NULL;
775            if (inchild >= 0)           /* children share stdout */
776                while (nr_out < cur_row)
777                    pause();            /* wait for our turn */
783              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
784 <                                res->rmp->ncols, res->rmp->dtype, stdout))
784 >                                res->rmp->ncols, res->rmp->dtype, stdout) ||
785 >                                 (inchild >= 0 && fflush(stdout) == EOF)) {
786 >                fprintf(stderr, "Conversion/write error at row %d\n",
787 >                                cur_row);
788                  return(0);
781            if (inchild >= 0) {         /* flush and notify group */
782                if (fflush(stdout) == EOF)
783                    return(0);
784                killpg(gpid, SIGIO);    /* increments everyone's nr_out */
789              }
790          }
791          return(inchild >= 0 || fflush(stdout) != EOF);
788 memerror:
789        fputs("Out of buffer space in combine_input()\n", stderr);
790        return(0);
792   multerror:
793 <        fputs("Unexpected matrix multiply error in combine_input()\n", stderr);
793 >        fputs("Unexpected matrix multiply error\n", stderr);
794          return(0);
795   }
796  
797 < static int
797 > int
798 > output_loop(void)
799 > {
800 >        const size_t    row_size = rmx_array_size(mop[nmats].rmp);
801 >        int             cur_child = 0;
802 >        int             i = nmats;
803 >
804 >        while (i-- > 0) {                               /* free input buffers */
805 >                rmx_reset(&mop[i].imx);
806 >                if (mop[i].rmp != &mop[i].imx) {
807 >                        rmx_free(mop[i].rmp);
808 >                        mop[i].rmp = &mop[i].imx;
809 >                }
810 >        }
811 >        if (mop[nmats].rmp != &mop[nmats].imx)          /* output is split? */
812 >                rmx_reset(&mop[nmats].imx);
813 > #ifdef getc_unlocked
814 >        flockfile(stdout);                              /* we own this, now */
815 > #endif
816 >        for ( ; ; ) {                                   /* loop until no more */
817 >                ssize_t         rv;
818 >                rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size);
819 >                if (!rv)                                /* out of rows? */
820 >                        break;
821 >                if (rv != row_size) {
822 >                        fputs("Read error\n", stderr);
823 >                        return(0);
824 >                }                                       /* do final conversion */
825 >                if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp,
826 >                                mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) {
827 >                        fputs("Conversion/write error\n", stderr);
828 >                        return(0);
829 >                }
830 >                cur_child++;
831 >                cur_child *= (cur_child < inchild);     /* loop over workers */
832 >        }
833 >        return(fflush(stdout) != EOF);
834 > }
835 >
836 > int
837   get_factors(double da[], int n, char *av[])
838   {
839          int     ac;
# Line 803 | Line 843 | get_factors(double da[], int n, char *av[])
843          return(ac);
844   }
845  
846 < static void
846 > void
847   resize_inparr(int n2alloc)
848   {
849          int     i;
850  
851          if (n2alloc == nall)
852                  return;
853 <        for (i = nall; i > n2alloc; i--) {
853 >        for (i = nall; i-- > n2alloc; ) {
854                  rmx_reset(&mop[i].imx);
855                  if (mop[i].rmp != &mop[i].imx)
856                          rmx_free(mop[i].rmp);
# Line 905 | Line 945 | main(int argc, char *argv[])
945                                  }
946                                  break;
947                          case 'C':
948 +                                mcat_last = 0;
949                                  if (!n || isflt(argv[i+1]))
950                                          goto userr;
951                                  defCsym = mop[nmats].preop.csym = argv[++i];
952                                  mop[nmats].preop.clen = 0;
912                                mcat_last = 0;
953                                  break;
954                          case 'c':
955 +                                mcat_last = 0;
956                                  if (n && !isflt(argv[i+1])) {
957                                          mop[nmats].preop.csym = argv[++i];
958                                          mop[nmats].preop.clen = 0;
# Line 927 | Line 968 | main(int argc, char *argv[])
968                                          goto userr;
969                                  }
970                                  mop[nmats].preop.csym = NULL;
930                                mcat_last = 0;
971                                  break;
972                          case 'm':
973 +                                mcat_last = 1;
974                                  if (!n) goto userr;
975                                  if (argv[++i][0] == '-' && !argv[i][1]) {
976                                          if (stdin_used++) goto stdin_error;
977                                          mcat_spec = stdin_name;
978                                  } else
979                                          mcat_spec = argv[i];
939                                mcat_last = 1;
980                                  break;
981                          default:
982                                  fprintf(stderr, "%s: unknown option '%s'\n",
# Line 951 | Line 991 | main(int argc, char *argv[])
991          resize_inparr(nmats+1);         /* extra matrix at end for result */
992          mop[nmats].inspec = "trailing_ops";
993                                          /* load final concatenation matrix */
994 <        if (mcat_spec && !(mcat = rmx_load(mcat_spec, RMPnone))) {
994 >        if (mcat_spec && !(mcat = rmx_load(mcat_spec))) {
995                  fprintf(stderr, "%s: error loading concatenation matrix: %s\n",
996                                  argv[0], mcat_spec);
997                  return(1);
# Line 994 | Line 1034 | main(int argc, char *argv[])
1034                          return(1);
1035                  mop[nmats].rmp->ncols = mcat->ncols;
1036          }
1037 + #if DTrmx_native==DTfloat
1038 +        if (outfmt == DTdouble)
1039 +                fprintf(stderr, "%s: warning - writing float result as double\n", argv[0]);
1040 + #endif
1041          newheader("RADIANCE", stdout);  /* write output header */
1042          if (echoheader)
1043                  output_headinfo(stdout);
# Line 1005 | 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