--- ray/src/rt/raypcalls.c 2011/08/20 18:23:38 2.28 +++ ray/src/rt/raypcalls.c 2024/04/30 22:25:46 2.38 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raypcalls.c,v 2.28 2011/08/20 18:23:38 greg Exp $"; +static const char RCSid[] = "$Id: raypcalls.c,v 2.38 2024/04/30 22:25:46 greg Exp $"; #endif /* * raypcalls.c - interface for parallel rendering using Radiance @@ -147,7 +147,7 @@ static const char RCSid[] = "$Id: raypcalls.c,v 2.28 2 #include "selcall.h" #ifndef RAYQLEN -#define RAYQLEN 12 /* # rays to send at once */ +#define RAYQLEN 24 /* # rays to send at once */ #endif #ifndef MAX_RPROCS @@ -164,7 +164,7 @@ int ray_pnprocs = 0; /* number of child processes */ int ray_pnidle = 0; /* number of idle children */ static struct child_proc { - int pid; /* child process id */ + RT_PID pid; /* child process id */ int fd_send; /* write to child here */ int fd_recv; /* read from child here */ int npending; /* # rays in process */ @@ -212,7 +212,7 @@ ray_pflush(void) /* send queued rays to idle childre for (i = ray_pnprocs; nc && i--; ) { if (r_proc[i].npending > 0) continue; /* child looks busy */ - n = (r_send_next - sfirst)/nc--; + n = (r_send_next - sfirst) / nc--; if (!n) continue; /* smuggle set size in crtype */ @@ -290,7 +290,7 @@ ray_presult( /* check for a completed ray */ static struct timeval tpoll; /* zero timeval struct */ static fd_set readset, errset; int n, ok; - register int pn; + int pn; if (r == NULL) return(0); @@ -368,7 +368,7 @@ getready: /* any children waiting for us? */ } /* preen returned rays */ for (n = r_recv_next - r_recv_first; n--; ) { - register RAY *rp = &r_queue[r_recv_first + n]; + RAY *rp = &r_queue[r_recv_first + n]; rp->rno = r_proc[pn].rno[n]; rp->parent = NULL; rp->newcset = rp->clipset = NULL; @@ -404,7 +404,7 @@ ray_pchild( /* process rays (never returns) */ ) { int n; - register int i; + int i; /* flag child process for quit() */ ray_pnprocs = -1; /* read each ray request set */ @@ -456,6 +456,8 @@ ray_popen( /* open the specified # processes */ nadd = MAX_NPROCS - ray_pnprocs; if (nadd <= 0) return; + if (nobjects <= 0) + error(CONSISTENCY, "ray_popen() called before scene loaded"); ambsync(); /* load any new ambient values */ if (shm_boundary == NULL) { /* first child process? */ preload_objs(); /* preload auxiliary data */ @@ -508,37 +510,57 @@ ray_pclose( /* close one or more child processes */ ) { static int inclose = 0; - RAY res; + RAY res; + int i, status = 0; + /* check no child / in child */ + if (ray_pnprocs <= 0) + return; /* check recursion */ if (inclose) return; inclose++; - /* check no child / in child */ - if (ray_pnprocs <= 0) - return; /* check argument */ if ((nsub <= 0) | (nsub > ray_pnprocs)) nsub = ray_pnprocs; /* clear our ray queue */ + i = r_send_next; + r_send_next = 0; while (ray_presult(&res,0) > 0) - ; - r_send_next = 0; /* hard reset in case of error */ + ++i; + if (i) { + sprintf(errmsg, "dropped %d rays in ray_pclose()", i); + error(WARNING, errmsg); + } r_recv_first = r_recv_next = RAYQLEN; - /* clean up children */ - while (nsub--) { - int status; - ray_pnprocs--; - close(r_proc[ray_pnprocs].fd_send); - if (waitpid(r_proc[ray_pnprocs].pid, &status, 0) < 0) + /* close send pipes */ + for (i = ray_pnprocs-nsub; i < ray_pnprocs; i++) + close(r_proc[i].fd_send); + + if (nsub == 1) { /* awaiting single process? */ + if (waitpid(r_proc[ray_pnprocs-1].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", - r_proc[ray_pnprocs].pid, status>>8); - error(WARNING, errmsg); + close(r_proc[ray_pnprocs-1].fd_recv); + } else /* else unordered wait */ + for (i = 0; i < nsub; ) { + int j, mystatus; + RT_PID pid = wait(&mystatus); + if (pid < 0) { + status = 127<<8; + break; + } + for (j = ray_pnprocs-nsub; j < ray_pnprocs; j++) + if (r_proc[j].pid == pid) { + if (mystatus) + status = mystatus; + close(r_proc[j].fd_recv); + ++i; + } } - ray_pnidle--; + ray_pnprocs -= nsub; + ray_pnidle -= nsub; + if (status) { + sprintf(errmsg, "rendering process exited with code %d", status>>8); + error(WARNING, errmsg); } inclose--; }