ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raypcalls.c
Revision: 2.13
Committed: Tue Dec 20 20:36:44 2005 UTC (18 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +14 -13 lines
Log Message:
Added ambient file synchronization and minor fixes

File Contents

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