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.2 by greg, Tue Oct 29 19:47:19 2024 UTC vs.
Revision 2.15 by greg, Fri Oct 17 17:43:53 2025 UTC

# Line 24 | Line 24 | extern const char      HDRSTR[];
24   extern const char       BIGEND[];
25   extern const char       FMTSTR[];
26  
27 int     contrib = 0;                    // computing contributions?
28
29 int     xres = 0;                       // horizontal (scan) size
30 int     yres = 0;                       // vertical resolution
31
27   // new/exclusive, overwrite if exists, or recover data
28   int     RSDOflags[] = {RDSwrite|RDSexcl|RDSextend, RDSwrite|RDSextend,
29                                  RDSread|RDSwrite};
# Line 40 | Line 35 | static const char      ROWZEROSTR[] = "NROWS=00000000000000
35   struct RcontribMod {
36          RcontribOutput *        opl;            // pointer to first output channel
37          char *                  params;         // parameters string
38 <        EPNODE *                binv;           // bin expression
38 >        EPNODE *                binv;           // bin expression (NULL if 1 bin)
39          int                     nbins;          // bin count this modifier
40          int                     coffset;        // column offset in bytes
41          DCOLORV                 cbin[1];        // bin accumulator (extends struct)
# Line 57 | Line 52 | struct RowAssignment {
52          uint32                  ac;             // accumulation count
53   };
54  
55 < // Our default data share function
61 < RdataShare *
62 < defDataShare(const char *name, RCOutputOp op, size_t siz)
63 < {
64 <        return new RdataShareMap(name, RSDOflags[op], siz);
65 < }
55 > static const char       ROW_DONE[] = "ROW FINISHED\n";
56  
57 < // Allocate rcontrib accumulator
58 < RcontribMod *
57 > // allocate rcontrib accumulator
58 > static RcontribMod *
59   NewRcMod(const char *prms, const char *binexpr, int ncbins)
60   {
71        if (ncbins <= 0) return NULL;
61          if (!prms) prms = "";
62 <        if (!binexpr | (ncbins == 1))
63 <                binexpr = "0";
64 <
62 >        if ((ncbins > 1) & !binexpr) {
63 >                error(USER, "missing bin expression");
64 >                return NULL;
65 >        }
66 >        if (ncbins < 1) ncbins = 1;
67 >        
68          RcontribMod *   mp = (RcontribMod *)ecalloc(1, sizeof(RcontribMod) +
69                                                  sizeof(DCOLORV)*(NCSAMP*ncbins-1) +
70                                                  strlen(prms)+1);
71  
72 +        if (binexpr) {                          // get/check bin expression
73 +                mp->binv = eparse(const_cast<char *>(binexpr));
74 +                if (mp->binv->type == NUM) {    // constant expression (0)?
75 +                        if ((int)evalue(mp->binv) != 0) {
76 +                                sprintf(errmsg, "illegal non-zero constant for bin (%s)",
77 +                                                binexpr);
78 +                                error(USER, errmsg);
79 +                        }
80 +                        if (ncbins > 1) {
81 +                                sprintf(errmsg, "bad bin count (%d should be 1)", ncbins);
82 +                                error(USER, errmsg);
83 +                        }
84 +                        epfree(mp->binv, true);
85 +                        mp->binv = NULL;
86 +                        prms = "";
87 +                        ncbins = 1;
88 +                }
89 +        }
90          mp->params = strcpy((char *)(mp->cbin + ncbins*NCSAMP), prms);
81        mp->binv = eparse(const_cast<char *>(binexpr));
91          mp->nbins = ncbins;
92          return mp;
93   }
94  
95 < // Free an RcontribMod
95 > // Free an RcontribMod (public for RcontribSimulManager constructor)
96   void
97   FreeRcMod(void *p)
98   {
99          if (!p) return;
100 <        epfree((*(RcontribMod *)p).binv, true);
100 >        EPNODE *        bep = (*(RcontribMod *)p).binv;
101 >        if (bep) epfree(bep, true);
102          efree(p);
103   }
104  
105 + // Get format identifier
106 + const char *
107 + formstr(int f)
108 + {
109 +        switch (f) {
110 +        case 'a': return("ascii");
111 +        case 'f': return("float");
112 +        case 'd': return("double");
113 +        case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT);
114 +        }
115 +        return("unknown");
116 + }
117 +
118 + // Our default data share function
119 + RdataShare *
120 + defDataShare(const char *name, RCOutputOp op, size_t siz)
121 + {
122 +        return new RdataShareMap(name, RSDOflags[op], siz);
123 + }
124 +
125   // Set output format ('f', 'd', or 'c')
126   bool
127   RcontribSimulManager::SetDataFormat(int ty)
# Line 135 | Line 165 | RcontribSimulManager::RctCall(RAY *r, void *cd)
165          if (!mp)
166                  return 0;               // not in our modifier list
167  
168 <        worldfunc(RCCONTEXT, r);        // compute bin #
169 <        set_eparams(mp->params);
170 <        double          bval = evalue(mp->binv);
171 <        if (bval <= -.5)
172 <                return 0;       // silently ignore negative bin index
173 <        DCOLORV *       dvp = (*mp)[int(bval + .5)];
168 >        int                     bi = 0; // get bin index
169 >        if (mp->binv) {
170 >                worldfunc(RCCONTEXT, r);
171 >                set_eparams(mp->params);
172 >                double          bval = evalue(mp->binv);
173 >                if (bval <= -.5)
174 >                        return 0;       // silently ignore negative bin index
175 >                bi = int(bval + .5);
176 >        }
177 >        DCOLORV *       dvp = (*mp)[bi];
178          if (!dvp) {
179 <                sprintf(errmsg, "bad bin number for '%s' (%.1f ignored)", mname, bval);
179 >                sprintf(errmsg, "bad bin number for '%s' (%d ignored)", mname, bi);
180                  error(WARNING, errmsg);
181                  return 0;
182          }
183          SCOLOR          contr;
184          raycontrib(contr, r, PRIMARY);          // compute coefficient
185 <        if (contrib)
185 >        if (rcp->HasFlag(RCcontrib))
186                  smultscolor(contr, r->rcol);    // -> value contribution
187 +
188          for (int i = 0; i < NCSAMP; i++)
189                  *dvp++ += contr[i];             // accumulate color/spectrum
190          return 1;
# Line 178 | Line 213 | bool
213   RcontribSimulManager::AddModifier(const char *modn, const char *outspec,
214                                  const char *prms, const char *binval, int bincnt)
215   {
216 <        if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) {
216 >        if (!modn | !outspec || !*modn | !*outspec) {
217                  error(WARNING, "ignoring bad call to AddModifier()");
218                  return false;
219          }
220 +        if (*outspec == '!') {
221 +                error(USER, "command output not supported by RcontribSimulManager");
222 +                return false;
223 +        }
224          if (!nChan) {                           // initial call?
225 +                if ((xres < 0) | (yres <= 0)) {
226 +                        error(USER, "xres, yres must be set before first modifier");
227 +                        return false;
228 +                }
229                  if (!SetDataFormat(dtyp))
230                          return false;
231                  nChan = NCSAMP;
232          } else if (nChan != NCSAMP) {
233 <                error(INTERNAL, "number of spectral channels must be fixed");
233 >                error(USER, "# spectral channels must be fixed in AddModifier()");
234                  return false;
235          }
236          if (Ready()) {
# Line 210 | Line 253 | RcontribSimulManager::AddModifier(const char *modn, co
253          const int       binndx = hasFormat(outspec, "diouxX");
254          int             bin0 = 0;
255          char            fnbuf[512];
256 <        if (!modndx | (modndx > binndx))
256 >        if (!modndx || (binndx > 0) & (modndx > binndx))
257                  sprintf(fnbuf, outspec, bin0, modn);
258          else
259                  sprintf(fnbuf, outspec, modn, bin0);
# Line 239 | Line 282 | RcontribSimulManager::AddModifier(const char *modn, co
282          mp->opl = op;                   // first (maybe only) output channel
283          if (modndx)                     // remember modifier if part of name
284                  op->omod = lp->key;
285 <        if (binndx > 0) {               // append output image/bin list
285 >        if (binndx) {                   // append output image/bin list
286                  op->rowBytes += dsiz;
287                  op->obin = bin0;
288                  for (bincnt = 1; bincnt < mp->nbins; bincnt++) {
289 <                        if (!modndx | (modndx > binndx))
289 >                        if (!modndx || (binndx > 0) &  (modndx > binndx))
290                                  sprintf(fnbuf, outspec, bin0+bincnt, modn);
291                          else
292                                  sprintf(fnbuf, outspec, modn, bin0+bincnt);
# Line 262 | Line 305 | RcontribSimulManager::AddModifier(const char *modn, co
305                          op->rowBytes += dsiz;
306                  }
307          } else                          // else send all results to this channel
308 <                op->rowBytes += bincnt*dsiz;
308 >                op->rowBytes += mp->nbins*dsiz;
309          return true;
310   }
311  
# Line 283 | Line 326 | RcontribSimulManager::AddModFile(const char *modfn, co
326          }
327          char            mod[MAXSTR];
328          while (fgetword(mod, sizeof(mod), fp))
329 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
329 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
330 >                        fclose(fp);
331                          return false;
332 +                }
333          fclose(fp);
334          return true;
335   }
336  
337 + // call-back to check if modifier has been loaded
338 + static int
339 + checkModExists(const LUENT *lp, void *p)
340 + {
341 +        OBJECT  mod = modifier(lp->key);
342 +
343 +        if ((mod != OVOID) & (mod < nsceneobjs))
344 +                return 1;
345 +
346 +        sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
347 +        error(WARNING, errmsg);
348 +        return 0;
349 + }
350 +
351   // Prepare output channels and return # completed rows
352   int
353   RcontribSimulManager::PrepOutput()
354   {
355          if (!outList || !RtraceSimulManager::Ready()) {
356 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
356 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
357                  return -1;
358          }
359 +        if (!cdsF) {
360 +                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
361 +                return -1;
362 +        }
363 +        if (lu_doall(&modLUT, checkModExists, NULL) < 0)
364 +                return -1;
365 +
366 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
367          int     remWarnings = 20;
368          for (RcontribOutput *op = outList; op; op = op->next) {
369                  if (op->rData) {
370                          error(INTERNAL, "output channel already open in PrepOutput()");
371                          return -1;
372                  }
373 <                op->nRows = yres * (xres + !xres);
373 >                op->nRows = outList->nRows;
374                  op->rData = (*cdsF)(op->ofname, outOp,
375                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
376                  freeqstr(op->ofname); op->ofname = NULL;
# Line 313 | Line 380 | RcontribSimulManager::PrepOutput()
380                                  return -1;
381                          if (rd >= op->nRows) {
382                                  if (remWarnings >= 0) {
383 <                                        sprintf(errmsg, "recovered output '%s' already done",
383 >                                        sprintf(errmsg, "recovered output '%s' is complete",
384                                                          op->GetName());
385 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
319 <                                        remWarnings--;
385 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
386                                  }
387                                  rd = op->nRows;
388                          }
# Line 358 | Line 424 | RcontribOutput::NewHeader(const RcontribSimulManager *
424          strcpy(hdr+begData, ROWZEROSTR);
425          rowCountPos = begData+LNROWSTR;
426          begData += sizeof(ROWZEROSTR)-1;
427 <        if (!xres | (rowBytes > esiz)) {
427 >        if (!rcp->xres | (rowBytes > esiz)) {
428                  sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz));
429                  begData += strlen(hdr+begData);
430          }
# Line 396 | Line 462 | RcontribOutput::NewHeader(const RcontribSimulManager *
462                          hdr[begData++] = ' ';
463          hdr[begData++] = '\n';          // EOL for data format
464          hdr[begData++] = '\n';          // end of nominal header
465 <        if ((xres > 0) & (rowBytes == esiz)) {  // tack on resolution string?
466 <                sprintf(hdr+begData, PIXSTDFMT, yres, xres);
465 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // tack on resolution string?
466 >                sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres);
467                  begData += strlen(hdr+begData);
468          }
469          return rData->ReleaseMemory(hdr, RDSwrite);
# Line 443 | Line 509 | RcontribOutput::CheckHeader(const RcontribSimulManager
509                  return -1;
510          }
511                                                  // check format
512 <        if (!(cp = findArgs(hdr, FMTSTR, begData)) || strcmp(cp, formstr(etyp))) {
512 >        if (!(cp = findArgs(hdr, FMTSTR, begData)) ||
513 >                                strncmp(cp, formstr(etyp), strlen(formstr(etyp)))) {
514                  sprintf(errmsg, "expected %s%s in '%s'", FMTSTR, formstr(etyp), GetName());
515                  error(USER, errmsg);
516                  return -1;
517          }
518                                                  // check #columns
519          if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
520 <                        !xres | (rowBytes > esiz)) {
520 >                        !rcp->xres | (rowBytes > esiz)) {
521                  sprintf(errmsg, "expected NCOLS=%d in '%s'",
522                                  int(rowBytes/esiz), GetName());
523                  error(USER, errmsg);
# Line 465 | Line 532 | RcontribOutput::CheckHeader(const RcontribSimulManager
532          rowCountPos = cp - hdr;
533          int     rlast = atoi(cp);
534          begData++;                              // advance past closing EOL
535 <        if ((xres > 0) & (rowBytes == esiz)) {  // check/skip resolution string?
535 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // check/skip resolution string?
536                  char    rbuf[64];
537 <                sprintf(rbuf, PIXSTDFMT, yres, xres);
537 >                sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres);
538                  int     rlen = strlen(rbuf);
539                  if (strncmp(rbuf, hdr+begData, rlen)) {
540                          sprintf(errmsg, "bad resolution string in '%s'", GetName());
# Line 541 | Line 608 | putModContrib(const LUENT *lp, void *p)
608                  }
609                  } break;
610          default:
611 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
611 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
612                  return -1;
613          }
614                                                  // clear for next tally
# Line 560 | Line 627 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
627                  return 0;
628          }
629          if (nkids > 0) {                        // in parent process?
630 <                int     k = GetChild();         // updates output rows
631 <                if (k < 0) return -1;           // can't really happen
630 >                int     k = GetChild(false);    // updates output rows
631 >                if (k < 0) return -1;           // someone died?
632                  RowAssignment   rass;
633                  rass.row = kidRow[k] = rInPos++;
634                  rass.ac = accum;
# Line 606 | Line 673 | RcontribSimulManager::GetChild(bool forceWait)
673                  return -1;
674                                                  // take inventory
675          int     pn, n = 0;
676 <        fd_set  writeset, errset;
677 <        FD_ZERO(&writeset); FD_ZERO(&errset);
676 >        fd_set  readset, errset;
677 >        FD_ZERO(&readset); FD_ZERO(&errset);
678          for (pn = nkids; pn--; ) {
679                  if (kidRow[pn] < 0) {           // child already ready?
680                          if (forceWait) continue;
681                          return pn;              // good enough
682                  }
683 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
684 <                FD_SET(kid[pn].w, &errset);
685 <                if (kid[pn].w >= n)
686 <                        n = kid[pn].w + 1;
683 >                FD_SET(kid[pn].r, &readset);    // will check on this one
684 >                FD_SET(kid[pn].r, &errset);
685 >                if (kid[pn].r >= n)
686 >                        n = kid[pn].r + 1;
687          }
688          if (!n)                                 // every child is idle?
689                  return -1;
690                                                  // wait on "busy" child(ren)
691 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
691 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
692                  if (errno != EINTR) {
693                          error(SYSTEM, "select call failed in GetChild()");
694                          return -1;
695                  }
696 +        char    buf[sizeof(ROW_DONE)] = "X";
697          pn = -1;                                // get flags set by select
698          for (n = nkids; n--; )
699                  if (kidRow[n] >= 0 &&
700 <                                FD_ISSET(kid[n].w, &writeset) |
701 <                                FD_ISSET(kid[n].w, &errset)) {
700 >                                FD_ISSET(kid[n].r, &readset) |
701 >                                FD_ISSET(kid[n].r, &errset)) {
702 >                                                // check for error
703 >                        if (FD_ISSET(kid[n].r, &errset) ||
704 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
705 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
706 >                                return -1;
707                                                  // update output row counts
708                          UpdateRowsDone(kidRow[n]);
709 <                        kidRow[n] = -1;         // flag it available
709 >                        kidRow[n] = -1;         // flag child available
710                          pn = n;
711                  }
712          return pn;
# Line 671 | Line 744 | RcontribSimulManager::RunChild()
744                          error(CONSISTENCY, "bad accumulator count in child");
745                          exit(1);
746                  }
747 <                if (rass.ac > accum)
748 <                        vecList = (FVECT *)erealloc(vecList,
749 <                                                sizeof(FVECT)*2*rass.ac);
747 >                if (rass.ac > accum) {
748 >                        efree(vecList);
749 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
750 >                }
751                  accum = rass.ac;
752                  rInPos = rass.row;
753  
# Line 683 | Line 757 | RcontribSimulManager::RunChild()
757  
758                  if (ComputeRecord(vecList) <= 0)
759                          exit(1);
760 +                                                // signal this row is done
761 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
762 +                        exit(1);
763          }
764          if (nr) {
765                  error(SYSTEM, "read error in child process");
# Line 708 | Line 785 | RcontribSimulManager::StartKids(int n2go)
785          fflush(stdout);                         // shouldn't use, anyway
786          while (nkids < n2go) {
787                  kid[nkids] = sp_inactive;
711                kid[nkids].w = dup(1);
712                kid[nkids].flags |= PF_FILT_OUT;
788                  int     rv = open_process(&kid[nkids], NULL);
789                  if (!rv) {                      // in child process?
790 <                        while (nkids-- > 0)
790 >                        while (nkids-- > 0) {
791 >                                close(kid[nkids].r);
792                                  close(kid[nkids].w);
793 +                        }
794                          free(kid); free(kidRow);
795                          kid = NULL; kidRow = NULL;
719                        rowsDone.NewBitMap(0);
796                          RunChild();             // should never return
797                          _exit(1);
798                  }
# Line 765 | Line 841 | RcontribSimulManager::SetThreadCount(int nt)
841                  return 0;
842          }
843          if (nt < 0)
844 <                return nkids;
844 >                return NThreads();
845          if (!nt) nt = GetNCores();
846          int     status = 0;
847          if (nt == 1)
# Line 778 | Line 854 | RcontribSimulManager::SetThreadCount(int nt)
854                  sprintf(errmsg, "non-zero (%d) status from child", status);
855                  error(WARNING, errmsg);
856          }
857 <        return nkids;
857 >        return NThreads();
858   }

Diff Legend

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