ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RcontribSimulManager.h
Revision: 2.6
Committed: Wed Nov 6 19:45:59 2024 UTC (5 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +16 -15 lines
Log Message:
feat(rxcontrib): Folded resolution and contrib flag into main class

File Contents

# Content
1 /* RCSid $Id: RcontribSimulManager.h,v 2.5 2024/11/06 18:28:52 greg Exp $ */
2 /*
3 * RcontribSimulManager.h
4 *
5 * Rcontrib simulation manager class declaration
6 *
7 * Created by Greg Ward on 10/11/2024.
8 */
9
10 #ifndef RcontribSimulManager_h
11 #define RcontribSimulManager_h
12
13 #include "RtraceSimulManager.h"
14 #include "RdataShare.h"
15 #include "lookup.h"
16 #include "rtprocess.h"
17
18 /*
19 * As with other single-object classes, many global variable affect
20 * behavior. Besides rendering parameters, there are spectral parameters
21 * and output dimensions taken from the environment.
22 *
23 * NOTE: "record" and "row" are used interchangeably throughout.
24 */
25
26 extern char RCCONTEXT[]; // global rcontrib context
27
28 class RcontribSimulManager; // need forward decl
29
30 /// Shared data object for record output (includes header; may be write-only)
31 class RcontribOutput {
32 RcontribOutput * next; // next in sorted list
33 char * ofname; // output file name
34 uint32 rowCountPos; // row count position in header
35 void * rowp; // current row memory
36 bool NewHeader(const RcontribSimulManager *rcp);
37 int CheckHeader(const RcontribSimulManager *rcp);
38 public:
39 RdataShare * rData; // data sharing object
40 size_t rowBytes; // byte count per row
41 const char * omod; // single modifier (or NULL)
42 int32 obin; // single output bin (or -1)
43 uint32 begData; // start of data (type-aligned)
44 int32 curRow; // current output row
45 uint32 nRows; // total number of rows
46 RcontribOutput(const char *fnm = NULL) {
47 next = NULL;
48 omod = NULL;
49 obin = -1;
50 rData = NULL;
51 ofname = savqstr(fnm);
52 rowBytes = 0;
53 nRows = 0;
54 rowp = NULL; curRow = -1;
55 }
56 ~RcontribOutput() {
57 DoneRow();
58 delete rData;
59 delete next;
60 freeqstr(ofname);
61 }
62 /// Return output channel name
63 const char * GetName() const {
64 if (rData) return rData->GetName();
65 return ofname;
66 }
67 /// Update output row count
68 bool SetRowsDone(int r) {
69 if (!rData | (0 >= r) | (r > nRows)) return false;
70 char * rbuf = (char *)rData->GetMemory(rowCountPos, 17, 0);
71 sprintf(rbuf, "%-16d", r);
72 rbuf[16] = '\n'; // replaces nul byte
73 return rData->ReleaseMemory(rbuf, RDSwrite);
74 }
75 /// Get buffer for indicated row (contents may be random)
76 void * GetRow(int r) {
77 if (!rData | (r < 0)) return NULL;
78 if (r != curRow) {
79 DoneRow();
80 if (r < nRows)
81 rowp = rData->GetMemory(begData + r*rowBytes,
82 rowBytes, 0);
83 if (rowp) curRow = r;
84 }
85 return rowp;
86 }
87 /// Current row with byte offset
88 void * InsertionP(int coffset) const {
89 if (!rowp | (coffset < 0) | (coffset >= rowBytes))
90 return NULL;
91 return (char *)rowp + coffset;
92 }
93 /// Release current row, writing contents
94 void DoneRow() {
95 if (rowp) rData->ReleaseMemory(rowp, RDSwrite);
96 rowp = NULL; curRow = -1;
97 }
98 /// Get next in list
99 const RcontribOutput * Next() const {
100 return next;
101 }
102 RcontribOutput * Next() {
103 return next;
104 }
105 /// RcontribSimulManager gets full access
106 friend class RcontribSimulManager;
107 };
108
109 typedef double DCOLORV; // color accumulator type
110
111 /// Modifier channel for recording contributions (no constructor/destructor)
112 struct RcontribMod;
113
114 /// Allocate rcontrib accumulator
115 extern RcontribMod * NewRcMod(const char *prms = NULL, const char *binexpr = NULL, int ncbins = 1);
116 /// Free an RcontribMod
117 extern lut_free_t FreeRcMod;
118
119 /*
120 * General RcontribSimulManager class operation:
121 *
122 * 1) Call LoadOctree(), then alter the header as desired
123 * 2) Set number of spectral samples (NCSAMP) and call SetDataFormat()
124 * 3) Set xres and yres to desired dimensions (xres>0 for picture output)
125 * 4) Call AddModifier() and AddModFile() to indicate tracked modifiers
126 * 5) Set outOp and cdsF according to desired output/recovery
127 * 6) Set desired computation flags via SetFlag()
128 * 7) Call PrepOutput() to open output channels
129 * 8) Call SetThreadCount() to fork children if desired
130 * 9) Set accum to the number of ray samples per record
131 * 10) Call ComputeRecord() with accum ray samples
132 * 11) Continue until GetRowMax() records have been sent
133 * 12) Call Cleanup()
134 *
135 * The order of some of these calls may be changed. Technically, the octree
136 * may be loaded anytime before PrepOutput() is called. Also, SetThreadCount()
137 * may be called anytime *after* PrepOutput(), and may be interleaved with
138 * calls to ComputeRecord(). The accum setting may be changed at any time.
139 * Finally, it is possible to restart the output using ResetRow(), and
140 * a zero argument will rewind to the beginning, whence all records
141 * may be recalculated. The previous output rows are not zeroed or deleted,
142 * but are overwritten as the calculation proceeds from the new starting point.
143 * However, the output file(s) will indicate in the NROWS= line in the header
144 * that only the newly calculated rows are present.
145 *
146 * It is not possible to write to standard output, but the output
147 * model is quite flexible thanks to the RdataShare polymorphic class.
148 * The current default output class creates a shared, memory-mapped file,
149 * which is the most efficient object on most systems.
150 *
151 * ASCII output is not supported, so full data recovery is.
152 */
153
154 /// Output channel opening options: new/exclusive, overwrite if exists, or recover data
155 enum RCOutputOp {RCOnew=0, RCOforce, RCOrecover};
156
157 /// Converts above to RdataShare open flags (may be adjusted by calling program)
158 extern int RSDOflags[];
159
160 /// Call-back function type to create named data channel (freed using "delete" operator)
161 typedef RdataShare * RcreateDataShareF(const char *name, RCOutputOp op, size_t siz);
162
163 /// Our default data share function
164 extern RcreateDataShareF defDataShare;
165
166 /// Modifiable ray-tracing flags for rcontrib
167 #define RCcontrib (RTmask+1) // compute contributions? (r.t. coefficients)
168 #define RCmask (RTlimDist|RTimmIrrad|RCcontrib)
169
170 /// rcontrib-like simulation manager (at most one such object)
171 class RcontribSimulManager : protected RtraceSimulManager {
172 protected:
173 static RayReportCall RctCall; // our callback for traced rays
174 ABitMap rowsDone; // bit mask of completed rows
175 uint32 rInPos; // which row (record) is next on input?
176 uby8 nChan; // NCSAMP setting for this calculation
177 char dtyp; // data type ('f', 'd', or 'c')
178 uint16 dsiz; // N-component element size in bytes
179 RcontribOutput * outList; // ordered list of output channels
180 LUTAB modLUT; // modifier lookup table
181 SUBPROC * kid; // array of child processes
182 int32 * kidRow; // row assigned to each child
183 int nkids; // child process count (-1 in child)
184 bool UpdateRowsDone(int r);
185 int GetChild(bool forceWait = false);
186 bool StartKids(int n2go);
187 int StopKids(int n2end = 0);
188 void RunChild();
189 public:
190 RCOutputOp outOp; // output operation
191 RcreateDataShareF * cdsF; // data share creator
192 int xres, yres; // output (picture) size
193 uint32 accum; // # rays to accumulate per record
194 RcontribSimulManager(const char *octn = NULL)
195 : RtraceSimulManager(NULL, NULL, octn) {
196 rInPos = 0;
197 nChan = 0;
198 dtyp = 'f';
199 dsiz = 0;
200 outList = NULL;
201 memset(&modLUT, 0, sizeof(modLUT));
202 modLUT.hashf = lu_shash;
203 modLUT.keycmp = strcmp;
204 modLUT.freek = efree;
205 modLUT.freed = FreeRcMod;
206 kid = NULL; kidRow = NULL; nkids = 0;
207 rtFlags = RTtraceSources;
208 SetTraceCall(&RctCall, this);
209 outOp = RCOnew;
210 cdsF = &defDataShare;
211 xres = yres = 0;
212 accum = 1;
213 }
214 ~RcontribSimulManager() {
215 Cleanup();
216 }
217 /// Check modifiable ray-tracing computation flag(s)
218 bool HasFlag(int fl) const {
219 return ((rtFlags & RCmask & fl) != 0);
220 }
221 /// Set/reset modifiable ray-tracing computation flag(s)
222 bool SetFlag(int fl, bool val = true) {
223 if (!(fl &= RCmask)) return false;
224 if (val) rtFlags |= fl;
225 else rtFlags &= ~fl;
226 return true;
227 }
228 /// Load octree and prepare renderer
229 bool LoadOctree(const char *octn) {
230 return RtraceSimulManager::LoadOctree(octn);
231 }
232 /// Prepare header from previous input (or clear)
233 bool NewHeader(const char *inspec=NULL) {
234 return RtraceSimulManager::NewHeader(inspec);
235 }
236 /// Add a string to header (adds newline if none)
237 bool AddHeader(const char *str) {
238 return RtraceSimulManager::AddHeader(str);
239 }
240 /// Append program line to header
241 bool AddHeader(int ac, char *av[]) {
242 return RtraceSimulManager::AddHeader(ac, av);
243 }
244 /// Get current header length in bytes
245 int GetHeadLen() const {
246 return RtraceSimulManager::GetHeadLen();
247 }
248 /// Get header lines if any
249 const char * GetHeadStr() const {
250 return RtraceSimulManager::GetHeadStr();
251 }
252 /// Look for specific header keyword, return value
253 const char * GetHeadStr(const char *key, bool inOK = false) const {
254 return RtraceSimulManager::GetHeadStr(key, inOK);
255 }
256 /// Set output format ('f', 'd', or 'c'), call before mods
257 bool SetDataFormat(int ty);
258 /// Get current format (and element size in bytes)
259 int GetFormat(int *siz = NULL) const {
260 if (siz) *siz = dsiz;
261 return dtyp;
262 }
263 /// Add a modifier and arguments, create output(s)
264 bool AddModifier(const char *modn, const char *outspec,
265 const char *prms = NULL,
266 const char *binval = NULL, int bincnt = 1);
267 /// Add a file of modifiers with associated arguments
268 bool AddModFile(const char *modfn, const char *outspec,
269 const char *prms = NULL,
270 const char *binval = NULL, int bincnt = 1);
271 /// Get named rcontrib output (or list)
272 const RcontribOutput * GetOutput(const char *nm = NULL) const {
273 if (!nm) return outList;
274 const RcontribOutput * op = outList;
275 while (op && strcmp(op->GetName(), nm))
276 op = op->next;
277 return op;
278 }
279 /// Open output channels and return # completed rows
280 int PrepOutput();
281 /// Are we ready to compute some records?
282 bool Ready() const {
283 return (rowsDone.Length() > 0) & (accum > 0);
284 }
285 /// Set number of computation threads (0 => #cores)
286 int SetThreadCount(int nt = 0);
287 /// Check thread count (1 means no multi-processing)
288 int NThreads() const {
289 return nkids + !nkids;
290 }
291 /// What is maximum row?
292 int GetRowMax() const {
293 if (!outList) return yres * (xres + !xres);
294 return outList->nRows;
295 }
296 /// Get current row count (# rows sent for computation)
297 int GetRowCount() const {
298 return rInPos;
299 }
300 /// Get # rows completed
301 int GetRowFinished() const {
302 if (!nkids) return rInPos;
303 uint32 nDone = rowsDone.Find(0, false);
304 if (nDone == ABMend)
305 return rowsDone.Length();
306 return nDone;
307 }
308 /// Rewind calculation (previous results unchanged)
309 bool ResetRow(int r);
310 /// Add a ray/bundle to compute next record (n=accum)
311 int ComputeRecord(const FVECT orig_direc[]);
312 /// Finish pending rays if multi-processing
313 bool FlushQueue() {
314 if (nkids <= 0) return true;
315 while (GetChild(true) >= 0)
316 ;
317 return true;
318 }
319 /// Close octree, free data, return status
320 int Cleanup(bool everything = false) {
321 if (nkids < 0) return 0; // skip 4 child
322 if (rowsDone.Length()) {
323 SetThreadCount(1);
324 cow_doneshare();
325 rowsDone.NewBitMap(0);
326 }
327 lu_done(&modLUT);
328 delete outList; outList = NULL;
329 nChan = 0;
330 return RtraceSimulManager::Cleanup(everything);
331 }
332 };
333
334 #endif /* RcontribSimulManager_h */