ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raypcalls.c
(Generate patch)

Comparing ray/src/rt/raypcalls.c (file contents):
Revision 2.5 by schorsch, Sun Jul 27 22:12:03 2003 UTC vs.
Revision 2.13 by greg, Tue Dec 20 20:36:44 2005 UTC

# Line 23 | Line 23 | static const char      RCSid[] = "$Id$";
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
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 as many rays as there are rendering
34 < *  processes.  Rays are queued and returned by a single
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
# Line 43 | Line 45 | static const char      RCSid[] = "$Id$";
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, NULL, PRIMARY, 1.0);
48 > *      rayorigin(&myRay, PRIMARY, NULL, NULL);
49   *      myRay.rno = ( my personal ray identifier )
50   *      if (ray_pqueue(&myRay) == 1)
51   *              { do something with results }
# Line 51 | Line 53 | static const char      RCSid[] = "$Id$";
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 for you by ray_trace().  The
57 < *  great thing is that ray_pqueue() will trace rays faster in
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.
# Line 81 | Line 83 | static const char      RCSid[] = "$Id$";
83   *              ray_psend(&myRay);
84   *      }
85   *
86 < *  The ray_presult() and/or ray_pqueue() functions may then be
87 < *  called to read back the results.
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.
# Line 99 | Line 102 | static const char      RCSid[] = "$Id$";
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.
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 system.  Although you can avoid exit() with
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
# Line 120 | Line 125 | static const char      RCSid[] = "$Id$";
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 < *  but otherwise your process should not be compromised.
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  "ray.h"
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         16              /* # rays to send at once */
143 > #define RAYQLEN         12              /* # rays to send at once */
144   #endif
145  
146   #ifndef MAX_RPROCS
# Line 160 | Line 171 | 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 < void
178 < ray_pinit(otnm, nproc)          /* initialize ray-tracing processes */
179 < char    *otnm;
180 < int     nproc;
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);
# Line 185 | Line 200 | int    nproc;
200  
201  
202   static int
203 < ray_pflush()                    /* send queued rays to idle children */
203 > ray_pflush(void)                        /* send queued rays to idle children */
204   {
205          int     nc, n, nw, i, sfirst;
206  
# Line 219 | Line 234 | ray_pflush()                   /* send queued rays to idle children */
234   }
235  
236  
237 < void
238 < ray_psend(r)                    /* add a ray to our send queue */
239 < RAY     *r;
237 > extern void
238 > ray_psend(                      /* add a ray to our send queue */
239 >        RAY     *r
240 > )
241   {
242          if (r == NULL)
243                  return;
# Line 234 | Line 250 | RAY    *r;
250   }
251  
252  
253 < int
254 < ray_pqueue(r)                   /* queue a ray for computation */
255 < RAY     *r;
253 > extern int
254 > ray_pqueue(                     /* queue a ray for computation */
255 >        RAY     *r
256 > )
257   {
258          if (r == NULL)
259                  return(0);
# Line 252 | Line 269 | RAY    *r;
269                  r_send_next++;
270                  return(rval);           /* done */
271          }
272 <                                        /* add ray to send queue */
272 >                                        /* else add ray to send queue */
273          r_queue[r_send_next] = *r;
274          r_send_next++;
275                                          /* check for returned ray... */
# Line 265 | Line 282 | RAY    *r;
282   }
283  
284  
285 < int
286 < ray_presult(r, poll)            /* check for a completed ray */
287 < RAY     *r;
288 < int     poll;
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;
# Line 354 | Line 372 | getready:                              /* any children waiting for us? */
372                  rp->slights = NULL;
373          }
374                                          /* return first ray received */
375 <        *r = r_queue[r_recv_first];
358 <        r_recv_first++;
375 >        *r = r_queue[r_recv_first++];
376          return(1);
377   }
378  
379  
380 < void
381 < ray_pdone(freall)               /* reap children and free data */
382 < int     freall;
380 > extern void
381 > ray_pdone(              /* reap children and free data */
382 >        int     freall
383 > )
384   {
385          ray_pclose(0);                  /* close child processes */
386  
# Line 375 | Line 393 | int    freall;
393  
394  
395   static void
396 < ray_pchild(fd_in, fd_out)       /* process rays (never returns) */
397 < int     fd_in;
398 < int     fd_out;
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))
406 >                if (n < sizeof(RAY))
407                          break;
389                n /= sizeof(RAY);
408                                          /* get smuggled set length */
409 <                n2 = r_queue[0].crtype - n;
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),
414 <                                        sizeof(RAY)*n2);
397 <                        if (i != sizeof(RAY)*n2)
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;
407                        r_queue[i].revf = raytrace;
425                          samplendx++;
426                          rayclear(&r_queue[i]);
427                          rayvalue(&r_queue[i]);
# Line 421 | Line 438 | int    fd_out;
438   }
439  
440  
441 < void
442 < ray_popen(nadd)                 /* open the specified # processes */
443 < int     nadd;
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 <        fflush(stderr);                 /* clear pending output */
452 <        fflush(stdout);
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)
# Line 449 | Line 467 | int    nadd;
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;
# Line 458 | Line 482 | int    nadd;
482   }
483  
484  
485 < void
486 < ray_pclose(nsub)                /* close one or more child processes */
487 < int     nsub;
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;
# Line 480 | Line 505 | int    nsub;
505                  ray_pnprocs--;
506                  close(r_proc[ray_pnprocs].fd_recv);
507                  close(r_proc[ray_pnprocs].fd_send);
508 <                while (wait(&status) != r_proc[ray_pnprocs].pid)
509 <                        ;
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",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines