ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/rt/RcontribSimulManager.cpp
(Generate patch)

Comparing ray/src/rt/RcontribSimulManager.cpp (file contents):
Revision 2.11 by greg, Thu Jan 2 16:16:49 2025 UTC vs.
Revision 2.22 by greg, Tue Nov 11 00:19:10 2025 UTC

# Line 52 | Line 52 | struct RowAssignment {
52          uint32                  ac;             // accumulation count
53   };
54  
55 < // Get format identifier
56 < const char *
57 < formstr(int f)
58 < {
59 <        switch (f) {
60 <        case 'a': return("ascii");
61 <        case 'f': return("float");
62 <        case 'd': return("double");
63 <        case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT);
64 <        }
65 <        return("unknown");
66 < }
55 > static const char       ROW_DONE[] = "ROW FINISHED\n";
56  
57 < // Our default data share function
58 < RdataShare *
70 < defDataShare(const char *name, RCOutputOp op, size_t siz)
71 < {
72 <        return new RdataShareMap(name, RSDOflags[op], siz);
73 < }
74 <
75 < // Allocate rcontrib accumulator
76 < RcontribMod *
57 > // allocate rcontrib accumulator
58 > static RcontribMod *
59   NewRcMod(const char *prms, const char *binexpr, int ncbins)
60   {
61 +        if (binexpr && !*binexpr) binexpr = NULL;
62          if (!prms) prms = "";
63          if ((ncbins > 1) & !binexpr) {
64                  error(USER, "missing bin expression");
# Line 110 | Line 93 | NewRcMod(const char *prms, const char *binexpr, int nc
93          return mp;
94   }
95  
96 < // Free an RcontribMod
96 > // Free an RcontribMod (public for RcontribSimulManager constructor)
97   void
98   FreeRcMod(void *p)
99   {
# Line 120 | Line 103 | FreeRcMod(void *p)
103          efree(p);
104   }
105  
106 + // Get format identifier
107 + const char *
108 + formstr(int f)
109 + {
110 +        switch (f) {
111 +        case 'a': return("ascii");
112 +        case 'f': return("float");
113 +        case 'd': return("double");
114 +        case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT);
115 +        }
116 +        return("unknown");
117 + }
118 +
119 + // Standard file data share function
120 + RdataShare *
121 + fileDataShare(const char *name, RCOutputOp op, size_t siz)
122 + {
123 +        if (op == RCOrecover && access(name, R_OK|W_OK) < 0) {
124 +                sprintf(errmsg, "cannot recover from '%s'", name);
125 +                error(SYSTEM, errmsg);
126 +                return NULL;
127 +        }
128 +        RdataShare *    rds = new RdataShareFile(name, RSDOflags[op],
129 +                                                 siz*(op != RCOforce));
130 +                                                
131 +        if (!rds || (op == RCOforce && rds->Resize(siz) < siz)) {
132 +                delete rds;
133 +                sprintf(errmsg, "cannot create %lu byte output file '%s'",
134 +                                        (unsigned long)siz, name);
135 +                error(SYSTEM, errmsg);
136 +                return NULL;
137 +        }
138 +        return rds;
139 + }
140 +
141 + // Memory-mapped data share function
142 + RdataShare *
143 + mapDataShare(const char *name, RCOutputOp op, size_t siz)
144 + {
145 +        if (op == RCOrecover && access(name, R_OK|W_OK) < 0) {
146 +                sprintf(errmsg, "cannot recover from '%s'", name);
147 +                error(SYSTEM, errmsg);
148 +                return NULL;
149 +        }
150 +        RdataShare *    rds = new RdataShareMap(name, RSDOflags[op],
151 +                                                 siz*(op != RCOforce));
152 +
153 +        if (!rds || (op == RCOforce && rds->Resize(siz) < siz)) {
154 +                delete rds;
155 +                sprintf(errmsg, "cannot create %lu byte output map '%s'",
156 +                                        (unsigned long)siz, name);
157 +                error(SYSTEM, errmsg);
158 +                return NULL;
159 +        }
160 +        return rds;
161 + }
162 +
163   // Set output format ('f', 'd', or 'c')
164   bool
165   RcontribSimulManager::SetDataFormat(int ty)
# Line 163 | Line 203 | RcontribSimulManager::RctCall(RAY *r, void *cd)
203          if (!mp)
204                  return 0;               // not in our modifier list
205  
206 +        if (rcp->HasFlag(RCcontrib) && sintens(r->rcol) <= FTINY)
207 +                return 0;               // zero contribution
208 +
209          int                     bi = 0; // get bin index
210          if (mp->binv) {
211                  worldfunc(RCCONTEXT, r);
# Line 215 | Line 258 | RcontribSimulManager::AddModifier(const char *modn, co
258                  error(WARNING, "ignoring bad call to AddModifier()");
259                  return false;
260          }
261 +        if (*outspec == '!') {
262 +                error(USER, "command output not supported by RcontribSimulManager");
263 +                return false;
264 +        }
265          if (!nChan) {                           // initial call?
266                  if ((xres < 0) | (yres <= 0)) {
267                          error(USER, "xres, yres must be set before first modifier");
# Line 373 | Line 420 | RcontribSimulManager::PrepOutput()
420                          if (rd < 0)
421                                  return -1;
422                          if (rd >= op->nRows) {
423 <                                if (remWarnings >= 0) {
423 >                                if (remWarnings > 0) {
424                                          sprintf(errmsg, "recovered output '%s' is complete",
425                                                          op->GetName());
426                                          error(WARNING, --remWarnings ? errmsg : "etc...");
# Line 390 | Line 437 | RcontribSimulManager::PrepOutput()
437          }
438          rowsDone.NewBitMap(outList->nRows);     // create row completion map
439          rowsDone.ClearBits(0, rInPos, true);
440 <        return rInPos;
440 >        return nrDone = rInPos;
441   }
442  
443   // Create header in open write-only channel
# Line 425 | Line 472 | RcontribOutput::NewHeader(const RcontribSimulManager *
472          sprintf(hdr+begData, "%s%d\n", NCOMPSTR, NCSAMP);
473          begData += strlen(hdr+begData);
474          if (NCSAMP > 3) {
475 <                sprintf(hdr+begData, "%s %f %f %f %f\n", WLSPLTSTR,
475 >                sprintf(hdr+begData, "%s %g %g %g %g\n", WLSPLTSTR,
476                                  WLPART[0], WLPART[1], WLPART[2], WLPART[3]);
477                  begData += strlen(hdr+begData);
478          }
# Line 510 | Line 557 | RcontribOutput::CheckHeader(const RcontribSimulManager
557                  return -1;
558          }
559                                                  // check #columns
560 <        if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
561 <                        !rcp->xres | (rowBytes > esiz)) {
562 <                sprintf(errmsg, "expected NCOLS=%d in '%s'",
516 <                                int(rowBytes/esiz), GetName());
560 >        if ((cp = findArgs(hdr, "NCOLS=", begData)) ? (atoi(cp)*esiz != rowBytes)
561 >                                                    : (!rcp->xres | (rowBytes > esiz))) {
562 >                sprintf(errmsg, "expected NCOLS=%d in '%s'", int(rowBytes/esiz), GetName());
563                  error(USER, errmsg);
564                  return -1;
565          }
# Line 555 | Line 601 | RcontribSimulManager::ResetRow(int r)
601                          return false;
602  
603          rowsDone.ClearBits(r, rInPos-r, false);
604 <        rInPos = r;
604 >        nrDone = rInPos = r;
605          return true;
606   }
607  
# Line 602 | Line 648 | putModContrib(const LUENT *lp, void *p)
648                  }
649                  } break;
650          default:
651 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
651 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
652                  return -1;
653          }
654                                                  // clear for next tally
# Line 621 | Line 667 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
667                  return 0;
668          }
669          if (nkids > 0) {                        // in parent process?
670 <                int     k = GetChild();         // updates output rows
671 <                if (k < 0) return -1;           // can't really happen
670 >                int     k = GetChild(false);    // updates output rows
671 >                if (k < 0) return -1;           // someone died?
672                  RowAssignment   rass;
673                  rass.row = kidRow[k] = rInPos++;
674                  rass.ac = accum;
# Line 667 | Line 713 | RcontribSimulManager::GetChild(bool forceWait)
713                  return -1;
714                                                  // take inventory
715          int     pn, n = 0;
716 <        fd_set  writeset, errset;
717 <        FD_ZERO(&writeset); FD_ZERO(&errset);
716 >        fd_set  readset, errset;
717 >        FD_ZERO(&readset); FD_ZERO(&errset);
718          for (pn = nkids; pn--; ) {
719                  if (kidRow[pn] < 0) {           // child already ready?
720                          if (forceWait) continue;
721                          return pn;              // good enough
722                  }
723 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
724 <                FD_SET(kid[pn].w, &errset);
725 <                if (kid[pn].w >= n)
726 <                        n = kid[pn].w + 1;
723 >                FD_SET(kid[pn].r, &readset);    // will check on this one
724 >                FD_SET(kid[pn].r, &errset);
725 >                if (kid[pn].r >= n)
726 >                        n = kid[pn].r + 1;
727          }
728          if (!n)                                 // every child is idle?
729                  return -1;
730                                                  // wait on "busy" child(ren)
731 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
731 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
732                  if (errno != EINTR) {
733                          error(SYSTEM, "select call failed in GetChild()");
734                          return -1;
735                  }
736 +        char    buf[sizeof(ROW_DONE)] = "X";
737          pn = -1;                                // get flags set by select
738          for (n = nkids; n--; )
739                  if (kidRow[n] >= 0 &&
740 <                                FD_ISSET(kid[n].w, &writeset) |
741 <                                FD_ISSET(kid[n].w, &errset)) {
740 >                                FD_ISSET(kid[n].r, &readset) |
741 >                                FD_ISSET(kid[n].r, &errset)) {
742 >                                                // check for error
743 >                        if (FD_ISSET(kid[n].r, &errset) ||
744 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
745 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
746 >                                return -1;
747                                                  // update output row counts
748 <                        if (!FD_ISSET(kid[n].w, &errset))
749 <                                UpdateRowsDone(kidRow[n]);
698 <                        kidRow[n] = -1;         // flag it available
748 >                        UpdateRowsDone(kidRow[n]);
749 >                        kidRow[n] = -1;         // flag child available
750                          pn = n;
751                  }
752          return pn;
# Line 709 | Line 760 | RcontribSimulManager::UpdateRowsDone(int r)
760                  error(WARNING, "redundant call to UpdateRowsDone()");
761                  return false;
762          }
763 <        int     nDone = GetRowFinished();
764 <        if (nDone <= r)
763 >        GetRowFinished();
764 >        if (nrDone <= r)
765                  return true;                    // nothing to update, yet
766          for (RcontribOutput *op = outList; op; op = op->next)
767 <                if (!op->SetRowsDone(nDone))
767 >                if (!op->SetRowsDone(nrDone))
768                          return false;
769          return true;                            // up-to-date
770   }
# Line 733 | Line 784 | RcontribSimulManager::RunChild()
784                          error(CONSISTENCY, "bad accumulator count in child");
785                          exit(1);
786                  }
787 <                if (rass.ac > accum)
788 <                        vecList = (FVECT *)erealloc(vecList,
789 <                                                sizeof(FVECT)*2*rass.ac);
787 >                if (rass.ac > accum) {
788 >                        efree(vecList);
789 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
790 >                }
791                  accum = rass.ac;
792                  rInPos = rass.row;
793  
# Line 745 | Line 797 | RcontribSimulManager::RunChild()
797  
798                  if (ComputeRecord(vecList) <= 0)
799                          exit(1);
800 +                                                // signal this row is done
801 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
802 +                        exit(1);
803          }
804          if (nr) {
805                  error(SYSTEM, "read error in child process");
# Line 770 | Line 825 | RcontribSimulManager::StartKids(int n2go)
825          fflush(stdout);                         // shouldn't use, anyway
826          while (nkids < n2go) {
827                  kid[nkids] = sp_inactive;
773                kid[nkids].w = dup(1);
774                kid[nkids].flags |= PF_FILT_OUT;
828                  int     rv = open_process(&kid[nkids], NULL);
829                  if (!rv) {                      // in child process?
830 <                        while (nkids-- > 0)
830 >                        while (nkids-- > 0) {
831 >                                close(kid[nkids].r);
832                                  close(kid[nkids].w);
833 +                        }
834                          free(kid); free(kidRow);
835                          kid = NULL; kidRow = NULL;
836                          RunChild();             // should never return

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)