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.13 by greg, Wed May 22 00:39:30 2024 UTC vs.
Revision 2.17 by greg, Thu May 23 17:13:52 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 <signal.h>
8   #include <math.h>
9   #include "platform.h"
10   #include "rtprocess.h"
# Line 57 | Line 56 | int            cur_chan;                       /* if we're looping channels */
56   SUBPROC         *cproc = NULL;                  /* child process array */
57   int             nchildren = 0;                  /* # of child processes */
58   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 */
59  
60   static int      checksymbolic(ROPMAT *rop);
61  
65 static void
66 on_sigio(int dummy)
67 {
68        nr_out++;                       /* happens when child outputs row */
69 }
70
62   static int
63   split_input(ROPMAT *rop)
64   {
# Line 552 | Line 543 | output_headinfo(FILE *fp)
543   static int
544   spawned_children(int np)
545   {
546 +        size_t  recsize = 0;
547          int     i, rv;
548  
549   #if defined(_WIN32) || defined(_WIN64)
# Line 567 | Line 559 | spawned_children(int np)
559                  mop[i].imx.nrows = 1;
560                  if (!rmx_prepare(&mop[i].imx))
561                          goto memerror;
562 +                recsize += rmx_array_size(&mop[i].imx);
563                  if (mop[i].rmp != &mop[i].imx) {
564                          mop[i].rmp->nrows = 1;
565                          if (!rmx_prepare(mop[i].rmp))
# Line 595 | Line 588 | spawned_children(int np)
588   #endif
589                  return(0);
590          }
598        pgid = setpgrp();       /* set process group ID */
599        signal(SIGIO, on_sigio);
591          fflush(stdout);         /* flush header & spawn children */
592          cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*np);
593          if (!cproc)
# Line 609 | Line 600 | spawned_children(int np)
600                  cproc[i].pid = -1;
601                  rv = open_process(&cproc[i], NULL);
602                  if (rv <= 0) break;
603 +                if (!i && 2*rv >= recsize) {
604 +                        fputs("Problem too small for multi-processing\n",
605 +                                        stderr);
606 +                        close_processes(cproc, 1);
607 +                        exit(1);
608 +                }
609          }
613        if (rv > 0)
614                return(1);      /* parent return value */
610          if (rv < 0) {
611                  perror("fork");
612 +                close_processes(cproc, i);
613                  exit(1);
614          }
615 +        if (rv > 0)             /* parent return? */
616 +                return(1);
617          inchild = i;            /* our child index */
618          while (i-- > 0)         /* don't share siblings' pipes */
619                  close(cproc[i].w);
# Line 672 | Line 670 | parent_loop()
670              if (fflush(ofp) == EOF)
671                  return(0);
672          }
673 <        for (i = 0; i < nchildren; i++)
673 >        for (i = 0; i < nchildren; i++) {
674 >                sleep(2);                       /* try to maintain order */
675                  fclose(outfp[i]);
676 +        }
677          free(outfp);
678          i = close_processes(cproc, nchildren);
679          free(cproc); cproc = NULL;
# Line 700 | Line 700 | combine_input()
700          int             set_r, set_c;
701          RMATRIX         *tmp = NULL;
702          int             co_set;
703        sigset_t        iomask;
703          int             i;
704  
705          if (mcat && mcat_last &&
# Line 721 | Line 720 | combine_input()
720          } else                          /* save a little time */
721                  set_r = set_c = 0;
722  
723 <        sigemptyset(&iomask);           /* read/process row-by-row */
725 <        sigaddset(&iomask, SIGIO);
723 >                                        /* read/process row-by-row */
724          for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) {
725              RMATRIX     *mres = NULL;
726 <            for (i = 0; i < nmats; i++) {
729 <                if (inchild >= 0) sigprocmask(SIG_BLOCK, &iomask, NULL);
726 >            for (i = 0; i < nmats; i++)
727                  if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
728                          if (cur_row > in_nrows) /* unknown #input rows? */
729                                  break;
# Line 734 | Line 731 | combine_input()
731                                          mop[i].inspec, cur_row);
732                          return(0);
733                  }
737                if (inchild >= 0) sigprocmask(SIG_UNBLOCK, &iomask, NULL);
738            }
734              if (i < nmats)
735                  break;
736              for (i = 0; i < nmats; i++)
# Line 778 | Line 773 | combine_input()
773              }
774              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 <                sigprocmask(SIG_BLOCK, &iomask, NULL);
776 >                i = getc(stdin);        /* signals it's our turn */
777 >                if (i != EOF) ungetc(i, stdin);
778              }
779              if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
780                                  res->rmp->ncols, res->rmp->dtype, stdout))
781                  return(0);
782 <            if (inchild >= 0) {         /* flush and notify group */
783 <                if (fflush(stdout) == EOF)
790 <                    return(0);
791 <                sigprocmask(SIG_UNBLOCK, &iomask, NULL);
792 <                killpg(pgid, SIGIO);    /* increments everyone's nr_out */
793 <            }
782 >            if (inchild >= 0 && fflush(stdout) == EOF)
783 >                return(0);
784          }
785          return(inchild >= 0 || fflush(stdout) != EOF);
786   memerror:
# Line 923 | Line 913 | main(int argc, char *argv[])
913                                  if (n && !isflt(argv[i+1])) {
914                                          mop[nmats].preop.csym = argv[++i];
915                                          mop[nmats].preop.clen = 0;
916 +                                        mcat_last = 0;
917                                          break;
918                                  }
919                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines