ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
Revision: 3.7
Committed: Tue Dec 2 16:35:45 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.6: +2 -1 lines
Log Message:
leave rtrace -w option alone if rholo -w not set

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Routines for local rtrace execution
9     */
10    
11     #include "rholo.h"
12     #include "random.h"
13 gregl 3.6 #include "paths.h"
14 gregl 3.3 #include "selcall.h"
15 gregl 3.1 #include <signal.h>
16     #include <sys/time.h>
17    
18     #ifndef MAXPROC
19     #define MAXPROC 16
20     #endif
21    
22 gregl 3.6 static char pfile[] = TEMPLATE; /* persist file name */
23 gregl 3.1
24     static int rtpd[MAXPROC][3]; /* process descriptors */
25     static float *rtbuf = NULL; /* allocated i/o buffer */
26     static int maxqlen = 0; /* maximum packets per queue */
27     static int nprocs = 0; /* number of processes */
28    
29     static PACKET *pqueue[MAXPROC]; /* packet queues */
30     static int pqlen[MAXPROC]; /* packet queue lengths */
31    
32    
33     int
34     start_rtrace() /* start rtrace process */
35     {
36     static char buf1[8];
37     int rmaxpack = 0;
38     int psiz, npt, n;
39     /* get number of processes */
40     if ((npt = ncprocs) <= 0)
41     return(0);
42     if (npt > MAXPROC) {
43     sprintf(errmsg,
44     "number of rtrace processes reduced from %d to %d",
45     npt, MAXPROC);
46     error(WARNING, errmsg);
47     npt = MAXPROC;
48     }
49     /* add compulsory options */
50     rtargv[rtargc++] = "-i-";
51     rtargv[rtargc++] = "-I-";
52     rtargv[rtargc++] = "-h-";
53     rtargv[rtargc++] = "-ld-";
54     sprintf(buf1, "%d", RPACKSIZ);
55     rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
56     rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
57     rtargv[rtargc++] = "-fff";
58 gregl 3.6 rtargv[rtargc++] = vbool(VDIST) ? "-ovl" : "-ovL";
59 gregl 3.7 if (nowarn)
60     rtargv[rtargc++] = "-w-";
61 gregl 3.1 if (npt > 1) {
62 gregl 3.6 mktemp(pfile);
63     rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = pfile;
64 gregl 3.1 }
65     rtargv[rtargc++] = vval(OCTREE);
66     rtargv[rtargc] = NULL;
67     maxqlen = 0;
68     for (nprocs = 0; nprocs < npt; nprocs++) { /* spawn children */
69     psiz = open_process(rtpd[nprocs], rtargv);
70     if (psiz <= 0)
71     error(SYSTEM, "cannot start rtrace process");
72     n = psiz/(RPACKSIZ*6*sizeof(float));
73     if (maxqlen == 0) {
74     if (!(maxqlen = n))
75     error(INTERNAL,
76     "bad pipe buffer size assumption");
77     } else if (n != maxqlen)
78     error(INTERNAL, "varying pipe buffer size!");
79     rmaxpack += n;
80     }
81     rtbuf = (float *)malloc(RPACKSIZ*6*sizeof(float)*maxqlen);
82     if (rtbuf == NULL)
83     error(SYSTEM, "malloc failure in start_rtrace");
84     return(rmaxpack);
85     }
86    
87    
88     static int
89     bestout() /* get best process to process packet */
90     {
91     int cnt;
92     register int pn, i;
93    
94     pn = 0; /* find shortest queue */
95     for (i = 1; i < nprocs; i++)
96     if (pqlen[i] < pqlen[pn])
97     pn = i;
98     /* sanity check */
99     if (pqlen[pn] == maxqlen)
100     return(-1);
101     cnt = 0; /* count number of ties */
102     for (i = pn; i < nprocs; i++)
103     if (pqlen[i] == pqlen[pn])
104     cnt++;
105     /* break ties fairly */
106     if ((cnt = random() % cnt))
107     for (i = pn; i < nprocs; i++)
108     if (pqlen[i] == pqlen[pn] && !cnt--)
109     return(i);
110     return(pn);
111     }
112    
113    
114     int
115     slots_avail() /* count packet slots available */
116     {
117     register int nslots = 0;
118     register int i;
119    
120     for (i = nprocs; i--; )
121     nslots += maxqlen - pqlen[i];
122     return(nslots);
123     }
124    
125    
126     queue_packet(p) /* queue up a beam packet */
127     register PACKET *p;
128     {
129     int pn, n;
130     /* determine process to write to */
131     if ((pn = bestout()) < 0)
132     error(INTERNAL, "rtrace input queues are full!");
133     /* write out the packet */
134     packrays(rtbuf, p);
135     if ((n = p->nr) < RPACKSIZ) /* add flush block? */
136     bzero((char *)(rtbuf+6*n++), 6*sizeof(float));
137     if (writebuf(rtpd[pn][1], (char *)rtbuf, 6*sizeof(float)*n) < 0)
138     error(SYSTEM, "write error in queue_packet");
139     p->next = NULL;
140     if (!pqlen[pn]++) /* add it to the end of the queue */
141     pqueue[pn] = p;
142     else {
143     register PACKET *rpl = pqueue[pn];
144     while (rpl->next != NULL)
145     rpl = rpl->next;
146     rpl->next = p;
147     }
148     }
149    
150    
151     PACKET *
152     get_packets(poll) /* read packets from rtrace processes */
153     int poll;
154     {
155     static struct timeval tpoll; /* zero timeval struct */
156     fd_set readset, errset;
157     PACKET *pldone = NULL, *plend;
158     register PACKET *p;
159     int n, nr;
160     register int pn;
161     float *bp;
162     /* prepare select call */
163     FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
164     for (pn = nprocs; pn--; ) {
165     if (pqlen[pn])
166     FD_SET(rtpd[pn][0], &readset);
167     FD_SET(rtpd[pn][0], &errset);
168     if (rtpd[pn][0] >= n)
169     n = rtpd[pn][0] + 1;
170     }
171     /* make the call */
172     n = select(n, &readset, (fd_set *)NULL, &errset,
173     poll ? &tpoll : (struct timeval *)NULL);
174     if (n < 0) {
175     if (errno == EINTR) /* interrupted select call */
176     return(NULL);
177     error(SYSTEM, "select call failure in get_packets");
178     }
179     if (n == 0) /* is nothing ready? */
180     return(NULL);
181     /* make read call(s) */
182     for (pn = 0; pn < nprocs; pn++) {
183     if (!FD_ISSET(rtpd[pn][0], &readset) &&
184     !FD_ISSET(rtpd[pn][0], &errset))
185     continue;
186     reread:
187     n = read(rtpd[pn][0], (char *)rtbuf,
188     4*sizeof(float)*RPACKSIZ*pqlen[pn]);
189     if (n < 0) {
190     if (errno == EINTR | errno == EAGAIN)
191     goto reread;
192     error(SYSTEM, "read error in get_packets");
193     }
194     if (n == 0)
195     goto eoferr;
196     bp = rtbuf; /* finish processing */
197     for (p = pqueue[pn]; n && p != NULL; p = p->next) {
198     if ((nr = p->nr) < RPACKSIZ)
199     nr++; /* add flush block */
200     n -= 4*sizeof(float)*nr;
201     if (n < 0) { /* get remainder */
202     n += readbuf(rtpd[pn][0],
203     (char *)(bp+4*nr)+n, -n);
204     if (n)
205     goto eoferr;
206     }
207     donerays(p, bp);
208     bp += 4*nr;
209     pqlen[pn]--;
210     }
211     if (n) /* read past end? */
212     error(INTERNAL, "packet sync error in get_packets");
213     /* take from queue */
214 gregl 3.2 if (pldone == NULL)
215 gregl 3.1 pldone = plend = pqueue[pn];
216     else
217     plend->next = pqueue[pn];
218     while (plend->next != p)
219     plend = plend->next;
220     plend->next = NULL;
221     pqueue[pn] = p;
222     }
223     return(pldone); /* return finished packets */
224     eoferr:
225 gregl 3.3 error(USER, "rtrace process died");
226 gregl 3.1 }
227    
228    
229     PACKET *
230     do_packets(pl) /* queue a packet list, return finished */
231     register PACKET *pl;
232     {
233     register PACKET *p;
234     /* consistency check */
235     if (nprocs < 1)
236     error(CONSISTENCY, "do_packets called with no active process");
237     /* queue each new packet */
238     while (pl != NULL) {
239     p = pl; pl = p->next; p->next = NULL;
240     queue_packet(p);
241     }
242     return(get_packets(slots_avail())); /* return processed packets */
243     }
244    
245    
246     PACKET *
247     flush_queue() /* empty all rtrace queues */
248     {
249     PACKET *rpdone = NULL;
250     register PACKET *rpl;
251     float *bp;
252     register PACKET *p;
253     int i, n, nr;
254    
255     for (i = 0; i < nprocs; i++)
256     if (pqlen[i]) {
257     if (rpdone == NULL) { /* tack on queue */
258     rpdone = rpl = pqueue[i];
259 gregl 3.4 if ((nr = rpl->nr) < RPACKSIZ) nr++;
260 gregl 3.1 } else {
261     rpl->next = pqueue[i];
262 gregl 3.2 nr = 0;
263 gregl 3.1 }
264 gregl 3.2 while (rpl->next != NULL) {
265     nr += (rpl = rpl->next)->nr;
266     if (rpl->nr < RPACKSIZ)
267     nr++; /* add flush block */
268     }
269 gregl 3.1 n = readbuf(rtpd[i][0], (char *)rtbuf,
270 gregl 3.2 4*sizeof(float)*nr);
271 gregl 3.1 if (n < 0)
272     error(SYSTEM, "read failure in flush_queue");
273     bp = rtbuf; /* process packets */
274     for (p = pqueue[i]; p != NULL; p = p->next) {
275     if ((nr = p->nr) < RPACKSIZ)
276     nr++; /* add flush block */
277     n -= 4*sizeof(float)*nr;
278     if (n >= 0)
279     donerays(p, bp);
280     else
281     p->nr = 0; /* short data error */
282     bp += 4*nr;
283     }
284     pqueue[i] = NULL; /* zero this queue */
285     pqlen[i] = 0;
286     }
287     return(rpdone); /* return all packets completed */
288     }
289    
290    
291     static
292     killpersist() /* kill persistent process */
293     {
294     FILE *fp;
295     int pid;
296    
297 gregl 3.6 if ((fp = fopen(pfile, "r")) == NULL)
298 gregl 3.1 return;
299     if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
300 gregl 3.6 unlink(pfile);
301 gregl 3.1 fclose(fp);
302     }
303    
304    
305     int
306     end_rtrace() /* close rtrace process(es) */
307     {
308     int status = 0, rv;
309    
310     if (nprocs > 1)
311     killpersist();
312     while (nprocs > 0) {
313     rv = close_process(rtpd[--nprocs]);
314     if (rv > 0)
315     status = rv;
316     }
317     free((char *)rtbuf);
318     rtbuf = NULL;
319     maxqlen = 0;
320     return(status);
321     }