--- ray/src/rt/raypcalls.c 2004/09/17 21:43:50 2.8 +++ ray/src/rt/raypcalls.c 2009/12/12 23:08:13 2.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raypcalls.c,v 2.8 2004/09/17 21:43:50 greg Exp $"; +static const char RCSid[] = "$Id: raypcalls.c,v 2.24 2009/12/12 23:08:13 greg Exp $"; #endif /* * raypcalls.c - interface for parallel rendering using Radiance @@ -13,24 +13,29 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * These calls are designed similarly to the ones in raycalls.c, * but allow for multiple rendering processes on the same host * machine. There is no sense in specifying more child processes - * than you have processors, but one child may help by allowing + * than you have processor cores, but one child may help by allowing * asynchronous ray computation in an interactive program, and * will protect the caller from fatal rendering errors. * - * You should first read and undrstand the header in raycalls.c, + * You should first read and understand the header in raycalls.c, * as some things are explained there that are not repated here. * * The first step is opening one or more rendering processes * with a call to ray_pinit(oct, nproc). Before calling fork(), * ray_pinit() loads the octree and data structures into the - * caller's memory. This permits all sorts of queries that - * wouldn't be possible otherwise, without causing any real + * caller's memory, and ray_popen() synchronizes the ambient + * file, if any. Shared memory permits all sorts of queries + * that wouldn't be possible otherwise without causing any real * memory overhead, since all the static data are shared - * between processes. Rays are then traced using a simple + * between processes. Rays are traced using a simple * queuing mechanism, explained below. * - * The ray queue holds as many rays as there are rendering - * processes. Rays are queued and returned by a single + * The ray queue buffers RAYQLEN rays before sending to + * children, each of which may internally buffer RAYQLEN rays + * during evaluation. Rays are not returned in the order + * they are sent when multiple processes are open. + * + * Rays are queued and returned by a single * ray_pqueue() call. A ray_pqueue() return * value of 0 indicates that no rays are ready * and the queue is not yet full. A return value of 1 @@ -43,7 +48,7 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * myRay.rorg = ( ray origin point ) * myRay.rdir = ( normalized ray direction ) * myRay.rmax = ( maximum length, or zero for no limit ) - * rayorigin(&myRay, NULL, PRIMARY, 1.0); + * rayorigin(&myRay, PRIMARY, NULL, NULL); * myRay.rno = ( my personal ray identifier ) * if (ray_pqueue(&myRay) == 1) * { do something with results } @@ -66,11 +71,13 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * * If the second argument is 1, the call won't block when * results aren't ready, but will immediately return 0. + * (A special value of -1 returns 0 unless a ray is + * ready in the queue and no system calls are needed.) * If the second argument is 0, the call will block * until a value is available, returning 0 only if the * queue is completely empty. A negative return value * indicates that a rendering process died. If this - * happens, ray_close(0) is automatically called to close + * happens, ray_pclose(0) is automatically called to close * all child processes, and ray_pnprocs is set to zero. * * If you just want to fill the ray queue without checking for @@ -90,8 +97,10 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * Any queued ray calculations will be awaited and discarded. * As with ray_done(), ray_pdone(0) hangs onto data files * and fonts that are likely to be used in subsequent renderings. - * Whether you want to bother cleaning up memory or not, you - * should at least call ray_pclose(0) to clean the child processes. + * Whether you need to clean up memory or not, you should + * at least call ray_pclose(0) to await the child processes. + * The caller should define a quit() function that calls + * ray_pclose(0) if ray_pnprocs > 0. * * Warning: You cannot affect any of the rendering processes * by changing global parameter values onece ray_pinit() has @@ -105,7 +114,7 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * Note: These routines are written to coordinate with the * definitions in raycalls.c, and in fact depend on them. * If you want to trace a ray and get a result synchronously, - * use the ray_trace() call to compute it in the parent process + * use the ray_trace() call to compute it in the parent process. * This will not interfere with any subprocess calculations, * but beware that a fatal error may end with a call to quit(). * @@ -121,24 +130,22 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.8 20 * returning a negative value from ray_pqueue() or * ray_presult(). If you get a negative value from either * of these calls, you can assume that the processes have - * been cleaned up with a call to ray_close(), though you + * been cleaned up with a call to ray_pclose(), though you * will have to call ray_pdone() yourself if you want to * free memory. Obviously, you cannot continue rendering * without risking further errors, but otherwise your * process should not be compromised. */ -#include -#include -#include /* XXX platform */ - #include "rtprocess.h" #include "ray.h" #include "ambient.h" +#include +#include #include "selcall.h" #ifndef RAYQLEN -#define RAYQLEN 16 /* # rays to send at once */ +#define RAYQLEN 12 /* # rays to send at once */ #endif #ifndef MAX_RPROCS @@ -159,21 +166,21 @@ static struct child_proc { int fd_send; /* write to child here */ int fd_recv; /* read from child here */ int npending; /* # rays in process */ - unsigned long rno[RAYQLEN]; /* working on these rays */ + RNUMBER rno[RAYQLEN]; /* working on these rays */ } r_proc[MAX_NPROCS]; /* our child processes */ static RAY r_queue[2*RAYQLEN]; /* ray i/o buffer */ -static int r_send_next; /* next send ray placement */ -static int r_recv_first; /* position of first unreported ray */ -static int r_recv_next; /* next receive ray placement */ +static int r_send_next = 0; /* next send ray placement */ +static int r_recv_first = RAYQLEN; /* position of first unreported ray */ +static int r_recv_next = RAYQLEN; /* next received ray placement */ #define sendq_full() (r_send_next >= RAYQLEN) static int ray_pflush(void); -static void ray_pchild(int fd_in, int fd_out); +static void ray_pchild(int fd_in, int fd_out); -extern void +void ray_pinit( /* initialize ray-tracing processes */ char *otnm, int nproc @@ -184,15 +191,6 @@ ray_pinit( /* initialize ray-tracing processes */ ray_init(otnm); /* load the shared scene */ - preload_objs(); /* preload auxiliary data */ - - /* set shared memory boundary */ - shm_boundary = (char *)malloc(16); - strcpy(shm_boundary, "SHM_BOUNDARY"); - - r_send_next = 0; /* set up queue */ - r_recv_first = r_recv_next = RAYQLEN; - ray_popen(nproc); /* fork children */ } @@ -226,13 +224,13 @@ ray_pflush(void) /* send queued rays to idle childre ray_pnidle--; /* now she's busy */ } if (sfirst != r_send_next) - error(CONSISTENCY, "code screwup in ray_pflush"); + error(CONSISTENCY, "code screwup in ray_pflush()"); r_send_next = 0; return(sfirst); /* return total # sent */ } -extern void +void ray_psend( /* add a ray to our send queue */ RAY *r ) @@ -241,14 +239,13 @@ ray_psend( /* add a ray to our send queue */ return; /* flush output if necessary */ if (sendq_full() && ray_pflush() <= 0) - error(INTERNAL, "ray_pflush failed in ray_psend"); + error(INTERNAL, "ray_pflush failed in ray_psend()"); - r_queue[r_send_next] = *r; - r_send_next++; + r_queue[r_send_next++] = *r; } -extern int +int ray_pqueue( /* queue a ray for computation */ RAY *r ) @@ -257,30 +254,27 @@ ray_pqueue( /* queue a ray for computation */ return(0); /* check for full send queue */ if (sendq_full()) { - RAY mySend; - int rval; - mySend = *r; + RAY mySend = *r; /* wait for a result */ - rval = ray_presult(r, 0); + if (ray_presult(r, 0) <= 0) + return(-1); /* put new ray in queue */ - r_queue[r_send_next] = mySend; - r_send_next++; - return(rval); /* done */ + r_queue[r_send_next++] = mySend; + /* XXX r_send_next may now be > RAYQLEN */ + return(1); } - /* add ray to send queue */ - r_queue[r_send_next] = *r; - r_send_next++; + /* else add ray to send queue */ + r_queue[r_send_next++] = *r; /* check for returned ray... */ if (r_recv_first >= r_recv_next) return(0); /* ...one is sitting in queue */ - *r = r_queue[r_recv_first]; - r_recv_first++; + *r = r_queue[r_recv_first++]; return(1); } -extern int +int ray_presult( /* check for a completed ray */ RAY *r, int poll @@ -295,10 +289,12 @@ ray_presult( /* check for a completed ray */ return(0); /* check queued results first */ if (r_recv_first < r_recv_next) { - *r = r_queue[r_recv_first]; - r_recv_first++; + *r = r_queue[r_recv_first++]; return(1); } + if (poll < 0) /* immediate polling mode? */ + return(0); + n = ray_pnprocs - ray_pnidle; /* pending before flush? */ if (ray_pflush() < 0) /* send new rays to process */ @@ -310,12 +306,15 @@ ray_presult( /* check for a completed ray */ n = ray_pnprocs - ray_pnidle; if (n <= 0) /* return if nothing to await */ return(0); + if (!poll && ray_pnprocs == 1) /* one process -> skip select() */ + FD_SET(r_proc[0].fd_recv, &readset); + getready: /* any children waiting for us? */ for (pn = ray_pnprocs; pn--; ) if (FD_ISSET(r_proc[pn].fd_recv, &readset) || FD_ISSET(r_proc[pn].fd_recv, &errset)) break; - /* call select if we must */ + /* call select() if we must */ if (pn < 0) { FD_ZERO(&readset); FD_ZERO(&errset); n = 0; for (pn = ray_pnprocs; pn--; ) { @@ -330,7 +329,7 @@ getready: /* any children waiting for us? */ poll ? &tpoll : (struct timeval *)NULL)) < 0) if (errno != EINTR) { error(WARNING, - "select call failed in ray_presult"); + "select call failed in ray_presult()"); ray_pclose(0); return(-1); } @@ -370,13 +369,12 @@ getready: /* any children waiting for us? */ rp->slights = NULL; } /* return first ray received */ - *r = r_queue[r_recv_first]; - r_recv_first++; + *r = r_queue[r_recv_first++]; return(1); } -extern void +void ray_pdone( /* reap children and free data */ int freall ) @@ -387,6 +385,7 @@ ray_pdone( /* reap children and free data */ free((void *)shm_boundary); shm_boundary = NULL; } + ray_done(freall); /* free rendering data */ } @@ -399,30 +398,31 @@ ray_pchild( /* process rays (never returns) */ { int n; register int i; + /* flag child process for quit() */ + ray_pnprocs = -1; /* read each ray request set */ while ((n = read(fd_in, (char *)r_queue, sizeof(r_queue))) > 0) { int n2; - if (n % sizeof(RAY)) + if (n < sizeof(RAY)) break; - n /= sizeof(RAY); /* get smuggled set length */ - n2 = r_queue[0].crtype - n; + n2 = sizeof(RAY)*r_queue[0].crtype - n; if (n2 < 0) - error(INTERNAL, "buffer over-read in ray_pchild"); + error(INTERNAL, "buffer over-read in ray_pchild()"); if (n2 > 0) { /* read the rest of the set */ - i = readbuf(fd_in, (char *)(r_queue+n), - sizeof(RAY)*n2); - if (i != sizeof(RAY)*n2) + i = readbuf(fd_in, (char *)r_queue + n, n2); + if (i != n2) break; n += n2; } + n /= sizeof(RAY); /* evaluate rays */ for (i = 0; i < n; i++) { r_queue[i].crtype = r_queue[i].rtype; r_queue[i].parent = NULL; r_queue[i].clipset = NULL; r_queue[i].slights = NULL; - r_queue[i].revf = raytrace; + r_queue[i].rlvl = 0; samplendx++; rayclear(&r_queue[i]); rayvalue(&r_queue[i]); @@ -430,16 +430,16 @@ ray_pchild( /* process rays (never returns) */ /* write back our results */ i = writebuf(fd_out, (char *)r_queue, sizeof(RAY)*n); if (i != sizeof(RAY)*n) - error(SYSTEM, "write error in ray_pchild"); + error(SYSTEM, "write error in ray_pchild()"); } if (n) - error(SYSTEM, "read error in ray_pchild"); + error(SYSTEM, "read error in ray_pchild()"); ambsync(); quit(0); /* normal exit */ } -extern void +void ray_popen( /* open the specified # processes */ int nadd ) @@ -449,8 +449,14 @@ ray_popen( /* open the specified # processes */ nadd = MAX_NPROCS - ray_pnprocs; if (nadd <= 0) return; - fflush(stderr); /* clear pending output */ - fflush(stdout); + ambsync(); /* load any new ambient values */ + if (shm_boundary == NULL) { /* first child process? */ + preload_objs(); /* preload auxiliary data */ + /* set shared memory boundary */ + shm_boundary = (char *)malloc(16); + strcpy(shm_boundary, "SHM_BOUNDARY"); + } + fflush(NULL); /* clear pending output */ while (nadd--) { /* fork each new process */ int p0[2], p1[2]; if (pipe(p0) < 0 || pipe(p1) < 0) @@ -462,12 +468,19 @@ ray_popen( /* open the specified # processes */ close(r_proc[pn].fd_recv); } close(p0[0]); close(p1[1]); + close(0); /* don't share stdin */ /* following call never returns */ ray_pchild(p1[0], p0[1]); } if (r_proc[ray_pnprocs].pid < 0) error(SYSTEM, "cannot fork child process"); close(p1[0]); close(p0[1]); + /* + * Close write stream on exec to avoid multiprocessing deadlock. + * No use in read stream without it, so set flag there as well. + */ + fcntl(p1[1], F_SETFD, FD_CLOEXEC); + fcntl(p0[0], F_SETFD, FD_CLOEXEC); r_proc[ray_pnprocs].fd_send = p1[1]; r_proc[ray_pnprocs].fd_recv = p0[0]; r_proc[ray_pnprocs].npending = 0; @@ -477,7 +490,7 @@ ray_popen( /* open the specified # processes */ } -extern void +void ray_pclose( /* close one or more child processes */ int nsub ) @@ -494,14 +507,16 @@ ray_pclose( /* close one or more child processes */ /* clear our ray queue */ while (ray_presult(&res,0) > 0) ; + r_send_next = 0; /* hard reset in case of error */ + r_recv_first = r_recv_next = RAYQLEN; /* clean up children */ while (nsub--) { int status; ray_pnprocs--; - close(r_proc[ray_pnprocs].fd_recv); close(r_proc[ray_pnprocs].fd_send); if (waitpid(r_proc[ray_pnprocs].pid, &status, 0) < 0) status = 127<<8; + close(r_proc[ray_pnprocs].fd_recv); if (status) { sprintf(errmsg, "rendering process %d exited with code %d", @@ -511,12 +526,4 @@ ray_pclose( /* close one or more child processes */ ray_pnidle--; } inclose--; -} - - -void -quit(ec) /* make sure exit is called */ -int ec; -{ - exit(ec); }