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.9 by greg, Tue Dec 3 17:39:42 2024 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 (!binexpr & (ncbins > 1)) {
63 >        if ((ncbins > 1) & !binexpr) {
64                  error(USER, "missing bin expression");
65                  return NULL;
66          }
67 <        if (ncbins <= 1) {              // shouldn't have bin expression?
68 <                if (binexpr && strcmp(binexpr, "0"))
86 <                        error(WARNING, "ignoring non-zero expression for single bin");
87 <                prms = "";
88 <                binexpr = NULL;
89 <                ncbins = 1;
90 <        }
67 >        if (ncbins < 1) ncbins = 1;
68 >        
69          RcontribMod *   mp = (RcontribMod *)ecalloc(1, sizeof(RcontribMod) +
70                                                  sizeof(DCOLORV)*(NCSAMP*ncbins-1) +
71                                                  strlen(prms)+1);
72  
73 <        mp->params = strcpy((char *)(mp->cbin + ncbins*NCSAMP), prms);
96 <        if (binexpr) {
73 >        if (binexpr) {                          // get/check bin expression
74                  mp->binv = eparse(const_cast<char *>(binexpr));
75 <                CHECK(mp->binv->type==NUM, WARNING, "constant bin expression");
75 >                if (mp->binv->type == NUM) {    // constant expression (0)?
76 >                        if ((int)evalue(mp->binv) != 0) {
77 >                                sprintf(errmsg, "illegal non-zero constant for bin (%s)",
78 >                                                binexpr);
79 >                                error(USER, errmsg);
80 >                        }
81 >                        if (ncbins > 1) {
82 >                                sprintf(errmsg, "bad bin count (%d should be 1)", ncbins);
83 >                                error(USER, errmsg);
84 >                        }
85 >                        epfree(mp->binv, true);
86 >                        mp->binv = NULL;
87 >                        prms = "";
88 >                        ncbins = 1;
89 >                }
90          }
91 +        mp->params = strcpy((char *)(mp->cbin + ncbins*NCSAMP), prms);
92          mp->nbins = ncbins;
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 111 | 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 154 | 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 173 | Line 225 | RcontribSimulManager::RctCall(RAY *r, void *cd)
225          raycontrib(contr, r, PRIMARY);          // compute coefficient
226          if (rcp->HasFlag(RCcontrib))
227                  smultscolor(contr, r->rcol);    // -> value contribution
228 +
229          for (int i = 0; i < NCSAMP; i++)
230                  *dvp++ += contr[i];             // accumulate color/spectrum
231          return 1;
# Line 205 | 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 214 | Line 271 | RcontribSimulManager::AddModifier(const char *modn, co
271                          return false;
272                  nChan = NCSAMP;
273          } else if (nChan != NCSAMP) {
274 <                error(USER, "number of spectral channels must be fixed");
274 >                error(USER, "# spectral channels must be fixed in AddModifier()");
275                  return false;
276          }
277          if (Ready()) {
# Line 310 | Line 367 | RcontribSimulManager::AddModFile(const char *modfn, co
367          }
368          char            mod[MAXSTR];
369          while (fgetword(mod, sizeof(mod), fp))
370 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
370 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
371 >                        fclose(fp);
372                          return false;
373 +                }
374          fclose(fp);
375          return true;
376   }
# Line 320 | Line 379 | RcontribSimulManager::AddModFile(const char *modfn, co
379   static int
380   checkModExists(const LUENT *lp, void *p)
381   {
382 <        if (modifier(lp->key) != OVOID)
382 >        OBJECT  mod = modifier(lp->key);
383 >
384 >        if ((mod != OVOID) & (mod < nsceneobjs))
385                  return 1;
386  
387          sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
# Line 333 | Line 394 | int
394   RcontribSimulManager::PrepOutput()
395   {
396          if (!outList || !RtraceSimulManager::Ready()) {
397 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
397 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
398                  return -1;
399          }
400          if (!cdsF) {
401 <                error(INTERNAL, "missing RdataShare constructor call (*cdsF)");
401 >                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
402                  return -1;
403          }
404          if (lu_doall(&modLUT, checkModExists, NULL) < 0)
405                  return -1;
406  
407 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
408          int     remWarnings = 20;
409          for (RcontribOutput *op = outList; op; op = op->next) {
410                  if (op->rData) {
411                          error(INTERNAL, "output channel already open in PrepOutput()");
412                          return -1;
413                  }
414 <                op->nRows = yres * (xres + !xres);
414 >                op->nRows = outList->nRows;
415                  op->rData = (*cdsF)(op->ofname, outOp,
416                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
417                  freeqstr(op->ofname); op->ofname = NULL;
# Line 358 | Line 420 | RcontribSimulManager::PrepOutput()
420                          if (rd < 0)
421                                  return -1;
422                          if (rd >= op->nRows) {
423 <                                if (remWarnings >= 0) {
424 <                                        sprintf(errmsg, "recovered output '%s' already done",
423 >                                if (remWarnings > 0) {
424 >                                        sprintf(errmsg, "recovered output '%s' is complete",
425                                                          op->GetName());
426 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
365 <                                        remWarnings--;
426 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
427                                  }
428                                  rd = op->nRows;
429                          }
# Line 376 | 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 411 | 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 496 | 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'",
502 <                                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 541 | 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 588 | 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 607 | 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 653 | 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]);
684 <                        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 695 | 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 719 | 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 731 | 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 756 | Line 825 | RcontribSimulManager::StartKids(int n2go)
825          fflush(stdout);                         // shouldn't use, anyway
826          while (nkids < n2go) {
827                  kid[nkids] = sp_inactive;
759                kid[nkids].w = dup(1);
760                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)