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.7 by greg, Fri Feb 23 03:47:57 2024 UTC vs.
Revision 2.14 by greg, Wed May 22 15:38:04 2024 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * General component matrix combiner, operating on a row at a time.
6   */
7  
8 < #include <errno.h>
8 > #include <signal.h>
9   #include <math.h>
10   #include "platform.h"
11 + #include "rtprocess.h"
12   #include "rtio.h"
12 #include "resolu.h"
13   #include "rmatrix.h"
14   #include "calcomp.h"
15 #include "paths.h"
15  
16   #ifndef M_PI
17   #define M_PI    3.14159265358979323846
# Line 55 | Line 54 | int            cur_row;                        /* current input/output row */
54   int             cur_col;                        /* current input/output column */
55   int             cur_chan;                       /* if we're looping channels */
56  
57 + SUBPROC         *cproc = NULL;                  /* child process array */
58 + int             nchildren = 0;                  /* # of child processes */
59 + int             inchild = -1;                   /* our child ID (-1: parent) */
60 + int             pgid = -1;                      /* process group ID */
61 + int             nr_out = 0;                     /* # of rows output by kids */
62 +
63   static int      checksymbolic(ROPMAT *rop);
64  
65 + static void
66 + on_sigio(int dummy)
67 + {
68 +        nr_out++;                       /* happens when child outputs row */
69 + }
70 +
71   static int
72   split_input(ROPMAT *rop)
73   {
# Line 391 | Line 402 | apply_op(RMATRIX *dst, const RMATRIX *src, const RUNAR
402                          return(0);
403                  rmx_free(res);
404          } else if (dst != src)
405 <                memcpy(dst->mtx, src->mtx,
395 <                                sizeof(double)*dst->ncomp*dst->ncols*dst->nrows);
405 >                memcpy(dst->mtx, src->mtx, rmx_array_size(dst));
406          if (ro->nsf == dst->ncomp)
407                  rmx_scale(dst, ro->sca);
408          return(1);
# Line 540 | Line 550 | output_headinfo(FILE *fp)
550   }
551  
552   static int
553 < combine_input(ROPMAT *res, FILE *fout)
553 > spawned_children(int np)
554   {
555 <        int     set_r, set_c;
556 <        RMATRIX *tmp = NULL;
557 <        int     co_set;
558 <        int     i;
559 <                                        /* allocate input row buffers */
555 >        int     i, rv;
556 >
557 > #if defined(_WIN32) || defined(_WIN64)
558 >        if (np > 1) {
559 >                fputs("Warning: only one process under Windows\n", stderr);
560 >                np = 1;
561 >        } else
562 > #endif
563 >        if ((in_nrows > 0) & (np > in_nrows))
564 >                np = in_nrows;
565 >                                /* we'll be doing a row at a time */
566          for (i = 0; i < nmats; i++) {
567 <                mop[i].imx.nrows = 1;   /* we'll be doing a row at a time */
567 >                mop[i].imx.nrows = 1;
568                  if (!rmx_prepare(&mop[i].imx))
569                          goto memerror;
570                  if (mop[i].rmp != &mop[i].imx) {
# Line 557 | Line 573 | combine_input(ROPMAT *res, FILE *fout)
573                                  goto memerror;
574                  }
575          }
576 <                                        /* prep output row buffers */
577 <        if (mcat || res->preop.clen > 0) {
578 <                if (!split_input(res))  /* need separate buffer */
576 >                                /* prep output row buffer */
577 >        if (mcat || mop[nmats].preop.clen > 0) {
578 >                if (!split_input(&mop[nmats]))  /* need separate buffer */
579                          return(0);
580 <                if (res->preop.clen > 0)
581 <                        res->rmp->ncomp = res->preop.clen / res->imx.ncomp;
582 <                res->rmp->nrows = 1;
583 <                if (!mcat | !mcat_last && !rmx_prepare(res->rmp))
580 >                if (mop[nmats].preop.clen > 0)
581 >                        mop[nmats].rmp->ncomp = mop[nmats].preop.clen /
582 >                                                mop[nmats].imx.ncomp;
583 >                mop[nmats].rmp->nrows = 1;
584 >                if (!mcat | !mcat_last && !rmx_prepare(mop[nmats].rmp))
585                          goto memerror;
586          }
587 +        mop[nmats].imx.nrows = 1;
588 +        if (!rmx_prepare(&mop[nmats].imx))
589 +                goto memerror;
590 +        if (np <= 1) {          /* single process return point */
591 + #ifdef getc_unlocked
592 +                for (i = 0; i < nmats; i++)
593 +                        flockfile(mop[i].infp);
594 +                flockfile(stdout);
595 + #endif
596 +                return(0);
597 +        }
598 +        pgid = setpgrp();       /* set process group ID */
599 +        signal(SIGIO, on_sigio);
600 +        fflush(stdout);         /* flush header & spawn children */
601 +        cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np);
602 +        if (!cproc)
603 +                goto memerror;
604 +        nchildren = np;
605 +        for (i = 0; i < np; i++) {
606 +                cproc[i].flags = PF_FILT_OUT;
607 +                cproc[i].w = dup(1);
608 +                cproc[i].r = 0;
609 +                cproc[i].pid = -1;
610 +                rv = open_process(&cproc[i], NULL);
611 +                if (rv <= 0) break;
612 +        }
613 +        if (rv > 0)
614 +                return(1);      /* parent return value */
615 +        if (rv < 0) {
616 +                perror("fork");
617 +                exit(1);
618 +        }
619 +        inchild = i;            /* our child index */
620 +        while (i-- > 0)         /* don't share siblings' pipes */
621 +                close(cproc[i].w);
622 +        fpurge(stdin);          /* discard previous matrix input */
623 + #ifdef getc_unlocked
624 +        flockfile(stdin);
625 + #endif
626 +        for (i = 0; i < nmats; i++) {
627 +                if (mop[i].infp != stdin)
628 +                        fclose(mop[i].infp);    /* ! pclose() */
629 +                mop[i].infp = stdin;
630 +                mop[i].imx.dtype = DTdouble;
631 +        }
632 +        return(0);              /* child return */
633 + memerror:
634 +        fputs("Out of memory in spawned_children()\n", stderr);
635 +        exit(1);
636 + }
637 +
638 + static int
639 + parent_loop()
640 + {
641 +        FILE    **outfp = (FILE **)malloc(nchildren*sizeof(FILE *));
642 +        int     i;
643 +
644 +        if (!outfp) goto memerror;
645 +        for (i = 0; i < nchildren; i++) {
646 +                outfp[i] = fdopen(cproc[i].w, "w");
647 +                if (!outfp[i]) goto memerror;
648 + #ifdef getc_unlocked
649 +                flockfile(outfp[i]);
650 + #endif
651 +        }
652 + #ifdef getc_unlocked
653 +        for (i = 0; i < nmats; i++)
654 +                flockfile(mop[i].infp);
655 + #endif
656 +        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
657 +            FILE        *ofp = outfp[cur_row % nchildren];
658 +            for (i = 0; i < nmats; i++)
659 +                if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
660 +                        if (cur_row > in_nrows) /* unknown #input rows? */
661 +                                break;
662 +                        fprintf(stderr, "%s: read error at row %d\n",
663 +                                        mop[i].inspec, cur_row);
664 +                        return(0);
665 +                }
666 +            if (i < nmats)
667 +                break;
668 +            for (i = 0; i < nmats; i++)
669 +                if (!rmx_write_data(mop[i].imx.mtx, mop[i].imx.ncomp,
670 +                                        mop[i].imx.ncols, DTdouble, ofp))
671 +                        return(0);
672 +            if (fflush(ofp) == EOF)
673 +                return(0);
674 +        }
675 +        for (i = 0; i < nchildren; i++)
676 +                fclose(outfp[i]);
677 +        free(outfp);
678 +        i = close_processes(cproc, nchildren);
679 +        free(cproc); cproc = NULL;
680 +        if (i < 0) {
681 +                fputs("Warning: missing child in parent_loop()\n", stderr);
682 +                return(1);
683 +        }
684 +        if (i > 0) {
685 +                fprintf(stderr, "Child exited with status %d\n", i);
686 +                return(0);
687 +        }
688 +        return(1);
689 + memerror:
690 +        fputs("Out of memory in parent_loop()\n", stderr);
691 +        exit(1);
692 + }
693 +
694 + static int
695 + combine_input()
696 + {
697 +        const int       row0 = (inchild >= 0)*inchild;
698 +        const int       rstep = nchildren + !nchildren;
699 +        ROPMAT          *res = &mop[nmats];
700 +        int             set_r, set_c;
701 +        RMATRIX         *tmp = NULL;
702 +        int             co_set;
703 +        sigset_t        iomask;
704 +        int             i;
705 +
706          if (mcat && mcat_last &&
707                          !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
708                  goto memerror;
573        res->imx.nrows = 1;
574        if (!rmx_prepare(&res->imx))
575                goto memerror;
709                                          /* figure out what the user set */
710          co_set = fundefined("co");
711          if (!co_set)
# Line 587 | Line 720 | combine_input(ROPMAT *res, FILE *fout)
720                  set_c = varlookup("c") != NULL && !vardefined("c");
721          } else                          /* save a little time */
722                  set_r = set_c = 0;
723 <                                        /* read/process row-by-row */
724 <        for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
723 >
724 >        sigemptyset(&iomask);           /* read/process row-by-row */
725 >        sigaddset(&iomask, SIGIO);
726 >        for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) {
727              RMATRIX     *mres = NULL;
728 <            for (i = 0; i < nmats; i++) {
728 >            if (inchild >= 0) sigprocmask(SIG_BLOCK, &iomask, NULL);
729 >            for (i = 0; i < nmats; i++)
730                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
731                          if (cur_row > in_nrows) /* unknown #input rows? */
732 <                                goto loop_exit;
732 >                                break;
733                          fprintf(stderr, "%s: read error at row %d\n",
734                                          mop[i].inspec, cur_row);
735                          return(0);
736                  }
737 +            if (inchild >= 0) sigprocmask(SIG_UNBLOCK, &iomask, NULL);
738 +            if (i < nmats)
739 +                break;
740 +            for (i = 0; i < nmats; i++)
741                  if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop))
742                          return(0);
603            }
743              if (set_r) varset("r", '=', cur_row);
744              for (cur_col = 0; cur_col < in_ncols; cur_col++) {
745                  if (set_c) varset("c", '=', cur_col);
# Line 637 | Line 776 | combine_input(ROPMAT *res, FILE *fout)
776                          return(0);
777              }
778              rmx_free(mres); mres = NULL;
779 +            if (inchild >= 0) {         /* children share stdout */
780 +                while (nr_out < cur_row)
781 +                    pause();            /* wait for our turn */
782 +                sigprocmask(SIG_BLOCK, &iomask, NULL);
783 +            }
784              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
785 <                                res->rmp->ncols, res->rmp->dtype, fout))
785 >                                res->rmp->ncols, res->rmp->dtype, stdout))
786                  return(0);
787 +            if (inchild >= 0) {         /* flush and notify group */
788 +                if (fflush(stdout) == EOF)
789 +                    return(0);
790 +                sigprocmask(SIG_UNBLOCK, &iomask, NULL);
791 +                killpg(pgid, SIGIO);    /* increments everyone's nr_out */
792 +            }
793          }
794 < 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);
794 >        return(inchild >= 0 || fflush(stdout) != EOF);
795   memerror:
796          fputs("Out of buffer space in combine_input()\n", stderr);
797          return(0);
# Line 706 | Line 841 | main(int argc, char *argv[])
841          const char      *defCsym = NULL;
842          int             echoheader = 1;
843          int             stdin_used = 0;
844 +        int             nproc = 1;
845          const char      *mcat_spec = NULL;
846          int             n2comp = 0;
847          uby8            comp_ndx[128];
# Line 733 | Line 869 | main(int argc, char *argv[])
869                          case 'h':
870                                  echoheader = !echoheader;
871                                  break;
872 +                        case 'n':
873 +                                nproc = atoi(argv[++i]);
874 +                                if (nproc <= 0)
875 +                                        goto userr;
876 +                                break;
877                          case 'e':
878                                  if (!n) goto userr;
879                                  comp_ndx[n2comp++] = i++;
# Line 871 | Line 1012 | main(int argc, char *argv[])
1012                  return(1);
1013          }
1014          doptimize(1);                   /* optimize definitions */
1015 +        if (spawned_children(nproc))    /* running in parent process? */
1016 +                return(parent_loop() ? 0 : 1);
1017                                          /* process & write rows */
1018 <        return(combine_input(&mop[nmats], stdout) ? 0 : 1);
1018 >        return(combine_input() ? 0 : 1);
1019   stdin_error:
1020          fprintf(stderr, "%s: %s used for more than one input\n",
1021                          argv[0], stdin_name);
1022          return(1);
1023   userr:
1024          fprintf(stderr,
1025 <        "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1025 >        "Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
1026                          argv[0]);
1027          return(1);
1028   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines