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.4 by greg, Tue Dec 19 16:09:20 2023 UTC vs.
Revision 2.21 by greg, Tue Jun 4 21:47:55 2024 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 <errno.h>
13   #include <math.h>
14   #include "platform.h"
15 + #include "rtprocess.h"
16   #include "rtio.h"
12 #include "resolu.h"
17   #include "rmatrix.h"
18   #include "calcomp.h"
15 #include "paths.h"
19  
20   #ifndef M_PI
21   #define M_PI    3.14159265358979323846
# Line 55 | Line 58 | int            cur_row;                        /* current input/output row */
58   int             cur_col;                        /* current input/output column */
59   int             cur_chan;                       /* if we're looping channels */
60  
61 + SUBPROC         *cproc = NULL;                  /* child process array */
62 + int             nchildren = 0;                  /* # of child processes */
63 + int             inchild = -1;                   /* our child ID (-1: parent) */
64 +
65   static int      checksymbolic(ROPMAT *rop);
66  
67   static int
# Line 391 | Line 398 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
398                          return(0);
399                  rmx_free(res);
400          } else if (dst != src)
401 <                memcpy(dst->mtx, src->mtx,
395 <                                sizeof(double)*dst->ncomp*dst->ncols*dst->nrows);
401 >                memcpy(dst->mtx, src->mtx, rmx_array_size(dst));
402          if (ro->nsf == dst->ncomp)
403                  rmx_scale(dst, ro->sca);
404          return(1);
# Line 540 | Line 546 | output_headinfo(FILE *fp)
546   }
547  
548   static int
549 < combine_input(ROPMAT *res, FILE *fout)
549 > output_loop(void)
550   {
551 <        int     set_r, set_c;
552 <        RMATRIX *tmp = NULL;
553 <        int     co_set;
554 <        int     i;
555 <                                        /* allocate input row buffers */
551 >        const size_t    row_size = rmx_array_size(mop[nmats].rmp);
552 >        int             i = nmats;
553 >        int             cur_child = 0;
554 >
555 >        if (mop[nmats].rmp != &mop[nmats].imx)          /* output is split? */
556 >                rmx_reset(&mop[nmats].imx);
557 >        while (i-- > 0) {                               /* close input matrices */
558 >                fclose(mop[i].infp);            /* ! pclose() */
559 >                mop[i].infp = NULL;
560 >                rmx_reset(&mop[i].imx);
561 >                if (mop[i].rmp != &mop[i].imx) {
562 >                        rmx_free(mop[i].rmp);
563 >                        mop[i].rmp = &mop[i].imx;
564 >                }
565 >        }
566 > #ifdef getc_unlocked
567 >        flockfile(stdout);                              /* we own this, now */
568 > #endif
569 >        for ( ; ; ) {                                   /* loop until no more */
570 >                ssize_t         rv;
571 >                rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size);
572 >                if (!rv)                                /* out of rows? */
573 >                        break;
574 >                if (rv != row_size) {
575 >                        fputs("Read error in output loop\n", stderr);
576 >                        return(0);
577 >                }                                       /* do final conversion */
578 >                if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp,
579 >                                mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) {
580 >                        fputs("Conversion/write error in output loop\n", stderr);
581 >                        return(0);
582 >                }
583 >                cur_child++;
584 >                cur_child *= (cur_child < inchild);
585 >        }
586 >        return(fflush(stdout) != EOF);
587 > }
588 >
589 > static int
590 > spawned_children(int np)
591 > {
592 >        int     i, rv;
593 >
594 > #if defined(_WIN32) || defined(_WIN64)
595 >        if (np > 1) {
596 >                fputs("Warning: only one process under Windows\n", stderr);
597 >                np = 1;
598 >        } else
599 > #endif
600 >        if ((in_nrows > 0) & (np*4 > in_nrows))
601 >                np = in_nrows/4;
602 >                                /* we'll be doing a row at a time */
603          for (i = 0; i < nmats; i++) {
604 <                mop[i].imx.nrows = 1;   /* we'll be doing a row at a time */
604 >                mop[i].imx.nrows = 1;
605                  if (!rmx_prepare(&mop[i].imx))
606                          goto memerror;
607                  if (mop[i].rmp != &mop[i].imx) {
# Line 557 | Line 610 | combine_input(ROPMAT *res, FILE *fout)
610                                  goto memerror;
611                  }
612          }
613 <                                        /* prep output row buffers */
614 <        if (mcat || res->preop.clen > 0) {
615 <                if (!split_input(res))  /* need separate buffer */
613 >                                /* prep output row buffer(s) */
614 >        if (mcat || mop[nmats].preop.clen > 0) {
615 >                if (!split_input(&mop[nmats]))  /* need separate buffer */
616                          return(0);
617 <                if (res->preop.clen > 0)
618 <                        res->rmp->ncomp = res->preop.clen / res->imx.ncomp;
619 <                res->rmp->nrows = 1;
620 <                if (!mcat | !mcat_last && !rmx_prepare(res->rmp))
617 >                if (mop[nmats].preop.clen > 0)
618 >                        mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
619 >                                                mop[nmats].imx.ncomp;
620 >        }
621 >        mop[nmats].imx.nrows = 1;
622 >        if (!rmx_prepare(&mop[nmats].imx))
623 >                goto memerror;
624 >        if (mop[nmats].rmp != &mop[nmats].imx) {
625 >                mop[nmats].rmp->nrows = 1;
626 >                if (!rmx_prepare(mop[nmats].rmp))
627                          goto memerror;
628          }
629 +        if (np <= 1) {          /* single process return */
630 + #ifdef getc_unlocked
631 +                for (i = 0; i < nmats; i++)
632 +                        flockfile(mop[i].infp);
633 +                flockfile(stdout);
634 + #endif
635 +                return(0);
636 +        }
637 +        fflush(stdout);         /* flush header & spawn children */
638 +        nchildren = np + 1;     /* extra child to sequence output */
639 +        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*nchildren);
640 +        if (!cproc)
641 +                goto memerror;
642 +        for (i = nchildren; i--; ) cproc[i] = sp_inactive;
643 +        cproc[nchildren-1].flags |= PF_FILT_OUT;
644 +                                /* start each child */
645 +        for (i = 0; i < nchildren; i++) {
646 +                rv = open_process(&cproc[i], NULL);
647 +                if (rv <= 0) break;
648 +        }
649 +        if (rv < 0) {
650 +                perror("fork");
651 +                close_processes(cproc, i);
652 +                exit(1);
653 +        }
654 +        if (rv) {               /* are we the parent? */
655 +                i = nchildren-1;        /* last child is sole reader */
656 +                while (i-- > 0) {
657 +                        close(cproc[i].r);
658 +                        cproc[i].r = -1;
659 +                }
660 +                return(1);      /* parent return value */
661 +        }
662 +        inchild = i;            /* our child index */
663 +        while (i-- > 0)         /* only parent writes siblings */
664 +                close(cproc[i].w);
665 +
666 +        if (inchild == nchildren-1)     /* last child sequences output */
667 +                exit(output_loop() ? 0 : 1);
668 +
669 +        i = inchild;            /* won't read from siblings */
670 +        while (i-- > 0)
671 +                close(cproc[i].r);
672 +        i = nmats;              /* redirect input matrix streams */
673 +        while (i-- > 0) {
674 +                if (mop[i].infp != stdin)
675 +                        fclose(mop[i].infp);    /* ! pclose() */
676 +                mop[i].infp = stdin;
677 +                mop[i].imx.dtype = DTrmx_native;
678 +                mop[i].imx.pflags &= ~RMF_SWAPIN;
679 +        }
680 +        fpurge(stdin);          /* discard any previous matrix input */
681 + #ifdef getc_unlocked
682 +        flockfile(stdin);
683 + #endif
684 +        mop[nmats].rmp->dtype = DTrmx_native;
685 +        return(0);              /* worker child return value */
686 + memerror:
687 +        fputs("Out of memory in spawned_children()\n", stderr);
688 +        exit(1);
689 + }
690 +
691 + static int
692 + parent_loop(void)
693 + {
694 +        int     i;
695 +
696 +        rmx_reset(&mop[nmats].imx);             /* not touching output side */
697 +        if (mop[nmats].rmp != &mop[nmats].imx) {
698 +                rmx_free(mop[nmats].rmp);
699 +                mop[nmats].rmp = &mop[nmats].imx;
700 +        }
701 + #ifdef getc_unlocked
702 +        for (i = 0; i < nmats; i++)             /* we handle matrix inputs */
703 +                flockfile(mop[i].infp);
704 + #endif
705 +                                                /* load & send rows to kids */
706 +        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
707 +            int         wfd = cproc[cur_row % (nchildren-1)].w;
708 +            for (i = 0; i < nmats; i++)
709 +                if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
710 +                        if (cur_row > in_nrows) /* unknown #input rows? */
711 +                                break;
712 +                        fprintf(stderr, "%s: parent read error at row %d\n",
713 +                                        mop[i].inspec, cur_row);
714 +                        return(0);
715 +                }
716 +            if (i < nmats)
717 +                break;
718 +            for (i = 0; i < nmats; i++)
719 +                if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx))
720 +                                        != rmx_array_size(&mop[i].imx))
721 +                        return(0);
722 +        }
723 +        i = close_processes(cproc, nchildren);
724 +        free(cproc); cproc = NULL; nchildren = 0;
725 +        if (i < 0) {
726 +                fputs("Warning: lost child in parent_loop()\n", stderr);
727 +                return(1);
728 +        }
729 +        if (i > 0) {
730 +                fprintf(stderr, "Child exited with status %d\n", i);
731 +                return(0);
732 +        }
733 +        return(1);                              /* return success! */
734 + memerror:
735 +        fputs("Out of memory in parent_loop()\n", stderr);
736 +        exit(1);
737 + }
738 +
739 + static int
740 + combine_input(void)
741 + {
742 +        const int       row0 = (inchild >= 0)*inchild;
743 +        const int       rstep = nchildren ? nchildren-1 : 1;
744 +        ROPMAT          *res = &mop[nmats];
745 +        int             set_r, set_c;
746 +        RMATRIX         *tmp = NULL;
747 +        int             co_set;
748 +        int             i;
749 +
750          if (mcat && mcat_last &&
751                          !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
752                  goto memerror;
573        res->imx.nrows = 1;
574        if (!rmx_prepare(&res->imx))
575                goto memerror;
753                                          /* figure out what the user set */
754          co_set = fundefined("co");
755          if (!co_set)
# Line 588 | Line 765 | combine_input(ROPMAT *res, FILE *fout)
765          } else                          /* save a little time */
766                  set_r = set_c = 0;
767                                          /* read/process row-by-row */
768 <        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
768 >        for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) {
769              RMATRIX     *mres = NULL;
770 <            for (i = 0; i < nmats; i++) {
770 >            for (i = 0; i < nmats; i++)
771                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
772 <                        if (in_nrows <= 0)      /* normal end? */
773 <                                goto loop_exit;
772 >                        if (cur_row > in_nrows) /* unknown #input rows? */
773 >                                break;
774                          fprintf(stderr, "%s: read error at row %d\n",
775                                          mop[i].inspec, cur_row);
776                          return(0);
777                  }
778 +            if (i < nmats)
779 +                break;
780 +            for (i = 0; i < nmats; i++)
781                  if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop))
782                          return(0);
603            }
783              if (set_r) varset("r", '=', cur_row);
784              for (cur_col = 0; cur_col < in_ncols; cur_col++) {
785                  if (set_c) varset("c", '=', cur_col);
# Line 638 | Line 817 | combine_input(ROPMAT *res, FILE *fout)
817              }
818              rmx_free(mres); mres = NULL;
819              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
820 <                                res->rmp->ncols, res->rmp->dtype, fout))
820 >                                res->rmp->ncols, res->rmp->dtype, stdout))
821                  return(0);
822 +            if (inchild >= 0 && fflush(stdout) == EOF)
823 +                return(0);
824          }
825 < loop_exit:
645 < #if 0           /* we're about to exit, so who cares? */
646 <        rmx_free(tmp);                  /* clean up */
647 <        rmx_reset(res->rmp);
648 <        rmx_reset(&res->imx);
649 <        for (i = 0; i < nmats; i++) {
650 <                rmx_reset(mop[i].rmp);
651 <                rmx_reset(&mop[i].imx);
652 <                if (mop[i].inspec[0] == '!')
653 <                        pclose(mop[i].infp);
654 <                else if (mop[i].inspec != stdin_name)
655 <                        fclose(mop[i].infp);
656 <                mop[i].infp = NULL;
657 <        }
658 < #endif
659 <        return(fflush(fout) != EOF);
825 >        return(inchild >= 0 || fflush(stdout) != EOF);
826   memerror:
827          fputs("Out of buffer space in combine_input()\n", stderr);
828          return(0);
# Line 680 | Line 846 | resize_inparr(int n2alloc)
846   {
847          int     i;
848  
849 <        for (i = nmats; i > n2alloc; i--) {
849 >        if (n2alloc == nall)
850 >                return;
851 >        for (i = nall; i > n2alloc; i--) {
852                  rmx_reset(&mop[i].imx);
853                  if (mop[i].rmp != &mop[i].imx)
854                          rmx_free(mop[i].rmp);
# Line 690 | Line 858 | resize_inparr(int n2alloc)
858                  fputs("Out of memory in resize_inparr()\n", stderr);
859                  exit(1);
860          }
861 <        if (n2alloc > nmats)
862 <                memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
861 >        if (n2alloc > nall)
862 >                memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT));
863          nall = n2alloc;
864   }
865  
# Line 704 | Line 872 | main(int argc, char *argv[])
872          const char      *defCsym = NULL;
873          int             echoheader = 1;
874          int             stdin_used = 0;
875 +        int             nproc = 1;
876          const char      *mcat_spec = NULL;
877          int             n2comp = 0;
878          uby8            comp_ndx[128];
# Line 731 | Line 900 | main(int argc, char *argv[])
900                          case 'h':
901                                  echoheader = !echoheader;
902                                  break;
903 +                        case 'n':
904 +                                nproc = atoi(argv[++i]);
905 +                                if (nproc <= 0)
906 +                                        goto userr;
907 +                                break;
908                          case 'e':
909                                  if (!n) goto userr;
910                                  comp_ndx[n2comp++] = i++;
# Line 779 | Line 953 | main(int argc, char *argv[])
953                                  if (n && !isflt(argv[i+1])) {
954                                          mop[nmats].preop.csym = argv[++i];
955                                          mop[nmats].preop.clen = 0;
956 +                                        mcat_last = 0;
957                                          break;
958                                  }
959                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
# Line 868 | Line 1043 | main(int argc, char *argv[])
1043                  fprintf(stderr, "%s: unsupported output format\n", argv[0]);
1044                  return(1);
1045          }
1046 +        doptimize(1);                   /* optimize definitions */
1047 +        if (spawned_children(nproc))    /* running in parent process? */
1048 +                return(parent_loop() ? 0 : 1);
1049                                          /* process & write rows */
1050 <        return(combine_input(&mop[nmats], stdout) ? 0 : 1);
1050 >        return(combine_input() ? 0 : 1);
1051   stdin_error:
1052          fprintf(stderr, "%s: %s used for more than one input\n",
1053                          argv[0], stdin_name);
1054          return(1);
1055   userr:
1056          fprintf(stderr,
1057 <        "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1057 >        "Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1058                          argv[0]);
1059          return(1);
1060   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines