56 |
|
* proportion to the number of CPUs you have available on your |
57 |
|
* system. If the ray queue is full before the call, ray_pqueue() |
58 |
|
* will block until a result is ready so it can queue this one. |
59 |
< |
* The global int ray_idle indicates the number of currently idle |
59 |
> |
* The global int ray_pnidle indicates the number of currently idle |
60 |
|
* children. If you want to check for completed rays without blocking, |
61 |
|
* or get the results from rays that have been queued without |
62 |
|
* queuing any new ones, the ray_presult() call is for you: |
71 |
|
* queue is completely empty. A negative return value |
72 |
|
* indicates that a rendering process died. If this |
73 |
|
* happens, ray_close(0) is automatically called to close |
74 |
< |
* all child processes, and ray_nprocs is set to zero. |
74 |
> |
* all child processes, and ray_pnprocs is set to zero. |
75 |
|
* |
76 |
|
* If you just want to fill the ray queue without checking for |
77 |
< |
* results, check ray_idle and call ray_psend(): |
77 |
> |
* results, check ray_pnidle and call ray_psend(): |
78 |
|
* |
79 |
< |
* while (ray_idle) { |
79 |
> |
* while (ray_pnidle) { |
80 |
|
* ( set up ray ) |
81 |
|
* ray_psend(&myRay); |
82 |
|
* } |
124 |
|
* but otherwise your process should not be compromised. |
125 |
|
*/ |
126 |
|
|
127 |
< |
#include "ray.h" |
127 |
> |
#include <stdio.h> |
128 |
> |
#include <sys/types.h> |
129 |
> |
#include <sys/wait.h> /* XXX platform */ |
130 |
|
|
131 |
+ |
#include "rtprocess.h" |
132 |
+ |
#include "ray.h" |
133 |
+ |
#include "ambient.h" |
134 |
|
#include "selcall.h" |
135 |
|
|
136 |
|
#ifndef RAYQLEN |
147 |
|
|
148 |
|
extern char *shm_boundary; /* boundary of shared memory */ |
149 |
|
|
150 |
< |
int ray_nprocs = 0; /* number of child processes */ |
151 |
< |
int ray_idle = 0; /* number of idle children */ |
150 |
> |
int ray_pnprocs = 0; /* number of child processes */ |
151 |
> |
int ray_pnidle = 0; /* number of idle children */ |
152 |
|
|
153 |
|
static struct child_proc { |
154 |
|
int pid; /* child process id */ |
165 |
|
|
166 |
|
#define sendq_full() (r_send_next >= RAYQLEN) |
167 |
|
|
168 |
+ |
static int ray_pflush(void); |
169 |
+ |
static void ray_pchild(int fd_in, int fd_out); |
170 |
|
|
171 |
< |
void |
172 |
< |
ray_pinit(otnm, nproc) /* initialize ray-tracing processes */ |
173 |
< |
char *otnm; |
174 |
< |
int nproc; |
171 |
> |
|
172 |
> |
extern void |
173 |
> |
ray_pinit( /* initialize ray-tracing processes */ |
174 |
> |
char *otnm, |
175 |
> |
int nproc |
176 |
> |
) |
177 |
|
{ |
178 |
|
if (nobjects > 0) /* close old calculation */ |
179 |
|
ray_pdone(0); |
194 |
|
|
195 |
|
|
196 |
|
static int |
197 |
< |
ray_pflush() /* send queued rays to idle children */ |
197 |
> |
ray_pflush(void) /* send queued rays to idle children */ |
198 |
|
{ |
199 |
|
int nc, n, nw, i, sfirst; |
200 |
|
|
201 |
< |
if ((ray_idle <= 0 | r_send_next <= 0)) |
201 |
> |
if ((ray_pnidle <= 0) | (r_send_next <= 0)) |
202 |
|
return(0); /* nothing we can send */ |
203 |
|
|
204 |
|
sfirst = 0; /* divvy up labor */ |
205 |
< |
nc = ray_idle; |
206 |
< |
for (i = ray_nprocs; nc && i--; ) { |
205 |
> |
nc = ray_pnidle; |
206 |
> |
for (i = ray_pnprocs; nc && i--; ) { |
207 |
|
if (r_proc[i].npending > 0) |
208 |
|
continue; /* child looks busy */ |
209 |
|
n = (r_send_next - sfirst)/nc--; |
219 |
|
while (n--) /* record ray IDs */ |
220 |
|
r_proc[i].rno[n] = r_queue[sfirst+n].rno; |
221 |
|
sfirst += r_proc[i].npending; |
222 |
< |
ray_idle--; /* now she's busy */ |
222 |
> |
ray_pnidle--; /* now she's busy */ |
223 |
|
} |
224 |
|
if (sfirst != r_send_next) |
225 |
|
error(CONSISTENCY, "code screwup in ray_pflush"); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
< |
void |
232 |
< |
ray_psend(r) /* add a ray to our send queue */ |
233 |
< |
RAY *r; |
231 |
> |
extern void |
232 |
> |
ray_psend( /* add a ray to our send queue */ |
233 |
> |
RAY *r |
234 |
> |
) |
235 |
|
{ |
236 |
|
if (r == NULL) |
237 |
|
return; |
239 |
|
if (sendq_full() && ray_pflush() <= 0) |
240 |
|
error(INTERNAL, "ray_pflush failed in ray_psend"); |
241 |
|
|
242 |
< |
copystruct(&r_queue[r_send_next], r); |
242 |
> |
r_queue[r_send_next] = *r; |
243 |
|
r_send_next++; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
< |
int |
248 |
< |
ray_pqueue(r) /* queue a ray for computation */ |
249 |
< |
RAY *r; |
247 |
> |
extern int |
248 |
> |
ray_pqueue( /* queue a ray for computation */ |
249 |
> |
RAY *r |
250 |
> |
) |
251 |
|
{ |
252 |
|
if (r == NULL) |
253 |
|
return(0); |
255 |
|
if (sendq_full()) { |
256 |
|
RAY mySend; |
257 |
|
int rval; |
258 |
< |
copystruct(&mySend, r); |
258 |
> |
mySend = *r; |
259 |
|
/* wait for a result */ |
260 |
|
rval = ray_presult(r, 0); |
261 |
|
/* put new ray in queue */ |
262 |
< |
copystruct(&r_queue[r_send_next], &mySend); |
262 |
> |
r_queue[r_send_next] = mySend; |
263 |
|
r_send_next++; |
264 |
|
return(rval); /* done */ |
265 |
|
} |
266 |
|
/* add ray to send queue */ |
267 |
< |
copystruct(&r_queue[r_send_next], r); |
267 |
> |
r_queue[r_send_next] = *r; |
268 |
|
r_send_next++; |
269 |
|
/* check for returned ray... */ |
270 |
|
if (r_recv_first >= r_recv_next) |
271 |
|
return(0); |
272 |
|
/* ...one is sitting in queue */ |
273 |
< |
copystruct(r, &r_queue[r_recv_first]); |
273 |
> |
*r = r_queue[r_recv_first]; |
274 |
|
r_recv_first++; |
275 |
|
return(1); |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
< |
int |
280 |
< |
ray_presult(r, poll) /* check for a completed ray */ |
281 |
< |
RAY *r; |
282 |
< |
int poll; |
279 |
> |
extern int |
280 |
> |
ray_presult( /* check for a completed ray */ |
281 |
> |
RAY *r, |
282 |
> |
int poll |
283 |
> |
) |
284 |
|
{ |
285 |
|
static struct timeval tpoll; /* zero timeval struct */ |
286 |
|
static fd_set readset, errset; |
291 |
|
return(0); |
292 |
|
/* check queued results first */ |
293 |
|
if (r_recv_first < r_recv_next) { |
294 |
< |
copystruct(r, &r_queue[r_recv_first]); |
294 |
> |
*r = r_queue[r_recv_first]; |
295 |
|
r_recv_first++; |
296 |
|
return(1); |
297 |
|
} |
298 |
< |
n = ray_nprocs - ray_idle; /* pending before flush? */ |
298 |
> |
n = ray_pnprocs - ray_pnidle; /* pending before flush? */ |
299 |
|
|
300 |
|
if (ray_pflush() < 0) /* send new rays to process */ |
301 |
|
return(-1); |
303 |
|
r_recv_first = r_recv_next = RAYQLEN; |
304 |
|
|
305 |
|
if (!poll) /* count newly sent unless polling */ |
306 |
< |
n = ray_nprocs - ray_idle; |
306 |
> |
n = ray_pnprocs - ray_pnidle; |
307 |
|
if (n <= 0) /* return if nothing to await */ |
308 |
|
return(0); |
309 |
|
getready: /* any children waiting for us? */ |
310 |
< |
for (pn = ray_nprocs; pn--; ) |
310 |
> |
for (pn = ray_pnprocs; pn--; ) |
311 |
|
if (FD_ISSET(r_proc[pn].fd_recv, &readset) || |
312 |
|
FD_ISSET(r_proc[pn].fd_recv, &errset)) |
313 |
|
break; |
314 |
|
/* call select if we must */ |
315 |
|
if (pn < 0) { |
316 |
|
FD_ZERO(&readset); FD_ZERO(&errset); n = 0; |
317 |
< |
for (pn = ray_nprocs; pn--; ) { |
317 |
> |
for (pn = ray_pnprocs; pn--; ) { |
318 |
|
if (r_proc[pn].npending > 0) |
319 |
|
FD_SET(r_proc[pn].fd_recv, &readset); |
320 |
|
FD_SET(r_proc[pn].fd_recv, &errset); |
350 |
|
if (n <= 0) |
351 |
|
FD_CLR(r_proc[pn].fd_recv, &errset); |
352 |
|
r_proc[pn].npending = 0; |
353 |
< |
ray_idle++; |
353 |
> |
ray_pnidle++; |
354 |
|
/* check for rendering errors */ |
355 |
|
if (!ok) { |
356 |
|
ray_pclose(0); /* process died -- clean up */ |
366 |
|
rp->slights = NULL; |
367 |
|
} |
368 |
|
/* return first ray received */ |
369 |
< |
copystruct(r, &r_queue[r_recv_first]); |
369 |
> |
*r = r_queue[r_recv_first]; |
370 |
|
r_recv_first++; |
371 |
|
return(1); |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
< |
void |
376 |
< |
ray_pdone(freall) /* reap children and free data */ |
377 |
< |
int freall; |
375 |
> |
extern void |
376 |
> |
ray_pdone( /* reap children and free data */ |
377 |
> |
int freall |
378 |
> |
) |
379 |
|
{ |
380 |
|
ray_pclose(0); /* close child processes */ |
381 |
|
|
388 |
|
|
389 |
|
|
390 |
|
static void |
391 |
< |
ray_pchild(fd_in, fd_out) /* process rays (never returns) */ |
392 |
< |
int fd_in; |
393 |
< |
int fd_out; |
391 |
> |
ray_pchild( /* process rays (never returns) */ |
392 |
> |
int fd_in, |
393 |
> |
int fd_out |
394 |
> |
) |
395 |
|
{ |
396 |
|
int n; |
397 |
|
register int i; |
435 |
|
} |
436 |
|
|
437 |
|
|
438 |
< |
void |
439 |
< |
ray_popen(nadd) /* open the specified # processes */ |
440 |
< |
int nadd; |
438 |
> |
extern void |
439 |
> |
ray_popen( /* open the specified # processes */ |
440 |
> |
int nadd |
441 |
> |
) |
442 |
|
{ |
443 |
|
/* check if our table has room */ |
444 |
< |
if (ray_nprocs + nadd > MAX_NPROCS) |
445 |
< |
nadd = MAX_NPROCS - ray_nprocs; |
444 |
> |
if (ray_pnprocs + nadd > MAX_NPROCS) |
445 |
> |
nadd = MAX_NPROCS - ray_pnprocs; |
446 |
|
if (nadd <= 0) |
447 |
|
return; |
448 |
|
fflush(stderr); /* clear pending output */ |
451 |
|
int p0[2], p1[2]; |
452 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
453 |
|
error(SYSTEM, "cannot create pipe"); |
454 |
< |
if ((r_proc[ray_nprocs].pid = fork()) == 0) { |
454 |
> |
if ((r_proc[ray_pnprocs].pid = fork()) == 0) { |
455 |
|
int pn; /* close others' descriptors */ |
456 |
< |
for (pn = ray_nprocs; pn--; ) { |
456 |
> |
for (pn = ray_pnprocs; pn--; ) { |
457 |
|
close(r_proc[pn].fd_send); |
458 |
|
close(r_proc[pn].fd_recv); |
459 |
|
} |
461 |
|
/* following call never returns */ |
462 |
|
ray_pchild(p1[0], p0[1]); |
463 |
|
} |
464 |
< |
if (r_proc[ray_nprocs].pid < 0) |
464 |
> |
if (r_proc[ray_pnprocs].pid < 0) |
465 |
|
error(SYSTEM, "cannot fork child process"); |
466 |
|
close(p1[0]); close(p0[1]); |
467 |
< |
r_proc[ray_nprocs].fd_send = p1[1]; |
468 |
< |
r_proc[ray_nprocs].fd_recv = p0[0]; |
469 |
< |
r_proc[ray_nprocs].npending = 0; |
470 |
< |
ray_nprocs++; |
471 |
< |
ray_idle++; |
467 |
> |
r_proc[ray_pnprocs].fd_send = p1[1]; |
468 |
> |
r_proc[ray_pnprocs].fd_recv = p0[0]; |
469 |
> |
r_proc[ray_pnprocs].npending = 0; |
470 |
> |
ray_pnprocs++; |
471 |
> |
ray_pnidle++; |
472 |
|
} |
473 |
|
} |
474 |
|
|
475 |
|
|
476 |
< |
void |
477 |
< |
ray_pclose(nsub) /* close one or more child processes */ |
478 |
< |
int nsub; |
476 |
> |
extern void |
477 |
> |
ray_pclose( /* close one or more child processes */ |
478 |
> |
int nsub |
479 |
> |
) |
480 |
|
{ |
481 |
|
static int inclose = 0; |
482 |
|
RAY res; |
485 |
|
return; |
486 |
|
inclose++; |
487 |
|
/* check argument */ |
488 |
< |
if ((nsub <= 0 | nsub > ray_nprocs)) |
489 |
< |
nsub = ray_nprocs; |
488 |
> |
if ((nsub <= 0) | (nsub > ray_pnprocs)) |
489 |
> |
nsub = ray_pnprocs; |
490 |
|
/* clear our ray queue */ |
491 |
|
while (ray_presult(&res,0) > 0) |
492 |
|
; |
493 |
|
/* clean up children */ |
494 |
|
while (nsub--) { |
495 |
|
int status; |
496 |
< |
ray_nprocs--; |
497 |
< |
close(r_proc[ray_nprocs].fd_recv); |
498 |
< |
close(r_proc[ray_nprocs].fd_send); |
499 |
< |
while (wait(&status) != r_proc[ray_nprocs].pid) |
496 |
> |
ray_pnprocs--; |
497 |
> |
close(r_proc[ray_pnprocs].fd_recv); |
498 |
> |
close(r_proc[ray_pnprocs].fd_send); |
499 |
> |
while (wait(&status) != r_proc[ray_pnprocs].pid) |
500 |
|
; |
501 |
|
if (status) { |
502 |
|
sprintf(errmsg, |
503 |
|
"rendering process %d exited with code %d", |
504 |
< |
r_proc[ray_nprocs].pid, status>>8); |
504 |
> |
r_proc[ray_pnprocs].pid, status>>8); |
505 |
|
error(WARNING, errmsg); |
506 |
|
} |
507 |
< |
ray_idle--; |
507 |
> |
ray_pnidle--; |
508 |
|
} |
509 |
|
inclose--; |
510 |
|
} |