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.23 by greg, Tue Nov 11 02:08:52 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 +        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 146 | Line 203 | RcontribSimulManager::RctCall(RAY *r, void *cd)
203          if (!mp)
204                  return 0;               // not in our modifier list
205  
206 <        int                     bi = 0; // get bin index
206 >        int     i;                      // pre-emptive check for zero
207 >        if (rcp->HasFlag(RCcontrib)) {
208 >                for (i = NCSAMP; i--; )
209 >                        if (r->rcoef[i]*r->rcol[i] > FTINY)
210 >                                break;
211 >                if (i < 0)
212 >                        return 0;       // zero contribution
213 >        } else if (sintens(r->rcoef) <= FTINY)
214 >                return 0;               // zero coefficient
215 >
216 >        int     bi = 0;                 // get bin index
217          if (mp->binv) {
218                  worldfunc(RCCONTEXT, r);
219                  set_eparams(mp->params);
# Line 163 | Line 230 | RcontribSimulManager::RctCall(RAY *r, void *cd)
230          }
231          SCOLOR          contr;
232          raycontrib(contr, r, PRIMARY);          // compute coefficient
233 <        if (contrib)
233 >        if (rcp->HasFlag(RCcontrib))
234                  smultscolor(contr, r->rcol);    // -> value contribution
235 <        for (int i = 0; i < NCSAMP; i++)
235 >
236 >        for (i = 0; i < NCSAMP; i++)
237                  *dvp++ += contr[i];             // accumulate color/spectrum
238          return 1;
239   }
# Line 193 | Line 261 | bool
261   RcontribSimulManager::AddModifier(const char *modn, const char *outspec,
262                                  const char *prms, const char *binval, int bincnt)
263   {
264 <        if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) {
264 >        if (!modn | !outspec || !*modn | !*outspec) {
265                  error(WARNING, "ignoring bad call to AddModifier()");
266                  return false;
267          }
268 +        if (*outspec == '!') {
269 +                error(USER, "command output not supported by RcontribSimulManager");
270 +                return false;
271 +        }
272          if (!nChan) {                           // initial call?
273 +                if ((xres < 0) | (yres <= 0)) {
274 +                        error(USER, "xres, yres must be set before first modifier");
275 +                        return false;
276 +                }
277                  if (!SetDataFormat(dtyp))
278                          return false;
279                  nChan = NCSAMP;
280          } else if (nChan != NCSAMP) {
281 <                error(INTERNAL, "number of spectral channels must be fixed");
281 >                error(USER, "# spectral channels must be fixed in AddModifier()");
282                  return false;
283          }
284          if (Ready()) {
# Line 225 | Line 301 | RcontribSimulManager::AddModifier(const char *modn, co
301          const int       binndx = hasFormat(outspec, "diouxX");
302          int             bin0 = 0;
303          char            fnbuf[512];
304 <        if (!modndx | (modndx > binndx))
304 >        if (!modndx || (binndx > 0) & (modndx > binndx))
305                  sprintf(fnbuf, outspec, bin0, modn);
306          else
307                  sprintf(fnbuf, outspec, modn, bin0);
# Line 254 | Line 330 | RcontribSimulManager::AddModifier(const char *modn, co
330          mp->opl = op;                   // first (maybe only) output channel
331          if (modndx)                     // remember modifier if part of name
332                  op->omod = lp->key;
333 <        if (binndx > 0) {               // append output image/bin list
333 >        if (binndx) {                   // append output image/bin list
334                  op->rowBytes += dsiz;
335                  op->obin = bin0;
336                  for (bincnt = 1; bincnt < mp->nbins; bincnt++) {
337 <                        if (!modndx | (modndx > binndx))
337 >                        if (!modndx || (binndx > 0) &  (modndx > binndx))
338                                  sprintf(fnbuf, outspec, bin0+bincnt, modn);
339                          else
340                                  sprintf(fnbuf, outspec, modn, bin0+bincnt);
# Line 277 | Line 353 | RcontribSimulManager::AddModifier(const char *modn, co
353                          op->rowBytes += dsiz;
354                  }
355          } else                          // else send all results to this channel
356 <                op->rowBytes += bincnt*dsiz;
356 >                op->rowBytes += mp->nbins*dsiz;
357          return true;
358   }
359  
# Line 298 | Line 374 | RcontribSimulManager::AddModFile(const char *modfn, co
374          }
375          char            mod[MAXSTR];
376          while (fgetword(mod, sizeof(mod), fp))
377 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
377 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
378 >                        fclose(fp);
379                          return false;
380 +                }
381          fclose(fp);
382          return true;
383   }
# Line 308 | Line 386 | RcontribSimulManager::AddModFile(const char *modfn, co
386   static int
387   checkModExists(const LUENT *lp, void *p)
388   {
389 <        if (modifier(lp->key) != OVOID)
389 >        OBJECT  mod = modifier(lp->key);
390 >
391 >        if ((mod != OVOID) & (mod < nsceneobjs))
392                  return 1;
393  
394          sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
# Line 321 | Line 401 | int
401   RcontribSimulManager::PrepOutput()
402   {
403          if (!outList || !RtraceSimulManager::Ready()) {
404 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
404 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
405                  return -1;
406          }
407          if (!cdsF) {
408 <                error(INTERNAL, "missing RdataShare constructor call (*cdsF)");
408 >                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
409                  return -1;
410          }
411          if (lu_doall(&modLUT, checkModExists, NULL) < 0)
412                  return -1;
413  
414 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
415          int     remWarnings = 20;
416          for (RcontribOutput *op = outList; op; op = op->next) {
417                  if (op->rData) {
418                          error(INTERNAL, "output channel already open in PrepOutput()");
419                          return -1;
420                  }
421 <                op->nRows = yres * (xres + !xres);
421 >                op->nRows = outList->nRows;
422                  op->rData = (*cdsF)(op->ofname, outOp,
423                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
424                  freeqstr(op->ofname); op->ofname = NULL;
# Line 346 | Line 427 | RcontribSimulManager::PrepOutput()
427                          if (rd < 0)
428                                  return -1;
429                          if (rd >= op->nRows) {
430 <                                if (remWarnings >= 0) {
431 <                                        sprintf(errmsg, "recovered output '%s' already done",
430 >                                if (remWarnings > 0) {
431 >                                        sprintf(errmsg, "recovered output '%s' is complete",
432                                                          op->GetName());
433 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
353 <                                        remWarnings--;
433 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
434                                  }
435                                  rd = op->nRows;
436                          }
# Line 364 | Line 444 | RcontribSimulManager::PrepOutput()
444          }
445          rowsDone.NewBitMap(outList->nRows);     // create row completion map
446          rowsDone.ClearBits(0, rInPos, true);
447 <        return rInPos;
447 >        return nrDone = rInPos;
448   }
449  
450   // Create header in open write-only channel
# Line 392 | Line 472 | RcontribOutput::NewHeader(const RcontribSimulManager *
472          strcpy(hdr+begData, ROWZEROSTR);
473          rowCountPos = begData+LNROWSTR;
474          begData += sizeof(ROWZEROSTR)-1;
475 <        if (!xres | (rowBytes > esiz)) {
475 >        if (!rcp->xres | (rowBytes > esiz)) {
476                  sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz));
477                  begData += strlen(hdr+begData);
478          }
479          sprintf(hdr+begData, "%s%d\n", NCOMPSTR, NCSAMP);
480          begData += strlen(hdr+begData);
481          if (NCSAMP > 3) {
482 <                sprintf(hdr+begData, "%s %f %f %f %f\n", WLSPLTSTR,
482 >                sprintf(hdr+begData, "%s %g %g %g %g\n", WLSPLTSTR,
483                                  WLPART[0], WLPART[1], WLPART[2], WLPART[3]);
484                  begData += strlen(hdr+begData);
485          }
# Line 430 | Line 510 | RcontribOutput::NewHeader(const RcontribSimulManager *
510                          hdr[begData++] = ' ';
511          hdr[begData++] = '\n';          // EOL for data format
512          hdr[begData++] = '\n';          // end of nominal header
513 <        if ((xres > 0) & (rowBytes == esiz)) {  // tack on resolution string?
514 <                sprintf(hdr+begData, PIXSTDFMT, yres, xres);
513 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // tack on resolution string?
514 >                sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres);
515                  begData += strlen(hdr+begData);
516          }
517          return rData->ReleaseMemory(hdr, RDSwrite);
# Line 484 | Line 564 | RcontribOutput::CheckHeader(const RcontribSimulManager
564                  return -1;
565          }
566                                                  // check #columns
567 <        if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
568 <                        !xres | (rowBytes > esiz)) {
569 <                sprintf(errmsg, "expected NCOLS=%d in '%s'",
490 <                                int(rowBytes/esiz), GetName());
567 >        if ((cp = findArgs(hdr, "NCOLS=", begData)) ? (atoi(cp)*esiz != rowBytes)
568 >                                                    : (!rcp->xres | (rowBytes > esiz))) {
569 >                sprintf(errmsg, "expected NCOLS=%d in '%s'", int(rowBytes/esiz), GetName());
570                  error(USER, errmsg);
571                  return -1;
572          }
# Line 500 | Line 579 | RcontribOutput::CheckHeader(const RcontribSimulManager
579          rowCountPos = cp - hdr;
580          int     rlast = atoi(cp);
581          begData++;                              // advance past closing EOL
582 <        if ((xres > 0) & (rowBytes == esiz)) {  // check/skip resolution string?
582 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // check/skip resolution string?
583                  char    rbuf[64];
584 <                sprintf(rbuf, PIXSTDFMT, yres, xres);
584 >                sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres);
585                  int     rlen = strlen(rbuf);
586                  if (strncmp(rbuf, hdr+begData, rlen)) {
587                          sprintf(errmsg, "bad resolution string in '%s'", GetName());
# Line 529 | Line 608 | RcontribSimulManager::ResetRow(int r)
608                          return false;
609  
610          rowsDone.ClearBits(r, rInPos-r, false);
611 <        rInPos = r;
611 >        nrDone = rInPos = r;
612          return true;
613   }
614  
# Line 576 | Line 655 | putModContrib(const LUENT *lp, void *p)
655                  }
656                  } break;
657          default:
658 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
658 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
659                  return -1;
660          }
661                                                  // clear for next tally
# Line 595 | Line 674 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
674                  return 0;
675          }
676          if (nkids > 0) {                        // in parent process?
677 <                int     k = GetChild();         // updates output rows
678 <                if (k < 0) return -1;           // can't really happen
677 >                int     k = GetChild(false);    // updates output rows
678 >                if (k < 0) return -1;           // someone died?
679                  RowAssignment   rass;
680                  rass.row = kidRow[k] = rInPos++;
681                  rass.ac = accum;
# Line 641 | Line 720 | RcontribSimulManager::GetChild(bool forceWait)
720                  return -1;
721                                                  // take inventory
722          int     pn, n = 0;
723 <        fd_set  writeset, errset;
724 <        FD_ZERO(&writeset); FD_ZERO(&errset);
723 >        fd_set  readset, errset;
724 >        FD_ZERO(&readset); FD_ZERO(&errset);
725          for (pn = nkids; pn--; ) {
726                  if (kidRow[pn] < 0) {           // child already ready?
727                          if (forceWait) continue;
728                          return pn;              // good enough
729                  }
730 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
731 <                FD_SET(kid[pn].w, &errset);
732 <                if (kid[pn].w >= n)
733 <                        n = kid[pn].w + 1;
730 >                FD_SET(kid[pn].r, &readset);    // will check on this one
731 >                FD_SET(kid[pn].r, &errset);
732 >                if (kid[pn].r >= n)
733 >                        n = kid[pn].r + 1;
734          }
735          if (!n)                                 // every child is idle?
736                  return -1;
737                                                  // wait on "busy" child(ren)
738 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
738 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
739                  if (errno != EINTR) {
740                          error(SYSTEM, "select call failed in GetChild()");
741                          return -1;
742                  }
743 +        char    buf[sizeof(ROW_DONE)] = "X";
744          pn = -1;                                // get flags set by select
745          for (n = nkids; n--; )
746                  if (kidRow[n] >= 0 &&
747 <                                FD_ISSET(kid[n].w, &writeset) |
748 <                                FD_ISSET(kid[n].w, &errset)) {
747 >                                FD_ISSET(kid[n].r, &readset) |
748 >                                FD_ISSET(kid[n].r, &errset)) {
749 >                                                // check for error
750 >                        if (FD_ISSET(kid[n].r, &errset) ||
751 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
752 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
753 >                                return -1;
754                                                  // update output row counts
755 <                        if (!FD_ISSET(kid[n].w, &errset))
756 <                                UpdateRowsDone(kidRow[n]);
672 <                        kidRow[n] = -1;         // flag it available
755 >                        UpdateRowsDone(kidRow[n]);
756 >                        kidRow[n] = -1;         // flag child available
757                          pn = n;
758                  }
759          return pn;
# Line 683 | Line 767 | RcontribSimulManager::UpdateRowsDone(int r)
767                  error(WARNING, "redundant call to UpdateRowsDone()");
768                  return false;
769          }
770 <        int     nDone = GetRowFinished();
771 <        if (nDone <= r)
770 >        GetRowFinished();
771 >        if (nrDone <= r)
772                  return true;                    // nothing to update, yet
773          for (RcontribOutput *op = outList; op; op = op->next)
774 <                if (!op->SetRowsDone(nDone))
774 >                if (!op->SetRowsDone(nrDone))
775                          return false;
776          return true;                            // up-to-date
777   }
# Line 707 | Line 791 | RcontribSimulManager::RunChild()
791                          error(CONSISTENCY, "bad accumulator count in child");
792                          exit(1);
793                  }
794 <                if (rass.ac > accum)
795 <                        vecList = (FVECT *)erealloc(vecList,
796 <                                                sizeof(FVECT)*2*rass.ac);
794 >                if (rass.ac > accum) {
795 >                        efree(vecList);
796 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
797 >                }
798                  accum = rass.ac;
799                  rInPos = rass.row;
800  
# Line 719 | Line 804 | RcontribSimulManager::RunChild()
804  
805                  if (ComputeRecord(vecList) <= 0)
806                          exit(1);
807 +                                                // signal this row is done
808 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
809 +                        exit(1);
810          }
811          if (nr) {
812                  error(SYSTEM, "read error in child process");
# Line 744 | Line 832 | RcontribSimulManager::StartKids(int n2go)
832          fflush(stdout);                         // shouldn't use, anyway
833          while (nkids < n2go) {
834                  kid[nkids] = sp_inactive;
747                kid[nkids].w = dup(1);
748                kid[nkids].flags |= PF_FILT_OUT;
835                  int     rv = open_process(&kid[nkids], NULL);
836                  if (!rv) {                      // in child process?
837 <                        while (nkids-- > 0)
837 >                        while (nkids-- > 0) {
838 >                                close(kid[nkids].r);
839                                  close(kid[nkids].w);
840 +                        }
841                          free(kid); free(kidRow);
842                          kid = NULL; kidRow = NULL;
843                          RunChild();             // should never return
# Line 800 | Line 888 | RcontribSimulManager::SetThreadCount(int nt)
888                  return 0;
889          }
890          if (nt < 0)
891 <                return nkids;
891 >                return NThreads();
892          if (!nt) nt = GetNCores();
893          int     status = 0;
894          if (nt == 1)
# Line 813 | Line 901 | RcontribSimulManager::SetThreadCount(int nt)
901                  sprintf(errmsg, "non-zero (%d) status from child", status);
902                  error(WARNING, errmsg);
903          }
904 <        return nkids;
904 >        return NThreads();
905   }

Diff Legend

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