ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RtraceSimulManager.cpp
(Generate patch)

Comparing ray/src/rt/RtraceSimulManager.cpp (file contents):
Revision 2.8 by greg, Thu May 2 22:10:43 2024 UTC vs.
Revision 2.23 by greg, Wed Nov 13 18:47:01 2024 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   */
11  
12   #include <unistd.h>
13 + #include <ctype.h>
14   #include "RtraceSimulManager.h"
15   #include "source.h"
16  
# Line 22 | Line 23 | RadSimulManager::LoadOctree(const char *octn)
23                          return true;
24                  Cleanup();
25          }
26 <        if (!octn)
26 >        if (!octn)              // don't support stdin octree
27                  return false;
28  
29 +        NewHeader(octn);        // get octree header
30          ray_init((char *)octn);
31          return true;
32   }
33  
34 + // callback function for loading header
35 + static int
36 + add2header(char *str, void *p)
37 + {
38 +        if (isheadid(str))
39 +                return 0;
40 +        if (isformat(str))
41 +                return 0;
42 +
43 +        return (*(RadSimulManager *)p).AddHeader(str);
44 + }
45 +
46 + // Prepare header from previous input (or clear)
47 + // Normally called during octree load
48 + bool
49 + RadSimulManager::NewHeader(const char *inspec)
50 + {
51 +        if (hlen) {
52 +                free(header); header = NULL; hlen = 0;
53 +        }
54 +        if (!inspec || !*inspec)
55 +                return false;
56 +        if (inspec[0] == '!')           // save command as header?
57 +                return AddHeader(inspec+1);
58 +                                        // attempt to read from file
59 +        FILE    *fp = fopen(inspec, "rb");
60 +        if (!fp) return false;
61 +        bool    ok = (getheader(fp, add2header, this) >= 0);
62 +        fclose(fp);
63 +        return ok;
64 + }
65 +
66 + // Add a string to header (adds newline if missing)
67 + bool
68 + RadSimulManager::AddHeader(const char *str)
69 + {
70 +        if (!str) return false;
71 +        int     len = strlen(str);
72 +        while (len && (str[len-1] == '\n') | (str[len-1] == '\r'))
73 +                --len;                  // don't copy EOL(s)
74 +        if (!len)
75 +                return false;
76 +        if (hlen)
77 +                header = (char *)realloc(header, hlen+len+2);
78 +        else
79 +                header = (char *)malloc(len+2);
80 +        if (!header) {
81 +                hlen = 0;               // XXX should report?
82 +                return false;
83 +        }
84 +        memcpy(header+hlen, str, len);
85 +        hlen += len;
86 +        header[hlen++] = '\n';          // add single EOL
87 +        header[hlen] = '\0';
88 +        return true;
89 + }
90 +
91 + // helper function to check for white-space and quotations
92 + static int
93 + check_special(const char *s)
94 + {
95 +        int     space_found = 0;
96 +
97 +        while (*s) {
98 +                if ((*s == '"') | (*s == '\''))
99 +                        return *s;      // quotes have priority
100 +                if (isspace(*s))
101 +                        space_found = *s;
102 +                s++;
103 +        }
104 +        return space_found;
105 + }
106 +
107 + // Append program line to header
108 + bool
109 + RadSimulManager::AddHeader(int ac, char *av[])
110 + {
111 +        if ((ac <= 0) | !av) return false;
112 +        int     len = 0;
113 +        int     n;
114 +        for (n = 0; n < ac; n++) {
115 +                if (!av[n]) return false;
116 +                len += strlen(av[n]) + 3;
117 +        }
118 +        if (hlen)                       // add to header
119 +                header = (char *)realloc(header, hlen+len+1);
120 +        else
121 +                header = (char *)malloc(len+1);
122 +
123 +        for (n = 0; n < ac; n++) {
124 +                int     c = check_special(av[n]);
125 +                len = strlen(av[n]);
126 +                if (c | !len) {         // need to quote argument?
127 +                        if ((c == '"') | (c == '\n')) c = '\'';
128 +                        else c = '"';
129 +                        header[hlen++] = c;
130 +                        strcpy(header+hlen, av[n]);
131 +                        hlen += len;
132 +                        header[hlen++] = c;
133 +                } else {
134 +                        strcpy(header+hlen, av[n]);
135 +                        hlen += len;
136 +                }
137 +                header[hlen++] = ' ';
138 +        }
139 +        header[hlen-1] = '\n';          // terminate line
140 +        header[hlen] = '\0';
141 +        return true;
142 + }
143 +
144 + // Look for specific header keyword, return value
145 + const char *
146 + RadSimulManager::GetHeadStr(const char *key, bool inOK) const
147 + {
148 +        if (!key | !hlen || strchr(key, '\n'))
149 +                return NULL;
150 +        if (inOK)                       // skip leading spaces?
151 +                while (isspace(*key)) key++;
152 +
153 +        const int       klen = strlen(key);
154 +        if (!klen)
155 +                return NULL;
156 +        const char *    cp = header;
157 +        while (*cp) {
158 +                if (inOK) {             // skip leading spaces?
159 +                        while (isspace(*cp) && *cp++ != '\n')
160 +                                ;
161 +                        if (cp[-1] == '\n')
162 +                                continue;
163 +                }
164 +                if (!strncmp(cp, key, klen))
165 +                        return cp+klen; // found it!
166 +
167 +                while (*cp && *cp++ != '\n')
168 +                        ;
169 +        }
170 +        return NULL;
171 + }
172 +
173   // How many processors are available?
174   int
175   RadSimulManager::GetNCores()
# Line 40 | Line 181 | RadSimulManager::GetNCores()
181   int
182   RadSimulManager::SetThreadCount(int nt)
183   {
184 <        if (!Ready())
44 <                return 0;
184 >        if (!Ready()) return 0;
185  
186          if (nt <= 0) nt = castonly ? 1 : GetNCores();
187  
# Line 55 | Line 195 | RadSimulManager::SetThreadCount(int nt)
195          return NThreads();
196   }
197  
58 // Assign ray to subthread (fails if NThreads()<2)
59 bool
60 RadSimulManager::SplitRay(RAY *r)
61 {
62        if (!ray_pnprocs || ThreadsAvailable() < 1)
63                return false;
64
65        return (ray_psend(r) > 0);
66 }
67
198   // Process a ray (in subthread), optional result
199 < bool
199 > int
200   RadSimulManager::ProcessRay(RAY *r)
201   {
202 <        if (!Ready()) return false;
202 >        if (!Ready()) return -1;
203  
204          if (!ray_pnprocs) {     // single-threaded mode?
205                  samplendx++;
206                  rayvalue(r);
207 <                return true;
207 >                return 1;
208          }
209 <        int     rv = ray_pqueue(r);
80 <        if (rv < 0) {
81 <                error(WARNING, "ray tracing process(es) died");
82 <                return false;
83 <        }
84 <        return (rv > 0);
209 >        return ray_pqueue(r);
210   }
211  
212   // Wait for next result (or fail)
# Line 98 | Line 223 | RadSimulManager::WaitResult(RAY *r)
223   int
224   RadSimulManager::Cleanup(bool everything)
225   {
226 +        if (ray_pnprocs < 0)
227 +                return 0;               // skip in child process
228 +        NewHeader();
229          if (!ray_pnprocs)
102                ray_pdone(everything);
103        else
230                  ray_done(everything);
231 +        else
232 +                ray_pdone(everything);
233          return 0;
234   }
235  
108 // How many threads are currently unoccupied?
109 int
110 RadSimulManager::ThreadsAvailable() const
111 {
112        if (!ray_pnprocs) return 1;
113
114        return ray_pnidle;
115 }
116
236   // Global pointer to simulation manager for trace call-back (only one)
237   static const RtraceSimulManager *       ourRTsimMan = NULL;
238  
# Line 135 | Line 254 | RtraceSimulManager::Rfifout(RAY *r)
254   bool
255   RtraceSimulManager::UpdateMode()
256   {
138        rtFlags &= RTmask;
257          if (!cookedCall)
258                  rtFlags &= ~RTdoFIFO;
259          if (!traceCall)
# Line 143 | Line 261 | RtraceSimulManager::UpdateMode()
261          if (rtFlags & RTimmIrrad)
262                  rtFlags &= ~RTlimDist;
263  
264 <        int     misMatch = rtFlags ^ curFlags;
264 >        int     misMatch = (rtFlags ^ curFlags) & RTmask;
265                                  // updates based on toggled flags
266 <        if (misMatch & RTtraceSources) {
266 >        if (((misMatch & RTtraceSources) != 0) & (nsources > 0)) {
267 >                int     nt = NThreads();
268 >                if (nt > 1) {
269 >                        if (FlushQueue() < 0)
270 >                                return false;
271 >                        SetThreadCount(1);
272 >                }
273 >                int     sn = nsources;
274                  if (rtFlags & RTtraceSources) {
275 <                        for (int sn = 0; sn < nsources; sn++)
275 >                        srcFollowed.NewBitMap(nsources);
276 >                        while (sn--) {
277 >                                if (source[sn].sflags & SFOLLOW)
278 >                                        continue;
279                                  source[sn].sflags |= SFOLLOW;
280 <                } else          // cannot undo this...
281 <                        rtFlags |= RTtraceSources;
280 >                                srcFollowed.Set(sn);
281 >                        }
282 >                } else {
283 >                        while (sn--)
284 >                                if (srcFollowed.Check(sn))
285 >                                        source[sn].sflags &= ~SFOLLOW;
286 >                        srcFollowed.NewBitMap(0);
287 >                }
288 >                if (nt > 1) SetThreadCount(nt);
289          }
290          if (misMatch & RTdoFIFO && FlushQueue() < 0)
291                  return false;
# Line 213 | Line 348 | RtraceSimulManager::EnqueueBundle(const FVECT orig_dir
348          int     nqueued = 0;
349          RAY     res;
350  
351 <        if (!Ready())
217 <                return -1;
351 >        if (!Ready()) return -1;
352  
353          if (castonly && !cookedCall)
354                  error(INTERNAL, "EnqueueBundle() called in castonly mode without cookedCall");
# Line 222 | Line 356 | RtraceSimulManager::EnqueueBundle(const FVECT orig_dir
356          if (!UpdateMode())              // update rendering mode if requested
357                  return -1;
358  
359 +        if (rID0 && curFlags&RTdoFIFO)
360 +                error(INTERNAL, "Ray number assignment unsupported with FIFO");
361 +
362          while (n-- > 0) {               // queue each ray
363                  VCOPY(res.rorg, orig_direc[0]);
364                  VCOPY(res.rdir, orig_direc[1]);
365 +                res.rmax = .0;
366                  orig_direc += 2;
367                  rayorigin(&res, PRIMARY, NULL, NULL);
368 <                if (!rID0)
231 <                        res.rno = ++lastRayID;
232 <                else if (curFlags & RTdoFIFO)
233 <                        error(INTERNAL, "Ray number assignment unsupported with FIFO");
234 <                else
235 <                        res.rno = lastRayID = rID0++;
368 >                res.rno = rID0 ? (lastRayID = rID0++) : ++lastRayID;
369                  if (curFlags & RTimmIrrad)
370                          res.revf = rayirrad;
371                  else if (castonly)
372                          res.revf = raycast;
373                  double  d = normalize(res.rdir);
374                  bool    sendRes = (cookedCall != NULL);
375 <                if (d > 0) {            // direction vector is valid?
375 >                if (d > .0) {           // direction vector is valid?
376                          if (curFlags & RTlimDist)
377                                  res.rmax = d;
378                          if (((curFlags&RTdoFIFO) != 0) & (ray_pnprocs > 0)) {
379                                  if (ray_fifo_in(&res) < 0)
380                                          return -1;
381                                  sendRes = false;
382 <                        } else
383 <                                sendRes &= ProcessRay(&res);
382 >                        } else {
383 >                                int     rv = ProcessRay(&res);
384 >                                if (rv < 0)
385 >                                        return -1;
386 >                                sendRes &= (rv > 0);
387 >                        }
388                  } else if (ThreadsAvailable() < NThreads() &&
389                                  FlushQueue() < 0)
390                          return -1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines