ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
Revision: 3.8
Committed: Thu Dec 11 11:51:20 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.7: +3 -2 lines
Log Message:
increased MAXPROC from 16 to 64 and made nprocs variable global

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