ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
Revision: 3.6
Committed: Mon Dec 1 16:32:49 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.5: +7 -5 lines
Log Message:
added "VDISTANCE" variable for virtual distance calculation
made persist file vary with process id so multiple rholo's don't interfere

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