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

Comparing ray/src/hd/rholo2l.c (file contents):
Revision 3.6 by gregl, Mon Dec 1 16:32:49 1997 UTC vs.
Revision 3.23 by greg, Wed Jan 22 18:28:32 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for local rtrace execution
6   */
7  
8 + #include <signal.h>
9 + #include <sys/time.h>
10 + #include <string.h>
11 +
12   #include "rholo.h"
13   #include "random.h"
14   #include "paths.h"
15   #include "selcall.h"
16 < #include <signal.h>
16 < #include <sys/time.h>
16 > #include "rtprocess.h"
17  
18   #ifndef MAXPROC
19 < #define MAXPROC         16
19 > #define MAXPROC         64
20   #endif
21  
22 + int     nprocs = 0;                             /* running process count */
23 +
24   static char     pfile[] = TEMPLATE;             /* persist file name */
25  
26 < static int      rtpd[MAXPROC][3];               /* process descriptors */
26 > static SUBPROC  rtpd[MAXPROC];                  /* process descriptors */
27   static float    *rtbuf = NULL;                  /* allocated i/o buffer */
28 < static int      maxqlen = 0;                    /* maximum packets per queue */
27 < static int      nprocs = 0;                     /* number of processes */
28 > static int      maxqlen;                        /* maximum packets per queue */
29  
30   static PACKET   *pqueue[MAXPROC];               /* packet queues */
31   static int      pqlen[MAXPROC];                 /* packet queue lengths */
32  
33 + static int bestout(void);
34 + static int slots_avail(void);
35 + static void queue_packet(PACKET *p);
36 + static PACKET * get_packets(int poll);
37 + static void killpersist(void);
38  
39 +
40   int
41 < start_rtrace()                  /* start rtrace process */
41 > start_rtrace(void)                      /* start rtrace process */
42   {
43          static char     buf1[8];
44          int     rmaxpack = 0;
45 <        int     psiz, npt, n;
45 >        int     psiz, n;
46                                          /* get number of processes */
47 <        if ((npt = ncprocs) <= 0)
47 >        if (ncprocs <= 0 || nprocs > 0)
48                  return(0);
49 <        if (npt > MAXPROC) {
49 >        if (ncprocs > MAXPROC) {
50                  sprintf(errmsg,
51                          "number of rtrace processes reduced from %d to %d",
52 <                                npt, MAXPROC);
52 >                                ncprocs, MAXPROC);
53                  error(WARNING, errmsg);
54 <                npt = MAXPROC;
54 >                ncprocs = MAXPROC;
55          }
56 <                                        /* add compulsory options */
57 <        rtargv[rtargc++] = "-i-";
58 <        rtargv[rtargc++] = "-I-";
59 <        rtargv[rtargc++] = "-h-";
60 <        rtargv[rtargc++] = "-ld-";
61 <        sprintf(buf1, "%d", RPACKSIZ);
62 <        rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
63 <        rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
64 <        rtargv[rtargc++] = "-fff";
65 <        rtargv[rtargc++] = vbool(VDIST) ? "-ovl" : "-ovL";
66 <        rtargv[rtargc++] = nowarn ? "-w-" : "-w+";
67 <        if (npt > 1) {
68 <                mktemp(pfile);
69 <                rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = pfile;
56 >        if (rtargv[rtargc-1] != vval(OCTREE)) {
57 >                                                /* add compulsory options */
58 >                rtargv[rtargc++] = "-i-";
59 >                rtargv[rtargc++] = "-I-";
60 >                rtargv[rtargc++] = "-h-";
61 >                rtargv[rtargc++] = "-ld-";
62 >                rtargv[rtargc++] = "-co-";
63 >                sprintf(buf1, "%d", RPACKSIZ);
64 >                rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
65 >                rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
66 >                rtargv[rtargc++] = "-fff";
67 >                rtargv[rtargc++] = vbool(VDIST) ? "-ovl" : "-ovL";
68 >                if (nowarn)
69 >                        rtargv[rtargc++] = "-w-";
70 >                if (ncprocs > 1) {
71 >                        mktemp(pfile);
72 >                        rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = pfile;
73 >                }
74 >                rtargv[rtargc++] = vval(OCTREE);
75 >                rtargv[rtargc] = NULL;
76          }
64        rtargv[rtargc++] = vval(OCTREE);
65        rtargv[rtargc] = NULL;
77          maxqlen = 0;
78 <        for (nprocs = 0; nprocs < npt; nprocs++) {      /* spawn children */
79 <                psiz = open_process(rtpd[nprocs], rtargv);
78 >        for (nprocs = 0; nprocs < ncprocs; nprocs++) {  /* spawn children */
79 >                psiz = open_process(&rtpd[nprocs], rtargv);
80                  if (psiz <= 0)
81                          error(SYSTEM, "cannot start rtrace process");
82 <                n = psiz/(RPACKSIZ*6*sizeof(float));
83 <                if (maxqlen == 0) {
84 <                        if (!(maxqlen = n))
85 <                                error(INTERNAL,
75 <                                        "bad pipe buffer size assumption");
82 >                n = psiz/(RPACKSIZ*6*sizeof(float)) + 1;
83 >                if (!maxqlen) {
84 >                        maxqlen = n;
85 >                        sleep(2);
86                  } else if (n != maxqlen)
87                          error(INTERNAL, "varying pipe buffer size!");
88                  rmaxpack += n;
# Line 85 | Line 95 | start_rtrace()                 /* start rtrace process */
95  
96  
97   static int
98 < bestout()                       /* get best process to process packet */
98 > bestout(void)                   /* get best process to process packet */
99   {
100          int     cnt;
101 <        register int    pn, i;
101 >        int     pn, i;
102  
103          pn = 0;                 /* find shortest queue */
104          for (i = 1; i < nprocs; i++)
# Line 102 | Line 112 | bestout()                      /* get best process to process packet */
112                  if (pqlen[i] == pqlen[pn])
113                          cnt++;
114                                  /* break ties fairly */
115 <        if ((cnt = random() % cnt))
115 >        if ((cnt = irandom(cnt)))
116                  for (i = pn; i < nprocs; i++)
117                          if (pqlen[i] == pqlen[pn] && !cnt--)
118                                  return(i);
# Line 110 | Line 120 | bestout()                      /* get best process to process packet */
120   }
121  
122  
123 < int
124 < slots_avail()                   /* count packet slots available */
123 > static int
124 > slots_avail(void)                       /* count packet slots available */
125   {
126 <        register int    nslots = 0;
127 <        register int    i;
126 >        int     nslots = 0;
127 >        int     i;
128  
129          for (i = nprocs; i--; )
130                  nslots += maxqlen - pqlen[i];
# Line 122 | Line 132 | slots_avail()                  /* count packet slots available */
132   }
133  
134  
135 < queue_packet(p)                 /* queue up a beam packet */
136 < register PACKET *p;
135 > static void
136 > queue_packet(                   /* queue up a beam packet */
137 >        PACKET  *p
138 > )
139   {
140          int     pn, n;
141                                  /* determine process to write to */
# Line 132 | Line 144 | register PACKET        *p;
144                                  /* write out the packet */
145          packrays(rtbuf, p);
146          if ((n = p->nr) < RPACKSIZ)     /* add flush block? */
147 <                bzero((char *)(rtbuf+6*n++), 6*sizeof(float));
148 <        if (writebuf(rtpd[pn][1], (char *)rtbuf, 6*sizeof(float)*n) < 0)
147 >                memset((char *)(rtbuf+6*n++), '\0', 6*sizeof(float));
148 >        if (writebuf(rtpd[pn].w, rtbuf, 6*sizeof(float)*n) < 0)
149                  error(SYSTEM, "write error in queue_packet");
150          p->next = NULL;
151          if (!pqlen[pn]++)       /* add it to the end of the queue */
152                  pqueue[pn] = p;
153          else {
154 <                register PACKET *rpl = pqueue[pn];
154 >                PACKET  *rpl = pqueue[pn];
155                  while (rpl->next != NULL)
156                          rpl = rpl->next;
157                  rpl->next = p;
# Line 147 | Line 159 | register PACKET        *p;
159   }
160  
161  
162 < PACKET *
163 < get_packets(poll)               /* read packets from rtrace processes */
164 < int     poll;
162 > static PACKET *
163 > get_packets(            /* read packets from rtrace processes */
164 >        int     poll
165 > )
166   {
167          static struct timeval   tpoll;  /* zero timeval struct */
168          fd_set  readset, errset;
169          PACKET  *pldone = NULL, *plend;
170 <        register PACKET *p;
170 >        PACKET  *p;
171          int     n, nr;
172 <        register int    pn;
172 >        int     pn;
173          float   *bp;
174                                          /* prepare select call */
175          FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
176          for (pn = nprocs; pn--; ) {
177                  if (pqlen[pn])
178 <                        FD_SET(rtpd[pn][0], &readset);
179 <                FD_SET(rtpd[pn][0], &errset);
180 <                if (rtpd[pn][0] >= n)
181 <                        n = rtpd[pn][0] + 1;
178 >                        FD_SET(rtpd[pn].r, &readset);
179 >                FD_SET(rtpd[pn].r, &errset);
180 >                if (rtpd[pn].r >= n)
181 >                        n = rtpd[pn].r + 1;
182          }
183                                          /* make the call */
184          n = select(n, &readset, (fd_set *)NULL, &errset,
# Line 179 | Line 192 | int    poll;
192                  return(NULL);
193                                          /* make read call(s) */
194          for (pn = 0; pn < nprocs; pn++) {
195 <                if (!FD_ISSET(rtpd[pn][0], &readset) &&
196 <                                !FD_ISSET(rtpd[pn][0], &errset))
195 >                if (!FD_ISSET(rtpd[pn].r, &readset) &&
196 >                                !FD_ISSET(rtpd[pn].r, &errset))
197                          continue;
198          reread:
199 <                n = read(rtpd[pn][0], (char *)rtbuf,
199 >                n = read(rtpd[pn].r, (char *)rtbuf,
200                                  4*sizeof(float)*RPACKSIZ*pqlen[pn]);
201                  if (n < 0) {
202 <                        if (errno == EINTR | errno == EAGAIN)
202 >                        if ((errno == EINTR) | (errno == EAGAIN))
203                                  goto reread;
204                          error(SYSTEM, "read error in get_packets");
205                  }
# Line 198 | Line 211 | int    poll;
211                                  nr++;                   /* add flush block */
212                          n -= 4*sizeof(float)*nr;
213                          if (n < 0) {                    /* get remainder */
214 <                                n += readbuf(rtpd[pn][0],
214 >                                n += readbuf(rtpd[pn].r,
215                                                  (char *)(bp+4*nr)+n, -n);
216                                  if (n)
217                                          goto eoferr;
# Line 222 | Line 235 | int    poll;
235          return(pldone);                         /* return finished packets */
236   eoferr:
237          error(USER, "rtrace process died");
238 +        return NULL; /* pro forma return */
239   }
240  
241  
242   PACKET *
243 < do_packets(pl)                  /* queue a packet list, return finished */
244 < register PACKET *pl;
243 > do_packets(                     /* queue a packet list, return finished */
244 >        PACKET  *pl
245 > )
246   {
247 <        register PACKET *p;
247 >        PACKET  *p;
248                                          /* consistency check */
249          if (nprocs < 1)
250                  error(CONSISTENCY, "do_packets called with no active process");
# Line 243 | Line 258 | register PACKET        *pl;
258  
259  
260   PACKET *
261 < flush_queue()                   /* empty all rtrace queues */
261 > flush_queue(void)                       /* empty all rtrace queues */
262   {
263          PACKET  *rpdone = NULL;
264 <        register PACKET *rpl;
264 >        PACKET  *rpl = NULL;
265          float   *bp;
266 <        register PACKET *p;
266 >        PACKET  *p;
267          int     i, n, nr;
268  
269          for (i = 0; i < nprocs; i++)
# Line 265 | Line 280 | flush_queue()                  /* empty all rtrace queues */
280                                  if (rpl->nr < RPACKSIZ)
281                                          nr++;           /* add flush block */
282                          }
283 <                        n = readbuf(rtpd[i][0], (char *)rtbuf,
269 <                                        4*sizeof(float)*nr);
283 >                        n = readbuf(rtpd[i].r, rtbuf, 4*sizeof(float)*nr);
284                          if (n < 0)
285                                  error(SYSTEM, "read failure in flush_queue");
286                          bp = rtbuf;                     /* process packets */
# Line 287 | Line 301 | flush_queue()                  /* empty all rtrace queues */
301   }
302  
303  
304 < static
305 < killpersist()                   /* kill persistent process */
304 > static void
305 > killpersist(void)                       /* kill persistent process */
306   {
307          FILE    *fp;
308          int     pid;
# Line 302 | Line 316 | killpersist()                  /* kill persistent process */
316  
317  
318   int
319 < end_rtrace()                    /* close rtrace process(es) */
319 > end_rtrace(void)                        /* close rtrace process(es) */
320   {
321          int     status = 0, rv;
322  
323          if (nprocs > 1)
324                  killpersist();
325 <        while (nprocs > 0) {
326 <                rv = close_process(rtpd[--nprocs]);
327 <                if (rv > 0)
314 <                        status = rv;
315 <        }
316 <        free((char *)rtbuf);
325 >        status = close_processes(rtpd, nprocs);
326 >        nprocs = 0;
327 >        free((void *)rtbuf);
328          rtbuf = NULL;
329          maxqlen = 0;
330          return(status);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines