ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.22
Committed: Fri Dec 12 18:23:08 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.21: +4 -92 lines
Log Message:
moved a few routines from rholo.c to rholo2.c

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     * Radiance holodeck generation controller
9     */
10    
11     #include "rholo.h"
12 gregl 3.14 #include "random.h"
13 gregl 3.12 #include <signal.h>
14 gregl 3.1 #include <sys/types.h>
15 gregl 3.12 #include <sys/stat.h>
16 gregl 3.1
17     /* the following must be consistent with rholo.h */
18     int NVARS = NRHVARS; /* total number of variables */
19    
20     VARIABLE vv[] = RHVINIT; /* variable-value pairs */
21    
22     char *progname; /* our program name */
23     char *hdkfile; /* holodeck file name */
24 gregl 3.22 char froot[256]; /* root file name */
25 gregl 3.1
26     int nowarn = 0; /* turn warnings off? */
27    
28     int ncprocs = 0; /* desired number of compute processes */
29    
30     char *outdev = NULL; /* output device name */
31    
32 gregl 3.17 int readinp = 0; /* read commands from stdin */
33    
34 gregl 3.20 int force = 0; /* allow overwrite of holodeck */
35    
36 gregl 3.1 time_t starttime; /* time we got started */
37     time_t endtime; /* time we should end by */
38     time_t reporttime; /* time for next report */
39    
40 gregl 3.8 long maxdisk; /* maximum file space (bytes) */
41    
42 gregl 3.1 int rtargc = 1; /* rtrace command */
43     char *rtargv[128] = {"rtrace", NULL};
44    
45 gregl 3.12 int orig_mode = -1; /* original file mode (-1 if unchanged) */
46    
47 gregl 3.1 long nraysdone = 0L; /* number of rays done */
48     long npacksdone = 0L; /* number of packets done */
49    
50     PACKET *freepacks; /* available packets */
51    
52 gregl 3.12 char *sigerr[NSIG]; /* signal error messages */
53    
54 gregl 3.1 extern time_t time();
55    
56    
57     main(argc, argv)
58     int argc;
59     char *argv[];
60     {
61     HDGRID hdg;
62     int i;
63 gregl 3.3 /* mark start time */
64     starttime = time(NULL);
65 gregl 3.18 initurand(16384); /* initialize urand */
66 gregl 3.1 progname = argv[0]; /* get arguments */
67     for (i = 1; i < argc && argv[i][0] == '-'; i++)
68     switch (argv[i][1]) {
69     case 'w': /* turn off warnings */
70     nowarn++;
71     break;
72     case 'f': /* force overwrite */
73     force++;
74     break;
75 gregl 3.17 case 'i': /* read input from stdin */
76     readinp++;
77     break;
78 gregl 3.1 case 'n': /* compute processes */
79     if (i >= argc-2)
80     goto userr;
81     ncprocs = atoi(argv[++i]);
82     break;
83     case 'o': /* output display */
84     if (i >= argc-2)
85     goto userr;
86     outdev = argv[++i];
87     break;
88     default:
89     goto userr;
90     }
91     /* do we have a job? */
92     if (outdev == NULL && ncprocs <= 0)
93     goto userr;
94     /* get root file name */
95     rootname(froot, hdkfile=argv[i++]);
96     /* load... */
97     if (i < argc) { /* variables */
98     loadvars(argv[i++]);
99     /* cmdline settings */
100     for ( ; i < argc; i++)
101     if (setvariable(argv[i], matchvar) < 0) {
102     sprintf(errmsg, "unknown variable: %s",
103     argv[i]);
104     error(USER, errmsg);
105     }
106     /* check settings */
107     checkvalues();
108     /* load RIF if any */
109 gregl 3.22 getradfile();
110 gregl 3.1 /* set defaults */
111     setdefaults(&hdg);
112     /* holodeck exists? */
113     if (!force && access(hdkfile, R_OK|W_OK) == 0)
114     error(USER,
115     "holodeck file exists -- use -f to overwrite");
116     /* create holodeck */
117     creatholo(&hdg);
118 gregl 3.3 } else { /* else load holodeck */
119 gregl 3.1 loadholo();
120 gregl 3.15 /* check settings */
121     checkvalues();
122     /* load RIF if any */
123 gregl 3.22 getradfile();
124 gregl 3.15 /* set defaults */
125     setdefaults(NULL);
126 gregl 3.3 }
127 gregl 3.1 /* initialize */
128     initrholo();
129 gregl 3.15 /* main loop */
130 gregl 3.1 while (rholo())
131     ;
132     /* done */
133     quit(0);
134     userr:
135     fprintf(stderr,
136     "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
137     progname);
138     quit(1);
139     }
140    
141    
142 gregl 3.12 onsig(signo) /* fatal signal */
143     int signo;
144     {
145     static int gotsig = 0;
146    
147     if (gotsig++) /* two signals and we're gone! */
148     _exit(signo);
149    
150     alarm(30); /* allow 30 seconds to clean up */
151     signal(SIGALRM, SIG_DFL); /* make certain we do die */
152     eputs("signal - ");
153     eputs(sigerr[signo]);
154     eputs("\n");
155     quit(3);
156     }
157    
158    
159     sigdie(signo, msg) /* set fatal signal */
160     int signo;
161     char *msg;
162     {
163     if (signal(signo, onsig) == SIG_IGN)
164     signal(signo, SIG_IGN);
165     sigerr[signo] = msg;
166     }
167    
168    
169     int
170     resfmode(fd, mod) /* restrict open file access mode */
171     int fd, mod;
172     {
173     struct stat stbuf;
174     /* get original mode */
175     if (fstat(fd, &stbuf) < 0)
176     error(SYSTEM, "cannot stat open holodeck file");
177     mod &= stbuf.st_mode; /* always more restrictive */
178     if (mod == stbuf.st_mode)
179     return(-1); /* already set */
180     /* else change it */
181     if (fchmod(fd, mod) < 0) {
182     error(WARNING, "cannot change holodeck file access mode");
183     return(-1);
184     }
185     return(stbuf.st_mode); /* return original mode */
186     }
187    
188    
189 gregl 3.1 initrholo() /* get our holodeck running */
190     {
191     extern int global_packet();
192     register int i;
193 gregl 3.17 /* close holodeck on exec() */
194     fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
195 gregl 3.4
196     if (outdev != NULL) /* open output device */
197     disp_open(outdev);
198 gregl 3.1 else if (ncprocs > 0) /* else use global ray feed */
199     init_global();
200 gregl 3.8 /* record disk space limit */
201     if (!vdef(DISKSPACE))
202     maxdisk = 0;
203     else
204 gregl 3.9 maxdisk = 1024.*1024.*vflt(DISKSPACE);
205 gregl 3.3 /* record end time */
206 gregl 3.1 if (!vdef(TIME) || vflt(TIME) <= FTINY)
207     endtime = 0;
208     else
209     endtime = starttime + vflt(TIME)*3600.;
210     /* set up memory cache */
211 gregl 3.5 if (outdev == NULL)
212     hdcachesize = 0; /* manual flushing */
213     else if (vdef(CACHE))
214     hdcachesize = 1024.*1024.*vflt(CACHE);
215 gregl 3.1 /* open report file */
216     if (vdef(REPORT)) {
217     register char *s = sskip2(vval(REPORT), 1);
218     if (*s && freopen(s, "a", stderr) == NULL)
219     quit(2);
220     }
221     /* start rtrace */
222     if (ncprocs > 0) {
223     i = start_rtrace();
224     if (i < 1)
225 gregl 3.20 error(USER, "cannot start rtrace process(es)");
226 gregl 3.1 if (vdef(REPORT)) { /* make first report */
227     printargs(rtargc, rtargv, stderr);
228     report(0);
229     }
230     /* allocate packets */
231     freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
232     if (freepacks == NULL)
233     goto memerr;
234     freepacks[--i].nr = 0;
235     freepacks[i].next = NULL;
236     if (!vbool(OBSTRUCTIONS)) {
237     freepacks[i].offset = (float *)bmalloc(
238     RPACKSIZ*sizeof(float)*(i+1) );
239     if (freepacks[i].offset == NULL)
240     goto memerr;
241     } else
242     freepacks[i].offset = NULL;
243     while (i--) {
244     freepacks[i].nr = 0;
245     freepacks[i].offset = freepacks[i+1].offset == NULL ?
246     NULL : freepacks[i+1].offset+RPACKSIZ ;
247     freepacks[i].next = &freepacks[i+1];
248     }
249     }
250 gregl 3.12 /* set up signal handling */
251     sigdie(SIGINT, "Interrupt");
252     sigdie(SIGHUP, "Hangup");
253     sigdie(SIGTERM, "Terminate");
254     sigdie(SIGPIPE, "Broken pipe");
255     sigdie(SIGALRM, "Alarm clock");
256     #ifdef SIGXCPU
257     sigdie(SIGXCPU, "CPU limit exceeded");
258     sigdie(SIGXFSZ, "File size exceeded");
259     #endif
260     /* protect holodeck file */
261     orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
262 gregl 3.1 return;
263     memerr:
264     error(SYSTEM, "out of memory in initrholo");
265     }
266    
267    
268     rholo() /* holodeck main loop */
269     {
270     static int idle = 1;
271     PACKET *pl = NULL, *plend;
272     register PACKET *p;
273     time_t t;
274     long l;
275 gregl 3.4
276     if (outdev != NULL) /* check display */
277     if (!disp_check(idle))
278     return(0);
279 gregl 3.1 /* display only? */
280 gregl 3.19 if (nprocs <= 0)
281 gregl 3.21 return(outdev != NULL);
282 gregl 3.1 /* check file size */
283 gregl 3.9 if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
284 gregl 3.8 error(WARNING, "file limit exceeded");
285 gregl 3.21 done_rtrace();
286     idle = 1;
287     return(1); /* comes back */
288 gregl 3.8 }
289 gregl 3.1 /* check time */
290 gregl 3.3 if (endtime > 0 || reporttime > 0)
291 gregl 3.1 t = time(NULL);
292 gregl 3.8 if (endtime > 0 && t >= endtime) {
293     error(WARNING, "time limit exceeded");
294 gregl 3.21 done_rtrace();
295     idle = 1;
296     return(1); /* comes back */
297 gregl 3.8 }
298 gregl 3.3 if (reporttime > 0 && t >= reporttime)
299 gregl 3.1 report(t);
300     /* get packets to process */
301     while (freepacks != NULL) {
302     p = freepacks; freepacks = p->next; p->next = NULL;
303     if (!next_packet(p)) {
304     p->next = freepacks; freepacks = p;
305 gregl 3.21 idle = 1;
306 gregl 3.1 break;
307     }
308     if (pl == NULL) pl = p;
309     else plend->next = p;
310     plend = p;
311     }
312 gregl 3.21 /* process packets */
313 gregl 3.1 done_packets(do_packets(pl));
314     return(1); /* and continue */
315     }
316    
317    
318     report(t) /* report progress so far */
319     time_t t;
320     {
321 gregl 3.21 static time_t seconds2go = 1000000;
322    
323     if (t == 0L)
324 gregl 3.1 t = time(NULL);
325 gregl 3.21 sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n",
326     npacksdone, nraysdone, (t-starttime)/3600.);
327     eputs(errmsg);
328     if (seconds2go == 1000000)
329     seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L;
330     if (seconds2go)
331     reporttime = t + seconds2go;
332 gregl 3.1 }
333    
334    
335     setdefaults(gp) /* set default values */
336     register HDGRID *gp;
337     {
338     extern char *atos();
339     register int i;
340     double len[3], maxlen, d;
341     char buf[64];
342    
343     if (!vdef(SECTION)) {
344     sprintf(errmsg, "%s must be defined", vnam(SECTION));
345     error(USER, errmsg);
346     }
347     if (vdef(SECTION) > 1) {
348     sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
349     error(WARNING, errmsg);
350     }
351     if (!vdef(OCTREE)) {
352     if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
353     error(SYSTEM, "out of memory");
354     sprintf(vval(OCTREE), "%s.oct", froot);
355     vdef(OCTREE)++;
356     }
357     if (!vdef(OBSTRUCTIONS)) {
358     vval(OBSTRUCTIONS) = "T";
359     vdef(OBSTRUCTIONS)++;
360 gregl 3.14 }
361     if (!vdef(VDIST)) {
362     vval(VDIST) = "F";
363     vdef(VDIST)++;
364 gregl 3.1 }
365     if (!vdef(OCCUPANCY)) {
366     vval(OCCUPANCY) = "U";
367     vdef(OCCUPANCY)++;
368     }
369     /* append rendering options */
370     if (vdef(RENDER))
371     rtargc += wordstring(rtargv+rtargc, vval(RENDER));
372 gregl 3.15
373     if (gp == NULL) /* already initialized? */
374     return;
375     /* set grid parameters */
376 gregl 3.16 gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
377 gregl 3.15 if (sscanf(vval(SECTION),
378 gregl 3.16 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
379 gregl 3.15 &gp->orig[0], &gp->orig[1], &gp->orig[2],
380     &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
381     &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
382 gregl 3.16 &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
383     &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
384 gregl 3.15 badvalue(SECTION);
385     maxlen = 0.;
386     for (i = 0; i < 3; i++)
387     if ((len[i] = VLEN(gp->xv[i])) > maxlen)
388     maxlen = len[i];
389     if (!vdef(GRID)) {
390     sprintf(buf, "%.4f", maxlen/8.);
391     vval(GRID) = savqstr(buf);
392     vdef(GRID)++;
393     }
394     if ((d = vflt(GRID)) <= FTINY)
395     badvalue(GRID);
396     for (i = 0; i < 3; i++)
397 gregl 3.16 if (gp->grid[i] <= 0)
398     gp->grid[i] = len[i]/d + (1.-FTINY);
399 gregl 3.1 }
400    
401    
402     creatholo(gp) /* create a holodeck output file */
403     HDGRID *gp;
404     {
405 gregl 3.12 extern char VersionID[];
406 gregl 3.1 long endloc = 0;
407 gregl 3.10 int fd;
408 gregl 3.1 FILE *fp;
409     /* open & truncate file */
410     if ((fp = fopen(hdkfile, "w+")) == NULL) {
411     sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
412     error(SYSTEM, errmsg);
413     }
414     /* write information header */
415     newheader("RADIANCE", fp);
416 gregl 3.12 fprintf(fp, "SOFTWARE= %s\n", VersionID);
417 gregl 3.1 printvars(fp);
418     fputformat(HOLOFMT, fp);
419     fputc('\n', fp);
420     putw(HOLOMAGIC, fp); /* put magic number & terminus */
421     fwrite(&endloc, sizeof(long), 1, fp);
422 gregl 3.10 fd = dup(fileno(fp));
423     fclose(fp); /* flush and close stdio stream */
424     hdinit(fd, gp); /* allocate and initialize index */
425 gregl 3.1 }
426    
427    
428     headline(s) /* process information header line */
429     char *s;
430     {
431     extern char FMTSTR[];
432     register char *cp;
433     char fmt[32];
434    
435 gregl 3.2 if (formatval(fmt, s)) {
436 gregl 3.1 if (strcmp(fmt, HOLOFMT)) {
437     sprintf(errmsg, "%s file \"%s\" has %s%s",
438     HOLOFMT, hdkfile, FMTSTR, fmt);
439     error(USER, errmsg);
440     }
441     return;
442     }
443     for (cp = s; *cp; cp++) /* take off any comments */
444     if (*cp == '#') {
445     *cp = '\0';
446     break;
447     }
448     setvariable(s, matchvar); /* don't flag errors */
449     }
450    
451    
452     loadholo() /* start loading a holodeck from fname */
453     {
454 gregl 3.10 extern long ftell();
455 gregl 3.1 FILE *fp;
456 gregl 3.10 int fd;
457     long fpos;
458 gregl 3.4 /* open holodeck file */
459     if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
460     sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
461     ncprocs>0 ? "appending" : "reading");
462 gregl 3.1 error(SYSTEM, errmsg);
463     }
464     /* load variables from header */
465     getheader(fp, headline, NULL);
466     /* check magic number */
467     if (getw(fp) != HOLOMAGIC) {
468     sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
469     hdkfile);
470     error(USER, errmsg);
471     }
472 gregl 3.10 fread(&fpos, sizeof(long), 1, fp);
473     if (fpos != 0)
474 gregl 3.1 error(WARNING, "ignoring multiple sections in holodeck file");
475 gregl 3.10 fpos = ftell(fp); /* get stdio position */
476     fd = dup(fileno(fp));
477     fclose(fp); /* done with stdio */
478     lseek(fd, fpos, 0); /* align system file pointer */
479     hdinit(fd, NULL); /* allocate and load index */
480 gregl 3.1 }
481    
482    
483     done_packets(pl) /* handle finished packets */
484     PACKET *pl;
485     {
486 gregl 3.13 static int n2flush = 0;
487 gregl 3.1 register PACKET *p;
488    
489     while (pl != NULL) {
490     p = pl; pl = p->next; p->next = NULL;
491     if (p->nr > 0) { /* add to holodeck */
492     bcopy((char *)p->ra,
493     (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
494     p->nr*sizeof(RAYVAL));
495     if (outdev != NULL) /* display it */
496 gregl 3.7 disp_packet((PACKHEAD *)p);
497 gregl 3.13 if (hdcachesize <= 0) /* manual flushing */
498     n2flush += p->nr;
499 gregl 3.6 nraysdone += p->nr;
500     npacksdone++;
501 gregl 3.1 }
502     p->nr = 0; /* push onto free list */
503     p->next = freepacks;
504     freepacks = p;
505 gregl 3.5 }
506 gregl 3.19 if (n2flush > 512*RPACKSIZ*nprocs) {
507 gregl 3.5 hdflush(NULL); /* flush holodeck buffers */
508 gregl 3.13 n2flush = 0;
509 gregl 3.1 }
510     }
511    
512    
513     rootname(rn, fn) /* remove tail from end of fn */
514     register char *rn, *fn;
515     {
516     char *tp, *dp;
517    
518     for (tp = NULL, dp = rn; *rn = *fn++; rn++)
519 gregl 3.22 if (*rn == '/')
520 gregl 3.1 dp = rn;
521     else if (*rn == '.')
522     tp = rn;
523     if (tp != NULL && tp > dp)
524     *tp = '\0';
525     }
526    
527    
528     badvalue(vc) /* report bad variable value and exit */
529     int vc;
530     {
531     sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
532     error(USER, errmsg);
533     }
534    
535    
536     eputs(s) /* put error message to stderr */
537     register char *s;
538     {
539     static int midline = 0;
540    
541     if (!*s)
542     return;
543     if (!midline++) { /* prepend line with program name */
544     fputs(progname, stderr);
545     fputs(": ", stderr);
546     }
547     fputs(s, stderr);
548     if (s[strlen(s)-1] == '\n') {
549     fflush(stderr);
550     midline = 0;
551     }
552     }
553    
554    
555     wputs(s) /* put warning string to stderr */
556     char *s;
557     {
558     if (!nowarn)
559     eputs(s);
560     }
561    
562    
563     quit(ec) /* exit program gracefully */
564     int ec;
565     {
566     int status = 0;
567    
568     if (hdlist[0] != NULL) { /* flush holodeck */
569 gregl 3.21 if (nprocs > 0)
570     status = done_rtrace();
571 gregl 3.19 hdflush(NULL);
572     if (ncprocs > 0 && vdef(REPORT)) {
573     long fsiz, fuse;
574     fsiz = hdfilen(hdlist[0]->fd);
575     fuse = hdfiluse(hdlist[0]->fd, 1);
576     fprintf(stderr,
577 gregl 3.3 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
578 gregl 3.19 hdkfile, fsiz/(1024.*1024.),
579     100.*(fsiz-fuse)/fsiz);
580     }
581 gregl 3.1 }
582 gregl 3.12 if (orig_mode >= 0) /* reset holodeck access mode */
583     fchmod(hdlist[0]->fd, orig_mode);
584 gregl 3.11 if (outdev != NULL) /* close display */
585     disp_close();
586 gregl 3.1 exit(ec ? ec : status); /* exit */
587     }