ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raypcalls.c
Revision: 2.26
Committed: Wed Dec 16 01:06:50 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R0
Changes since 2.25: +7 -4 lines
Log Message:
Added check for calling ray_pclose() in child

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: raypcalls.c,v 2.25 2009/12/15 18:21:53 greg Exp $";
3 #endif
4 /*
5 * raypcalls.c - interface for parallel rendering using Radiance
6 *
7 * External symbols declared in ray.h
8 */
9
10 #include "copyright.h"
11
12 /*
13 * These calls are designed similarly to the ones in raycalls.c,
14 * but allow for multiple rendering processes on the same host
15 * machine. There is no sense in specifying more child processes
16 * than you have processor cores, but one child may help by allowing
17 * asynchronous ray computation in an interactive program, and
18 * will protect the caller from fatal rendering errors.
19 *
20 * You should first read and understand the header in raycalls.c,
21 * as some things are explained there that are not repated here.
22 *
23 * The first step is opening one or more rendering processes
24 * with a call to ray_pinit(oct, nproc). Before calling fork(),
25 * ray_pinit() loads the octree and data structures into the
26 * caller's memory, and ray_popen() synchronizes the ambient
27 * file, if any. Shared memory permits all sorts of queries
28 * that wouldn't be possible otherwise without causing any real
29 * memory overhead, since all the static data are shared
30 * between processes. Rays are traced using a simple
31 * queuing mechanism, explained below.
32 *
33 * The ray queue buffers RAYQLEN rays before sending to
34 * children, each of which may internally buffer RAYQLEN rays
35 * during evaluation. Rays are not returned in the order
36 * they are sent when multiple processes are open.
37 *
38 * Rays are queued and returned by a single
39 * ray_pqueue() call. A ray_pqueue() return
40 * value of 0 indicates that no rays are ready
41 * and the queue is not yet full. A return value of 1
42 * indicates that a ray was returned, though it is probably
43 * not the one you just requested. Rays may be identified by
44 * the rno member of the RAY struct, which is incremented
45 * by the rayorigin() call, or may be set explicitly by
46 * the caller. Below is an example call sequence:
47 *
48 * myRay.rorg = ( ray origin point )
49 * myRay.rdir = ( normalized ray direction )
50 * myRay.rmax = ( maximum length, or zero for no limit )
51 * rayorigin(&myRay, PRIMARY, NULL, NULL);
52 * myRay.rno = ( my personal ray identifier )
53 * if (ray_pqueue(&myRay) == 1)
54 * { do something with results }
55 *
56 * Note the differences between this and the simpler ray_trace()
57 * call. In particular, the call may or may not return a value
58 * in the passed ray structure. Also, you need to call rayorigin()
59 * yourself, which is normally called for you by ray_trace(). The
60 * benefit is that ray_pqueue() will trace rays faster in
61 * proportion to the number of CPUs you have available on your
62 * system. If the ray queue is full before the call, ray_pqueue()
63 * will block until a result is ready so it can queue this one.
64 * The global int ray_pnidle indicates the number of currently idle
65 * children. If you want to check for completed rays without blocking,
66 * or get the results from rays that have been queued without
67 * queuing any new ones, the ray_presult() call is for you:
68 *
69 * if (ray_presult(&myRay, 1) == 1)
70 * { do something with results }
71 *
72 * If the second argument is 1, the call won't block when
73 * results aren't ready, but will immediately return 0.
74 * If the second argument is 0, the call will block
75 * until a value is available, returning 0 only if the
76 * queue is completely empty. Setting the second argument
77 * to -1 returns 0 unless a ray is ready in the queue and
78 * no system calls are needed. A negative return value
79 * indicates that a rendering process died. If this
80 * happens, ray_pclose(0) is automatically called to close
81 * all child processes, and ray_pnprocs is set to zero.
82 *
83 * If you just want to fill the ray queue without checking for
84 * results, check ray_pnidle and call ray_psend():
85 *
86 * while (ray_pnidle) {
87 * ( set up ray )
88 * ray_psend(&myRay);
89 * }
90 *
91 * Note that it is a mistake to call ra_psend() when
92 * ray_pnidle is zero, and nothing will be sent in
93 * this case. Otherwise, the ray_presult() and/or ray_pqueue()
94 * functions may be called subsequently to read back the results
95 * of rays queued by ray_psend().
96 *
97 * When you are done, you may call ray_pdone(1) to close
98 * all child processes and clean up memory used by Radiance.
99 * Any queued ray calculations will be awaited and discarded.
100 * As with ray_done(), ray_pdone(0) hangs onto data files
101 * and fonts that are likely to be used in subsequent renderings.
102 * Whether you need to clean up memory or not, you should
103 * at least call ray_pclose(0) to await the child processes.
104 * The caller should define a quit() function that calls
105 * ray_pclose(0) if ray_pnprocs > 0.
106 *
107 * Warning: You cannot affect any of the rendering processes
108 * by changing global parameter values onece ray_pinit() has
109 * been called. Changing global parameters will have no effect
110 * until the next call to ray_pinit(), which restarts everything.
111 * If you just want to reap children so that you can alter the
112 * rendering parameters without reloading the scene, use the
113 * ray_pclose(0) and ray_popen(nproc) calls to close
114 * then restart the child processes after the changes are made.
115 *
116 * Note: These routines are written to coordinate with the
117 * definitions in raycalls.c, and in fact depend on them.
118 * If you want to trace a ray and get a result synchronously,
119 * use the ray_trace() call to compute it in the parent process.
120 * This will not interfere with any subprocess calculations,
121 * but beware that a fatal error may end with a call to quit().
122 *
123 * Note: One of the advantages of using separate processes
124 * is that it gives the calling program some immunity from
125 * fatal rendering errors. As discussed in raycalls.c,
126 * Radiance tends to throw up its hands and exit at the
127 * first sign of trouble, calling quit() to return control
128 * to the top level. Although you can avoid exit() with
129 * your own longjmp() in quit(), the cleanup afterwards
130 * is always suspect. Through the use of subprocesses,
131 * we avoid this pitfall by closing the processes and
132 * returning a negative value from ray_pqueue() or
133 * ray_presult(). If you get a negative value from either
134 * of these calls, you can assume that the processes have
135 * been cleaned up with a call to ray_pclose(), though you
136 * will have to call ray_pdone() yourself if you want to
137 * free memory. Obviously, you cannot continue rendering
138 * without risking further errors, but otherwise your
139 * process should not be compromised.
140 */
141
142 #include "rtprocess.h"
143 #include "ray.h"
144 #include "ambient.h"
145 #include <sys/types.h>
146 #include <sys/wait.h>
147 #include "selcall.h"
148
149 #ifndef RAYQLEN
150 #define RAYQLEN 12 /* # rays to send at once */
151 #endif
152
153 #ifndef MAX_RPROCS
154 #if (FD_SETSIZE/2-4 < 64)
155 #define MAX_NPROCS (FD_SETSIZE/2-4)
156 #else
157 #define MAX_NPROCS 64 /* max. # rendering processes */
158 #endif
159 #endif
160
161 extern char *shm_boundary; /* boundary of shared memory */
162
163 int ray_pnprocs = 0; /* number of child processes */
164 int ray_pnidle = 0; /* number of idle children */
165
166 static struct child_proc {
167 int pid; /* child process id */
168 int fd_send; /* write to child here */
169 int fd_recv; /* read from child here */
170 int npending; /* # rays in process */
171 RNUMBER rno[RAYQLEN]; /* working on these rays */
172 } r_proc[MAX_NPROCS]; /* our child processes */
173
174 static RAY r_queue[2*RAYQLEN]; /* ray i/o buffer */
175 static int r_send_next = 0; /* next send ray placement */
176 static int r_recv_first = RAYQLEN; /* position of first unreported ray */
177 static int r_recv_next = RAYQLEN; /* next received ray placement */
178
179 #define sendq_full() (r_send_next >= RAYQLEN)
180
181 static int ray_pflush(void);
182 static void ray_pchild(int fd_in, int fd_out);
183
184
185 void
186 ray_pinit( /* initialize ray-tracing processes */
187 char *otnm,
188 int nproc
189 )
190 {
191 if (nobjects > 0) /* close old calculation */
192 ray_pdone(0);
193
194 ray_init(otnm); /* load the shared scene */
195
196 ray_popen(nproc); /* fork children */
197 }
198
199
200 static int
201 ray_pflush(void) /* send queued rays to idle children */
202 {
203 int nc, n, nw, i, sfirst;
204
205 if ((ray_pnidle <= 0) | (r_send_next <= 0))
206 return(0); /* nothing we can send */
207
208 sfirst = 0; /* divvy up labor */
209 nc = ray_pnidle;
210 for (i = ray_pnprocs; nc && i--; ) {
211 if (r_proc[i].npending > 0)
212 continue; /* child looks busy */
213 n = (r_send_next - sfirst)/nc--;
214 if (!n)
215 continue;
216 /* smuggle set size in crtype */
217 r_queue[sfirst].crtype = n;
218 nw = writebuf(r_proc[i].fd_send, (char *)&r_queue[sfirst],
219 sizeof(RAY)*n);
220 if (nw != sizeof(RAY)*n)
221 return(-1); /* write error */
222 r_proc[i].npending = n;
223 while (n--) /* record ray IDs */
224 r_proc[i].rno[n] = r_queue[sfirst+n].rno;
225 sfirst += r_proc[i].npending;
226 ray_pnidle--; /* now she's busy */
227 }
228 if (sfirst != r_send_next)
229 error(CONSISTENCY, "code screwup in ray_pflush()");
230 r_send_next = 0;
231 return(sfirst); /* return total # sent */
232 }
233
234
235 int
236 ray_psend( /* add a ray to our send queue */
237 RAY *r
238 )
239 {
240 int rv;
241
242 if ((r == NULL) | (ray_pnidle <= 0))
243 return(0);
244 /* flush output if necessary */
245 if (sendq_full() && (rv = ray_pflush()) <= 0)
246 return(rv);
247
248 r_queue[r_send_next++] = *r;
249 return(1);
250 }
251
252
253 int
254 ray_pqueue( /* queue a ray for computation */
255 RAY *r
256 )
257 {
258 if (r == NULL)
259 return(0);
260 /* check for full send queue */
261 if (sendq_full()) {
262 RAY mySend = *r;
263 /* wait for a result */
264 if (ray_presult(r, 0) <= 0)
265 return(-1);
266 /* put new ray in queue */
267 r_queue[r_send_next++] = mySend;
268
269 return(1);
270 }
271 /* else add ray to send queue */
272 r_queue[r_send_next++] = *r;
273 /* check for returned ray... */
274 if (r_recv_first >= r_recv_next)
275 return(0);
276 /* ...one is sitting in queue */
277 *r = r_queue[r_recv_first++];
278 return(1);
279 }
280
281
282 int
283 ray_presult( /* check for a completed ray */
284 RAY *r,
285 int poll
286 )
287 {
288 static struct timeval tpoll; /* zero timeval struct */
289 static fd_set readset, errset;
290 int n, ok;
291 register int pn;
292
293 if (r == NULL)
294 return(0);
295 /* check queued results first */
296 if (r_recv_first < r_recv_next) {
297 *r = r_queue[r_recv_first++];
298 return(1);
299 }
300 if (poll < 0) /* immediate polling mode? */
301 return(0);
302
303 n = ray_pnprocs - ray_pnidle; /* pending before flush? */
304
305 if (ray_pflush() < 0) /* send new rays to process */
306 return(-1);
307 /* reset receive queue */
308 r_recv_first = r_recv_next = RAYQLEN;
309
310 if (!poll) /* count newly sent unless polling */
311 n = ray_pnprocs - ray_pnidle;
312 if (n <= 0) /* return if nothing to await */
313 return(0);
314 if (!poll && ray_pnprocs == 1) /* one process -> skip select() */
315 FD_SET(r_proc[0].fd_recv, &readset);
316
317 getready: /* any children waiting for us? */
318 for (pn = ray_pnprocs; pn--; )
319 if (FD_ISSET(r_proc[pn].fd_recv, &readset) ||
320 FD_ISSET(r_proc[pn].fd_recv, &errset))
321 break;
322 /* call select() if we must */
323 if (pn < 0) {
324 FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
325 for (pn = ray_pnprocs; pn--; ) {
326 if (r_proc[pn].npending > 0)
327 FD_SET(r_proc[pn].fd_recv, &readset);
328 FD_SET(r_proc[pn].fd_recv, &errset);
329 if (r_proc[pn].fd_recv >= n)
330 n = r_proc[pn].fd_recv + 1;
331 }
332 /* find out who is ready */
333 while ((n = select(n, &readset, (fd_set *)NULL, &errset,
334 poll ? &tpoll : (struct timeval *)NULL)) < 0)
335 if (errno != EINTR) {
336 error(WARNING,
337 "select call failed in ray_presult()");
338 ray_pclose(0);
339 return(-1);
340 }
341 if (n > 0) /* go back and get it */
342 goto getready;
343 return(0); /* else poll came up empty */
344 }
345 if (r_recv_next + r_proc[pn].npending > sizeof(r_queue)/sizeof(RAY))
346 error(CONSISTENCY, "buffer shortage in ray_presult()");
347
348 /* read rendered ray data */
349 n = readbuf(r_proc[pn].fd_recv, (char *)&r_queue[r_recv_next],
350 sizeof(RAY)*r_proc[pn].npending);
351 if (n > 0) {
352 r_recv_next += n/sizeof(RAY);
353 ok = (n == sizeof(RAY)*r_proc[pn].npending);
354 } else
355 ok = 0;
356 /* reset child's status */
357 FD_CLR(r_proc[pn].fd_recv, &readset);
358 if (n <= 0)
359 FD_CLR(r_proc[pn].fd_recv, &errset);
360 r_proc[pn].npending = 0;
361 ray_pnidle++;
362 /* check for rendering errors */
363 if (!ok) {
364 ray_pclose(0); /* process died -- clean up */
365 return(-1);
366 }
367 /* preen returned rays */
368 for (n = r_recv_next - r_recv_first; n--; ) {
369 register RAY *rp = &r_queue[r_recv_first + n];
370 rp->rno = r_proc[pn].rno[n];
371 rp->parent = NULL;
372 rp->newcset = rp->clipset = NULL;
373 rp->rox = NULL;
374 rp->slights = NULL;
375 }
376 /* return first ray received */
377 *r = r_queue[r_recv_first++];
378 return(1);
379 }
380
381
382 void
383 ray_pdone( /* reap children and free data */
384 int freall
385 )
386 {
387 ray_pclose(0); /* close child processes */
388
389 if (shm_boundary != NULL) { /* clear shared memory boundary */
390 free((void *)shm_boundary);
391 shm_boundary = NULL;
392 }
393
394 ray_done(freall); /* free rendering data */
395 }
396
397
398 static void
399 ray_pchild( /* process rays (never returns) */
400 int fd_in,
401 int fd_out
402 )
403 {
404 int n;
405 register int i;
406 /* flag child process for quit() */
407 ray_pnprocs = -1;
408 /* read each ray request set */
409 while ((n = read(fd_in, (char *)r_queue, sizeof(r_queue))) > 0) {
410 int n2;
411 if (n < sizeof(RAY))
412 break;
413 /* get smuggled set length */
414 n2 = sizeof(RAY)*r_queue[0].crtype - n;
415 if (n2 < 0)
416 error(INTERNAL, "buffer over-read in ray_pchild()");
417 if (n2 > 0) { /* read the rest of the set */
418 i = readbuf(fd_in, (char *)r_queue + n, n2);
419 if (i != n2)
420 break;
421 n += n2;
422 }
423 n /= sizeof(RAY);
424 /* evaluate rays */
425 for (i = 0; i < n; i++) {
426 r_queue[i].crtype = r_queue[i].rtype;
427 r_queue[i].parent = NULL;
428 r_queue[i].clipset = NULL;
429 r_queue[i].slights = NULL;
430 r_queue[i].rlvl = 0;
431 samplendx++;
432 rayclear(&r_queue[i]);
433 rayvalue(&r_queue[i]);
434 }
435 /* write back our results */
436 i = writebuf(fd_out, (char *)r_queue, sizeof(RAY)*n);
437 if (i != sizeof(RAY)*n)
438 error(SYSTEM, "write error in ray_pchild()");
439 }
440 if (n)
441 error(SYSTEM, "read error in ray_pchild()");
442 ambsync();
443 quit(0); /* normal exit */
444 }
445
446
447 void
448 ray_popen( /* open the specified # processes */
449 int nadd
450 )
451 {
452 /* check if our table has room */
453 if (ray_pnprocs + nadd > MAX_NPROCS)
454 nadd = MAX_NPROCS - ray_pnprocs;
455 if (nadd <= 0)
456 return;
457 ambsync(); /* load any new ambient values */
458 if (shm_boundary == NULL) { /* first child process? */
459 preload_objs(); /* preload auxiliary data */
460 /* set shared memory boundary */
461 shm_boundary = (char *)malloc(16);
462 strcpy(shm_boundary, "SHM_BOUNDARY");
463 }
464 fflush(NULL); /* clear pending output */
465 while (nadd--) { /* fork each new process */
466 int p0[2], p1[2];
467 if (pipe(p0) < 0 || pipe(p1) < 0)
468 error(SYSTEM, "cannot create pipe");
469 if ((r_proc[ray_pnprocs].pid = fork()) == 0) {
470 int pn; /* close others' descriptors */
471 for (pn = ray_pnprocs; pn--; ) {
472 close(r_proc[pn].fd_send);
473 close(r_proc[pn].fd_recv);
474 }
475 close(p0[0]); close(p1[1]);
476 close(0); /* don't share stdin */
477 /* following call never returns */
478 ray_pchild(p1[0], p0[1]);
479 }
480 if (r_proc[ray_pnprocs].pid < 0)
481 error(SYSTEM, "cannot fork child process");
482 close(p1[0]); close(p0[1]);
483 /*
484 * Close write stream on exec to avoid multiprocessing deadlock.
485 * No use in read stream without it, so set flag there as well.
486 */
487 fcntl(p1[1], F_SETFD, FD_CLOEXEC);
488 fcntl(p0[0], F_SETFD, FD_CLOEXEC);
489 r_proc[ray_pnprocs].fd_send = p1[1];
490 r_proc[ray_pnprocs].fd_recv = p0[0];
491 r_proc[ray_pnprocs].npending = 0;
492 ray_pnprocs++;
493 ray_pnidle++;
494 }
495 }
496
497
498 void
499 ray_pclose( /* close one or more child processes */
500 int nsub
501 )
502 {
503 static int inclose = 0;
504 RAY res;
505 /* check recursion */
506 if (inclose)
507 return;
508 inclose++;
509 /* check no child / in child */
510 if (ray_pnprocs <= 0)
511 return;
512 /* check argument */
513 if ((nsub <= 0) | (nsub > ray_pnprocs))
514 nsub = ray_pnprocs;
515 /* clear our ray queue */
516 while (ray_presult(&res,0) > 0)
517 ;
518 r_send_next = 0; /* hard reset in case of error */
519 r_recv_first = r_recv_next = RAYQLEN;
520 /* clean up children */
521 while (nsub--) {
522 int status;
523 ray_pnprocs--;
524 close(r_proc[ray_pnprocs].fd_send);
525 if (waitpid(r_proc[ray_pnprocs].pid, &status, 0) < 0)
526 status = 127<<8;
527 close(r_proc[ray_pnprocs].fd_recv);
528 if (status) {
529 sprintf(errmsg,
530 "rendering process %d exited with code %d",
531 r_proc[ray_pnprocs].pid, status>>8);
532 error(WARNING, errmsg);
533 }
534 ray_pnidle--;
535 }
536 inclose--;
537 }