ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.17
Committed: Mon Dec 8 18:51:15 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.16: +7 -0 lines
Log Message:
added command reading from server's stdin

File Contents

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