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.10 by greg, Tue Dec 10 00:38:59 2024 UTC vs.
Revision 2.16 by greg, Thu Oct 23 01:26:48 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 87 | Line 70 | NewRcMod(const char *prms, const char *binexpr, int nc
70                                                  sizeof(DCOLORV)*(NCSAMP*ncbins-1) +
71                                                  strlen(prms)+1);
72  
73 <        if (binexpr) {                          // check bin expression
73 >        if (binexpr) {                          // get/check bin expression
74                  mp->binv = eparse(const_cast<char *>(binexpr));
75                  if (mp->binv->type == NUM) {    // constant expression (0)?
76 <                        if ((int)(evalue(mp->binv) + .5) > 0) {
77 <                                sprintf(errmsg, "illegal positive constant for bin (%s)",
76 >                        if ((int)evalue(mp->binv) != 0) {
77 >                                sprintf(errmsg, "illegal non-zero constant for bin (%s)",
78                                                  binexpr);
79                                  error(USER, errmsg);
80                          }
# 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 + // Our default data share function
120 + RdataShare *
121 + defDataShare(const char *name, RCOutputOp op, size_t siz)
122 + {
123 +        return new RdataShareMap(name, RSDOflags[op], siz);
124 + }
125 +
126   // Set output format ('f', 'd', or 'c')
127   bool
128   RcontribSimulManager::SetDataFormat(int ty)
# Line 182 | Line 185 | RcontribSimulManager::RctCall(RAY *r, void *cd)
185          raycontrib(contr, r, PRIMARY);          // compute coefficient
186          if (rcp->HasFlag(RCcontrib))
187                  smultscolor(contr, r->rcol);    // -> value contribution
188 +
189          for (int i = 0; i < NCSAMP; i++)
190                  *dvp++ += contr[i];             // accumulate color/spectrum
191          return 1;
# Line 214 | Line 218 | RcontribSimulManager::AddModifier(const char *modn, co
218                  error(WARNING, "ignoring bad call to AddModifier()");
219                  return false;
220          }
221 +        if (*outspec == '!') {
222 +                error(USER, "command output not supported by RcontribSimulManager");
223 +                return false;
224 +        }
225          if (!nChan) {                           // initial call?
226                  if ((xres < 0) | (yres <= 0)) {
227                          error(USER, "xres, yres must be set before first modifier");
# Line 223 | Line 231 | RcontribSimulManager::AddModifier(const char *modn, co
231                          return false;
232                  nChan = NCSAMP;
233          } else if (nChan != NCSAMP) {
234 <                error(USER, "number of spectral channels must be fixed");
234 >                error(USER, "# spectral channels must be fixed in AddModifier()");
235                  return false;
236          }
237          if (Ready()) {
# Line 319 | Line 327 | RcontribSimulManager::AddModFile(const char *modfn, co
327          }
328          char            mod[MAXSTR];
329          while (fgetword(mod, sizeof(mod), fp))
330 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
330 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
331 >                        fclose(fp);
332                          return false;
333 +                }
334          fclose(fp);
335          return true;
336   }
# Line 329 | Line 339 | RcontribSimulManager::AddModFile(const char *modfn, co
339   static int
340   checkModExists(const LUENT *lp, void *p)
341   {
342 <        if (modifier(lp->key) != OVOID)
342 >        OBJECT  mod = modifier(lp->key);
343 >
344 >        if ((mod != OVOID) & (mod < nsceneobjs))
345                  return 1;
346  
347          sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
# Line 342 | Line 354 | int
354   RcontribSimulManager::PrepOutput()
355   {
356          if (!outList || !RtraceSimulManager::Ready()) {
357 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
357 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
358                  return -1;
359          }
360          if (!cdsF) {
361 <                error(INTERNAL, "missing RdataShare constructor call (*cdsF)");
361 >                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
362                  return -1;
363          }
364          if (lu_doall(&modLUT, checkModExists, NULL) < 0)
365                  return -1;
366  
367 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
368          int     remWarnings = 20;
369          for (RcontribOutput *op = outList; op; op = op->next) {
370                  if (op->rData) {
371                          error(INTERNAL, "output channel already open in PrepOutput()");
372                          return -1;
373                  }
374 <                op->nRows = yres * (xres + !xres);
374 >                op->nRows = outList->nRows;
375                  op->rData = (*cdsF)(op->ofname, outOp,
376                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
377                  freeqstr(op->ofname); op->ofname = NULL;
# Line 368 | Line 381 | RcontribSimulManager::PrepOutput()
381                                  return -1;
382                          if (rd >= op->nRows) {
383                                  if (remWarnings >= 0) {
384 <                                        sprintf(errmsg, "recovered output '%s' already done",
384 >                                        sprintf(errmsg, "recovered output '%s' is complete",
385                                                          op->GetName());
386 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
374 <                                        remWarnings--;
386 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
387                                  }
388                                  rd = op->nRows;
389                          }
# Line 597 | Line 609 | putModContrib(const LUENT *lp, void *p)
609                  }
610                  } break;
611          default:
612 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
612 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
613                  return -1;
614          }
615                                                  // clear for next tally
# Line 616 | Line 628 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
628                  return 0;
629          }
630          if (nkids > 0) {                        // in parent process?
631 <                int     k = GetChild();         // updates output rows
632 <                if (k < 0) return -1;           // can't really happen
631 >                int     k = GetChild(false);    // updates output rows
632 >                if (k < 0) return -1;           // someone died?
633                  RowAssignment   rass;
634                  rass.row = kidRow[k] = rInPos++;
635                  rass.ac = accum;
# Line 662 | Line 674 | RcontribSimulManager::GetChild(bool forceWait)
674                  return -1;
675                                                  // take inventory
676          int     pn, n = 0;
677 <        fd_set  writeset, errset;
678 <        FD_ZERO(&writeset); FD_ZERO(&errset);
677 >        fd_set  readset, errset;
678 >        FD_ZERO(&readset); FD_ZERO(&errset);
679          for (pn = nkids; pn--; ) {
680                  if (kidRow[pn] < 0) {           // child already ready?
681                          if (forceWait) continue;
682                          return pn;              // good enough
683                  }
684 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
685 <                FD_SET(kid[pn].w, &errset);
686 <                if (kid[pn].w >= n)
687 <                        n = kid[pn].w + 1;
684 >                FD_SET(kid[pn].r, &readset);    // will check on this one
685 >                FD_SET(kid[pn].r, &errset);
686 >                if (kid[pn].r >= n)
687 >                        n = kid[pn].r + 1;
688          }
689          if (!n)                                 // every child is idle?
690                  return -1;
691                                                  // wait on "busy" child(ren)
692 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
692 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
693                  if (errno != EINTR) {
694                          error(SYSTEM, "select call failed in GetChild()");
695                          return -1;
696                  }
697 +        char    buf[sizeof(ROW_DONE)] = "X";
698          pn = -1;                                // get flags set by select
699          for (n = nkids; n--; )
700                  if (kidRow[n] >= 0 &&
701 <                                FD_ISSET(kid[n].w, &writeset) |
702 <                                FD_ISSET(kid[n].w, &errset)) {
701 >                                FD_ISSET(kid[n].r, &readset) |
702 >                                FD_ISSET(kid[n].r, &errset)) {
703 >                                                // check for error
704 >                        if (FD_ISSET(kid[n].r, &errset) ||
705 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
706 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
707 >                                return -1;
708                                                  // update output row counts
709 <                        if (!FD_ISSET(kid[n].w, &errset))
710 <                                UpdateRowsDone(kidRow[n]);
693 <                        kidRow[n] = -1;         // flag it available
709 >                        UpdateRowsDone(kidRow[n]);
710 >                        kidRow[n] = -1;         // flag child available
711                          pn = n;
712                  }
713          return pn;
# Line 728 | Line 745 | RcontribSimulManager::RunChild()
745                          error(CONSISTENCY, "bad accumulator count in child");
746                          exit(1);
747                  }
748 <                if (rass.ac > accum)
749 <                        vecList = (FVECT *)erealloc(vecList,
750 <                                                sizeof(FVECT)*2*rass.ac);
748 >                if (rass.ac > accum) {
749 >                        efree(vecList);
750 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
751 >                }
752                  accum = rass.ac;
753                  rInPos = rass.row;
754  
# Line 740 | Line 758 | RcontribSimulManager::RunChild()
758  
759                  if (ComputeRecord(vecList) <= 0)
760                          exit(1);
761 +                                                // signal this row is done
762 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
763 +                        exit(1);
764          }
765          if (nr) {
766                  error(SYSTEM, "read error in child process");
# Line 765 | Line 786 | RcontribSimulManager::StartKids(int n2go)
786          fflush(stdout);                         // shouldn't use, anyway
787          while (nkids < n2go) {
788                  kid[nkids] = sp_inactive;
768                kid[nkids].w = dup(1);
769                kid[nkids].flags |= PF_FILT_OUT;
789                  int     rv = open_process(&kid[nkids], NULL);
790                  if (!rv) {                      // in child process?
791 <                        while (nkids-- > 0)
791 >                        while (nkids-- > 0) {
792 >                                close(kid[nkids].r);
793                                  close(kid[nkids].w);
794 +                        }
795                          free(kid); free(kidRow);
796                          kid = NULL; kidRow = NULL;
797                          RunChild();             // should never return

Diff Legend

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