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.5 by greg, Fri Nov 1 23:05:01 2024 UTC vs.
Revision 2.17 by greg, Thu Oct 23 16:33:39 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 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   {
61 <        if (ncbins <= 0) return NULL;
61 >        if (binexpr && !*binexpr) binexpr = NULL;
62          if (!prms) prms = "";
63 <        if (!binexpr & (ncbins > 1)) {
64 <                error(INTERNAL, "missing bin expression");
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"))
79 <                        error(WARNING, "ignoring non-zero expression for single bin");
80 <                prms = "";
81 <                binexpr = NULL;
82 <        }
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);
88 <        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 103 | 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 +        return new RdataShareFile(name, RSDOflags[op], siz);
124 + }
125 +
126 + // Memory-mapped data share function
127 + RdataShare *
128 + mapDataShare(const char *name, RCOutputOp op, size_t siz)
129 + {
130 +        return new RdataShareMap(name, RSDOflags[op], siz);
131 + }
132 +
133   // Set output format ('f', 'd', or 'c')
134   bool
135   RcontribSimulManager::SetDataFormat(int ty)
# Line 163 | Line 190 | RcontribSimulManager::RctCall(RAY *r, void *cd)
190          }
191          SCOLOR          contr;
192          raycontrib(contr, r, PRIMARY);          // compute coefficient
193 <        if (contrib)
193 >        if (rcp->HasFlag(RCcontrib))
194                  smultscolor(contr, r->rcol);    // -> value contribution
195 +
196          for (int i = 0; i < NCSAMP; i++)
197                  *dvp++ += contr[i];             // accumulate color/spectrum
198          return 1;
# Line 193 | Line 221 | bool
221   RcontribSimulManager::AddModifier(const char *modn, const char *outspec,
222                                  const char *prms, const char *binval, int bincnt)
223   {
224 <        if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) {
224 >        if (!modn | !outspec || !*modn | !*outspec) {
225                  error(WARNING, "ignoring bad call to AddModifier()");
226                  return false;
227          }
228 +        if (*outspec == '!') {
229 +                error(USER, "command output not supported by RcontribSimulManager");
230 +                return false;
231 +        }
232          if (!nChan) {                           // initial call?
233 +                if ((xres < 0) | (yres <= 0)) {
234 +                        error(USER, "xres, yres must be set before first modifier");
235 +                        return false;
236 +                }
237                  if (!SetDataFormat(dtyp))
238                          return false;
239                  nChan = NCSAMP;
240          } else if (nChan != NCSAMP) {
241 <                error(INTERNAL, "number of spectral channels must be fixed");
241 >                error(USER, "# spectral channels must be fixed in AddModifier()");
242                  return false;
243          }
244          if (Ready()) {
# Line 225 | Line 261 | RcontribSimulManager::AddModifier(const char *modn, co
261          const int       binndx = hasFormat(outspec, "diouxX");
262          int             bin0 = 0;
263          char            fnbuf[512];
264 <        if (!modndx | (modndx > binndx))
264 >        if (!modndx || (binndx > 0) & (modndx > binndx))
265                  sprintf(fnbuf, outspec, bin0, modn);
266          else
267                  sprintf(fnbuf, outspec, modn, bin0);
# Line 254 | Line 290 | RcontribSimulManager::AddModifier(const char *modn, co
290          mp->opl = op;                   // first (maybe only) output channel
291          if (modndx)                     // remember modifier if part of name
292                  op->omod = lp->key;
293 <        if (binndx > 0) {               // append output image/bin list
293 >        if (binndx) {                   // append output image/bin list
294                  op->rowBytes += dsiz;
295                  op->obin = bin0;
296                  for (bincnt = 1; bincnt < mp->nbins; bincnt++) {
297 <                        if (!modndx | (modndx > binndx))
297 >                        if (!modndx || (binndx > 0) &  (modndx > binndx))
298                                  sprintf(fnbuf, outspec, bin0+bincnt, modn);
299                          else
300                                  sprintf(fnbuf, outspec, modn, bin0+bincnt);
# Line 277 | Line 313 | RcontribSimulManager::AddModifier(const char *modn, co
313                          op->rowBytes += dsiz;
314                  }
315          } else                          // else send all results to this channel
316 <                op->rowBytes += bincnt*dsiz;
316 >                op->rowBytes += mp->nbins*dsiz;
317          return true;
318   }
319  
# Line 298 | Line 334 | RcontribSimulManager::AddModFile(const char *modfn, co
334          }
335          char            mod[MAXSTR];
336          while (fgetword(mod, sizeof(mod), fp))
337 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
337 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
338 >                        fclose(fp);
339                          return false;
340 +                }
341          fclose(fp);
342          return true;
343   }
# Line 308 | Line 346 | RcontribSimulManager::AddModFile(const char *modfn, co
346   static int
347   checkModExists(const LUENT *lp, void *p)
348   {
349 <        if (modifier(lp->key) != OVOID)
349 >        OBJECT  mod = modifier(lp->key);
350 >
351 >        if ((mod != OVOID) & (mod < nsceneobjs))
352                  return 1;
353  
354          sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
# Line 321 | Line 361 | int
361   RcontribSimulManager::PrepOutput()
362   {
363          if (!outList || !RtraceSimulManager::Ready()) {
364 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
364 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
365                  return -1;
366          }
367          if (!cdsF) {
368 <                error(INTERNAL, "missing RdataShare constructor call (*cdsF)");
368 >                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
369                  return -1;
370          }
371          if (lu_doall(&modLUT, checkModExists, NULL) < 0)
372                  return -1;
373  
374 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
375          int     remWarnings = 20;
376          for (RcontribOutput *op = outList; op; op = op->next) {
377                  if (op->rData) {
378                          error(INTERNAL, "output channel already open in PrepOutput()");
379                          return -1;
380                  }
381 <                op->nRows = yres * (xres + !xres);
381 >                op->nRows = outList->nRows;
382                  op->rData = (*cdsF)(op->ofname, outOp,
383                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
384                  freeqstr(op->ofname); op->ofname = NULL;
# Line 347 | Line 388 | RcontribSimulManager::PrepOutput()
388                                  return -1;
389                          if (rd >= op->nRows) {
390                                  if (remWarnings >= 0) {
391 <                                        sprintf(errmsg, "recovered output '%s' already done",
391 >                                        sprintf(errmsg, "recovered output '%s' is complete",
392                                                          op->GetName());
393 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
353 <                                        remWarnings--;
393 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
394                                  }
395                                  rd = op->nRows;
396                          }
# Line 392 | Line 432 | RcontribOutput::NewHeader(const RcontribSimulManager *
432          strcpy(hdr+begData, ROWZEROSTR);
433          rowCountPos = begData+LNROWSTR;
434          begData += sizeof(ROWZEROSTR)-1;
435 <        if (!xres | (rowBytes > esiz)) {
435 >        if (!rcp->xres | (rowBytes > esiz)) {
436                  sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz));
437                  begData += strlen(hdr+begData);
438          }
# Line 430 | Line 470 | RcontribOutput::NewHeader(const RcontribSimulManager *
470                          hdr[begData++] = ' ';
471          hdr[begData++] = '\n';          // EOL for data format
472          hdr[begData++] = '\n';          // end of nominal header
473 <        if ((xres > 0) & (rowBytes == esiz)) {  // tack on resolution string?
474 <                sprintf(hdr+begData, PIXSTDFMT, yres, xres);
473 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // tack on resolution string?
474 >                sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres);
475                  begData += strlen(hdr+begData);
476          }
477          return rData->ReleaseMemory(hdr, RDSwrite);
# Line 485 | Line 525 | RcontribOutput::CheckHeader(const RcontribSimulManager
525          }
526                                                  // check #columns
527          if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
528 <                        !xres | (rowBytes > esiz)) {
528 >                        !rcp->xres | (rowBytes > esiz)) {
529                  sprintf(errmsg, "expected NCOLS=%d in '%s'",
530                                  int(rowBytes/esiz), GetName());
531                  error(USER, errmsg);
# Line 500 | Line 540 | RcontribOutput::CheckHeader(const RcontribSimulManager
540          rowCountPos = cp - hdr;
541          int     rlast = atoi(cp);
542          begData++;                              // advance past closing EOL
543 <        if ((xres > 0) & (rowBytes == esiz)) {  // check/skip resolution string?
543 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // check/skip resolution string?
544                  char    rbuf[64];
545 <                sprintf(rbuf, PIXSTDFMT, yres, xres);
545 >                sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres);
546                  int     rlen = strlen(rbuf);
547                  if (strncmp(rbuf, hdr+begData, rlen)) {
548                          sprintf(errmsg, "bad resolution string in '%s'", GetName());
# Line 576 | Line 616 | putModContrib(const LUENT *lp, void *p)
616                  }
617                  } break;
618          default:
619 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
619 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
620                  return -1;
621          }
622                                                  // clear for next tally
# Line 595 | Line 635 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
635                  return 0;
636          }
637          if (nkids > 0) {                        // in parent process?
638 <                int     k = GetChild();         // updates output rows
639 <                if (k < 0) return -1;           // can't really happen
638 >                int     k = GetChild(false);    // updates output rows
639 >                if (k < 0) return -1;           // someone died?
640                  RowAssignment   rass;
641                  rass.row = kidRow[k] = rInPos++;
642                  rass.ac = accum;
# Line 641 | Line 681 | RcontribSimulManager::GetChild(bool forceWait)
681                  return -1;
682                                                  // take inventory
683          int     pn, n = 0;
684 <        fd_set  writeset, errset;
685 <        FD_ZERO(&writeset); FD_ZERO(&errset);
684 >        fd_set  readset, errset;
685 >        FD_ZERO(&readset); FD_ZERO(&errset);
686          for (pn = nkids; pn--; ) {
687                  if (kidRow[pn] < 0) {           // child already ready?
688                          if (forceWait) continue;
689                          return pn;              // good enough
690                  }
691 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
692 <                FD_SET(kid[pn].w, &errset);
693 <                if (kid[pn].w >= n)
694 <                        n = kid[pn].w + 1;
691 >                FD_SET(kid[pn].r, &readset);    // will check on this one
692 >                FD_SET(kid[pn].r, &errset);
693 >                if (kid[pn].r >= n)
694 >                        n = kid[pn].r + 1;
695          }
696          if (!n)                                 // every child is idle?
697                  return -1;
698                                                  // wait on "busy" child(ren)
699 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
699 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
700                  if (errno != EINTR) {
701                          error(SYSTEM, "select call failed in GetChild()");
702                          return -1;
703                  }
704 +        char    buf[sizeof(ROW_DONE)] = "X";
705          pn = -1;                                // get flags set by select
706          for (n = nkids; n--; )
707                  if (kidRow[n] >= 0 &&
708 <                                FD_ISSET(kid[n].w, &writeset) |
709 <                                FD_ISSET(kid[n].w, &errset)) {
708 >                                FD_ISSET(kid[n].r, &readset) |
709 >                                FD_ISSET(kid[n].r, &errset)) {
710 >                                                // check for error
711 >                        if (FD_ISSET(kid[n].r, &errset) ||
712 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
713 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
714 >                                return -1;
715                                                  // update output row counts
716 <                        if (!FD_ISSET(kid[n].w, &errset))
717 <                                UpdateRowsDone(kidRow[n]);
672 <                        kidRow[n] = -1;         // flag it available
716 >                        UpdateRowsDone(kidRow[n]);
717 >                        kidRow[n] = -1;         // flag child available
718                          pn = n;
719                  }
720          return pn;
# Line 707 | Line 752 | RcontribSimulManager::RunChild()
752                          error(CONSISTENCY, "bad accumulator count in child");
753                          exit(1);
754                  }
755 <                if (rass.ac > accum)
756 <                        vecList = (FVECT *)erealloc(vecList,
757 <                                                sizeof(FVECT)*2*rass.ac);
755 >                if (rass.ac > accum) {
756 >                        efree(vecList);
757 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
758 >                }
759                  accum = rass.ac;
760                  rInPos = rass.row;
761  
# Line 719 | Line 765 | RcontribSimulManager::RunChild()
765  
766                  if (ComputeRecord(vecList) <= 0)
767                          exit(1);
768 +                                                // signal this row is done
769 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
770 +                        exit(1);
771          }
772          if (nr) {
773                  error(SYSTEM, "read error in child process");
# Line 744 | Line 793 | RcontribSimulManager::StartKids(int n2go)
793          fflush(stdout);                         // shouldn't use, anyway
794          while (nkids < n2go) {
795                  kid[nkids] = sp_inactive;
747                kid[nkids].w = dup(1);
748                kid[nkids].flags |= PF_FILT_OUT;
796                  int     rv = open_process(&kid[nkids], NULL);
797                  if (!rv) {                      // in child process?
798 <                        while (nkids-- > 0)
798 >                        while (nkids-- > 0) {
799 >                                close(kid[nkids].r);
800                                  close(kid[nkids].w);
801 +                        }
802                          free(kid); free(kidRow);
803                          kid = NULL; kidRow = NULL;
804                          RunChild();             // should never return
# Line 800 | Line 849 | RcontribSimulManager::SetThreadCount(int nt)
849                  return 0;
850          }
851          if (nt < 0)
852 <                return nkids;
852 >                return NThreads();
853          if (!nt) nt = GetNCores();
854          int     status = 0;
855          if (nt == 1)
# Line 813 | Line 862 | RcontribSimulManager::SetThreadCount(int nt)
862                  sprintf(errmsg, "non-zero (%d) status from child", status);
863                  error(WARNING, errmsg);
864          }
865 <        return nkids;
865 >        return NThreads();
866   }

Diff Legend

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