69 |
|
{ |
70 |
|
if (!str) return false; |
71 |
|
int len = strlen(str); |
72 |
< |
while (len && str[len-1] == '\n') |
72 |
> |
while (len && (str[len-1] == '\n') | (str[len-1] == '\r')) |
73 |
|
--len; // don't copy EOL(s) |
74 |
|
if (!len) |
75 |
|
return false; |
106 |
|
|
107 |
|
// Append program line to header |
108 |
|
bool |
109 |
< |
RadSimulManager::AddHeader(int ac, const char *av[]) |
109 |
> |
RadSimulManager::AddHeader(int ac, char *av[]) |
110 |
|
{ |
111 |
|
if ((ac <= 0) | !av) return false; |
112 |
|
int len = 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() |
181 |
|
int |
182 |
|
RadSimulManager::SetThreadCount(int nt) |
183 |
|
{ |
184 |
< |
if (!Ready()) |
156 |
< |
return 0; |
184 |
> |
if (!Ready()) return 0; |
185 |
|
|
186 |
|
if (nt <= 0) nt = castonly ? 1 : GetNCores(); |
187 |
|
|
195 |
|
return NThreads(); |
196 |
|
} |
197 |
|
|
170 |
– |
// Assign ray to subthread (fails if NThreads()<2) |
171 |
– |
bool |
172 |
– |
RadSimulManager::SplitRay(RAY *r) |
173 |
– |
{ |
174 |
– |
if (!ray_pnprocs || ThreadsAvailable() < 1) |
175 |
– |
return false; |
176 |
– |
|
177 |
– |
return (ray_psend(r) > 0); |
178 |
– |
} |
179 |
– |
|
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); |
192 |
< |
if (rv < 0) { |
193 |
< |
error(WARNING, "ray tracing process(es) died"); |
194 |
< |
return false; |
195 |
< |
} |
196 |
< |
return (rv > 0); |
209 |
> |
return ray_pqueue(r); |
210 |
|
} |
211 |
|
|
212 |
|
// Wait for next result (or fail) |
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) |
230 |
|
ray_done(everything); |
233 |
|
return 0; |
234 |
|
} |
235 |
|
|
221 |
– |
// How many threads are currently unoccupied? |
222 |
– |
int |
223 |
– |
RadSimulManager::ThreadsAvailable() const |
224 |
– |
{ |
225 |
– |
if (!ray_pnprocs) return 1; |
226 |
– |
|
227 |
– |
return ray_pnidle; |
228 |
– |
} |
229 |
– |
|
236 |
|
// Global pointer to simulation manager for trace call-back (only one) |
237 |
|
static const RtraceSimulManager * ourRTsimMan = NULL; |
238 |
|
|
254 |
|
bool |
255 |
|
RtraceSimulManager::UpdateMode() |
256 |
|
{ |
251 |
– |
rtFlags &= RTmask; |
257 |
|
if (!cookedCall) |
258 |
|
rtFlags &= ~RTdoFIFO; |
259 |
|
if (!traceCall) |
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; |
315 |
|
static void |
316 |
|
rayirrad(RAY *r) |
317 |
|
{ |
318 |
< |
/* pretend we hit surface */ |
319 |
< |
r->rxt = r->rot = 1e-5; |
320 |
< |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
321 |
< |
r->ron[0] = -r->rdir[0]; |
322 |
< |
r->ron[1] = -r->rdir[1]; |
323 |
< |
r->ron[2] = -r->rdir[2]; |
318 |
> |
/* orientation -> normal */ |
319 |
> |
VCOPY(r->ron, r->rdir); |
320 |
> |
/* pretend normal incidence */ |
321 |
> |
r->rdir[0] = -r->ron[0]; |
322 |
> |
r->rdir[1] = -r->ron[1]; |
323 |
> |
r->rdir[2] = -r->ron[2]; |
324 |
|
r->rod = 1.0; |
325 |
+ |
/* counterfeit other params */ |
326 |
+ |
r->rxt = r->rot = 1e-4; |
327 |
+ |
/* move comfortably above sample pos. */ |
328 |
+ |
VSUM(r->rop, r->rorg, r->ron, r->rot); |
329 |
+ |
/* leap-frog for pretend origin */ |
330 |
+ |
VSUM(r->rorg, r->rop, r->ron, r->rot); |
331 |
|
/* compute result */ |
332 |
|
r->revf = raytrace; |
333 |
|
m_normal(&Lamb, r); |
354 |
|
int nqueued = 0; |
355 |
|
RAY res; |
356 |
|
|
357 |
< |
if (!Ready()) |
330 |
< |
return -1; |
357 |
> |
if (!Ready()) return -1; |
358 |
|
|
359 |
|
if (castonly && !cookedCall) |
360 |
|
error(INTERNAL, "EnqueueBundle() called in castonly mode without cookedCall"); |
368 |
|
while (n-- > 0) { // queue each ray |
369 |
|
VCOPY(res.rorg, orig_direc[0]); |
370 |
|
VCOPY(res.rdir, orig_direc[1]); |
371 |
+ |
res.rmax = .0; |
372 |
|
orig_direc += 2; |
373 |
|
rayorigin(&res, PRIMARY, NULL, NULL); |
374 |
|
res.rno = rID0 ? (lastRayID = rID0++) : ++lastRayID; |
385 |
|
if (ray_fifo_in(&res) < 0) |
386 |
|
return -1; |
387 |
|
sendRes = false; |
388 |
< |
} else |
389 |
< |
sendRes &= ProcessRay(&res); |
388 |
> |
} else { |
389 |
> |
int rv = ProcessRay(&res); |
390 |
> |
if (rv < 0) |
391 |
> |
return -1; |
392 |
> |
sendRes &= (rv > 0); |
393 |
> |
} |
394 |
|
} else if (ThreadsAvailable() < NThreads() && |
395 |
|
FlushQueue() < 0) |
396 |
|
return -1; |