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.13 by greg, Thu Oct 16 23:14:31 2025 UTC vs.
Revision 2.24 by greg, Wed Nov 12 19:41:21 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 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 + // 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 151 | Line 191 | RcontribSimulManager::SetDataFormat(int ty)
191   int
192   RcontribSimulManager::RctCall(RAY *r, void *cd)
193   {
194 <        if (!r->ro || r->ro->omod == OVOID)     // hit nothing?
195 <                return 0;
196 <                                                // shadow ray not on source?
194 >        int     i;
195 >
196 >        if (!r->ro || (i = r->ro->omod) == OVOID)
197 >                return 0;               // hit nothing
198 >
199          if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
200 <                return 0;
200 >                return 0;               // shadow ray not on source
201  
202 <        const char *            mname = objptr(r->ro->omod)->oname;
202 >        const char *            mname = objptr(i)->oname;
203          RcontribSimulManager *  rcp = (RcontribSimulManager *)cd;
204          RcontribMod *           mp = (RcontribMod *)lu_find(&rcp->modLUT,mname)->data;
205          if (!mp)
206                  return 0;               // not in our modifier list
207  
208 <        int                     bi = 0; // get bin index
208 >        if (rcp->HasFlag(RCcontrib)) {  // pre-emptive check for zero
209 >                for (i = NCSAMP; i--; )
210 >                        if (r->rcoef[i]*r->rcol[i] > FTINY)
211 >                                break;
212 >                if (i < 0)
213 >                        return 0;       // zero contribution
214 >        } else if (sintens(r->rcoef) <= FTINY)
215 >                return 0;               // zero coefficient
216 >
217 >        int     bi = 0;                 // get bin index
218          if (mp->binv) {
219                  worldfunc(RCCONTEXT, r);
220                  set_eparams(mp->params);
# Line 183 | Line 234 | RcontribSimulManager::RctCall(RAY *r, void *cd)
234          if (rcp->HasFlag(RCcontrib))
235                  smultscolor(contr, r->rcol);    // -> value contribution
236  
237 <        for (int i = 0; i < NCSAMP; i++)
237 >        for (i = 0; i < NCSAMP; i++)
238                  *dvp++ += contr[i];             // accumulate color/spectrum
239          return 1;
240   }
# Line 377 | Line 428 | RcontribSimulManager::PrepOutput()
428                          if (rd < 0)
429                                  return -1;
430                          if (rd >= op->nRows) {
431 <                                if (remWarnings >= 0) {
431 >                                if (remWarnings > 0) {
432                                          sprintf(errmsg, "recovered output '%s' is complete",
433                                                          op->GetName());
434                                          error(WARNING, --remWarnings ? errmsg : "etc...");
# Line 394 | Line 445 | RcontribSimulManager::PrepOutput()
445          }
446          rowsDone.NewBitMap(outList->nRows);     // create row completion map
447          rowsDone.ClearBits(0, rInPos, true);
448 <        return rInPos;
448 >        return nrDone = rInPos;
449   }
450  
451   // Create header in open write-only channel
# Line 429 | Line 480 | RcontribOutput::NewHeader(const RcontribSimulManager *
480          sprintf(hdr+begData, "%s%d\n", NCOMPSTR, NCSAMP);
481          begData += strlen(hdr+begData);
482          if (NCSAMP > 3) {
483 <                sprintf(hdr+begData, "%s %f %f %f %f\n", WLSPLTSTR,
483 >                sprintf(hdr+begData, "%s %g %g %g %g\n", WLSPLTSTR,
484                                  WLPART[0], WLPART[1], WLPART[2], WLPART[3]);
485                  begData += strlen(hdr+begData);
486          }
# Line 514 | Line 565 | RcontribOutput::CheckHeader(const RcontribSimulManager
565                  return -1;
566          }
567                                                  // check #columns
568 <        if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
569 <                        !rcp->xres | (rowBytes > esiz)) {
570 <                sprintf(errmsg, "expected NCOLS=%d in '%s'",
520 <                                int(rowBytes/esiz), GetName());
568 >        if ((cp = findArgs(hdr, "NCOLS=", begData)) ? (atoi(cp)*esiz != rowBytes)
569 >                                                    : (!rcp->xres | (rowBytes > esiz))) {
570 >                sprintf(errmsg, "expected NCOLS=%d in '%s'", int(rowBytes/esiz), GetName());
571                  error(USER, errmsg);
572                  return -1;
573          }
# Line 559 | Line 609 | RcontribSimulManager::ResetRow(int r)
609                          return false;
610  
611          rowsDone.ClearBits(r, rInPos-r, false);
612 <        rInPos = r;
612 >        nrDone = rInPos = r;
613          return true;
614   }
615  
# Line 625 | Line 675 | RcontribSimulManager::ComputeRecord(const FVECT orig_d
675                  return 0;
676          }
677          if (nkids > 0) {                        // in parent process?
678 <                int     k = GetChild();         // updates output rows
679 <                if (k < 0) return -1;           // can't really happen
678 >                int     k = GetChild(false);    // updates output rows
679 >                if (k < 0) return -1;           // someone died?
680                  RowAssignment   rass;
681                  rass.row = kidRow[k] = rInPos++;
682                  rass.ac = accum;
# Line 671 | Line 721 | RcontribSimulManager::GetChild(bool forceWait)
721                  return -1;
722                                                  // take inventory
723          int     pn, n = 0;
724 <        fd_set  writeset, errset;
725 <        FD_ZERO(&writeset); FD_ZERO(&errset);
724 >        fd_set  readset, errset;
725 >        FD_ZERO(&readset); FD_ZERO(&errset);
726          for (pn = nkids; pn--; ) {
727                  if (kidRow[pn] < 0) {           // child already ready?
728                          if (forceWait) continue;
729                          return pn;              // good enough
730                  }
731 <                FD_SET(kid[pn].w, &writeset);   // will check on this one
732 <                FD_SET(kid[pn].w, &errset);
733 <                if (kid[pn].w >= n)
734 <                        n = kid[pn].w + 1;
731 >                FD_SET(kid[pn].r, &readset);    // will check on this one
732 >                FD_SET(kid[pn].r, &errset);
733 >                if (kid[pn].r >= n)
734 >                        n = kid[pn].r + 1;
735          }
736          if (!n)                                 // every child is idle?
737                  return -1;
738                                                  // wait on "busy" child(ren)
739 <        while ((n = select(n, NULL, &writeset, &errset, NULL)) <= 0)
739 >        while ((n = select(n, &readset, NULL, &errset, NULL)) <= 0)
740                  if (errno != EINTR) {
741                          error(SYSTEM, "select call failed in GetChild()");
742                          return -1;
743                  }
744 +        char    buf[sizeof(ROW_DONE)] = "X";
745          pn = -1;                                // get flags set by select
746          for (n = nkids; n--; )
747                  if (kidRow[n] >= 0 &&
748 <                                FD_ISSET(kid[n].w, &writeset) |
749 <                                FD_ISSET(kid[n].w, &errset)) {
748 >                                FD_ISSET(kid[n].r, &readset) |
749 >                                FD_ISSET(kid[n].r, &errset)) {
750 >                                                // check for error
751 >                        if (FD_ISSET(kid[n].r, &errset) ||
752 >                                        read(kid[n].r, buf, sizeof(ROW_DONE)) <= 0 ||
753 >                                        memcmp(buf, ROW_DONE, sizeof(ROW_DONE)))
754 >                                return -1;
755                                                  // update output row counts
756 <                        if (!FD_ISSET(kid[n].w, &errset))
757 <                                UpdateRowsDone(kidRow[n]);
702 <                        kidRow[n] = -1;         // flag it available
756 >                        UpdateRowsDone(kidRow[n]);
757 >                        kidRow[n] = -1;         // flag child available
758                          pn = n;
759                  }
760          return pn;
# Line 713 | Line 768 | RcontribSimulManager::UpdateRowsDone(int r)
768                  error(WARNING, "redundant call to UpdateRowsDone()");
769                  return false;
770          }
771 <        int     nDone = GetRowFinished();
772 <        if (nDone <= r)
771 >        GetRowFinished();
772 >        if (nrDone <= r)
773                  return true;                    // nothing to update, yet
774          for (RcontribOutput *op = outList; op; op = op->next)
775 <                if (!op->SetRowsDone(nDone))
775 >                if (!op->SetRowsDone(nrDone))
776                          return false;
777          return true;                            // up-to-date
778   }
# Line 750 | Line 805 | RcontribSimulManager::RunChild()
805  
806                  if (ComputeRecord(vecList) <= 0)
807                          exit(1);
808 +                                                // signal this row is done
809 +                if (write(1, ROW_DONE, sizeof(ROW_DONE)) != sizeof(ROW_DONE))
810 +                        exit(1);
811          }
812          if (nr) {
813                  error(SYSTEM, "read error in child process");
# Line 775 | Line 833 | RcontribSimulManager::StartKids(int n2go)
833          fflush(stdout);                         // shouldn't use, anyway
834          while (nkids < n2go) {
835                  kid[nkids] = sp_inactive;
778                kid[nkids].w = dup(1);
779                kid[nkids].flags |= PF_FILT_OUT;
836                  int     rv = open_process(&kid[nkids], NULL);
837                  if (!rv) {                      // in child process?
838 <                        while (nkids-- > 0)
838 >                        while (nkids-- > 0) {
839 >                                close(kid[nkids].r);
840                                  close(kid[nkids].w);
841 +                        }
842                          free(kid); free(kidRow);
843                          kid = NULL; kidRow = NULL;
844                          RunChild();             // should never return

Diff Legend

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