ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raypcalls.c
Revision: 2.4
Committed: Mon Jul 21 22:30:19 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.3: +8 -8 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

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